Python Algorithm Implementation
from scipy import stats
from pylab import *
def knnClassify(Target_feature, dataSet, p, k): dataSet_x = dataSet[:, 0:-1] dataSet_y = dataSet[:, -1] L_p = [] for i in range(len(dataSet_x)): l_p = 0 for x_i in range(len(dataSet_x[i])): l_p = (abs(dataSet_x[i][x_i] - Target_feature[x_i]))**p+l_p l_p =math.pow(l_p, 1/p) L_p.append(l_p) L_p = np.array(L_p) y_i = L_p.argsort() y = [] max_L_p = [] for k_i in range(0,k): y.append(dataSet_y[y_i[k_i]]) max_L_p.append(L_p[y_i[k_i]]) print(y) max_L = max(max_L_p) y_result = stats.mode(y)[0][0] return y_result,max_L
if __name__ == '__main__': Target_feature = np.array([1, 3]) dataSet = np.array([[1, 4, 1], [0.5, 2, 1], [2, 2.3, 1], [1, 0.5, -1], [2, 1, -1], [4, 1, -1], [3.5, 4, 1], [3, 2.2, -1]]) y,max_r = knnClassify(Target_feature=Target_feature,dataSet=dataSet,p=2,k=3) print(y,max_r)
fig = plt.figure() plt.subplot(111)
x_x1 = dataSet[dataSet[:, -1] == -1][:, :-1][:, 0].tolist() x_x2 = dataSet[dataSet[:, -1] == -1][:, :-1][:, 1].tolist() o_x1 = dataSet[dataSet[:, -1] == 1][:, :-1][:, 0].tolist() o_x2 = dataSet[dataSet[:, -1] == 1][:, :-1][:, 1].tolist()
plt.scatter(x_x1, x_x2, marker='x', s=20, c='red') plt.scatter(o_x1, o_x2, marker='o', s=20, c='blue')
# Draw Circle a = np.arange(Target_feature[0] - max_r, Target_feature[0] + max_r, 0.0001) b1 = np.sqrt(np.square(max_r) - np.square(a - Target_feature[0])) + Target_feature[1] b2 = Target_feature[1] - np.sqrt(np.square(max_r) - np.square(a - Target_feature[0])) plt.plot(a, b1, color='g', linestyle=':') plt.plot(a, b2, color='g', linestyle=':') plt.scatter(Target_feature[0], Target_feature[1], c='g', marker='o',s=200)
plt.xlabel('X(1)') plt.ylabel('X(2)') plt.show()
The format is dull, but still, dare to ask.