Comments (1)
`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)`
from pythondatasciencehandbook.
Related Issues (20)
- Python HOT 2
- Warning on 04.11 Customizing Matplotlib - Configurations and Stylesheets
- Python for Data Science HOT 4
- data failed HOT 1
- Figure 2-4. Visualization of NumPy broadcasting
- Links to open in Google Colab are no longer working
- `join_axes` keyword and `append` method deprecated HOT 2
- Purpose of Suppressing Out in python HOT 1
- indexes as ordered set - issue in code
- check on 03.05 in notebooks_v1 HOT 1
- inconsistencies between tables and commands in 3.8 line [18]
- gdgdgd
- gdgdsngnd
- gdgndgdn
- Ok
- PythonDataScience
- Notebooks use "images/" instead of "figures/" for Markdown images
- Star
- 핸드북
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from pythondatasciencehandbook.