本文整理匯總了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)