编程语言
首页 > 编程语言> > Python Sklearn线性回归值错误

Python Sklearn线性回归值错误

作者:互联网

香港专业教育学院一直在尝试使用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,pandas,linear-regression,machine-learning,sklearn-pandas
来源: https://codeday.me/bug/20191009/1877850.html