本文整理匯總了Python中KNN.knn_classify方法的典型用法代碼示例。如果您正苦於以下問題:Python KNN.knn_classify方法的具體用法?Python KNN.knn_classify怎麽用?Python KNN.knn_classify使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類KNN
的用法示例。
在下文中一共展示了KNN.knn_classify方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: test_simple
# 需要導入模塊: import KNN [as 別名]
# 或者: from KNN import knn_classify [as 別名]
def test_simple():
data_set, labels = KNN.create_data_set()
test1 = array([1.2, 1.0])
test2 = array([0.1, 0.3])
k = 3
output_label1 = KNN.knn_classify(test1, data_set, labels, k)
output_label2 = KNN.knn_classify(test2, data_set, labels, k)
print test1, output_label1
print test2, output_label2
示例2: date_class_test
# 需要導入模塊: import KNN [as 別名]
# 或者: from KNN import knn_classify [as 別名]
def date_class_test():
ratio = 0.04 # ratio of the test examples
# data_set:1000*3, data_labels: 1000*1
data_set, data_labels = KNN.file_to_matrix('datingTestSet2.txt')
# normilize the data_set. Note: data_labels is not nessary to normlize
norm_set, ranges, min_val = KNN.normalize(data_set)
all_rows = norm_set.shape[0] # number of all rows
test_rows = int(ratio * all_rows) # number of test rows
error_num = 0
for i in range(test_rows):
# return the predict labels
label_res = KNN.knn_classify(norm_set[i, :], norm_set[test_rows: all_rows, :],\
data_labels[test_rows: all_rows, :], 3)
print 'Classifier predict: %d, real result is: %d' % (label_res, data_labels[i])
if label_res != data_labels[i]:
error_num += 1
print 'total error rate is: %f ' % (error_num * 1.0 / float(test_rows))