【发布时间】:2018-01-05 19:38:26
【问题描述】:
使用以下代码会产生错误: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'
【问题讨论】:
-
能否请您发布整个错误消息?这是所有代码还是还有更多?
-
能不能也贴几行self.features?
-
@2Obe 添加了完整的回溯,代码更多但大约 200 行。但是,我认为这不会导致错误。谢谢!
-
@Linda 我添加了self.features的前5个元素谢谢!
-
散点图中的
y是什么?如果我设置例如,您的代码按原样对我有用y = np.arange(len(features))。我怀疑您的y不是有效的颜色序列。
标签: pandas numpy matplotlib scikit-learn pca