本文整理匯總了Python中KNN.train方法的典型用法代碼示例。如果您正苦於以下問題:Python KNN.train方法的具體用法?Python KNN.train怎麽用?Python KNN.train使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類KNN
的用法示例。
在下文中一共展示了KNN.train方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: classify_function
# 需要導入模塊: import KNN [as 別名]
# 或者: from KNN import train [as 別名]
def classify_function(filename, method, k, kernal,n):
if filename == '':
raise data.ValidationError('please select file')
X, Y, subjectID = data.load_data("control_features_combinedSubject.txt", "dementia_features_combinedSubject.txt")
X = data.get_useful_features_mat(X)
alz_count = 0
for y in Y:
if y:
alz_count = alz_count + 1
#normalize features
X_scaled, normalizer = data.normalize_features(X)
#print X_scaled
#print filename
testList = []
filenameSubj = filename[0:3]
testList.append(filenameSubj)
try:
X_train, Y_train, X_test, Y_test, trainID, testID = data.split_train_test(X_scaled,Y,subjectID,testID=testList)
except ValueError:
print filenameSubj
raise data.ValidationError("combined data missing!")
#PCA
if n < 1 and n != -1:
raise data.ValidationError('# features has to be greater than 0')
elif n > 16:
raise data.ValidationError('# features has to be less than 16')
elif (n >= 1 and n <= 16):
pca, explained_variance_ratio_ = data.reduce_dimension(X_train, n)
X_train = pca.transform(X_train)
#load the real testing data! Y_test should remain the same!
X_test = data.load_testing_visit("control_features_per_visit.txt", "dementia_features_per_visit.txt", filename[0:5])
#print "visit"
#print X_test
X_test = data.get_useful_features_mat(X_test)
#print "visit"
#print X_test
X_test = normalizer.transform(X_test)
#print "visit"
#print X_test
#if use PCA
if (n >= 1 and n <= 16):
X_test = pca.transform(X_test)
#print "visit"
#print X_test
if method == 0:
raise data.ValidationError('please select classification method')
#SVM
elif method == 2:
if kernal == 0:
raise data.ValidationError('please select kernel')
clf = SVM.train(X_train,Y_train,kernal)
result = SVM.test(X_test,clf)
#print X_train
#print X_test
#print result
#print clf
#KNN
elif method == 1:
if k == '':
raise data.ValidationError('please select number of neighbors (k)')
neigh = KNN.train(X_train,Y_train,k)
result = KNN.test(X_test,neigh)
return result[0], Y_test[0]