本文介绍了sklearn:在调用 LinearRegression.fit() 时发现样本数量不一致的数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
只是试图做一个简单的线性回归,但我对这个错误感到困惑:
Just trying to do a simple linear regression but I'm baffled by this error for:
regr = LinearRegression()
regr.fit(df2.iloc[1:1000, 5].values, df2.iloc[1:1000, 2].values)
产生:
ValueError: Found arrays with inconsistent numbers of samples: [ 1 999]
这些选择必须具有相同的维度,并且它们应该是 numpy 数组,那么我错过了什么?
These selections must have the same dimensions, and they should be numpy arrays, so what am I missing?
推荐答案
貌似sklearn需要(行号,列号)的数据形状.如果您的数据形状是 (row number, ) 之类的 (999, )
,则它不起作用.通过使用 numpy.reshape()
,您应该将数组的形状更改为 (999, 1)
,例如使用
It looks like sklearn requires the data shape of (row number, column number). If your data shape is (row number, ) like (999, )
, it does not work. By using numpy.reshape()
, you should change the shape of the array to (999, 1)
, e.g. using
data=data.reshape((999,1))
就我而言,它适用于这一点.
In my case, it worked with that.
这篇关于sklearn:在调用 LinearRegression.fit() 时发现样本数量不一致的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!