ValueError:发现输入变量的样本数量不一致:[24, 6]

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

我在构建回归模型时遇到值错误。以下是我的代码供参考。

import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
dataset = pd.read_csv('Salary_Data.csv')
X = dataset.iloc[:, :-1].values
y = dataset.iloc[:, -1].values
from sklearn.model_selection import train_test_split
X_train,y_train,X_test,y_test=train_test_split(X,y,test_size=0.2,random_state=0)
#build a model to understand co-relation between no of years of expereince and salary.
#regression to be used when you have to predict the continuos value and classification when you have to predict category.
from sklearn.linear_model import LinearRegression
regressor=LinearRegression()
#The function that connects into training set is called as fit function (method of linear regression class or predict future results of test set)
regressor.fit(X_train, y_train)

Attaching Dataset for reference

我尝试构建一个使用

fit
函数连接两个训练集的模型,但无法进一步进行。

Error image

python pandas numpy machine-learning linear-regression
1个回答
0
投票

您在变量顺序中存在

train_test_split
错误:

# Not X_train, y_train, X_test, y_test  (swap y_train and X_test)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=0)
© www.soinside.com 2019 - 2024. All rights reserved.