机器学习python模型正则化

这里主要是用多项式线性拟合,然后通过两种方法来优化过拟合和欠拟合

from sklearn.linear_model import LinearRegression
from sklearn.preprocessing import PolynomialFeatures
import numpy as np
import matplotlib.pyplot as plt
X_train = [[6], [8], [10], [14], [18]]
y_train = [[7], [9], [13], [17.5], [18]]
#线性回归
model = LinearRegression()
model.fit(X_train,y_train)
# #在0到25上均匀取100个点
xx = np.linspace(0,26,100)
xx = xx.reshape(xx.shape[0],1)
yy = model.predict(xx)
#
# plt.scatter(X_train,y_train)
# plt1, = plt.plot(xx,yy,label="Degree=1")
# plt.axis([0,25,0,25])
# plt.legend(handles = [plt1])
# plt.show()

#二次多项式
poly2 = PolynomialFeatures(degree=2)
'''以线性回归器为基础,用上面函数映射出2次多项式特征'''
X_train_poly2 = poly2.fit_transform(X_train)
model2 = LinearRegression()
model2.fit(X_train_poly2,y_train)
xx_poly2 = poly2.transform(xx)
yy_poly2 = model2.predict(xx_poly2)

# plt.scatter(X_
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值