【发布时间】:2021-02-02 05:37:25
【问题描述】:
我在尝试使用 PCA 将我的高维向量转换为二维时遇到数据错误。
这是我的输入data,每行有300个维度:
vector
0 [0.01053525, -0.007869658, 0.0024931028, -0.04...
1 [-0.024436072, -0.016484523, 0.03859031, 0.000...
2 [0.015011676, -0.020465894, 0.004854744, -0.00...
3 [-0.010836455, -0.006562917, 0.00265073, 0.022...
4 [-0.018123362, -0.026007563, 0.04781856, -0.03...
... ...
45124 [-0.016111804, -0.041917775, 0.010192914, -0.0...
45125 [0.0311568, -0.013044083, 0.030656694, -0.0126...
45126 [-0.021875003, -0.005635035, 0.0076896898, -0....
45127 [-0.0062000924, -0.041035958, 0.0077403532, 0....
45128 [0.007794927, 0.0019561667, 0.15995999, -0.054...
[45129 rows x 1 columns]
我的代码:
data = pd.read_parquet('1.parquet', engine='fastparquet')
reduced = pca.fit_transform(data)
错误:
TypeError Traceback (most recent call last)
TypeError: float() argument must be a string or a number, not 'list'
The above exception was the direct cause of the following exception:
ValueError Traceback (most recent call last)
<ipython-input-15-8e547411a212> in <module>
----> 1 reduced = pca.fit_transform(data)
...
...
ValueError: setting an array element with a sequence.
编辑
>>data.shape
(45129, 1)
>>data.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 45129 entries, 0 to 45128
Data columns (total 1 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 vector 45129 non-null object
dtypes: object(1)
memory usage: 352.7+ KB
【问题讨论】:
-
你能发布
data.shape的输出吗?您说数据有 300 列,但上面的代码显示[45129 rows x 1 columns],这表明您读取镶木地板文件的方式有问题 -
@matchkarov 添加。谢谢!
-
@mbatchkarov 镶木地板文件是正确的。它是 word2vec 的输出。每个行向量代表一个词。例如矢量
[0.01, 0.02, 0.03](在这种情况下为3维)代表hello。 -
我看不出您阅读镶木地板文件的方式有什么问题,但数据框显然只有一列。输入的文件一定是错的,不保密的可以上传吗?你是怎么写这个文件的?
-
@mbatchkarov 正如我上面提到的,parquet 文件来自 word2vec。它确实只有 1 列。我要做的就是将这一列中的 300 维数据减少为 2 维。
标签: python pandas scikit-learn pca