![](/img/trans.png)
[英]Sklearn: ValueError:Found input variables with inconsistent numbers of samples: [500, 1]
[英]Sklearn : ValueError: Found input variables with inconsistent numbers of samples: [1, 6]
X = [ 1994. 1995. 1996. 1997. 1998. 1999.]
y = [1.2 2.3 3.4 4.5 5.6 6.7]
clf = LinearRegression()
clf.fit(X,y)
这给出了上述错误。 X和y都是numpy arrays
如何删除此错误?
我尝试了这里给出的方法,并使用X.reshape((-1,1))
和y.reshape((-1,1))
重塑了 X 和 y。 然而,它没有成功。
这对我很好。 在重新整形之前,请确保数组是numpy数组。
import numpy as np
from sklearn.linear_model import LinearRegression
X = np.asarray([ 1994., 1995., 1996., 1997., 1998., 1999.])
y = np.asarray([1.2, 2.3, 3.4, 4.5, 5.6, 6.7])
clf = LinearRegression()
clf.fit(X.reshape(-1,1),y)
clf.predict([1997])
#Output: array([ 4.5])
clf.predict([2001])
#Output: array([ 8.9])
当火车测试溢出我有一个不平衡变量样本时,我遇到了类似的问题。 对于我的情况,我通过传递分层参数来解决它。
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.3, stratify=y, random_state=42)
import pandas as pd
import numpy as np
from sklearn import linear_model
from sklearn.cross_validation import train_test_split
df_house = pd.read_csv('CSVFiles/kc_house_data.csv',index_col = 0,engine ='c')
df_house.drop(df_house.columns[[1, 0, 10, 11,12, 13, 14, 15, 16, 17,18]], axis=1, inplace=True)
reg=linear_model.LinearRegression()
df_y=df_house[df_house.columns[1:2]]
df_house.drop(df_house.columns[[6, 7, 8, 5]], axis=1, inplace=True)
x_train, x_test, y_train, y_test=train_test_split(df_house, df_y, test_size=0.1, random_state=7)
print(x_train.shape, y_train.shape)
reg.fit(x_train, x_test)
LinearRegression(copy_x=True, fit_intercept=True, n_jobs=1, normalize=False )
My Shape is :
(19451, 5) (19451, 1)
ValueError: Found input variables with inconsistent numbers of samples: [19451, 2162]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.