MCPcopy Index your code
hub / github.com/Giyn/DataMiningVisualizationSystem / KNN

Class KNN

MachineLearningAlgorithm/KNN/KNN.py:30–193  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

28
29"""
30class KNN:
31 def __init__(self,X_train,y_train):
32 self.X_train = X_train
33 self.y_train =y_train
34 self.assess_matrix = None
35 self.presion = 0
36
37
38
39 def fit(self,X_test, y_test):
40 k = 11 # 超参数取11
41 matrix = DataFrame(np.zeros((3,3)),index=['pear', 'ginkgo', 'poplar'],columns=['pear', 'ginkgo', 'poplar'])
42 predict_true = 0 # the num of right predicted
43 max = X_test.shape[0] # the max num of iteration
44 y_predict = []
45 for i in range(max):
46 x_p = X_test[i]
47 y_p = y_test[i]
48
49 distances = [np.sqrt(np.sum((x_p - x) ** 2)) for x in self.X_train]
50 # calculate the distance between point in x_p and point in x
51 d = np.sort(distances)
52 # sort the distances
53 nearest = np.argsort(distances)
54 # the index of sorted data
55 # print(nearest)
56
57 topk_y = [self.y_train[j] for j in nearest[:k]]
58 # select k nearest num
59
60 classCount = {}
61 for i in range(0, k):
62 voteLabel = topk_y[i]
63 weight = gaussian(distances[nearest[i]])
64 # print(index, dist[index],weight)
65 ## 这里不再是加一,而是权重*1
66 classCount[voteLabel] = classCount.get(voteLabel, 0) + weight * 1
67
68 maxCount = 0
69
70 for key, value in classCount.items():
71 if value > maxCount:
72 maxCount = value
73 classes = key
74 # select the type of max num
75 if (classes == y_p):
76 predict_true += 1
77 y_predict.append(classes)
78
79 for i in range(len(y_predict)):
80 matrix.loc[y_predict[i]][y_test[i]] += 1
81
82 accuracy = float(predict_true / max)
83 assess_matrix = DataFrame(np.zeros((2, 3)), index=['precision', 'recall'],
84 columns=['pear', 'ginkgo', 'poplar'])
85
86 precision = matrix.apply(lambda x: x.sum())
87 recall = matrix.apply(lambda x: x.sum(), axis=1)

Callers 1

KNNTrainingFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected