【问题标题】:Expected 2D array, got scalar array instead error in pandas regression预期的二维数组,在熊猫回归中得到标量数组而不是错误
【发布时间】:2019-06-05 03:14:03
【问题描述】:

当我尝试在 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 小时后参加关于该主题的考试。你能帮帮我吗?

【问题讨论】:

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


    【解决方案1】:

    试试:

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

    更多情况下,您可能在numpy 数组中有数据,如下所示:

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

    现在错误信息告诉你该怎么做:

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

    【讨论】:

      猜你喜欢
      • 2020-05-11
      • 2019-03-28
      • 2019-06-20
      • 2023-03-07
      • 2022-01-22
      • 2018-01-15
      • 2019-08-17
      • 2020-06-05
      • 2018-07-05
      相关资源
      最近更新 更多