(self,X_test, y_test)
| 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) |
| 88 | |
| 89 | for i in range(3): # compute assess_matrix |
| 90 | numerator = matrix.iat[i, i] |
| 91 | print(numerator / precision[i]) |
| 92 | if precision[i] == 0.0: |
| 93 | assess_matrix.iat[0, i] = None |
| 94 | else: |
| 95 | assess_matrix.iat[0, i] = numerator / precision[i] |
| 96 |
no test coverage detected