hw7

py

School

Arizona State University *

*We aren’t endorsed by this school

Course

591PYTHON

Subject

Electrical Engineering

Date

Jan 9, 2024

Type

py

Pages

3

Uploaded by UltraBoulderHare16

Report
# -*- coding: utf-8 -*- ################################### # EEE 591 Additional Assignment # # Name: Rohith Kalyan Kavadapu # # ASU ID: 1219703966 # ################################### import pandas as pd # for source data analysis import numpy as np # for generating arrays import seaborn as sns # data visualization import matplotlib.pyplot as plt # for plotting from pandas.plotting import scatter_matrix # for plotting scatter matrix #from m5_plotdr import plot_decision_regions from sklearn.model_selection import train_test_split # for splitting the data into test and train from sklearn.preprocessing import StandardScaler # for scaling feature/variable to unit variance from sklearn.linear_model import Perceptron # ML algorithm for binary classification task from sklearn.svm import SVC # Support Vector Classifier from sklearn.linear_model import LogisticRegression # ML Classification Algorithm from sklearn.metrics import accuracy_score # score for evaluating classification models from sklearn.tree import DecisionTreeClassifier # For Decision Tree Algorithm from sklearn.ensemble import RandomForestClassifier # for Random Forest Algorithm from sklearn.neighbors import KNeighborsClassifier # to be used for KNN from sklearn.tree import export_graphviz data_frame = pd.read_csv('heart1.csv') #print(data_frame) heart_data = data_frame.values X = heart_data[:,0:13] y = heart_data[:,13] y = y - 1 X_train, X_test, y_train, y_test = \ train_test_split(X,y,test_size=0.3,random_state=0) sc = StandardScaler() # create the standard scalar sc.fit(X_train) # compute the required transformation X_train_std = sc.transform(X_train) # apply to the training data X_test_std = sc.transform(X_test) # and SAME transformation of test data # Perceptron percep = Perceptron(max_iter=10, tol=1e-3, eta0=0.001, fit_intercept=True, random_state=0, verbose=True) percep.fit(X_train_std, y_train) # do the training #print('Number in test ',len(y_test)) y_pred_ppn = percep.predict(X_test_std) # now try with the test data # print('Accuracy: %.2f' % accuracy_score(y_test, y_pred)) # Calculating the
accuracy # X_combined_std=np.vstack((X_train_std,X_test_std)) # y_combined=np.hstack((y_train,y_test)) # y_combined_pred = percep.predict(X_combined_std) # Note that this only counts the samples where the predicted value was wrong #print('Misclassified samples: ' % (y_test != y_pred).sum()) # how'd we do? print('\n Accuracy of Perceptron: %.2f' % accuracy_score(y_test, y_pred_ppn)) lr = LogisticRegression(C=1, solver='liblinear', \ multi_class='ovr', random_state=0) # Logistic Regression lr.fit(X_train_std, y_train) # apply the algorithm to training data y_pred_lr = lr.predict(X_test_std) # X_combined_std = np.vstack((X_train_std, X_test_std)) # Y_combined = np.hstack((y_train, y_test)) # Y_combined_pred = lr.predict(X_combined_std) # Y_pred=lr.predict(X_test) # print('Misclassified samples: %d' % (y_test != y_pred).sum()) # print('Accuracy: %.2f\n' % accuracy_score(Y_combined, Y_combined_pred)) # X_combined_std=np.vstack((X_train_std,X_test_std)) # y_combined=np.hstack((y_train,y_test)) # y_combined_pred = percep.predict(X_combined_std) print('\nAccuracy of Logistic Regression: %.2f' % accuracy_score(y_test, y_pred_lr)) svm = SVC(kernel='linear', C=10.0, random_state=0) # Using SVM svm.fit(X_train_std, y_train) # do the training y_pred_svm = svm.predict(X_test_std) # work on the test data # X_combined_std=np.vstack((X_train_std,X_test_std)) # y_combined=np.hstack((y_train,y_test)) # y_combined_pred = percep.predict(X_combined_std) # print('Misclassified combined samples: %d' % (y_combined != y_combined_pred).sum()) # print('Combined Accuracy: %.2f\n' % accuracy_score(y_combined, y_combined_pred)) # print("**************************************************************************") print('\nAccuracy of SVM: %.2f' % accuracy_score(y_test, y_pred_svm)) tree = DecisionTreeClassifier(criterion='entropy',max_depth=3) # Decision Tree tree.fit(X_train,y_train) y_pred_dt = tree.predict(X_test_std) # X_combined = np.vstack((X_train, X_test)) # y_combined = np.hstack((y_train, y_test)) # y_pred= tree.predict(X_test) # # see how we do on the combined data # y_combined_pred = tree.predict(X_combined) # print('Misclassified samples: %d' % (y_test != y_pred).sum()) # print('Accuracy: %.2f \n' % accuracy_score(y_test, y_pred)) # print('Misclassified combined samples: %d' % \ # (y_combined != y_combined_pred).sum()) # print('Combined Accuracy: %.2f\n' % accuracy_score(y_combined, y_combined_pred)) print('\nAccuracy of Decision Tree: %.2f' % accuracy_score(y_test, y_pred_dt))
knn = KNeighborsClassifier(n_neighbors=5,p=2,metric='minkowski') # KNN knn.fit(X_train_std,y_train) # run on the test data and print results and check accuracy y_pred_knn = knn.predict(X_test_std) # print('Number in test ',len(y_test)) # print('Misclassified samples: %d' % (y_test != y_pred).sum()) # print('Accuracy: %.2f \n' % accuracy_score(y_test, y_pred)) # y_combined_pred = knn.predict(X_combined_std) # print('Misclassified combined samples: %d' % (y_combined != y_combined_pred).sum()) # print('Combined Accuracy: %.2f\n' % \ # accuracy_score(y_combined, y_combined_pred)) # print("**************************************************************************") #print('Number in test ',len(y_test)) #print('Misclassified samples: %d' % (y_test != y_pred).sum()) print('\nAccuracy of KNN: %.2f' % accuracy_score(y_test, y_pred_knn)) #for trees in [ 1, 5, 11, 101]: forest = RandomForestClassifier(criterion='entropy', n_estimators=5, \ random_state=1, n_jobs=4) # Random Forest Classifier forest.fit(X_train,y_train) y_pred_rf = forest.predict(X_test) # see how we do on the test data print('\nAccuracy of RandomForestClassifier: %.2f' % accuracy_score(y_test, y_pred_rf)) print('\nEnsemble learning wiith 3 methods') # Using 3 methods logistic regression, perceptron and SVM summa = (y_pred_svm + y_pred_ppn + y_pred_lr) y_pred_3_methods = np.where(summa > 1.5, 1, 0) print('Misclassified samples: %d' % (y_test != y_pred_3_methods).sum()) print('Accuracy while using 3 methods: %.2f' % accuracy_score(y_test, y_pred_3_methods)) print('\nEnsemble learning with 4 methods') # Using 4 methods with logistic regressiom, perceptron, SVM and KNN summa = (y_pred_svm + y_pred_ppn + y_pred_lr + y_pred_knn) y_pred_4_methods = np.where(summa > 2, 1, 0) print('Misclassified samples: %d' % (y_test != y_pred_4_methods).sum()) print('Accuracy while using 4 methods: %.2f' % accuracy_score(y_test, y_pred_4_methods)) print('\nEnsemble learning with 5 methods') # Using 5 methods with Random Forest, SVM, KNN, LR, Perceptron summa = (y_pred_svm + y_pred_knn + y_pred_lr + y_pred_rf + y_pred_ppn) y_pred_5_methods = np.where(summa > 2.5, 1, 0) print('Misclassified samples: %d' % (y_test != y_pred_5_methods).sum()) print('Accuracy while using 5 methods: %.2f' % accuracy_score(y_test, y_pred_5_methods))
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help