Exemple #1
0
def stacking_train_predict(X_train, y_train, X_test, y_test=None, estimator='average'):
    if estimator == 'average':
        y_pred = X_test.apply('mean', axis=1)
    
    if estimator == 'logreg':
        model = LogisticRegression()
        params = {'C': np.arange(0.05,5,0.05)}
        clf = GridSearchCV(model, params, cv=10, scoring='neg_log_loss', refit=True)
        clf.fit(X_train, y_train)
        y_pred = clf.predict_proba(X_test)[:,1]

    if estimator == 'nn':
        model = Sequential()
        model.add(Dense(128, input_dim=X_train.shape[1], activation='relu'))
        model.add(Dropout(0.5))
        model.add(Dense(128, activation='relu'))
        model.add(Dropout(0.5))
        model.add(Dense(1, activation='sigmoid'))
        opti = Adam(lr=0.01, decay=0.01)
        model.compile(loss='binary_crossentropy', optimizer=opti, metrics=['accuracy'])
        validation_data = (X_test, y_test) if y_test is not None else None
        model.fit(X_train, y_train, epochs=200, batch_size=64, verbose=1, validation_data=validation_data)
        y_pred = model.predict(X_test).transpose()[0]
        y_pred = np.clip(y_pred, 0.0001, 0.9999)
    
    return y_pred
Exemple #2
0
estimator = KerasClassifier(build_fn=baseline_model, epochs=2, batch_size=5, verbose=0)


# In[ ]:


from keras.models import Sequential
from keras.layers.core import Dense, Dropout, Activation
from keras.utils import np_utils
from keras.optimizers import RMSprop
from keras.optimizers import Adam

model = Sequential()

model.add(Dense(input_dim=127, output_dim=200, init="uniform", activation="relu"))
model.add(Dropout(0.5))
model.add(Dense(input_dim=200, output_dim=200, init="uniform", activation="relu"))
model.add(Dropout(0.5))
model.add(Dense(input_dim=200, output_dim=9, init="uniform", activation="softmax"))


# In[ ]:


adam=Adam(lr=0.001, beta_1=0.9, beta_2=0.999, epsilon=1e-08, decay=0.0)

model.compile(loss='categorical_crossentropy', optimizer=adam, metrics=['accuracy'])


# In[ ]:
Exemple #3
0
params = {'learning_rate' : 0.1,
          'depth'         : 2,
          'iterations'    : 50,
          'random_seed'   : 42}

vars_for_model = [x for x in df.columns.tolist() if x not in \
                            list(['stroke', 'id'] + single_interaction_vars )]
                            # and feature_importances_dict[x] > 0]

model = CatBoostClassifier(learning_rate = params['learning_rate'],
                           depth         = params['depth'],
                           iterations    = params['iterations'],
                           random_seed   = params['random_seed'])

'''
'''
# NN
from keras.models import Sequential, Model, load_model
from keras.layers import Dense, Dropout, Flatten, Input, concatenate, BatchNormalization
from keras.callbacks import EarlyStopping, ModelCheckpoint
from keras.optimizers import Adam, SGD
from keras.utils import plot_model
from keras import regularizers
from keras import backend as K

vars_for_model = [x for x in df.columns.tolist() if x not in \
                            list(['stroke', 'id'] + single_interaction_vars )]

model = Sequential()
model.add(BatchNormalization(input_shape = (len(vars_for_model),)))
model.add(Dense(units = 10, activation = 'relu'))
       'Humidity_N(t-1)',
       'Dew_PointF_N(t-1)',
       'Wind_Speed_mps(t-1)',
       'Temperature_C_N(t-1)',
       "Hour_sin","Hour_cos",
       "Conditions_Name(t-1)",
       "Conditions_Name(t)"]]

Conditions_Name= encode_text_index(dataset1,"Conditions_Name(t)")
X,Y = to_xy(dataset1,"Conditions_Name(t)")
X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size=0.2)
n_epochs=20
n_batch =1

model = Sequential()
model.add(Dense(100, input_dim=X_train.shape[1], activation='relu'))
model.add(Dense(100, activation='relu'))
model.add(Dense(100, kernel_regularizer=regularizers.l2(0.01),
                  activity_regularizer=regularizers.l1(0.01),
                  activation='relu'))
#model.add(LSTM(300,return_sequences=True,activation='tanh',stateful=True,
#               batch_input_shape=(n_batch, time_step, X_train.shape[2])))
#model.add(LSTM(100, input_shape=(time_step,n_features)))
model.add(Dense(Y_train.shape[1], activation='softmax'))
# Compile model
adam=keras.optimizers.Adam(lr=0.01, beta_1=0.99, beta_2=0.99, epsilon=None, decay=0.0)
model.compile(loss='categorical_crossentropy', optimizer="adam", metrics=['accuracy'])
checkpoint = ModelCheckpoint(filepath="best_weights_mod5.hdf5", monitor='val_acc', verbose=1, save_best_only=True, mode='max')
callbacks_list = [checkpoint]
history = model.fit(X_train,Y_train,validation_data=(X_test,Y_test),callbacks=callbacks_list,verbose=1,epochs=n_epochs,batch_size=n_batch)
###plot loss
Exemple #5
0
clf = KNeighborsClassifier(n_neighbors=12)
knnModel = clf.fit(Scaled_trainData, trainLabelE)
y_te_pred = clf.predict(Scaled_testData)

acc = accuracy_score(testLabelE, y_te_pred)
print("K-Nearest Neighbors Accuracy: %.5f" % (acc))  #.907

##

#Keras model with different optimizers and dropout layers
Y_test = to_categorical(testLabelE)  # one-hot encoded labels
Y_train = to_categorical(trainLabelE)

model = Sequential()
model.add(Dense(64, input_dim=Scaled_trainData.shape[1], activation="relu"))
model.add(Dropout(0.5))  # To avoid overfitting
model.add(Dense(64, activation="relu"))
model.add(Dropout(0.5))
model.add(Dense(6, activation="softmax"))

sgd = SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True)
model.compile(loss="categorical_crossentropy",
              optimizer=sgd,
              metrics=['accuracy'])
model.fit(Scaled_trainData, Y_train, nb_epoch=30, batch_size=128)
score = model.evaluate(Scaled_testData, Y_test, batch_size=128)
print(score)  # 0.956

model.compile(loss="categorical_crossentropy",
              optimizer="adam",
model.fit(X_train, y_train)

y_pred = model.predict(X_test)

# Testing Set Performance
cm = confusion_matrix(y_pred, y_test)
sns.heatmap(cm, annot=True)

print(classification_report(y_test, y_pred))

"""# TASK #9: TRAIN AND EVALUATE A DEEP LEARNING MODEL """

import tensorflow as tf

model = tf.keras.models.Sequential ()
model.add(tf.keras.layers.Dense(units = 500, activation = 'relu', input_shape = (50,)))
model.add(tf.keras.layers.Dense(units=500, activation='relu'))
model.add(tf.keras.layers.Dense(units=500, activation='relu'))
model.add(tf.keras.layers.Dense(units=1, activation='sigmoid'))

model.summary()

model.compile(optimizer= 'Adam', loss = 'binary_crossentropy', metrics = ['accuracy'])

epochs_hist = model.fit(X_train, y_train, epochs = 100, batch_size = 50)

# oversampler = SMOTE(random_state=0)
# smote_train, smote_target = oversampler.fit_sample(X_train, y_train)
# epochs_hist = model.fit(smote_train, smote_target, epochs = 100, batch_size = 50)

y_pred = model.predict(X_test)
Exemple #7
0
    pickle.dump(everything,f)
winsound.Beep(frequency, duration)

# %% ROC-AUC Score for Model 7
model6.fit(X_train,y_train)
everything.loc['SVM']['ROC-AUC Score (Train)']=roc_auc_score(y_train, model6.predict_proba(X_train)[:,1])
everything.loc['SVM']['ROC-AUC Score (Test)']=roc_auc_score(y_test, model6.predict_proba(X_test)[:,1])
print(everything[['ROC-AUC Score (Train)','ROC-AUC Score (Test)']])
with open(r'C:\Users\RAGHAV VERMA\Semester5Project\results.pickle','wb') as f:
    pickle.dump(everything,f)
winsound.Beep(frequency, duration)

# %% ROC-AUC Score for NN
def baseline_model():
    model=models.Sequential()
    model.add(layers.Dense(140,input_shape=X_train.shape,kernel_initializer='LecunNormal'))
    model.add(layers.Dropout(0.22, noise_shape=None, seed=42))
    model.add(layers.Dense(140,activation='selu'))
    model.add(layers.Dropout(0.17, noise_shape=None, seed=42))
    model.add(layers.Dense(140,activation='selu'))
    model.add(layers.Dropout(0.13, noise_shape=None, seed=7))
    model.add(layers.Dense(140,activation='selu'))
    model.add(layers.Dropout(0.095, noise_shape=None, seed=42))
    model.add(layers.Dense(140,activation='selu'))
    model.add(layers.Dropout(0.06, noise_shape=None, seed=42))
    model.add(layers.Dense(2,activation='softmax'))
    return model
model7=baseline_model()
earlystop = tf.keras.callbacks.EarlyStopping(monitor='loss', patience=12,restore_best_weights=True)
opt = Adamax(learning_rate=0.0012)
model.compile(loss='categorical_crossentropy', optimizer=opt, metrics=['accuracy'])
Exemple #8
0
# Making the Confusion Matrix
accuracy = (cm.item(0, 0) + cm.item(1, 1)) / len(X_test)

# Deep Learning ------------------------------------------------------------------------------
# Artificial Neural Networks
# 'Theano', 'TensorFlow' (mostly to develop your own) for efficient utiliation of CPU, GPU (wrap numpy)
# Importing the Keras libraries and packages
import keras
from keras.models import Sequential  # required to init ANN
from keras.layers import Dense  #required to build layers of ANN

classifier = Sequential()

# Adding the input layer and the first hidden layer
# output dim=>average #output+input=> (no of input columns+ output )/2=>(11+1)/2
classifier.add(
    Dense(output_dim=6, init='uniform', activation='relu', input_dim=11))  #
classifier.add(Dense(output_dim=6, init='uniform', activation='relu'))
#if output is more than 1 catagorical variable apply activation='softmax'and output_dim="#outputs"
classifier.add(Dense(output_dim=1, init='uniform', activation='sigmoid'))

#adam-type of schocastic gradient decent   #loss = 'binary_crossentropy'
classifier.compile(optimizer='adam',
                   loss='binary_crossentropy',
                   metrics=['accuracy'])
classifier.fit(X_train, y_train, batch_size=10, nb_epoch=100)

# Convolutional Neural Network
# Part 1 - Building the CNN
# Importing the Keras libraries and packages
from keras.models import Sequential  # initialize nural network
from keras.layers import Conv2D  # add convolution layers 2D:to images
from keras.models import Sequential
from keras.layers import Dense

# In[145]:

X_train.shape[1]

# In[146]:

# Initialising the ANN
classifier = Sequential()

# Adding the input layer and the first hidden layer
classifier.add(
    Dense(output_dim=120,
          kernel_initializer='normal',
          activation='relu',
          input_dim=X_train.shape[1]))

# Adding the second hidden layer
classifier.add(
    Dense(output_dim=100, kernel_initializer='normal', activation='relu'))
classifier.add(
    Dense(output_dim=80, kernel_initializer='normal', activation='relu'))
classifier.add(
    Dense(output_dim=60, kernel_initializer='normal', activation='relu'))
classifier.add(
    Dense(output_dim=40, kernel_initializer='normal', activation='relu'))
classifier.add(
    Dense(output_dim=20, kernel_initializer='normal', activation='relu'))
classifier.add(


tokenizer = Tokenizer(num_words = max_features, filters="""!"#$%&()*+,-./:;<=>?@[\\]^_`{|}~\t\n',""")
tokenizer.fit_on_texts(cleanedWithKickTimeBlurb.values)
word_index = tokenizer.word_index
X_blurb = tokenizer.texts_to_sequences(cleanedWithKickTimeBlurb)
X_blurb = pad_sequences(X_blurb, maxlen=maxlen)
X_train_blurb, X_test_blurb, y_train_blurb, y_test_blurb = train_test_split(X_blurb,y, test_size = 0.20)


#REG = 1.0
#DROP = 0.05

model = Sequential()
model.add(Embedding(input_dim=max_features, output_dim=40, 
                    embeddings_initializer='glorot_uniform'))
model.add(SpatialDropout1D(0.2))
model.add(LSTM(100, dropout=0.2, recurrent_dropout=0.2))
model.add(Dropout(0.20))
model.add(Dense(1, activation='softmax'))
model.compile(loss='binary_crossentropy',
                  optimizer='adam',
                  metrics=['accuracy'])

model.summary()
epochs = 10
batch_size = 32
model.fit(X_train_blurb, y_train_blurb, batch_size=batch_size, epochs=epochs, 
              validation_data=(X_test_blurb, y_test_blurb),
              callbacks=[EarlyStopping(patience=8, verbose=1,restore_best_weights=True)])
hidden_features_blurb = model.predict(X_test_blurb)
Exemple #11
0
for k in range (0,len(test_labels)):
  if test_labels[k] =='STANDING':
    test_labelss[k][0]=1
  elif test_labels[k] =='WALKING':
    test_labelss[k][1]=1
  elif test_labels[k] =='WALKING_UPSTAIRS':
    test_labelss[k][2]=1
  elif test_labels[k] =='WALKING_DOWNSTAIRS':
    test_labelss[k][3]=1
  elif test_labels[k] =='SITTING':
    test_labelss[k][4]=1
  else:
    test_labelss[k][5]=1

model = Sequential()
model.add(Dense(64, activation='relu', input_dim=561))
model.add(Dropout(0.5))
model.add(Dense(64, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(6, activation='softmax'))

sgd = SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True)
model.compile(loss='categorical_crossentropy',optimizer=sgd,metrics=['accuracy'])
model.fit(train_data, train_labelss,nb_epoch=30,batch_size=128)
score = model.evaluate(test_data, test_labelss, batch_size=128)
print(score)

model.compile(loss='categorical_crossentropy',optimizer='adam',metrics=['accuracy'])
model.fit(train_data, train_labelss,nb_epoch=30,batch_size=128)
score = model.evaluate(test_data, test_labelss, batch_size=128)
print(score)
import keras
from sklearn import datasets
from sklearn.preprocessing import LabelEncoder
from keras.utils import np_utils

# encode class values as integers
encoder = LabelEncoder()
encoder.fit(label)
encoded_Y = encoder.transform(label)
# convert integers to dummy variables (i.e. one hot encoded)
dummy_y = np_utils.to_categorical(encoded_Y)

from sklearn.model_selection import train_test_split
from keras.callbacks import TensorBoard
from keras.models import Sequential
from keras.layers import Dense
from keras.layers.core import Dropout
from time import time
from keras import losses

model = Sequential()
model.add(Dense(8, input_dim=4, activation='relu'))
model.add(Dense(80, activation='relu'))
model.add(Dense(80, activation='relu'))
model.add(Dense(80, activation='relu'))
model.add(Dense(2, activation='softmax'))
model.compile(loss=losses.mean_squared_error,
              optimizer='sgd',
              metrics=['accuracy'])
model.fit(feature, dummy_y, batch_size=32, epochs=1000)
def train_model(classifier):
    if (classifier == 'LR'):
        model = LogisticRegression(random_state=seed)
        model.fit(X_train, y_train)
        return model
    if (classifier == 'KNN'):
        print("\n  K TREINO  TESTE")
        print(" -- ------ ------")
        for k in range(1, 130, 2):
            model = KNeighborsClassifier(n_neighbors=k,
                                         weights='uniform',
                                         metric='minkowski',
                                         p=2)
            model = model.fit(X_train, y_train)
            y_resposta_treino = model.predict(X_train)
            y_resposta_teste = model.predict(X_test)
            acuracia_treino = sum(y_resposta_treino == y_train) / len(y_train)
            acuracia_teste = sum(y_resposta_teste == y_test) / len(y_test)
            print("%3d" % k, "%6.1f" % (100 * acuracia_treino),
                  "%6.1f" % (100 * acuracia_teste))
        return model
    if (classifier == 'SV'):
        model = SVC(kernel='linear', random_state=seed)  # kernel = 'rbf'
        model.fit(X_train, y_train)
        return model
    if (classifier == 'NB'):
        model = GaussianNB()
        model.fit(X_train, y_train)
        return model
    if (classifier == 'DT'):
        model = DecisionTreeClassifier(criterion='entropy', random_state=seed)
        model.fit(X_train, y_train)
        return model
    if (classifier == 'RF'):
        # Hiper-parâmetros selecionados após a busca:
        model = RandomForestClassifier(n_estimators=1600,
                                       min_samples_split=2,
                                       min_samples_leaf=4,
                                       max_features='sqrt',
                                       max_depth=10,
                                       bootstrap=True,
                                       random_state=seed)
        model.fit(X_train, y_train)
        print(model.feature_importances_)
        return model
    if (classifier == 'RG'):
        model = RidgeClassifier(alpha=1,
                                class_weight='balanced',
                                solver='auto')
        model.fit(X_train, y_train)
        return model
    if (classifier == 'GBC'):
        # Hiper-parâmetros selecionados após a busca:
        model = GradientBoostingClassifier(
            random_state=seed,
            n_estimators=200,
            min_samples_split=5,
            min_samples_leaf=1,
            max_features='sqrt',
            max_depth=10,
        )
        rfe = RFE(model)
        rfe = rfe.fit(X_train, y_train)
        return rfe
    if (classifier == 'MLP'):
        kfold = StratifiedKFold(n_splits=3, shuffle=True, random_state=seed)
        cvscores = []
        for treino, teste in kfold.split(X_train, y_train):
            model = tf.keras.models.Sequential()
            model.add(tf.keras.layers.Dense(units=20, activation='relu'))
            model.add(tf.keras.layers.Dense(units=10, activation='relu'))
            model.add(tf.keras.layers.Dense(units=1, activation='sigmoid'))
            model.compile(optimizer='adam',
                          loss='binary_crossentropy',
                          metrics=['accuracy'])
            model.fit(X_train, y_train, batch_size=32, epochs=100, verbose=0)
            scores = model.evaluate(X_test, y_test, verbose=0)
            print("%s: %.2f%%" % (model.metrics_names[1], scores[1] * 100))
            cvscores.append(scores[1] * 100)
        print("%.2f%% (+/- %.2f%%)" % (np.mean(cvscores), np.std(cvscores)))
        model.summary()
        return model
# Random seeds
np.random.seed(123)
rn.seed(123)
tf.set_random_seed(123)

# Convert Attrition to one-hot encoding for NN to be able to read
y_train = to_categorical(y_train)
y_test = to_categorical(y_test)

# Neural Network Architecture

# Create initial set of linear layers
model=Sequential()
# Now, add to our linear layers and note their neurons in each added layer
# Input dimension only needs to be noted for the first layer and it is the number of features/columns
model.add(Dense(input_dim=27, units=8, activation='relu', name='output_1'))
model.add(Dense(units=16, activation='relu', name='output_2'))
# Make sure output later has two neurons for each type of classification of attrition
model.add(Dense(units=2, activation='sigmoid'))

# Compile the Network
# More information on optimizer types:
# https://keras.io/optimizers/
model.compile(optimizer=Adam(lr=0.01), loss='binary_crossentropy', metrics=['accuracy'])
# loss='binary_crossentropy' specifies that your model should optimize the log 
# loss for binary classification.  
# metrics=['accuracy'] specifies that accuracy should be printed out

# Review NN configuration
model.summary()
Exemple #15
0
metrics.accuracy_score(target_val, predicted)
scores = cross_val_score(svmmodel,
                         attrition_factored_column,
                         target,
                         scoring='accuracy',
                         cv=10)
#87.21

# Neural Networks
from keras.models import Sequential
from keras.layers import Dense

np.random.seed(7)
# create model
model = Sequential()
model.add(Dense(48, input_dim=31, activation='relu'))
model.add(Dense(24, activation='relu'))
model.add(Dense(1, activation='sigmoid'))

model.compile(loss='binary_crossentropy',
              optimizer='adam',
              metrics=['accuracy'])
model.fit(np.array(train), np.array(target_train), epochs=1000, batch_size=10)

predictions = model.predict(np.array(test))
predictions = [round(x[0]) for x in predictions]
metrics.confusion_matrix(target_val,
                         predictions,
                         labels=None,
                         sample_weight=None)
metrics.accuracy_score(target_val, predictions)
from sklearn.metrics import confusion_matrix
cm = confusion_matrix(y_test, y_pred)

#*********************************************************
# Importing the Keras libraries and packages
import keras
from keras.models import Sequential
from keras.layers import Dense

# Initialising the ANN
classifier = Sequential()

# Adding the input layer and the first hidden layer
classifier.add(
    Dense(units=6,
          kernel_initializer='uniform',
          activation='relu',
          input_dim=8))

classifier.add(Dense(units=6, kernel_initializer='uniform', activation='relu'))

classifier.add(Dense(units=1, kernel_initializer='uniform', activation='tanh'))

# Compiling the ANN
classifier.compile(optimizer='adam',
                   loss='binary_crossentropy',
                   metrics=['accuracy'])

# Fitting the ANN to the Training set
classifier.fit(X_train, y_train, batch_size=10, nb_epoch=500)
Exemple #17
0
#######################
# CNN
# first neural network with keras tutorial
from numpy import loadtxt
from keras.models import Sequential
from keras.layers import Dense

...
# define the keras model
#The model expects rows of data with 8 variables (the input_dim=8 argument)
#The first hidden layer has 12 nodes and uses the relu activation function.
#The second hidden layer has 8 nodes and uses the relu activation function.
#The output layer has one node and uses the sigmoid activation function.
model = Sequential()
model.add(Dense(12, input_dim=7, activation='relu'))
model.add(Dense(7, activation='relu'))
model.add(Dense(1, activation='sigmoid'))

# compile the keras model
model.compile(loss='binary_crossentropy',
              optimizer='adam',
              metrics=['accuracy'])

# fit the keras model on the dataset
# Using class as y
x = df1[['Timestamp', 'Arbitration_ID', 'DLC', 'd1', 'd2', 'd3', 'd4']]
y = df1[['Class']]
yy = y
yy[yy['Class'] == 'Normal'] = 1
yy[yy['Class'] == 'Attack'] = 0
Exemple #18
0
# Use the newest version of TensorFlow 2.0
# %tensorflow_version 2.x

# Check to ensure we're using our GPU
import tensorflow as tf

tf.test.gpu_device_name()

# Create a simple model
import tensorflow.keras
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense

model = Sequential()

model.add(
    Dense(20, kernel_initializer="uniform", activation="relu", input_dim=40))
model.add(Dense(1, kernel_initializer="uniform", activation="sigmoid"))

model.compile(optimizer="adam",
              loss="binary_crossentropy",
              metrics=["accuracy"])

# Display Model Summary and Show Parameters
model.summary()

# Start Training Our Classifier

batch_size = 64
epochs = 25

history = model.fit(X_train,
Exemple #19
0
accuracy_score(y_test, y_pred)

import matplotlib.pyplot as plt

fig, ax = plt.subplots()
fig, ax = plt.subplots()
ax.plot(y_test)
ax.plot(y_pred)

#Neural Network
from keras.layers import Dense, Dropout
from keras.models import Sequential
from keras.regularizers import l2

model = Sequential()
model.add(
    Dense(100, activation='relu', input_dim=8, kernel_regularizer=l2(0.01)))
model.add(Dropout(0.3, noise_shape=None, seed=None))

model.add(Dense(100, activation='relu', kernel_regularizer=l2(0.01)))
model.add(Dropout(0.3, noise_shape=None, seed=None))

model.add(Dense(1, activation='sigmoid'))
model.compile(loss='binary_crossentropy',
              optimizer='adam',
              metrics=['accuracy'])

model.summary()

model_output = model.fit(
    x_train_scaled,
    y_train,
--------------------------------------------------------------------------
"""
from sklearn.preprocessing import StandardScaler
sc = StandardScaler()
train_X = sc.fit_transform(train_X)
val_X = sc.transform(val_X)

# Build ANN
from keras.models import Sequential
from keras.layers import Dense

# initialize the ANN
classifier = Sequential()

# Adding the input layer and the first hidden layer
classifier.add(Dense(12, input_dim=8, activation='relu'))
classifier.add(Dense(8, activation='relu'))
classifier.add(Dense(1, activation='sigmoid'))

# Compiling the ANN
classifier.compile(optimizer='adam',
                   loss='binary_crossentropy',
                   metrics=['accuracy'])

# Fitting the ANN
classifier.fit(train_X, train_y, batch_size=10, nb_epoch=100)

# Predicting the Test set results
y_pred = classifier.predict(val_X)

scores = classifier.evaluate(val_X, val_y)
Exemple #21
0

model_results = pd.DataFrame([['XGBoost', acc, prec, rec, f1]],
             columns = ["Model", "Accuracy", "Precision", "Recall", "F1"])
results = results.append(model_results,ignore_index = True)


#ANN

import keras
from keras.models import Sequential
from keras.layers import Dense

classifier = Sequential()

classifier.add(Dense(units= 9, kernel_initializer = 'uniform', activation = 'relu', input_dim = 19))
classifier.add(Dense(units= 9, kernel_initializer = 'uniform', activation = 'relu'))
classifier.add(Dense(units= 1, kernel_initializer = 'uniform', activation = 'sigmoid'))

classifier.compile(optimizer = 'adam', loss = 'binary_crossentropy', metrics = ['accuracy'])

classifier.fit(x_train,y_train, batch_size= 10, nb_epoch=100)


y_pred = classifier.predict(x_test)
y_pred = (y_pred>0.5)


acc = accuracy_score(y_test, y_pred)
prec = precision_score(y_test, y_pred)
rec = recall_score(y_test,y_pred)
Exemple #22
0
from sklearn.metrics import classification_report

print(classification_report(y_test, y_pred))

from sklearn.metrics import accuracy_score
print("Accuracy (0:.2f)%".format(100 * accuracy_score(y_pred, y_test)))

import keras

from keras.models import Sequential

from keras.layers import Dense

classifier = Sequential()

classifier.add(
    Dense(output_dim=9, init='uniform', activation='relu', input_dim=18))

classifier.add(Dense(output_dim=9, init='uniform', activation='relu'))

classifier.add(Dense(output_dim=1, init='uniform', activation='sigmoid'))

classifier.compile(optimizer='adam',
                   loss='binary_crossentropy',
                   metrics=['accuracy'])

classifier.fit(X_train, y_train, batch_size=10, nb_epoch=100)

y_pred = classifier.predict(X_test)

y_pred1 = (y_pred > 0.6)
cm1 = confusion_matrix(y_test, y_pred1)
#Label encoding
from sklearn.preprocessing import LabelBinarizer
encoder = LabelBinarizer()
y = encoder.fit_transform(y)

#test, train split
X_train, X_test, y_train, y_test = train_test_split(X,
                                                    y,
                                                    test_size=0.2,
                                                    random_state=42)

# Initialize the constructor
model = Sequential()
# input layer
model.add(Dense(40, input_shape=(40, ), activation='relu'))
# hidden layer
model.add(Dropout(0.2))
model.add(Dense(40, input_shape=(40, ), activation='relu'))
model.add(Dropout(0.2))
model.add(Dense(40, input_shape=(40, ), activation='relu'))
# output layer
model.add(Dense(5, input_shape=(40, ), activation='softmax'))

#Model details
model.output_shape
model.summary()
model.get_config()
model.get_weights()

model.compile(
Exemple #24
0
x = pd.get_dummies(x_, columns=['listed_in(type)', 'listed_in(city)'])

X_train, X_test, y_train, y_test = train_test_split(x,
                                                    y,
                                                    test_size=0.4,
                                                    random_state=0)
scaler = StandardScaler()
scaler.fit(X_train)

#Normalizing data
X_train = scaler.transform(X_train)
X_test = scaler.transform(X_test)

#Model
model = Sequential()
model.add(Dense(32, input_dim=40, init='uniform', activation='relu'))
model.add(Dense(6, init='uniform', activation='softmax'))
model.compile(loss='categorical_crossentropy',
              optimizer='adam',
              metrics=['accuracy'])

history = model.fit(X_train, y_train, nb_epoch=50, batch_size=50, verbose=1)

#Print Accuracy
scores = model.evaluate(X_test, y_test)

pred = model.predict(X_test)

l = y_test.values.tolist()
act_final = [l[i].index(max(l[i])) for i in range(len(l))]
m = list(pred)
Exemple #25
0
partial_y_train = y_train[10000:]

print("x_val ", X_val.shape)
print("partial_x_train ", partial_X_train.shape)
print("y_val ", y_val.shape)
print("partial_y_train ", partial_y_train.shape)


# In[111]:


# NN model

model = models.Sequential()
model.add(layers.Dense(
    16, kernel_regularizer=regularizers.l1(0.001), activation='relu', input_shape=(10000,))
)
model.add(layers.Dropout(0.5))
model.add(layers.Dense(
    16, kernel_regularizer=regularizers.l1(0.001),activation='relu')
)
model.add(layers.Dropout(0.5))
model.add(layers.Dense(1, activation='sigmoid'))


# In[113]:


NumEpochs = 10
BatchSize = 512
Exemple #26
0
    lstm_train_y[ind] = y_train[ind+lb]
for ind in range(lstm_test_x.shape[0]):
    lstm_test_x[ind,:,:] = X_test[ind:ind+lb,:]
    lstm_test_y[ind] = y_test[ind+lb]

print(lstm_train_x.shape,lstm_test_x.shape)
print(lstm_train_y.shape,lstm_test_y.shape)

import tensorflow as tf
from keras.models import Sequential, Model
from keras.layers.recurrent import LSTM
from keras.layers.core import Dense,Dropout
from keras.callbacks import *

clf = Sequential()
clf.add(LSTM(units=150, input_shape=(lb,lstm_train_x.shape[2])))
clf.add(Dropout(0.2))
clf.add(Dense(units = 1))

clf.compile(optimizer=keras.optimizers.Adam(learning_rate=0.001),loss = 'binary_crossentropy', metrics = 'accuracy')
clf.fit(lstm_train_x,lstm_train_y, epochs = 100, batch_size = 100, callbacks = EarlyStopping(monitor='loss', mode = 'min'))

test_loss, test_acc = clf.evaluate(lstm_test_x,lstm_test_y)
train_loss, train_acc = clf.evaluate(lstm_train_x,lstm_train_y)
print("Single Layer LSTM")
print("Train accuracy", train_acc)
print("Test accuracy", test_acc)

clf = Sequential()
clf.add(LSTM(units=150,return_sequences = True, input_shape=(lb,lstm_train_x.shape[2])))
clf.add(Dropout(0.3))
# here we pad the tokens to ensure that the reviewis all are the same length in order to pass it into the nueral net
maxlen = 100
x = pad_sequences(list_tokenized_train, maxlen=maxlen)
y = np.array(sentiment_p_train + sentiment_n_train)

# just do the same for test reviews
y_test = np.array(sentiment_p_test + sentiment_n_test)
list_tokenized_test = tokenizer.texts_to_sequences(review_p_test +
                                                   review_n_test)
x_test = pad_sequences(list_tokenized_test, maxlen=maxlen)

# define the nueral net
embed_size = 256
model = Sequential()
model.add(Embedding(max_features, embed_size))
model.add(Bidirectional(LSTM(32, return_sequences=True)))
model.add(GlobalMaxPool1D())
model.add(Dense(20, activation="relu"))
model.add(Dropout(0.03))
model.add(Dense(1, activation="sigmoid"))
model.compile(loss='binary_crossentropy',
              optimizer='adam',
              metrics=['accuracy'])

# actually training the NN
model.fit(x, y, batch_size=100, epochs=3, validation_split=0.2)

# testing our model
prediction = model.predict(x_test)
y_pred = (prediction > 0.5)
Exemple #28
0
        loss = weight * cross_entropy
        # Sum the losses in mini_batch
        loss = K.sum(loss, axis=1)

        return loss * 1000

    return focal_loss


model = Sequential()

input_dim = xtrain.shape[1]
#nb_classes = y_train.shape[1]

model = Sequential()
model.add(Dense(input_dim=input_dim, units=1))
model.add(Activation('sigmoid'))
from keras import optimizers
opt = optimizers.Nadam(lr=0.002,
                       beta_1=0.9,
                       beta_2=0.999,
                       epsilon=1e-08,
                       schedule_decay=0.004)
model.compile(loss=binary_focal_loss(gamma=2, alpha=0.9),
              optimizer=opt,
              metrics=[f1])

history = model.fit(xtrain, ytrain, epochs=15, batch_size=500, verbose=1)

# Training
print(
data = concat([Y, X], axis=1)

train, test = train_test_split(data, test_size=0.3, random_state=0, stratify=data['Survived'])
X_train = train[train.columns[1:]]
Y_train = train[train.columns[:1]]
X_test = test[test.columns[1:]]
Y_test = test[test.columns[:1]]

print(X_train.shape)

model = LogisticRegression()
model.fit(X_train, Y_train)
print(model.score(X_test, Y_test))


from tensorflow.python.keras.layers import Dense
from tensorflow.python.keras.models import Sequential

# create first model
model = Sequential()
model.add(Dense(18, input_dim=18, activation='relu'))
model.add(Dense(18, activation='relu'))
model.add(Dense(1, activation='sigmoid'))
# Compile model
model.compile(loss='binary_crossentropy', optimizer='Nadam', metrics=['accuracy'])
# Fit the model
model.fit(X_train, Y_train, epochs=1000, batch_size=32)
# evaluate the model
scores = model.evaluate(X, Y)
print("\n%s: %.2f%%" % (model.metrics_names[1], scores[1]*100))
    def create_model(self, model_name):
        if model_name == 'SVM':
            model = svm.SVC(kernel='linear', C=10, max_iter=10000)
        elif model_name == 'Logistic':
            model = LogisticRegression(max_iter=1000)
        elif model_name == 'CNN':
            patience = 5
            krs = (3, 3)
            input_shape = (128, 128, 3)
            num_classes = 2
            model = Sequential()
            model.add(Conv2D(32, kernel_size=krs, input_shape=input_shape))

            model.add(MaxPooling2D(pool_size=(2, 2)))
            model.add(BatchNormalization())
            model.add(LeakyReLU(alpha=0.05))
            model.add(Dropout(0.3))
            '''model.add(Conv2D(64, krs))
            model.add(MaxPooling2D(pool_size=(2, 2)))
            model.add(BatchNormalization())
            model.add(LeakyReLU(alpha=0.05))
            model.add(Dropout(0.3))
            
            model.add(Conv2D(128, krs))
            model.add(MaxPooling2D(pool_size=(2, 2)))
            model.add(BatchNormalization())
            model.add(Activation('relu'))
            model.add(Dropout(0.3))'''

            model.add(Flatten())

            # model.add(Dense(1024))
            # model.add(LeakyReLU(alpha=0.05))
            # model.add(Dropout(0.5))

            # model.add(Dense(512))
            #model.add(LeakyReLU(alpha=0.05))
            #model.add(Dropout(0.3))

            model.add(Dense(128))
            model.add(LeakyReLU(alpha=0.05))
            model.add(Dropout(0.3))

            model.add(Dense(num_classes, activation='softmax'))
            model.compile(loss=keras.losses.categorical_crossentropy,
                          optimizer=keras.optimizers.Adam(lr=0.00001),
                          metrics=['accuracy'])
        return model