class Classification(Supervised): def __init__(self, X, y, split=True, split_ratio=0.2): Supervised.__init__(self, X, y, split, split_ratio) self.LR = None self.DTC = None self.RFC = None self.GNB = None def fit(): """ Acronyms ---------- LR : Logistic Regression DTC : Decision Tree Classifier RFC : Random Forest Classifier GNB : Gaussian Naive Bayes Returns ------- None """ self.LR = LogisticRegression(random_state=0).fit(X_train, y_train) self.DTC = DecisionTreeClassifier().fit(X_train, y_train) self.RFC = RandomForestClassifier(max_depth=None, random_state=0).fit( X_train, y_train) self.GNB = GaussianNB().fit(X_train, y_train) def evaluate(): if (self.X_test != None): lr_eval = self.LR.evaluate(X_test, y_test) dtc_eval = self.DTC.evaluate(X_test, y_test) rfc_eval = self.RFC.evaluate(X_test, y_test) gnb_eval = self.GNB.evaluate(X_test, y_test)
return precision def f1_m(y_true, y_pred): precision = precision_m(y_true, y_pred) recall = recall_m(y_true, y_pred) return 2*((precision*recall)/(precision+recall+K.epsilon())) # define the keras model model = Sequential() model.add(Dense(8, 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 history = model.fit(np.asarray(X_train), np.asarray(y_train), epochs=15, batch_size=1) # evaluate the keras model _, accuracy = model.evaluate(np.asarray(X_test), np.asarray(y_test)) print('Accuracy: %.2f' % (accuracy*100)) # make class predictions with the model expected = np.asarray(y_test) predicted = model.predict_classes(np.asarray(X_test)) # summarize the fit of the model classification_model = metrics.classification_report(expected, predicted) confusion_model = metrics.confusion_matrix(expected, predicted) accuracy_model = metrics.accuracy_score(expected, predicted) print(classification_model) print(confusion_model) print("Accuracy NN with best parameters: ",accuracy_model) #Save metrics with open(save_report, "a") as text_file: text_file.write("\nAccuracy NN with best parameters: ")
# fix random seed for reproducibility np.random.seed(7) model = Sequential() model.add(Dense(13, input_dim=13, activation='relu')) model.add(Dense(7, activation='relu')) model.add(Dense(5, activation='relu')) model.add(Dense(1, activation='sigmoid')) model.compile(optimizer='Adam', loss='binary_crossentropy', metrics=['accuracy']) ann = model.fit(X_train, y_train, epochs=400, batch_size=1024, validation_data = (X_test,y_test), shuffle=True) (loss_score, accuracy_score) = model.evaluate(X_test,y_test,verbose=0) print('Loss score',loss_score) print('Accuracy score',accuracy_score) plt.plot(ann.history['accuracy']) plt.plot(ann.history['val_accuracy']) plt.title('Model Accuracy') plt.ylabel('Accuracy') plt.xlabel('Epochs') plt.legend(['train', 'test']) plt.show() plt.plot(ann.history['loss']) plt.plot(ann.history['val_loss']) plt.title('Model Loss') plt.ylabel('Loss')
model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy']) training = model.fit(Xtrain.values, ytrain.values, validation_split=0.1, epochs=200, batch_size=10, verbose=2) model.summary() """## Testing the accuracy""" accuracy = model.evaluate(Xtrain, ytrain) print(accuracy) # Resultate mit tatsächlichen Werten vergleichen print(model.predict_classes(Xtest)) print("[", end="") for i in ytest: print(i, end=" ") print("\b]") """## Plotting the results""" from matplotlib import pyplot as plt
Dense(2), Activation('softmax'), ]) model.compile(loss='binary_crossentropy', optimizer='sgd', metrics=['accuracy']) y_train_one_hot = keras.utils.to_categorical(y_train) y_test_one_hot = keras.utils.to_categorical(y_test) model.fit(x_train_neural, y_train_one_hot, epochs=5, batch_size=32) toc = timeit.default_timer() loss_and_metrics = model.evaluate(x_test_neural, y_test_one_hot, batch_size=128) print('loss: ', loss_and_metrics[0]) print('accuracy: ', loss_and_metrics[1]) print() probs = model.predict(x_test_neural, batch_size=128) print(probs[0]) classes = np.argmax(probs, axis=1) #print('Predicted class: ',classes[0],'and probability: ',probs[0,classes[0]]) print(confusion_matrix(y_test, classes)) print('time:', toc - tic) tic = timeit.default_timer()