Python Sklearn 线性回归值误差
Python Sklearn Linear Regression Value Error
我一直在尝试使用 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)
请帮帮我。我是一名学生,正在努力学习机器学习基础知识。
将您的 X 作为数据框而不是系列传递,您可以使用 [[]] "double brackets" 或 to_frame()
作为单个特征:
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']]
或者
x = data['TV'].to_frame()
y = data['Sales']
lm = LinearRegression()
lm.fit(x,y)
输出:
LinearRegression(copy_X=True, fit_intercept=True, n_jobs=1, normalize=False)
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)
我一直在尝试使用 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)
请帮帮我。我是一名学生,正在努力学习机器学习基础知识。
将您的 X 作为数据框而不是系列传递,您可以使用 [[]] "double brackets" 或 to_frame()
作为单个特征:
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']]
或者
x = data['TV'].to_frame()
y = data['Sales']
lm = LinearRegression()
lm.fit(x,y)
输出:
LinearRegression(copy_X=True, fit_intercept=True, n_jobs=1, normalize=False)
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)