Problem_2_code

py

School

Arizona State University *

*We aren’t endorsed by this school

Course

591PYTHON

Subject

Computer Science

Date

Jan 9, 2024

Type

py

Pages

3

Uploaded by UltraBoulderHare16

Report
################################################ # Results for different classification methods # # Name: Rohith Kalyan Kavadapu # # ASU ID: 1219703966 # ################################################ from sklearn import datasets # read the data sets import numpy as np # needed for arrays from sklearn.model_selection import train_test_split # splits database from sklearn.preprocessing import StandardScaler # standardize data from sklearn.linear_model import Perceptron # the algorithm from sklearn.metrics import accuracy_score from sklearn.svm import SVC import pandas as pd from sklearn.neighbors import KNeighborsClassifier from sklearn.tree import DecisionTreeClassifier # the algorithm from sklearn.linear_model import LogisticRegression from sklearn.ensemble import RandomForestClassifier data_frame = pd.read_csv('heart1.csv') data_frame =data_frame[0:] corr=data_frame.corr() corr *= np.tri(*corr.values.shape, k=-1).T print(corr) X=data_frame[['age','sex','cpt','rbp','sc','fbs','rer','mhr','eia','opst','nmvcf',' thal']] y=data_frame['a1p2'] X_train,X_test,y_train,y_test=train_test_split(X,y,test_size=0.35) sc=StandardScaler() sc.fit(X_train) X_train_std = sc.transform(X_train) X_test_std = sc.transform(X_test) percep = Perceptron(max_iter=50,tol=1e- 3,eta0=0.001,fit_intercept=True,random_state=0,verbose=True) # Perceptron method percep.fit(X_train_std,y_train) # taking 65% and 35% split between training and testing y_pred=percep.predict(X_test_std) print("\n") print("Perceptron Algorithm\n") print('Misclassified samples: %d' % (y_test != y_pred).sum()) # Calculating misclassified samples 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) print('Misclassified combined samples: %d' % (y_combined != y_combined_pred).sum()) print('Combined Accuracy: %.2f\n' % accuracy_score(y_combined, y_combined_pred)) print("**************************************************************************") ### Support Vector Machine Algorithm c_val=2.0
svm = SVC(kernel='linear', C=c_val, random_state=0) svm.fit(X_train_std, y_train) # do the training y_pred = svm.predict(X_test_std) # work on the test data # show the results print("Support Vector Machine Algorithm\n") print('For C value =',c_val) 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)) 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("**************************************************************************") ### K nearest Neighbors#### neighs=18 print("K Nearest Neighbors") print('NEIGHBORS:',neighs) knn = KNeighborsClassifier(n_neighbors=neighs,p=3,metric='minkowski') knn.fit(X_train_std,y_train) # run on the test data and print results and check accuracy y_pred = 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("Decision Tree Algorithm") # Decision Trees tree = DecisionTreeClassifier(criterion='entropy',max_depth=6 ,random_state=0) tree.fit(X_train,y_train) 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("**************************************************************************") c_val=10; print("Logistic Regression\n") #Logistic Regression lr = LogisticRegression(C=c_val, solver='liblinear', \ multi_class='ovr', random_state=0) lr.fit(X_train_std, y_train) # apply the algorithm to training data # combine the train and test data 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('Misclassified combined samples: %d' % (y_combined != y_combined_pred).sum()) print('Combined Accuracy: %.2f\n' % accuracy_score(y_combined, y_combined_pred)) print("**************************************************************************") trees=51 print("Random Forest\n") # Randon forest print("Number of trees: ",trees) # create the classifier and train it # n_estimators is the number of trees in the forest # n_jobs allows multiple processors to be used forest = RandomForestClassifier(criterion='entropy', n_estimators=trees, \ random_state=1, n_jobs=4) forest.fit(X_train,y_train) y_pred = forest.predict(X_test) # see how we do on the test data 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)) X_combined = np.vstack((X_train, X_test)) Y_combined = np.hstack((y_train, y_test)) Y_combined_pred = forest.predict(X_combined) print('Misclassified samples: %d' % (Y_combined != y_combined_pred).sum()) print('Combined Accuracy: %.2f' % \ accuracy_score(Y_combined, Y_combined_pred)) print("**************************************************************************")
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