期望的2D数组,而是获得1D数组:对于Python- Jupyter Notebook中的简单线性回归模型

问题描述 投票:0回答:1

我正在尝试使用在线资料从头开始学习Python和数据科学。

我刚刚尝试创建一个简单的线性回归模型,以便在阅读大量材料后开始实践。但是,尝试执行此操作时出现以下错误。

您能帮助您理解此错误并查看我做错了什么吗。

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

from numpy.random import randn
np.random.seed(101)

df3=pd.DataFrame(randn(5,2),index ='0 1 2 3 4'.split(), columns='Test Price'.split())

y= df3['Price']
x= df3['Test']

import sklearn.model_selection as model_selection
X_train, X_test, y_train, y_test = model_selection.train_test_split(x, y, test_size=0.2, random_state=101)

from sklearn.linear_model import LinearRegression
lm2= LinearRegression()

lm2.fit(X_train,y_train)

错误

ValueError: Expected 2D array, got 1D array instead:
array=[-2.01816824  0.65111795  0.90796945 -0.84807698].
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.
python pandas jupyter-notebook linear-regression
1个回答
0
投票

检查文档Link

参数

X:{数组状,稀疏矩阵}的形状(n_samples,n_features)训练数据

因此您必须将X调整为(n_samples, 1)

使用

lm2.fit(X_train.values.reshape(-1,1),y_train)

© www.soinside.com 2019 - 2024. All rights reserved.