Python Sklearn线性回归值错误 [英] Python Sklearn Linear Regression Value Error
本文介绍了Python Sklearn线性回归值错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我一直在尝试使用sklearn进行线性回归.有时我会收到一个值错误,有时它可以正常工作.我不确定使用哪种方法. 错误消息如下:
Ive been trying out Linear Regression using sklearn. Sometime I get a value error, sometimes it works fine. Im not sure which approach to use. Error Message is as follows:
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)
请帮帮我.我是一名学生,试图学习机器学习的基础知识.
Please help me out. I am a student, trying to pick up on Machine Learning basics.
推荐答案
lm.fit
期望X
是
numpy数组或形状为[n_samples,n_features]的稀疏矩阵
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线性回归值错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文