ValueError:发现的输入变量,样本数量不一致:[12925,517]
from sklearn.linear_model import LinearRegression
import numpy as np
X9=dataset.iloc[:,[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24]].values
y9=dataset.iloc[:,29].values
#X9=pd.DataFrame(data=X9)
#y9=pd.DataFrame(data=y9)
X9 = X9.astype('float32')
y9 = LabelEncoder().fit_transform(y9.astype('str'))
#X9 = np.array(X9).reshape(12925,1)
#y9 = np.reshape(517,1)
X9 = X9.reshape((12925, 1))
y9 = y9.reshape((517, 1))
linreg = LinearRegression().fit(X9,y9)
linreg.intercept_
linreg.coef_
我是Python的初学者。我得到以下错误
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-51-0f2045f4e5e6> in <module>()
16 y9 = y9.reshape((517, 1))
17
---> 18 linreg = LinearRegression().fit(X9,y9)
19
20 linreg.intercept_
3 frames
/usr/local/lib/python3.7/dist-packages/sklearn/utils/validation.py in check_consistent_length(*arrays)
332 raise ValueError(
333 "Found input variables with inconsistent numbers of samples: %r"
--> 334 % [int(l) for l in lengths]
335 )
336
ValueError: Found input variables with inconsistent numbers of samples: [12925, 517]
我的x9.形状是:(12925,1) y9。形状为:(517,1)
您能指导我解决此错误吗?我想做先生 使用最小二乘算法优化参数。
from sklearn.linear_model import LinearRegression
import numpy as np
X9=dataset.iloc[:,[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24]].values
y9=dataset.iloc[:,29].values
#X9=pd.DataFrame(data=X9)
#y9=pd.DataFrame(data=y9)
X9 = X9.astype('float32')
y9 = LabelEncoder().fit_transform(y9.astype('str'))
#X9 = np.array(X9).reshape(12925,1)
#y9 = np.reshape(517,1)
X9 = X9.reshape((12925, 1))
y9 = y9.reshape((517, 1))
linreg = LinearRegression().fit(X9,y9)
linreg.intercept_
linreg.coef_
I am a beginner in python. I get the below error
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-51-0f2045f4e5e6> in <module>()
16 y9 = y9.reshape((517, 1))
17
---> 18 linreg = LinearRegression().fit(X9,y9)
19
20 linreg.intercept_
3 frames
/usr/local/lib/python3.7/dist-packages/sklearn/utils/validation.py in check_consistent_length(*arrays)
332 raise ValueError(
333 "Found input variables with inconsistent numbers of samples: %r"
--> 334 % [int(l) for l in lengths]
335 )
336
ValueError: Found input variables with inconsistent numbers of samples: [12925, 517]
my X9.shape is: (12925, 1)
y9.shape is: (517, 1)
could you please guide me to solve this error. im trying to do The MR
parameters were optimized using a least squares algorithm.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的X9 中有 12925行,Y9 中只有 517行。它们应该是相同的数字,因为对于
x9
中的每个示例,您需要在y9
中的示例来计算线性回归。我没有您的数据,因此我无法真正复制并提供适当的解决方案。
第一个猜测是重新检查数据集的形状。
另一个猜测是您必须调整重塑:
You are having 12925 lines in your X9 and only 517 lines in your y9. They should be the same number because for every sample in
X9
you would need a sample in youry9
to calculate the linear regression.I don't have your data so I can't really reproduce and provide a proper solution.
First guess would be to recheck the shape of your dataset.
Another guess would be that you have to adjust your reshape: