sklearn模型并适合列表

时间:2017-12-06 01:35:03

标签: python numpy scikit-learn

我有2个数组x和y,它们是:

x=    [['1', '2', '3', '4', '5'], ['3', '6', '9', '12', '24'], 
       ['1', '2', '3', '4', '5'], ['3', '6', '9', '12', '24'], 
       ['1', '2', '3', '4', '5'], ['3', '6', '9', '12', '24'], 
       ['1', '2', '3', '4', '5'], ['3', '6', '9', '12', '24'], 
       ['1', '2', '3', '4', '5'], ['3', '6', '9', '12', '24'], 
       ['1', '2', '3', '4', '5'], ['3', '6', '9', '12', '24'], 
       ['1', '2', '3', '4', '5'], ['3', '6', '9', '12', '24']]


y=    [['2', '4', '6', '8', '10\n'], ['6', '12', '18', '24', '48\n'],
       ['2', '4', '6', '8', '10\n'], ['6', '12', '18', '24', '48\n'], 
       ['2', '4', '6', '8', '10\n'], ['6', '12', '18', '24', '48\n'], 
       ['2', '4', '6', '8', '10\n'], ['6', '12', '18', '24', '48\n'], 
       ['2', '4', '6', '8', '10\n'], ['6', '12', '18', '24', '48\n'], 
       ['2', '4', '6', '8', '10\n'], ['6', '12', '18', '24', '48\n'], 
       ['2', '4', '6', '8', '10\n'], ['6', '12', '18', '24', '48']]

试图通过以下方式获得预测:

model.fit(array(x),array(y))

model.predict(array(x[10]))

得到错误:

Traceback (most recent call last):
  File "test.py", line 29, in <module>
    model.predict(array(x[10]))
  File "C:\U.....\AppData\Local\Continuum\anaconda3\lib\site-packages\sklearn\linear_model\base.py", line 256, in predict
    return self._decision_function(X)
  File "C:\U....\AppData\Local\Continuum\anaconda3\lib\site-packages\sklearn\linear_model\base.py", line 239, in _decision_function
    X = check_array(X, accept_sparse=['csr', 'csc', 'coo'])
  File "C:....\AppData\Local\Continuum\anaconda3\lib\site-packages\sklearn\utils\validation.py", line 441, in check_array
    "if it contains a single sample.".format(array))
ValueError: Expected 2D array, got 1D array instead:
array=['1' '2' '3' '4' '5'].
Reshape your data either using array.reshape(-1, 1) if your data has a single feature or array.reshape(1, -1)

所以我正在寻找一个模型的线性回归,其中输入是一个数组,输出也是数组..

是的,请你纠正我错过的内容

1 个答案:

答案 0 :(得分:0)

只需用代码替换代码中的predict()行:

import numpy as np
model.predict(np.array(x[10], dtype=np.int32).reshape(1,-1))

错误消息表明您需要为predict()方法提供X的二维数组。 目前x [10]将提供单维数组:

[1 2 3 4 5]

但我们需要这样做:

[[1 2 3 4 5]]

因此,根据错误消息中的提示,您可以将数据重新整形为包含1行和多列。这是通过重塑(1,-1)来实现的。

或者如@furas的评论所述,只需将您的数据再次包装到列表中:

model.predict([array(x[10])])