def constructSimilartyMatrixCosinePCA(self):
#This is a simpole k nearest neighbour approach based on the cosine distance
#for this takefrom modshogun import RealFeatures, MulticlassLabels
#then find the k nearest neighbours for each node
ks=[3,5,7,10,12,15,20,22,25,27,30,33,35,37,40,43,45,47,50,53,55,57,60,65]
accs=[]
for k in ks:
self.pwdis=pairwise_distances(self.allDataPCA,metric='cosine')
#now we have all the pairwise cosine distances between all the sentences
#now we need to do a knnNeighbour search
#now we can construct the diagonal weight marix , which has the sum of all the weights
self.D=np.zeros(self.pwdis.shape)
for i in range(0,self.pwdis.shape[0]):
l1=self.pwdis[i].tolist()
#print 'l1 is ',l1,'\n\n'
allnearestNeighbours=sorted(range(len(l1)),key=lambda i : l1[i])
#now set the all the weights except for k+1 to 0
self.pwdis[i,allnearestNeighbours[k:]]=0
self.D[i,i]=sum(self.pwdis[i])
print 'Now computing accuracy for cosine metric on PCA data'
accs.append(self.labelPropogation())
plt.figure()
plt.plot(ks,accs)
plt.title('Plot of accuracy vs k using cosine PCA data')
plt.savefig(pp,format='pdf')
plt.close()
#now we have the weight matrix graph based on the cosine distance
#print 'self.D is ',self.D
graphssl.py 文件源码
python
阅读 23
收藏 0
点赞 0
评论 0
评论列表
文章目录