def dtree_classifier(X_train, y_train, X_test, y_test, plotting): clf = DecisionTreeClassifier(random_state=3169, max_depth=3) st = time.time() clf.fit(X_train, y_train) end = time.time() train_time = end - st stt = time.time() y_pred = clf.predict(X_test) endt = time.time() y_test = np.array(y_test) y_pred = np.array(y_pred) title = 'Confusion Matrix with Decision Tree' #plot_tree(clf.fit(X_train, y_train),filled=True) plt.show() clf_name = 'dtree' if plotting == True: plot_learning_curve(clf, 'Learning Curve for Decision Tree', X_train, y_train, (0.7, 1.01), n_jobs=4) plot_validation_curve(X_train, y_train, clf, clf_name) confusion(y_test, y_pred, title) print('Accuracy for Decision Tree classifier is ' + str(accuracy_score(y_test, y_pred))) print('Training time for Decision Tree classifier: ' + str(train_time) + ' seconds') print('Testing time for Decision Tree classifier: ' + str(endt - stt) + ' seconds') print()
def knn_classifier(X_train, y_train, X_test, y_test, neighbors, plotting): clf = KNeighborsClassifier(n_neighbors=2) st = time.time() clf.fit(X_train, y_train) end = time.time() train_time = end - st stt = time.time() y_pred = clf.predict(X_test) endt = time.time() y_test = np.array(y_test) y_pred = np.array(y_pred) title = 'Confusion Matrix with KNN' clf_name = 'knn' if plotting == True: plot_learning_curve(clf, 'Learning Curve for KNN', X_train, y_train, (0.7, 1.01), n_jobs=4) plot_validation_curve(X_train, y_train, clf, clf_name) confusion(y_test, y_pred, title) print('Accuracy for KNN classifier is ' + str(accuracy_score(y_test, y_pred))) print('Training time for KNN classifier: ' + str(train_time) + ' seconds') print('Testing time for KNN SVM classifier: ' + str(endt - stt) + ' seconds') print()
def neural_net(X_train, y_train, X_test, y_test, learning_rate, plotting): clf = MLPClassifier(activation='tanh', alpha=1e-03, batch_size='auto', learning_rate='adaptive', learning_rate_init=learning_rate, solver='adam') st = time.time() clf.fit(X_train, y_train) end = time.time() train_time = end - st stt = time.time() y_pred = clf.predict(X_test) endt = time.time() y_test = np.array(y_test) y_pred = np.array(y_pred) title = 'Confusion Matrix with Neural Network' clf_name = 'neural_net' if plotting == True: plot_learning_curve(clf, 'Learning Curve for Neural Network', X_train, y_train, (0.7, 1.01), n_jobs=4) plot_validation_curve(X_train, y_train, clf, clf_name) confusion(y_test, y_pred, title) print('Accuracy for Neural Network is ' + str(accuracy_score(y_test, y_pred))) print('Training time for Neural Network: ' + str(train_time) + ' seconds') print('Testing time for Neural Network: ' + str(endt - stt) + ' seconds') print()
def svm_classifier(X_train, y_train, X_test, y_test, kernel, gamma, plotting): # Apply SVM clf = svm.SVC(gamma='auto', random_state=3169, kernel=kernel, C=4) st = time.time() clf.fit(X_train, y_train) end = time.time() train_time = end - st stt = time.time() y_pred = clf.predict(X_test) endt = time.time() y_test = np.array(y_test) y_pred = np.array(y_pred) title = 'Confusion Matrix with SVM' clf_name = 'svm' print(str(clf.get_params)) if plotting == True: plot_learning_curve(clf, 'Learning Curve for SVM', X_train, y_train, (0.7, 1.01), n_jobs=5) plot_validation_curve(X_train, y_train, clf, clf_name) confusion(y_test, y_pred, title) print('Accuracy for SVM classifier is ' + str(accuracy_score(y_test, y_pred))) print('Training time for SVM classifier: ' + str(train_time) + ' seconds') print('Testing time for SVM classifier: ' + str(endt - stt) + ' seconds')
def boost_classifier(X_train, y_train, X_test, y_test, plotting): clf = DecisionTreeClassifier(random_state=3169, max_depth=2, min_samples_leaf=1) boost_clf = AdaBoostClassifier(base_estimator=clf, random_state=3169, n_estimators=100) st = time.time() boost_clf.fit(X_train, y_train) end = time.time() train_time = end - st stt = time.time() y_pred = boost_clf.predict(X_test) endt = time.time() y_test = np.array(y_test) y_pred = np.array(y_pred) title = 'Confusion Matrix with Boosting' #plot_tree(clf.fit(X_train, y_train),filled=True) plt.show() clf_name = 'boosting' if plotting == True: plot_learning_curve(boost_clf, 'Learning Curve for Boosting', X_train, y_train, (0.7, 1.01), n_jobs=4) plot_validation_curve(X_train, y_train, boost_clf, clf_name) confusion(y_test, y_pred, title) print('Accuracy for Boosting classifier is ' + str(accuracy_score(y_test, y_pred))) print('Training time for Boosting classifier: ' + str(train_time) + ' seconds') print('Testing time for Boosting classifier: ' + str(endt - stt) + ' seconds') print()