sklearn model and fit for a list

I have 2 arrays x and y which are :

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']]

trying to get the prediction by:

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

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

getting Error:

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)

so I am looking for linear regression for a model where the input is a array and output is also array ..

can you please correct me what I am missing

1 answer

  • answered 2017-12-06 01:35 Vivek Kumar

    Just replace the predict() line in your code with this:

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

    The error message says that you need to provide a 2-d array of X to predict() method. Currently x[10] will give a single dimension array:

    [1 2 3 4 5]
    

    But we need to do it like:

    [[1 2 3 4 5]]
    

    So according to the hint in error message you can reshape the data to have 1 row and multiple columns. This is achieved by reshape(1,-1).

    Or else as stated in the comments by @furas, just wrap your data into a list again like this:

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