/
problem4.py
57 lines (41 loc) · 1.21 KB
/
problem4.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
import numpy as np
import matplotlib as plt
from matplotlib import pyplot as plt
from sklearn import svm
from sklearn.model_selection import KFold
from sklearn.metrics import confusion_matrix
from sklearn.neighbors import KNeighborsClassifier
import random
from sklearn.neighbors import KNeighborsClassifier
data = np.loadtxt("Datos misteriosos.txt")
# print(len(data[0]))
# print(data[0][0])
# print(data[661][0])
# print(data.shape)
cantidadDeDatos = data.shape[0]
cantidadDeParametros = data.shape[1]-1
#Etiquetas
y = data[:,0]
x = []
print(y)
# print(data[0][1::])
for i in range(0, cantidadDeDatos):
x.append(data[i][1:])
x = np.array(x)
kf = KFold(n_splits = 5, shuffle=True)
for i in range(1,11):
print("=================== Medición con K = ",i)
clf = KNeighborsClassifier(n_neighbors=i)
accp = 0
for train_index, test_index in kf.split(x):
x_train = x[train_index, :]
y_train = y[train_index]
clf.fit(x_train, y_train)
x_test = x[test_index, :]
y_test = y[test_index]
y_pred = clf.predict(x_test)
cm = confusion_matrix(y_test, y_pred)
print(cm)
print("acc = ", (cm[0,0]+cm[1,1])/len(y_test))
accp += (cm[0,0]+cm[1,1])/len(y_test)
print("Average accuracy is ", accp/5)