例如,我有以下numpy数组:
import pandas as pd
import numpy as np
# points could be in n dimension, i need a solution that would cover that up
# and being able to calculate distance between points so flattening the data
# is not my goal.
points = np.array([[1, 2], [2, 1], [100, 100], [-2, -1], [0, 0], [-1, -2]]) # a 2d numpy array containing points in space
labels = np.array([0, 1, 1, 1, 0, 0]) # the labels of the points (not necessarily only 0 and 1)
我试图制作一个字典,然后从中创建pandas datafram:
my_dict = {'point': points, 'label': labels}
df = pd.DataFrame(my_dict, columns=['point', 'label'])
但这没有用,我遇到了以下异常:
Exception: Data must be 1-dimensional
可能是由于点的numpy数组(二维numpy数组)所致。
预期结果:
point label
0 [1, 2] 0
1 [2, 1] 1
2 [100, 100] 1
3 [-2, -1] 0
4 [0, 0] 0
5 [-1, -2] 1
在此先感谢所有帮助者:)
您应始终尝试规范化数据,以使每一列仅包含奇异值,而不包含具有维的数据。
在这种情况下,我将执行以下操作:
>>> df = pd.DataFrame({'x': points[:,0], 'y': points[:, 1], 'label': labels},
columns=['x', 'y', 'label'])
>>> df
x y label
0 1 2 0
1 2 1 1
2 100 100 1
3 -2 -1 1
4 0 0 0
5 -1 -2 0
如果您确实坚持要保持这样的点,则在传递它们之前将它们转换为列表列表或元组列表,pandas
以避免发生此错误。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句