预期的2D数组,在熊猫回归中得到标量数组错误

时间:2019-01-10 09:25:16

标签: python python-3.x pandas scikit-learn sklearn-pandas

当我尝试计算熊猫回归时,我犯了一个错误。这是代码:

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)

当我尝试运行代码时,出现此错误:

"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.

我将在3个小时内对该主题进行一次考试。你能帮我吗?

1 个答案:

答案 0 :(得分:0)

尝试:

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

通常,您可能会像这样在numpy数组中存储数据:

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

现在错误消息告诉您该怎么做:

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