Unbalanced Class Classification/Script.py
def neural_nets(self):
        '''
        This method will:
            1. Design a Deep Feed Forward Neural Network
            2. One-hot encode y
            3. train the NN model
            4. evaluate the model
        returns:
            scores: F1 scores from cross validation
        '''
        nclass = np.shape(np.unique(self.y))[0]
        input_dim = self.X.shape[1]
        
        #Design the computational graph
        model = Sequential()
        model.add(Dense(256,input_shape=(input_dim,),activation='relu'))
        model.add(Dropout(0.5))
        model.add(Dense(256,activation='relu'))
        model.add(Dropout(0.5))
        model.add(Dense(256,activation='sigmoid'))
        model.add(Dropout(0.5))
        model.add(Dense(nclass,activation='softmax'))
        model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
        
        #training variables
        nbatch = 512
        nepochs = 100
        class_weights = {0:54.1, 1:7.1, #weights are calculated from data
                         2:1, 3:5.1, 4:18.7}
        
        cv = StratifiedKFold(n_splits=5)
        scores = []
        
        #One-Hot encode y
        for train, test in cv.split(self.X,self.y):
            X_train = self.X[train]
            y_train = self.y[train]
            
            X_test = self.X[test]
            y_test = self.y[test]
            
            y_train_onehot = to_categorical(y_train)
            y_test_onehot = to_categorical(y_test)
                
            history = model.fit(X_train, y_train_onehot, 
                                batch_size=nbatch, epochs=nepochs, 
                                verbose=0,class_weight=class_weights, 
                                validation_data=(X_test, y_test_onehot))
            
            y_pred = model.predict(X_test)
            
            pred_class=[]
            for i in y_pred:
                pred_class.append(np.argmax(i))
                
            #evaluate
            scores.append(f1_score(y_test,pred_class,average='weighted'))
        
        print(scores)
        return model, scores

        This method will:
            1. Design a Deep Feed Forward Neural Network
            2. One-hot encode y
            3. train the NN model
            4. evaluate the model
        returns:
            scores: F1 scores from cross validation
        
This method will:
1. Design a Deep Feed Forward Neural Network
2. One-hot encode y
3. train the NN model
4. evaluate the model
returns:
scores: F1 scores from cross validation
This method will : 1 . Design a Deep Feed Forward Neural Network 2 . One - hot encode y 3 . train the NN model 4 . evaluate the model returns : scores : F1 scores from cross validation
def neural_nets(self):
        nclass = np.shape(np.unique(self.y))[0]
        input_dim = self.X.shape[1]
        model = Sequential()
        model.add(Dense(256,input_shape=(input_dim,),activation='relu'))
        model.add(Dropout(0.5))
        model.add(Dense(256,activation='relu'))
        model.add(Dropout(0.5))
        model.add(Dense(256,activation='sigmoid'))
        model.add(Dropout(0.5))
        model.add(Dense(nclass,activation='softmax'))
        model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
        nbatch = 512
        nepochs = 100
        class_weights = {0:54.1, 1:7.1, 
                         2:1, 3:5.1, 4:18.7}
        cv = StratifiedKFold(n_splits=5)
        scores = []
        for train, test in cv.split(self.X,self.y):
            X_train = self.X[train]
            y_train = self.y[train]
            X_test = self.X[test]
            y_test = self.y[test]
            y_train_onehot = to_categorical(y_train)
            y_test_onehot = to_categorical(y_test)
            history = model.fit(X_train, y_train_onehot, 
                                batch_size=nbatch, epochs=nepochs, 
                                verbose=0,class_weight=class_weights, 
                                validation_data=(X_test, y_test_onehot))
            y_pred = model.predict(X_test)
            pred_class=[]
            for i in y_pred:
                pred_class.append(np.argmax(i))
            scores.append(f1_score(y_test,pred_class,average='weighted'))
        print(scores)
        return model, scores
def neural_nets ( self ) : nclass = np . shape ( np . unique ( self . y ) ) [ 0 ] input_dim = self . X . shape [ 1 ] model = Sequential ( ) model . add ( Dense ( 256 , input_shape = ( input_dim , ) , activation = 'relu' ) ) model . add ( Dropout ( 0.5 ) ) model . add ( Dense ( 256 , activation = 'relu' ) ) model . add ( Dropout ( 0.5 ) ) model . add ( Dense ( 256 , activation = 'sigmoid' ) ) model . add ( Dropout ( 0.5 ) ) model . add ( Dense ( nclass , activation = 'softmax' ) ) model . compile ( optimizer = 'adam' , loss = 'categorical_crossentropy' , metrics = [ 'accuracy' ] ) nbatch = 512 nepochs = 100 class_weights = { 0 : 54.1 , 1 : 7.1 , 2 : 1 , 3 : 5.1 , 4 : 18.7 } cv = StratifiedKFold ( n_splits = 5 ) scores = [ ] for train , test in cv . split ( self . X , self . y ) : X_train = self . X [ train ] y_train = self . y [ train ] X_test = self . X [ test ] y_test = self . y [ test ] y_train_onehot = to_categorical ( y_train ) y_test_onehot = to_categorical ( y_test ) history = model . fit ( X_train , y_train_onehot , batch_size = nbatch , epochs = nepochs , verbose = 0 , class_weight = class_weights , validation_data = ( X_test , y_test_onehot ) ) y_pred = model . predict ( X_test ) pred_class = [ ] for i in y_pred : pred_class . append ( np . argmax ( i ) ) scores . append ( f1_score ( y_test , pred_class , average = 'weighted' ) ) print ( scores ) return model , scores
This method will:
1.
This method will : 1 .
'''
        This method will:
            1. Design a Deep Feed Forward Neural Network
            2. One-hot encode y
            3. train the NN model
            4. evaluate the model
        returns:
            scores: F1 scores from cross validation
        ''' #Design the computational graph #training variables #weights are calculated from data #One-Hot encode y #evaluate
self