ValueError:发现的输入变量,样本数量不一致:[12925,517]

发布于 2025-02-04 11:15:38 字数 1405 浏览 3 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我不是你的备胎 2025-02-11 11:15:38

您的X9 中有 12925行,Y9 中只有 517行。它们应该是相同的数字,因为对于x9中的每个示例,您需要在y9中的示例来计算线性回归。

我没有您的数据,因此我无法真正复制并提供适当的解决方案。

第一个猜测是重新检查数据集的形状。

另一个猜测是您必须调整重塑:

X9 = X9.reshape((12925, 1))
y9 = y9.reshape((12925, 1))

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 your y9 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:

X9 = X9.reshape((12925, 1))
y9 = y9.reshape((12925, 1))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文