Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

5.3.4 grid.fit(X,y) ;A ValueErrorValueError error occurred #367

Open
YoungBooker opened this issue Mar 31, 2023 · 1 comment
Open

5.3.4 grid.fit(X,y) ;A ValueErrorValueError error occurred #367

YoungBooker opened this issue Mar 31, 2023 · 1 comment

Comments

@YoungBooker
Copy link

`from sklearn.model_selection import GridSearchCV
param_grid = {'polynomialfeatures__degree': np.arange(21),
'linearregression__fit_intercept': [True, False],
'linearregression__normalize': [True, False]}

grid = GridSearchCV(PolynomialRegression(), param_grid, cv=7)
grid.fit(X,y)`

Sorry to bother you. I had a small issue in section 5.3.4 of this book. Jupyter prompts me with the following error。
I also ran the code you gave, but this kind of problem also occurs. (I ran the code for 5.3 through until grid.fit(X,y))
My English is not very good, sorry again.

ValueError: Invalid parameter 'normalize' for estimator LinearRegression(). Valid parameters are: ['copy_X', 'fit_intercept', 'n_jobs', 'positive'].

@YoungBooker
Copy link
Author

`from sklearn.model_selection import GridSearchCV
from sklearn.preprocessing import PolynomialFeatures
from sklearn.linear_model import LinearRegression
from sklearn.pipeline import make_pipeline
import matplotlib.pyplot as plt
import numpy as np
import seaborn

def PolynomialRegression(degree=2,**kwargs):
#建立多项式回归模型
return make_pipeline(PolynomialFeatures(degree),
LinearRegression(**kwargs))

def make_data(N,err=1.0,rseed=1):
#随机抽样数据
rng=np.random.RandomState(rseed)
X=rng.rand(N,1)**2
y=10-1./(X.ravel()+0.1)
if err > 0:
y+=err*rng.randn(N)
return X,y

X,y=make_data(40)
param_grid = {'polynomialfeatures__degree': np.arange(21),
'linearregression__fit_intercept': [True, False]}

#网格搜索
grid = GridSearchCV(PolynomialRegression(), param_grid, cv=7)

#调用方法fit(),并同时记录每个点的得分
grid.fit(X,y)

#打印最优参数
print(grid.best_params_)

#设置图样格式
seaborn.set()
#用最优参数的模型拟合数据
model=grid.best_estimator_
#画出随机数据的散点图
plt.scatter(X.ravel(),y)

lim=plt.axis()
X_test=np.linspace(-0.1,1.1,500)[:,None]
#predict是训练后返回预测结果,是标签值
y_test=model.fit(X,y).predict(X_test)
plt.plot(X_test.ravel(),y_test)
plt.axis(lim)`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant