Search code examples
pythonpython-3.xpandasscikit-learnsklearn-pandas

Expected 2D array, got scalar array instead error in pandas regression


I'm taking an error when i try to calculate regression in pandas. Here is the code:

import pandas as pd
import matplotlib.pyplot as plt

df=pd.DataFrame({"haftalar":[1,2,3,4,5,6,7],
                 "degerler":[6.11,5.66,5.30,5.32,5.25,5.37,5.28]})
haftalar=df[['haftalar']]
degerler=df[['degerler']]

from sklearn.model_selection import train_test_split
x_train, x_test, y_train, y_test = train_test_split(
    haftalar, degerler, test_size=0.57, random_state=0) 

from sklearn.linear_model import LinearRegression

lr=LinearRegression()

lr.fit(x_train,y_train)
tahmin=lr.predict(8)
print(tahmin)

when i try to run the code, I'm taking this error:

"if it contains a single sample.".format(array))

ValueError: Expected 2D array, got scalar array instead:
array=8.
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 have an exam in 3 hours about that subject. Can you help me?


Solution

  • Try:

    tahmin=lr.predict([[8]])
    

    More often, you may have data in numpy arrays like so:

    import numpy as np
    x_test = np.array([8])
    

    Now the error message tells you what to do:

    tahmin=lr.predict(x_test.reshape(-1, 1))