本文介绍了Sklearn:ValueError:发现样本数量不一致的输入变量:[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 数组

This gives the above mentioned error. Both X and y are numpy arrays

如何消除此错误?

我尝试了here 并使用 X.reshape((-1,1))y.reshape((-1,1)) 对 X 和 y 进行整形.然而它并没有奏效.

I tried the method given here and reshaped X and y by using X.reshape((-1,1)) and y.reshape((-1,1)). However it did not work out.

推荐答案

这对我来说很好用.在重塑之前,请确保数组是 numpy 数组.

This is working for me fine. Before reshaping make sure that the arrays are numpy arrays.

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])

这篇关于Sklearn:ValueError:发现样本数量不一致的输入变量:[1, 6]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-21 07:27