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



当我尝试计算熊猫的回归时,我遇到了错误。这是代码:

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 小时内有关于该主题的考试。你可以帮我吗?

尝试:

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

更常见的是,您可能将数据放在numpy数组中,如下所示:

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

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

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

相关内容

  • 没有找到相关文章

最新更新