本文整理匯總了Python中KNN.file_to_matrix方法的典型用法代碼示例。如果您正苦於以下問題:Python KNN.file_to_matrix方法的具體用法?Python KNN.file_to_matrix怎麽用?Python KNN.file_to_matrix使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類KNN
的用法示例。
在下文中一共展示了KNN.file_to_matrix方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: test_non_norm
# 需要導入模塊: import KNN [as 別名]
# 或者: from KNN import file_to_matrix [as 別名]
def test_non_norm():
dating_mat, dating_label = KNN.file_to_matrix('datingTestSet2.txt')
for i in range(30):
print dating_mat[i], dating_label[i]
fig = plt.figure()
ax = fig.add_subplot(111)
ax.scatter(dating_mat[:, 0], dating_mat[:, 1],
15.0 * array(dating_label), 15.0 * array(dating_label))
plt.show()
示例2: date_class_test
# 需要導入模塊: import KNN [as 別名]
# 或者: from KNN import file_to_matrix [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))