用Python对案例进行Ridge算法和Lasso算法的比较

1. 加载数据

import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
import pandas as pd
import warnings  # 警告处理

from sklearn.linear_model import LassoCV, RidgeCV  # 回归模型
from sklearn.model_selection import train_test_split  # 划分数据集的类
from sklearn.preprocessing import StandardScaler  # 数据标准化的类
from sklearn.preprocessing import PolynomialFeatures  # 模型特征的构造
from sklearn.pipeline import Pipeline  # 管道
from sklearn.model_selection import GridSearchCV  # 模型最优参数选择
from sklearn.linear_model.coordinate_descent import ConvergenceWarning  # 警告处理
from matplotlib.font_manager import FontProperties
# 设置显示中文字体
my_font = FontProperties(fname="/usr/share/fonts/chinese/simsun.ttc")
# fontproperties = my_font
# 设置正常显示符号
mpl.rcParams["axes.unicode_minus"] = False
# 拦截异常
warnings.filterwarnings(action = 'ignore', category=ConvergenceWarning)
# 用于预处理数据
def notEmpty(s):
    return s != ''

"""
部分数据:
0.00632  18.00   2.310  0  0.5380  6.5750  65.20  4.0900   1  296.0  15.30 396.90   4.98  24.00
0.02731   0.00   7.070  0  0.4690  6.4210  78.90  4.9671   2  242.0  17.80 396.90   9.14  21.60
0.02729   0.00   7.070  0  0.4690  7.1850  61.10  4.9671   2  242.0  17.80 392.83   4.03  34.70
0.03237   0.00   2.180  0  0.4580  6.9980  45.80  6.0622   3  222.0  18.70 394.63   2.94  33.40

有14列数据
"""

# 1.加载数据
path = "/root/zhj/python3/code/data/boston_housing.data"
# 由于每条数据的格式不统一,所以可以先按一行一条记录的方式来读取,然后再进行数据预处理
fd = pd.read_csv(path, header = None)  # header = None表示没有数据对应的名称,可以给数据加上

2.数据处理

data = np.empty((len(fd), 14))  # 生成形状为[len(fd), 14]的空数组
# 对每条记录依次处理
for i, d in enumerate(fd.values):  # enumerate生成一列索引i(表示fd中的每一条记录), d为其元素(此处d就是fd的一条记录内容)
    d = map(float, filter(notEmpty, d[0].split(' '))) # filter一个函数,一个list
    """
	d[0].split(' '):将每条记录按空格切分,生成list,可迭代
	notEmpty:调用前面的自定义的函数,将空格表示为False,非空格表示为True
	filter(function,iterable):将迭代器传入函数中
	map(function,iterable):对迭代器进行function操作,这里表示根据filter结果是否为真,来过滤list中的空格项
    """
    # map操作后的类型为map类型,转为list类型,并将该条记录存在之前定义的空数组中
    data[i] = list(d)
    # 遍历完所有数据,数据也就处理好了

3.划分数据

X, Y = np.split(data, (13,), axis=1)  # 前13个数据划为X,最后一个划为Y
# 将Y拉直为一个扁平的数组
Y = Y.ravel()
# 查看下数据
print(Y.shape)
print ("样本数据量:%d, 特征个数:%d" % X.shape)
print ("target样本数据量:%d" % Y.shape[0])
(506,)
样本数据量:506, 特征个数:13
target样本数据量:506

4.搭建管道

models = [
    Pipeline([
            ('ss', StandardScaler()),  # 数据标准化
            ('poly', PolynomialFeatures()),  # 模型特征的构造
            ('linear', RidgeCV(alphas=np.logspace(-3,1,20)))  # Ridge模型,带交叉验证
        ]),
    Pipeline([
            ('ss', StandardScaler()),
            ('poly', PolynomialFeatures()),
            ('linear', LassoCV(alphas=np.logspace(-3,1,20)))  # LASSO模型,带交叉验证
        ])
] 
# 参数字典,字典中的key是属性的名称,value是可选的参数列表
parameters = {
    "poly__degree": [3,2,1], 
    "poly__interaction_only": [True, False], # 不产生交互项,如X1*X2就是交叉项, X1*X1为非交叉项
    "poly__include_bias": [True, False], # 多项式幂为零的特征作为线性模型中的截距;true表示包含
    "linear__fit_intercept": [True, False]
}

5.数据分割

x_train, x_test, y_train, y_test = train_test_split(X, Y, test_size=0.2, random_state=0)

6. 画图

# Lasso和Ridge模型比较运行图表展示
titles = ['Ridge', 'Lasso']
colors = ['g-', 'b-']
plt.figure(figsize=(16,8), facecolor='w')  # 画板,大小(16,8),颜色白色
ln_x_test = range(len(x_test))

plt.plot(ln_x_test, y_test, 'r-', lw=2, label=u'真实值')  # 画真实值,红色

for t in range(2):
    # 获取模型并设置参数
    # GridSearchCV: 进行交叉验证,选择出最优的参数值出来
    # 第一个输入参数:进行参数选择的模型
    # param_grid: 用于进行模型选择的参数字段,要求是字典类型
    # cv: 进行几折交叉验证
    model = GridSearchCV(models[t], param_grid = parameters,cv=5, n_jobs=1) # 五折交叉验证
    # 模型训练-网格搜索
    model.fit(x_train, y_train)
    # 模型效果值获取(最优参数)
    print ("%s算法:最优参数:" % titles[t],model.best_params_)
    print ("%s算法:R值=%.3f" % (titles[t], model.best_score_))
    # 模型预测
    y_predict = model.predict(x_test)
    # 画图
    plt.plot(ln_x_test, y_predict, colors[t], lw = t + 3, label=u'%s算法估计值,$R^2$=%.3f' % (titles[t],model.best_score_))
# 图形显示
plt.legend(loc = 'upper left',prop=my_font)
plt.grid(True)
plt.title(u"波士顿房屋价格预测",fontproperties=my_font)
plt.show()

Ridge算法:最优参数: {'linear__fit_intercept': True, 'poly__degree': 2, 'poly__include_bias': False, 'poly__interaction_only': True}
Ridge算法:R值=0.874
Lasso算法:最优参数: {'linear__fit_intercept': False, 'poly__degree': 3, 'poly__include_bias': True, 'poly__interaction_only': True}
Lasso算法:R值=0.857

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-8gMffbu0-1586178702801)(output_6_1.png)]

7. 特别说明

# 补充(查看每个特征的对应的模型参数)
#  我们选择上面Lasso回归得到的最优参数
names = ['CRIM','ZN', 'INDUS','CHAS','NOX','RM','AGE','DIS','RAD','TAX','PTRATIO','B','LSTAT']
model = Pipeline([
            ('ss', StandardScaler()),
            ('poly', PolynomialFeatures(degree=3, include_bias=False, interaction_only=True)),
            ('linear', LassoCV(alphas=np.logspace(-3,1,20), fit_intercept=False))
        ])
# 模型训练
model.fit(x_train, y_train)

# 数据输出
print ("参数:", list(zip(names, model.get_params('linear')['linear'].coef_)))
print ("截距:", model.get_params('linear')['linear'].intercept_)
参数: [('CRIM', -39.474093448970564), ('ZN', -2.5807924390756809), ('INDUS', 0.11881764117918749), ('CHAS', 0.0), ('NOX', -0.0), ('RM', 1.07502423529986), ('AGE', 0.0), ('DIS', -0.73915927509665647), ('RAD', -0.0), ('TAX', -0.0), ('PTRATIO', -1.8723014820610093), ('B', 0.024826171208050633), ('LSTAT', -3.5793432957403586)]
截距: 0.0

补充程序会输出线性模型的参数

比如:

参数: [(‘CRIM’, -39.474093448970564), (‘ZN’, -2.5807924390756809), (‘INDUS’, 0.11881764117918749), (‘CHAS’, 0.0), (‘NOX’, -0.0), (‘RM’, 1.07502423529986), (‘AGE’, 0.0), (‘DIS’, -0.73915927509665647), (‘RAD’, -0.0), (‘TAX’, -0.0), (‘PTRATIO’, -1.8723014820610093), (‘B’, 0.024826171208050633), (‘LSTAT’, -3.5793432957403586)]
截距: 0.0

其中有些参数是接近0的,那么我们可以认为当前参数对应的特征属性在模型的判别中没有太大的决策信息,

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Ridge回归是一种机器学习算法,它是线性回归的一种变种。它在标准线性回归的基础上加入了一个正则化项,以防止过拟合。Ridge回归的核心思想是通过最小化代价函数来找到最优的回归系数。代价函数由两部分组成,一部分是均方误差,用来衡量预测值与实际值之间的差距;另一部分是正则化项,用来控制回归系数的大小。正则化项中的参数&lambda;决定了正则化的程度,越大则对回归系数的限制越严格。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [机器学习算法-线性回归、Lasso回归、Ridge回归算法python实现](https://download.csdn.net/download/LYQZDX/87921627)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [机器学习算法系列(四)- 岭回归算法Ridge Regression Algorithm)](https://blog.csdn.net/sai_simon/article/details/122337097)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [机器学习算法系列篇9:LassoRidge回归算法](https://blog.csdn.net/robot_learner/article/details/103942849)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值