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


Python PCA.kneighbors方法代码示例

本文整理汇总了Python中sklearn.decomposition.PCA.kneighbors方法的典型用法代码示例。如果您正苦于以下问题:Python PCA.kneighbors方法的具体用法?Python PCA.kneighbors怎么用?Python PCA.kneighbors使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在sklearn.decomposition.PCA的用法示例。


在下文中一共展示了PCA.kneighbors方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: KNN_A

# 需要导入模块: from sklearn.decomposition import PCA [as 别名]
# 或者: from sklearn.decomposition.PCA import kneighbors [as 别名]
def KNN_A(rootdir, posdir, posnum, negnum_p):
    pos = []
    neg = [] 
    pathpos = []
    pathneg = []
    folders = []
    imgspos = []
    imgsneg = []
    with open('list.txt', 'r') as f:
        for line in f:
            line = line.strip()
            folders.append(line)
    gbf = igbf.GABOR_FEAT()
    for folder in folders:
        fname = os.path.join(rootdir, folder)
        if 0 == cmp(folder, posdir):
            fvs,imgs = gbf.gen_folder(fname, posnum)
            if fvs is None:
                print 'pos None ',fname
                continue
            pos.extend(fvs)
            imgspos.extend(imgs)
            pathpos.extend([folder for k in range(len(fvs))])
        else:
            fvs,imgs = gbf.gen_folder(fname, negnum_p)
            if fvs is None:
                print 'neg None ', fname
                continue
            neg.extend(fvs)
            imgsneg.extend(imgs)
            pathneg.extend([folder for k in range(len(fvs))])
    label0 = [0 for k in range(len(pos))]
    label1 = [1 for k in range(len(neg))]
    samples = np.array(pos + neg)
    labels = np.array(label0 + label1)
    paths = pathpos + pathneg
    imgs = imgspos + imgsneg
    clf = PCA(100)
    print 'before pca : ', samples.shape
    samples = clf.fit_transform(samples)
    print 'after pca : ', samples.shape
    if 0:
        clf = KNeighborsClassifier(5)
        clf.fit(samples,labels)

        res = [] 
        for k in range(samples.shape[0]):
            prd = clf.predict(samples[k,:])
            res.append((paths[k],prd))
        res = sorted(res, key = lambda k : k[0])
        line = ""
        for path, prd in res:
            line += path + ' ' + str(prd) + '\n'
        with open('result.txt', 'w') as f:
            f.writelines(line)
    else:
        clf = NearestNeighbors(5).fit(samples)
        dists,idxs = clf.kneighbors(samples, 5)
        line = ""
        for k in range(len(idxs)):
            for j in range(len(idxs[k])):
                line += paths[idxs[k][j]] + ' '
            line += '\n'
        with open('result.txt', 'w') as f:
            f.writelines(line)
    return 
开发者ID:z01nl1o02,项目名称:tests,代码行数:68,代码来源:a_knn.py


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