-
Notifications
You must be signed in to change notification settings - Fork 5
/
train_k_nearest_neighbours_1d.py
56 lines (48 loc) · 2.19 KB
/
train_k_nearest_neighbours_1d.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
from sklearn.preprocessing import LabelEncoder
from read_data_1d_other_classifiers import read_data
from sklearn.model_selection import StratifiedKFold
from sklearn.neighbors import KNeighborsClassifier
import numpy as np
import random
from sklearn.metrics import accuracy_score
from sklearn.model_selection import train_test_split
def run():
X, y = read_data()
skf = StratifiedKFold(n_splits=10, shuffle=True)
skf.get_n_splits(X, y)
label_encoder = LabelEncoder()
integer_encoded = label_encoder.fit_transform(y)
y = integer_encoded.reshape(len(integer_encoded), 1)
combined = list(zip(X, y))
random.shuffle(combined)
X[:], y[:] = zip(*combined)
'''
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33, random_state=42)
clf = KNeighborsClassifier(n_neighbors=100)
print("Training new iteration on " + str(X_train.shape[0]) + " training samples, " + str(
X_test.shape[0]) + " validation samples, this may be a while...")
history = clf.fit(X_train, y_train.ravel())
y_pred = clf.predict(X_test)
acc = accuracy_score(y_test, np.array(y_pred))
'''
accs = []
for index, (train_index, test_index) in enumerate(skf.split(X, y)):
X_train, X_test = X[train_index], X[test_index]
y_train, y_test = y[train_index], y[test_index]
clf = KNeighborsClassifier(n_neighbors=100)
print("Training new iteration on " + str(X_train.shape[0]) + " training samples, " + str(
X_test.shape[0]) + " validation samples, this may be a while...")
history = clf.fit(X_train, y_train.ravel())
y_pred = clf.predict(X_test)
predicted_class1 = np.zeros(y_pred.shape)
acc = accuracy_score(y_test, np.array(y_pred))
accs.append(acc)
print("Accuracy " + str(index + 1) + " is: " + str(acc * 100) + "%")
print("Average accuracy " + str(np.mean(accs) * 100) + "%")
with open("Results_other_classifiers.txt", "a+") as f:
f.write(
"Accuracy for K Nearest Neighbours Classifier with 100 neighbours with 1d data with 10-fold cross validation is: " + str(
np.mean(accs) * 100) + "%" + "\n")
f.close()
if __name__ == "__main__":
run()