Python 3.5 尝试使用 sklearn 和 matplotlib 绘制 PCA

Python 3.5 Trying to plot PCA with sklearn and matplotlib

使用以下代码会产生错误:TypeError: float() argument must be a string or a number, not 'Pred':

我正在努力找出导致此错误的原因。

self.features 是一个由三个浮点数组成的列表。 [1.1, 1.2, 1.3] self.features:

的例子
[array([-1.67191985,  0.1       ,  9.69981494]), array([-0.68486623,  0.05      ,  9.99085024]), array([ -1.36      ,   0.1       ,  10.44720459]), array([-2.46918915,  0.        ,  3.5483372 ]), array([-0.835     ,  0.1       ,  4.02740479])]

这是抛出错误的方法。

def pca(self):        
    pca = PCA(n_components=2)
    x_np = np.asarray(self.features)
    pca.fit(x_np)
    X_reduced = pca.transform(x_np)
    plt.figure(figsize=(10, 8))
    plt.scatter(X_reduced[:, 0], X_reduced[:, 1], c=y, cmap='RdBu')
    plt.xlabel('First component')
    plt.ylabel('Second component')

完整的追溯是:

Traceback (most recent call last):
  File "/Users/user/PycharmProjects/Post-Translational-Modification-                
Prediction/pred.py", line 244, in <module>
y.generate_pca()
  File "/Users/user/PycharmProjects/Post-Translational-Modification-
Prediction/pred.py", line 222, in generate_pca
plt.scatter(X_reduced[:, 0], X_reduced[:, 1], c=y, cmap='RdBu')
File "/usr/local/lib/python3.5/site-packages/matplotlib/pyplot.py", 
line 3435, in scatter
edgecolors=edgecolors, data=data, **kwargs)
File "/usr/local/lib/python3.5/site-packages/matplotlib/__init__.py", 
line 1892, in inner
return func(ax, *args, **kwargs)
File "/usr/local/lib/python3.5/site-packages/matplotlib/axes/_axes.py", line 3976, in scatter
c_array = np.asanyarray(c, dtype=float)
File "/usr/local/lib/python3.5/site-packages/numpy/core/numeric.py", line 583, in asanyarray
return array(a, dtype, copy=False, order=order, subok=True)
TypeError: float() argument must be a string or a number, not 'Pred'

@WhoIsJack 建议的修复方法是添加 np.arange(len(self.features))

运行类似问题的功能代码是:

def generate_pca(self):
    y= np.arange(len(self.features))
    pca = PCA(n_components=2)
    x_np = np.asarray(self.features)
    pca.fit(x_np)
    X_reduced = pca.transform(x_np)
    plt.figure(figsize=(10, 8))
    plt.scatter(X_reduced[:, 0], X_reduced[:, 1], c=y, cmap='RdBu')
    plt.xlabel('First component')
    plt.ylabel('Second component')
    plt.show()