Solution: Expected 2D array, got scalar array instead

I was trying to fit a basic machine learning model of linear regression. My code was simple.

import pandas as pd
import numpy as np
from sklearn import linear_model

df = pd.read_csv('homeprices.csv')

reg = linear_model.LinearRegression()
reg.fit(df[['area']], df.price)

reg.predict(3300)

I got the following error.

ValueError                                Traceback (most recent call last)
/tmp/ipykernel_10591/2177379720.py in <module>
----> 1 reg.predict(3300)

~/.local/lib/python3.8/site-packages/sklearn/linear_model/_base.py in predict(self, X)
    360             Returns predicted values.
    361         """
--> 362         return self._decision_function(X)
    363 
    364     _preprocess_data = staticmethod(_preprocess_data)

~/.local/lib/python3.8/site-packages/sklearn/linear_model/_base.py in _decision_function(self, X)
    343         check_is_fitted(self)
    344 
--> 345         X = self._validate_data(X, accept_sparse=["csr", "csc", "coo"], reset=False)
    346         return safe_sparse_dot(X, self.coef_.T, dense_output=True) + self.intercept_
    347 

~/.local/lib/python3.8/site-packages/sklearn/base.py in _validate_data(self, X, y, reset, validate_separately, **check_params)
    564             raise ValueError("Validation should be done on X, y or both.")
    565         elif not no_val_X and no_val_y:
--> 566             X = check_array(X, **check_params)
    567             out = X
    568         elif no_val_X and not no_val_y:

~/.local/lib/python3.8/site-packages/sklearn/utils/validation.py in check_array(array, accept_sparse, accept_large_sparse, dtype, order, copy, force_all_finite, ensure_2d, allow_nd, ensure_min_samples, ensure_min_features, estimator)
    759             # If input is scalar raise error
    760             if array.ndim == 0:
--> 761                 raise ValueError(
    762                     "Expected 2D array, got scalar array instead:\narray={}.\n"
    763                     "Reshape your data either using array.reshape(-1, 1) if "

ValueError: Expected 2D array, got scalar array instead:
array=3300.
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.

I solved the issue by making the array 2D from 1D.

reg.predict([[3300]])

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.