day9 python学习笔记 【generalized linear models】

本文通过实验比较了线性回归、岭回归、Lasso回归及贝叶斯岭回归等几种线性模型在相同数据集上的预测表现,并展示了各自的预测误差。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

# -*- coding: utf-8 -*-
"""
Created on Mon May 14 09:17:53 2018

@author: sun_y
"""

import numpy as np
import sklearn as sk
import os
import matplotlib.pyplot as plt
from scipy.stats import norm  
import pickle
from sklearn import linear_model



def load_dataset(filename):

    with open(os.path.join("E:\\python\\sklearn model",filename), 'rb') as f:

        return pickle.load(f)
    
    
data=load_dataset('basisData.pkl')
X=data['X']
y=data['y']
Xtest=data['Xtest']
ytest=data['ytest']
"""
x=np.random.rand(100,1)*20-10

mu=0
sigma=0.1
s= norm.rvs(0, size=(100,1), scale=0.5) 
y=0.5*x+10+s

"""
fig=plt.figure()
ax1=fig.add_subplot(321)
plt.scatter(Xtest,ytest)
ax1.set_title('linear regression')

reg=linear_model.LinearRegression()
reg.fit(X,y)
ypredict=reg.predict(Xtest)
plt.plot(Xtest,ypredict,'r')
test_error=np.mean((ypredict - ytest)**2)
print("Logistic Regression test error is %f" % test_error)

ax2=fig.add_subplot(322)
plt.scatter(Xtest,ytest)
ax2.set_title('Ridge Regression')

reg = linear_model.Ridge (alpha = 0.9)
reg.fit(X,y)
ypredict=reg.predict(Xtest)
plt.plot(Xtest,ypredict,'r')
test_error=np.mean((ypredict - ytest)**2)
print("Ridge Regression test error is %f" % test_error)

ax3=fig.add_subplot(323)
plt.scatter(Xtest,ytest)
ax3.set_title('Lasso')

reg = linear_model.Lasso(alpha = 0.9)
reg.fit(X,y)

ypredict=reg.predict(Xtest)
plt.plot(Xtest,ypredict,'r')
test_error=np.mean((ypredict - ytest)**2)
print("Lasso test error is %f" % test_error)

ax3=fig.add_subplot(324)
plt.scatter(Xtest,ytest)
ax3.set_title('BayesRidge')
reg = linear_model.BayesianRidge()
reg.fit(X,y)

ypredict=reg.predict(Xtest)
plt.plot(Xtest,ypredict,'r')
test_error=np.mean((ypredict - ytest)**2)
print("BayesRidge test error is %f" % test_error)




Linear Regression test error is 3393.869098
Ridge Regression test error is 3393.614383
Lasso test error is 26459.575158
BayesRidge test error is 26458.745573

 
但拟合效果不是很好,可继续尝试其他模型:RBF , Polynomial regression
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值