8

While i am predicting the one sample from my data it gives reshape error but my model have equal no of rows, what is problem guys, found similar question but different unexplained.

import pandas as pd
from sklearn.linear_model import LinearRegression
import numpy as np
x = np.array([2.0 , 2.4, 1.5, 3.5, 3.5, 3.5, 3.5, 3.7, 3.7])
y = np.array([196, 221, 136, 255, 244, 230, 232, 255, 267])

lr = LinearRegression()
lr.fit(x,y)

print(lr.predict(2.4))

Error is

"if it contains a single sample.".format(array)) ValueError: Expected 2D array, got scalar array instead: array=2.4. Reshape your data either using array.reshape(-1, 1) if your data has a single feature or array.reshape(1, -1) if it contains a single sample.

  • if you want to know why fitting a model requires 2d model -> [here](https://stackoverflow.com/questions/61367841/why-does-the-fit-method-in-sklearns-linearregression-only-accept-2d-array-for-t) – Dozzy Norm Apr 14 '22 at 01:08

1 Answers1

13

You should reshape your X to be a 2D array not 1D array. Fitting a model requires requires a 2D array. i.e (n_samples, n_features)

x = np.array([2.0 , 2.4, 1.5, 3.5, 3.5, 3.5, 3.5, 3.7, 3.7])
y = np.array([196, 221, 136, 255, 244, 230, 232, 255, 267])

lr = LinearRegression()
lr.fit(x.reshape(-1, 1), y)

print(lr.predict([[2.4]]))
Abhi
  • 3,708
  • 1
  • 12
  • 27
  • 2
    Thanks man. But what's that reshape does and why not using it causes error. –  Nov 01 '19 at 18:17
  • 1
    @razzOn2bull When fitting a model your X needs to be 2D array. i.e (n_samples, n_features). When you use `.reshape(1, -1)` it adds one dimension to the data. You can see this question ('https://stackoverflow.com/questions/18691084/what-does-1-mean-in-numpy-reshape') for more info regarding it. – Abhi Nov 01 '19 at 18:20