当前位置: 首页>>代码示例>>Python>>正文


Python KNN.train方法代码示例

本文整理汇总了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]
开发者ID:annajh,项目名称:AD,代码行数:72,代码来源:UI.py


注:本文中的KNN.train方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。