gpt4 book ai didi

python - 使用 numpy 和 matplotlib 进行 PCA 标注的困难

转载 作者:行者123 更新时间:2023-11-30 23:34:25 25 4
gpt4 key购买 nike

我正在尝试对我们的 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

最佳答案

如果我理解正确的话,你只需要使用列标题来注释你的图。这是一个最小的例子:

import matplotlib.pylab as plt
import numpy as np

x = np.linspace(0, 10 ,100)
y = np.sin(x)

plt.plot(x, y , "ro")
plt.annotate(s=" some string", xy=(x[25], y[25]))

example

我怀疑您需要添加一些格式才能将字符串放在正确的位置。

关于python - 使用 numpy 和 matplotlib 进行 PCA 标注的困难,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18149594/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com