gpt4 book ai didi

Python Sklearn 线性回归值误差

转载 作者:行者123 更新时间:2023-11-30 09:00:01 24 4
gpt4 key购买 nike

我一直在使用 sklearn 尝试线性回归。有时我会得到一个值错误,有时它工作正常。我不确定使用哪种方法。错误信息如下:

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/sklearn/linear_model/base.py", line 512, in fit
y_numeric=True, multi_output=True)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/sklearn/utils/validation.py", line 531, in check_X_y
check_consistent_length(X, y)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/sklearn/utils/validation.py", line 181, in check_consistent_length
" samples: %r" % [int(l) for l in lengths])
ValueError: Found input variables with inconsistent numbers of samples: [1, 200]

代码是这样的:

import pandas as pd
from sklearn.linear_model import LinearRegression
data = pd.read_csv('http://www-bcf.usc.edu/~gareth/ISL/Advertising.csv', index_col=0);
x = data['TV']
y = data['Sales']
lm = LinearRegression()
lm.fit(x,y)

请帮帮我。我是一名学生,正在尝试学习机器学习基础知识。

最佳答案

lm.fit 期望 X

numpy array or sparse matrix of shape [n_samples,n_features]

你的x具有形状:

In [6]: x.shape
Out[6]: (200,)

只需使用:

lm.fit(x.reshape(-1,1) ,y)

关于Python Sklearn 线性回归值误差,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43971588/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com