我正在尝试为我们的 Repertory Grid Tool 进行 PCA 分析。我有一个矩阵,其中包含我需要的所有信息,但是我想将替代项的名称(列名)放在分析中的点上。我的代码是这样的:
matrixAlternatives= transpose(matrixAlternatives)
var_grid = np.array(matrixAlternatives)
#improve output readability
np.set_printoptions(precision=2)
np.set_printoptions(suppress=True)
print "var_grid:"
print var_grid
#Create the PCA node and train it
pcan = mdp.nodes.PCANode(output_dim=2, svd=True)
pcar = pcan.execute(var_grid)
print "\npcar"
print pcar
print "\neigenvalues:"
print pcan.d
print "\nexplained variance:"
print pcan.explained_variance
print "\neigenvectors:"
print pcan.v
#Graph results
#pcar[3,0],pcar[3,1] has the projections of alternative3 on the
#first two principal components (0 and 1)
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(pcar[:, 0], pcar[:, 1], 'r^')
ax.plot(pcan.v[:,0], pcan.v[:,1], 'ro')
#draw axes
ax.axhline(0, color='black')
ax.axvline(0, color='black')
#annotations each concern
id=0
for xpoint, ypoint in pcan.v:
ax.annotate('C{:.0f}'.format(id), (xpoint, ypoint), ha='center',
va='center', bbox=dict(fc='white',ec='none'))
id+=1
#calculate accounted for variance
var_accounted_PC1 = pcan.d[0] * pcan.explained_variance * 100 /(pcan.d[0] + pcan.d[1])
var_accounted_PC2 = pcan.d[1] * pcan.explained_variance * 100 /(pcan.d[0] + pcan.d[1])
#Show variance accounted for
ax.set_xlabel('Accounted variance on PC1 (%.1f%%)' % (var_accounted_PC1))
ax.set_ylabel('Accounted variance on PC2 (%.1f%%)' % (var_accounted_PC2))
canvas = FigureCanvas(fig)
response = HttpResponse(content_type='image/png')
canvas.print_png(response)
fig.clf()
plt.close()
plt.clf()
del var_grid
gc.collect()
return response