def constructCovarianceMatrix(self):
#this function constructs the covariance matrix for the dataset and then does a label propagation over it
self.covarianceMatrix=np.cov(self.trainVectorsPCA.T) #as numpy treats them as column vetcors
self.inverseCovarianceMatrix=np.linalg.inv(self.covarianceMatrix)
#compute the cholesky decomposition and then transform the data into the new space
self.L_cov=np.linalg.cholesky(self.covarianceMatrix)
self.allDataCov=np.dot(self.allDataPCA,self.L_cov.T)
self.pwdis=pairwise_distances(self.allDataCov)
self.D=np.zeros(self.pwdis.shape)
projectedDigits=TSNE(random_state=randomState).fit_transform(self.allDataCov)
plt.figure()
plt.scatter(projectedDigits[:,0],projectedDigits[:,1],c=self.labels)
plt.title('Data projected by Covariance Matrix in Mahalanobis metric')
plt.savefig(pp,format='pdf')
plt.close()
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:
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]+0.01)
print 'accuracy by using Covariance Matrix for Mahalanobis Distance for k= ',k,'\n'
accs.append(self.labelPropogation())
plt.figure()
plt.plot(ks,accs)
plt.title('Plot of accuracy vs k using Covariance Matrix in Mahalanobis metric')
plt.savefig(pp,format='pdf')
graphssl.py 文件源码
python
阅读 22
收藏 0
点赞 0
评论 0
评论列表
文章目录