学习|线性回归| Fi

2024-05-19 12:52:02 发布

您现在位置:Python中文网/ 问答频道 /正文

我在Scikit Learn中遇到了一些与LinearRegression算法有关的问题-我在论坛上搜索了很多东西,但是由于某些原因,我没有设法绕过这个错误。我使用的是python3.5

下面是我所尝试的,但是一直得到一个值错误:“找到了样本数不一致的输入变量:[403174]”

X = df[["Impressions", "Clicks", "Eligible_Impressions", "Measureable_Impressions", "Viewable_Impressions"]].values

y = df["Total_Conversions"].values.reshape(-1,1)

print ("The shape of X is {}".format(X.shape))
print ("The shape of y is {}".format(y.shape))

The shape of X is (577, 5)
The shape of y is (577, 1)

X_train, y_train, X_test, y_test = train_test_split(X, y, test_size=0.3, random_state = 42)
linreg = LinearRegression()
linreg.fit(X_train, y_train)
y_pred = linreg.predict(X_test)
print (y_pred)

print ("The shape of X_train is {}".format(X_train.shape))
print ("The shape of y_train is {}".format(y_train.shape))
print ("The shape of X_test is {}".format(X_test.shape))
print ("The shape of y_test is {}".format(y_test.shape))

The shape of X_train is (403, 5)
The shape of y_train is (174, 5)
The shape of X_test is (403, 1)
The shape of y_test is (174, 1)

我是不是遗漏了一些显而易见的东西?在

任何帮助都将不胜感激。在

谨致问候, 阿德里安


Tags: ofthetestformatdfis错误train
1条回答
网友
1楼 · 发布于 2024-05-19 12:52:02

看起来您的Train和Tests包含不同数量的X和y行,因为您以错误的顺序存储了Train_test_split()的返回值

改变这个

X_train, y_train, X_test, y_test = train_test_split(X, y, test_size=0.3, random_state = 42)

为了这个

^{pr2}$

相关问题 更多 >