-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathcode.py
99 lines (64 loc) · 2.48 KB
/
code.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# Importing the libraries
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
from sklearn.metrics import accuracy_score
# Importing the dataset
df = pd.read_csv('cancer.csv')
df.replace('?',-99999,inplace=True)
df.drop(['id'],1,inplace=True)
X=np.array(df.drop(['classes'],1))
y=np.array(df['classes'])
# Splitting the dataset into the Training set and Test set
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.35, random_state = 42)
# Feature Scaling
from sklearn.preprocessing import StandardScaler
sc = StandardScaler()
X_train = sc.fit_transform(X_train)
X_test = sc.transform(X_test)
#principle component analysis
from sklearn.decomposition import PCA
pca = PCA(n_components=2)
X_train = pca.fit_transform(X_train)
X_test = pca.fit_transform(X_test)
explained_variance=pca.explained_variance_ratio_
# Fitting KNN to the Training set
from sklearn.neighbors import KNeighborsClassifier
knn = []
for i in range(1,21):
classifier = KNeighborsClassifier(n_neighbors=i)
trained_model=classifier.fit(X_train,y_train)
trained_model.fit(X_train,y_train )
# Predicting the Test set results
y_pred = classifier.predict(X_test)
# Making the Confusion Matrix
from sklearn.metrics import confusion_matrix
cm_KNN = confusion_matrix(y_test, y_pred)
print(cm_KNN)
print("Accuracy score of train KNN")
print(accuracy_score(y_train, trained_model.predict(X_train))*100)
print("Accuracy score of test KNN")
print(accuracy_score(y_test, y_pred)*100)
knn.append(accuracy_score(y_test, y_pred)*100)
plt.figure(figsize=(12, 6))
plt.plot(range(1, 21),knn, color='red', linestyle='dashed', marker='o',
markerfacecolor='blue', markersize=10)
plt.title('Accuracy for different K Value')
plt.xlabel('K Value')
plt.ylabel('Accuracy')
# Fitting SVM to the Training set
from sklearn.svm import SVC
classifier = SVC(kernel = 'linear', random_state = 0)
trained_model=classifier.fit(X_train,y_train)
trained_model.fit(X_train,y_train )
# Predicting the Test set results
y_pred = classifier.predict(X_test)
# Making the Confusion Matrix
from sklearn.metrics import confusion_matrix
cm_SVM = confusion_matrix(y_test, y_pred)
print(cm_SVM)
print("Accuracy score of train SVM")
print(accuracy_score(y_train, trained_model.predict(X_train))*100)
print("Accuracy score of test SVM")
print(accuracy_score(y_test, y_pred)*100)