预期的二维数组,在 pandas 回归中得到标量数组而不是错误
Expected 2D array, got scalar array instead error in pandas regression
我尝试计算 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 小时后参加该科目的考试。你能帮帮我吗?
尝试:
tahmin=lr.predict([[8]])
更常见的是,您可能在 numpy
数组中有数据,如下所示:
import numpy as np
x_test = np.array([8])
现在错误消息会告诉您该怎么做:
tahmin=lr.predict(x_test.reshape(-1, 1))
我尝试计算 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 小时后参加该科目的考试。你能帮帮我吗?
尝试:
tahmin=lr.predict([[8]])
更常见的是,您可能在 numpy
数组中有数据,如下所示:
import numpy as np
x_test = np.array([8])
现在错误消息会告诉您该怎么做:
tahmin=lr.predict(x_test.reshape(-1, 1))