本文整理汇总了Python中KNN.knearest方法的典型用法代码示例。如果您正苦于以下问题:Python KNN.knearest方法的具体用法?Python KNN.knearest怎么用?Python KNN.knearest使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类KNN
的用法示例。
在下文中一共展示了KNN.knearest方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: cross_validation_nn
# 需要导入模块: import KNN [as 别名]
# 或者: from KNN import knearest [as 别名]
def cross_validation_nn(k,folds_array):
#Initial values
corrects = 0
incorrects = 0
#Separate train and test data
for i in range(0,10):
training_data = []
test_data = []
for j in range(0,10):
if j == i:
test_data = folds_array[j]
else:
training_data = training_data + folds_array[j]
#Predict values
for j in range(0,len(test_data)):
prediction = KNN.knearest(k,training_data,test_data[j],True)
length = len(test_data[j])-1
#Check if the value is correct
if prediction == test_data[j][length]:
corrects = corrects + 1
else:
incorrects = incorrects + 1
return float(corrects)/float(corrects+incorrects)
示例2: open
# 需要导入模块: import KNN [as 别名]
# 或者: from KNN import knearest [as 别名]
train = NaiveBayes.train_nb(data)
#Read example data
f = open(examples, 'r')
#Test every example
for line in f:
array_line = line.split(',')
row = []
length = len(array_line)
for i in range (0,length):
row.append(float(array_line[i]))
#Apply the algorithm
if algorithm != 'NB':
print KNN.knearest(int(algorithm),data,row)
else:
print NaiveBayes.naive_bayes(row,train)
##############################
# #
# Cross validation called #
# when need it #
# #
##############################
#folds = CrossValidation.fold_divide(data)
#print CrossValidation.cross_validation_nn(1,folds)
#print CrossValidation.cross_validation_nb(folds)