-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3. Measure performance.py
More file actions
55 lines (36 loc) · 1.33 KB
/
3. Measure performance.py
File metadata and controls
55 lines (36 loc) · 1.33 KB
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
from sklearn import datasets
from sklearn.neighbors import KNeighborsClassifier
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
iris = datasets.load_iris()
from sklearn.model_selection import train_test_split
x= iris['data'] #features
y= iris['target'] #target
#Train/test split
x_train, x_test, y_train, y_test = train_test_split(x, y,
test_size=0.3,
random_state=21,
stratify=y)
knn = KNeighborsClassifier(n_neighbors=8)
knn.fit(x_train, y_train)
y_pred = knn.predict(x_test)
print('Test set predictions:\n{}'.format(y_pred))
knn.score(x_test, y_test)
#
#Underfitting and overfitting
neighbors = np.arange(1, 9)
train_accuracy = np.empty(len(neighbors))
test_accuracy = np.empty(len(neighbors))
for i, k in enumerate(neighbors):
knn = KNeighborsClassifier(n_neighbors=k)
knn.fit(x_train,y_train)
train_accuracy[i] = knn.score(x_train, y_train)
test_accuracy[i] = knn.score(x_test, y_test)
plt.title('k-NN: Varying Number of Neighbors')
plt.plot(neighbors, test_accuracy, label = 'Testing Accuracy')
plt.plot(neighbors, train_accuracy, label = 'Training Accuracy')
plt.legend()
plt.xlabel('Number of Neighbors')
plt.ylabel('Accuracy')
plt.show()