Learn practical skills, build real-world projects, and advance your career
from keras.models import Sequential
from keras.layers import Dense, Dropout, BatchNormalization,Dropout, Flatten, Conv2D, MaxPool2D
from keras.wrappers.scikit_learn import KerasClassifier
# dir(keras.models)
import keras
def build_model():
    
    model = Sequential()
    
    model.add(Dense(128,activation='relu',kernel_initializer='uniform'))  
    model.add(Dropout(0.4))

    model.add(Dense(64,activation='relu',kernel_initializer='uniform'))  
    model.add(Dropout(0.4))
    
    model.add(Dense(32,activation='softmax',kernel_initializer='uniform'))  
    model.add(Dropout(0.4))

    model.add(Dense(1, activation='sigmoid',kernel_initializer='uniform'))
    model.compile(optimizer = 'adam', 
              loss='binary_crossentropy', metrics=['accuracy'])
    return model
def plot_history(history):
    loss_list = [s for s in history.history.keys() if 'loss' in s and 'val' not in s]
    val_loss_list = [s for s in history.history.keys() if 'loss' in s and 'val' in s]
    acc_list = [s for s in history.history.keys() if 'acc' in s and 'val' not in s]
    val_acc_list = [s for s in history.history.keys() if 'acc' in s and 'val' in s]
    
    if len(loss_list) == 0:
        print('Loss is missing in history')
        return 
    
    ## As loss always exists
    epochs = range(1,len(history.history[loss_list[0]]) + 1)
    
    ## Loss
    plt.figure(1)
    for l in loss_list:
        plt.plot(epochs, history.history[l], 'b', label='Training loss (' + str(str(format(history.history[l][-1],'.5f'))+')'))
    for l in val_loss_list:
        plt.plot(epochs, history.history[l], 'g', label='Validation loss (' + str(str(format(history.history[l][-1],'.5f'))+')'))
    
    plt.title('Loss')
    plt.xlabel('Epochs')
    plt.ylabel('Loss')
    plt.legend()
    
    ## Accuracy
    plt.figure(2)
    for l in acc_list:
        plt.plot(epochs, history.history[l], 'b', label='Training accuracy (' + str(format(history.history[l][-1],'.5f'))+')')
    for l in val_acc_list:    
        plt.plot(epochs, history.history[l], 'g', label='Validation accuracy (' + str(format(history.history[l][-1],'.5f'))+')')

    plt.title('Accuracy')
    plt.xlabel('Epochs')
    plt.ylabel('Accuracy')
    plt.legend()
    plt.show()
import numpy as np
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import train_test_split
seed = 1
X_train = np.genfromtxt("data/train_features.csv")
y_train = np.genfromtxt("data/train_labels.csv")
X_test = np.genfromtxt("data/test_features.csv")
y_test = np.genfromtxt("data/test_labels.csv")

scaler = StandardScaler()
scaler.fit_transform(X_train)
scaler.transform(X_test)
# X_train, X_val, y_train, y_val = train_test_split(X_train, y_train, train_size=0.7, random_state=seed)

# X_train =  StandardScaler().fit_transform(X_train)
# y_train =  StandardScaler().fit_transform(y_train.reshape(len(y_train),1))[:,0]
# X_test =  StandardScaler().transform(X_test)
# y_test =  StandardScaler().fit_transform(y_test.reshape(len(y_test),1))[:,0]

array([[-0.87435336,  0.01974375,  0.86441302, ..., -0.22096082,
        -0.0342635 , -0.30615861],
       [-0.16476808,  0.79507471,  0.42013199, ..., -2.73058585,
         0.16269352,  1.2041592 ],
       [ 2.83724905,  0.92444955, -0.8688915 , ...,  1.53001282,
         0.76332502,  0.04799408],
       ...,
       [ 0.06943275,  0.69008292,  1.31141008, ..., -1.09784552,
         0.74185984, -0.37298363],
       [ 0.62871357,  0.41798702,  0.20949398, ...,  0.23019833,
         0.89953664,  0.77535207],
       [-0.47107004,  0.33360722, -0.37766817, ...,  0.56577421,
         0.53961027, -0.63835347]])
model = KerasClassifier(build_fn=build_model, epochs=10, batch_size=30, verbose=1)
# model = build_model()
# history = model.fit(X_train, 
#                     y_train,
#                     epochs = 200,
#                     batch_size = 16,
#                     verbose=1,
#                     validation_data=(X_val,y_val))