# training try: myDML.train(moons_train) except Exception as e: print(e) sys.exit(-1) # evaluating for K in [1, 3, 5]: # testing with Euclidean_distance predict_label = np.zeros(test_X.shape[0]) for i in range(test_X.shape[0]): distance_vector = np.zeros(train_X.shape[0]) for j in range(train_X.shape[0]): distance_vector[j] = myDML.Euclidean_distance( test_X[i], train_X[j]) labels_of_K_neighbor = train_Y[distance_vector.argsort()[0:K]] predict_label[i] = collections.Counter( labels_of_K_neighbor).most_common(n=1)[0][0] test_error = np.sum(predict_label != test_Y) / test_Y.shape[0] print('Euclidean_distance+knn(k=' + str(K) + '): \t%f' % test_error) # testing with your distance predict_label = np.zeros(test_X.shape[0]) for i in range(test_X.shape[0]): distance_vector = np.zeros(train_X.shape[0]) for j in range(train_X.shape[0]): distance_vector[j] = myDML.distance(test_X[i], train_X[j]) labels_of_K_neighbor = train_Y[distance_vector.argsort()[0:K]] predict_label[i] = collections.Counter( labels_of_K_neighbor).most_common(n=1)[0][0]
# training try: myDML.train(moons_train) except Exception as e: print(e) sys.exit(-1) # evaluating for K in [1, 3, 5]: # testing with Euclidean_distance predict_label = np.zeros(test_X.shape[0]) for i in range(test_X.shape[0]): distance_vector = np.zeros(train_X.shape[0]) for j in range(train_X.shape[0]): distance_vector[j] = myDML.Euclidean_distance( test_X[i], train_X[j]) #计算测试集和训练集之间的距离 labels_of_K_neighbor = train_Y[distance_vector.argsort() [0:K]] #取前k小个进行投票 predict_label[i] = collections.Counter( labels_of_K_neighbor).most_common( n=1)[0][0] #Counter计算元素的个数 most_common取TopN列表 test_error = np.sum(predict_label != test_Y) / test_Y.shape[0] print('Euclidean_distance+knn(k=' + str(K) + '): \t%f' % test_error) # testing with your distance predict_label = np.zeros(test_X.shape[0]) for i in range(test_X.shape[0]): distance_vector = np.zeros(train_X.shape[0]) for j in range(train_X.shape[0]): distance_vector[j] = myDML.distance(test_X[i], train_X[j]) labels_of_K_neighbor = train_Y[distance_vector.argsort()[0:K]]