编程语言
首页 > 编程语言> > Python:ValueError:使用序列设置数组元素

Python:ValueError:使用序列设置数组元素

作者:互联网

我正在尝试使用scikit-learn做一些ML.

我正在使用预处理模块准备数据.数据的类型为float.

通过阅读有关此问题的其他问题:ValueError: setting an array element with a sequence,这可能是由于我的数据结构错误,还是因为我的数据是字符串类型.这里似乎都不是这样.

如果您有任何解决方法或其含义,请告诉我.谢谢.

编码:

print(X)

pred_X = np.array(pred_X)
pred_Y = np.array(pred_Y)
X = np.array(X)
Y = np.array(Y)

X = preprocessing.scale(X)
pred_X = preprocessing.scale(pred_X)

打印(x):

[[547180.0, 120.0, 113.0, 456701.0, 1.0, 6.43, -1.0, 0.313, 0.42, 0.267 3.0, 11800.0, 607208.0, 120.0, 113.0, 456701.0, 1.0, 0.273, 0.331, 0.154, 6.0, 10300.0, 458015.0, 113.0, 120.0, 45328 6.0, 1.0, 2.54, -1.0, 0.32, 0.443, 0.257, 3.0, 92000.0, 543685.0, 120.0, 113.0, 456701.0, 1.0, 6.43, 1.0, 0.296, 0.4, 0.234, 2.0, 8800.0, 594809.0, 475582.0, 120.0, 113.0, 456701.0, 1.0, 1.0, 0.295, 0.384, 0.264, 4.0, 7700.0], 
[547180.0, 120.0, 113.0, 456701.0, 1.0, 6.43, -1.0, 0.313, 0.42, 0.267, 3.0, 11800.0, 607208.0, 120.0, 113.0, 456701.0, 1.0, 0.273, 0.331, 0.154, 6.0, 10300.0, 458015.0, 113.0, 120.0, 453286.0, 1.0, 2.54, -1.0, 0.32, 0.443, 0.257, 3.0, 92000.0, 543685.0, 120.0, 113.0, 456701.0, 1.0, 6.43, 1.0, 0.296, 0.4, 0.234, 2.0, 8800.0, 594809.0, 435062.0, 120.0, 113.0, 456701.0, 1.0, 1.0, 0.312, 0.364, 0.154, 5.0, 6900.0], 
[547180.0, 120.0, 113.0, 456701.0, 1.0, 6.43, -1.0, 0.313, 0.42, 0.267, 3.0, 11800.0, 607208.0, 120.0, 113.0, 456701.0, 1.0, 0.273, 0.331, 0.154, 6.0, 10300.0, 458015.0, 113.0, 120.0, 453286.0, 1.0, 2.54, -1.0, 0.32, 0.443, 0.257, 3.0, 92000.0, 543685.0, 120.0, 113.0, 456701.0, 1.0, 6.43, 1.0, 0.296, 0.4, 0.234, 2.0, 8800.0, 594809.0, 446308.0, 120.0, 113.0, 456701.0, 1.0, 0.0, 0.221, 0.28e, 0.115, 8.0, 6400.0]]

错误:

Traceback (most recent call last):
  File "sampleSVM.py", line 46, in <module>
    X = preprocessing.scale(X)
  File "/home/user/.local/lib/python3.5/site-packages/sklearn/preprocessing/data.py", line 133, in scale
    dtype=FLOAT_DTYPES)
  File "/home/user/.local/lib/python3.5/site-packages/sklearn/utils/validation.py", line 433, in check_array
    array = np.array(array, dtype=dtype, order=order, copy=copy)
ValueError: setting an array element with a sequence.

解决方法:

您的输入数组X格式不正确.第1行有59个元素,第2行和第58行有58个元素. 3.当您转换为numpy数组时,它将变为具有dtype = Object的形状为(3,)的数组.

解决方案是检查并修复您的输入数据. X中的每一行必须具有相同的长度.

标签:scikit-learn,python,preprocessor,numpy
来源: https://codeday.me/bug/20191025/1926816.html