本文整理汇总了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