2017-08-18 42 views
2

我在Scikit Learn中遇到了LinearRegression算法的一些问题 - 我已经通过论坛搜索了很多,但是由于某种原因,我没有设法绕过这个错误。我使用Python 3.5Sklearn | LinearRegression | Fit

下面是我已经尝试,但要得到一个数值错误:“与样品不一致的数字发现输入变量:[403,174]”

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) 

我错过显而易见的东西?

任何帮助将不胜感激。

亲切的问候, 阿德里安

回答

2

看起来你的培训和测试包含不同数量的行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) 

对此

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

这个工作。知道这是愚蠢的。感谢鲍勃 – AdrianC