我正在尝试构建一个循环遍历范围的脚本,并保存一个主成分与其他主成分的所有图形,没有重复,没有相同与相同,---例如,如果我生成 3 乘 3 的对称矩阵,我将拥有3 个有意义的数字:fig_1_2、fig_1_3 和 fig_2_3。我带来了那个有问题的解决方案
#!/usr/env python
import mdp
import numpy as np
import matplotlib.pyplot as plt
#
set1 = 'set1_smthing.txt'
set2 = 'set2_smthing.txt'
set3 = 'set3_smthing.txt'
bname = set1.split(".")[0].split("_")[0]
set1d = np.loadtxt(set1, delimiter=',')
set2d = np.loadtxt(set2, delimiter=',')
set3d = np.loadtxt(fchembl, delimiter=',')
set_comb = np.vstack([set1d,set2d,set3d])
# performing PCA with MDP
set_comb_pca = mdp.pca(set_comb,svd=True)
pcan = mdp.nodes.PCANode(output_dim=3)
pcar = pcan.execute(set_comb)
# graph the results - lower triangle
for i in range(1,6):
for j in range(1,6):
if i != j and i < j:
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(pcar[(len(set1d)+1):(len(set1d)+len(set2d)), i], pcar[(len(set1d)+1):(len(set1d)+len(set2d)), j], marker='.', color='grey',linestyle="None")
ax.plot(pcar[(len(set1d)+len(set2d)):, i], pcar[(len(set1d)+len(set2d)):, j], marker='.', color='blue',linestyle="None")
ax.plot(pcar[1:len(set1d),i], pcar[1:len(set1d), j], marker='.', color='red',linestyle="None")
# labels and title
ax.set_xlabel('PC' + str(i) + '(%.3f%%)' % (pcan.d[i]))
ax.set_ylabel('PC' + str(j) + '(%.3f%%)' % (pcan.d[j]))
plt.title(gname)
gname = bname + "_pc" + str(i) + "_vs_" + "pc" + str(j)
plt.title(bname)
# saving image
fig.savefig(gname + ".png")
plt.close(fig)
该脚本仅生成 1 个图形 PC1 vs PC2 并退出。看来我的错误在枚举中。你能建议我更正吗?我用以下方法对其进行了测试: print gname - 一切正常。错误脚本的输出如下。
<matplotlib.text.Text object at 0x11817e10>
[<matplotlib.lines.Line2D object at 0x11814610>]
[<matplotlib.lines.Line2D object at 0xd2d7710>]
[<matplotlib.lines.Line2D object at 0xd2d7bd0>]
<matplotlib.text.Text object at 0x11812690>
<matplotlib.text.Text object at 0x11814d10>
<matplotlib.text.Text object at 0x11817e10>
<matplotlib.text.Text object at 0xd2ff090>
Traceback (most recent call last):
File "<stdin>", line 9, in <module>
IndexError: invalid index