机器学习实战笔记30-31:逻辑回归及对应调参实验代码

  1. 逻辑回归模型参数详解

求解对偶问题:如果数据量小但特征很多可以改成true

一般可以设置max_iter 大一些而tol小一些

Class_weight:输入{0:1,1:3}则代表1类样本的每条数据在计算损失函数时都会*3,当输入balanced,则调整为真实样本比例的反比,以达到平衡,但实际情况中不常用

Multi_class:默认情况auto,模型会优先根据惩罚项和solver选择OVR还是MVM

Solver:

  1. 逻辑回归调参实验

首先介绍PolynomialFeatures函数,其参数有degree(最高阶数)、interaction_only(是否包含交叉项)、include_bias(是否只包含0阶计算结果、偏置项)

具体实验代码如下:

#!/usr/bin/env python

coding: utf-8

In[1]:

import numpy as np

import pandas as pd

In[2]:

import matplotlib as mpl

import matplotlib.pyplot as plt

In[3]:

from sklearn.preprocessing import StandardScaler

from sklearn.preprocessing import PolynomialFeatures

from sklearn.linear_model import LogisticRegression

from sklearn.pipeline import make_pipeline

In[4]:

from sklearn.model_selection import train_test_split

from sklearn.metrics import accuracy_score

In[5]:

np.random.seed(24)

x=np.random.normal(0,1,size=(1000,2))

In[6]:

y=np.array(x[:,0]+x[:,1]**2<1.5,int)

In[7]:

plt.scatter(x[:,0],x[:,1],c=y)#c表示颜色

In[8]:

np.random.seed(24)

for i in range(200):

y[np.random.randint(1000)]=1

y[np.random.randint(1000)]=0

In[9]:

plt.scatter(x[:,0],x[:,1],c=y)

In[10]:

x_train,x_test,y_train,y_test=train_test_split(x,y,train_size=0.7,random_state=42)

In[26]:

def plr(degree=1,penalty='none',C=1.0):

pipe=make_pipeline(PolynomialFeatures(degree=degree,include_bias=False),

StandardScaler(),

LogisticRegression(penalty=penalty,tol=1e-4,C=C,max_iter=int(1e4)))

return pipe

#UI多迭代10的6次方次,tol是优化算法的收敛容忍度,c是正则化项参数

In[27]:

pl1=plr()

In[28]:

pl1.fit(x_train,y_train)

In[29]:

pl1.score(x_train,y_train),pl1.score(x_test,y_test)

In[37]:

def plot_decision_boundary(x,y,model):

x1,x2=np.meshgrid(

np.linspace(x[:,0].min()-1,x[:,0].max()+1,1000).reshape(-1,1),

np.linspace(x[:,1].min()-1,x[:,1].max()+1,1000).reshape(-1,1))

x_temp=np.concatenate([x1.reshape(-1,1),x2.reshape(-1,1)],1)

yhat_temp=model.predict(x_temp)

yhat=yhat_temp.reshape(x1.shape)

from matplotlib.colors import ListedColormap

custom_cmap=ListedColormap(['#EF9A9A','#90CAF9'])

plt.contourf(x1,x2,yhat,cmap=custom_cmap)

plt.scatter(x[(y==0).flatten(),0],x[(y==0).flatten(),1],color='red')

plt.scatter(x[(y==1).flatten(),0],x[(y==1).flatten(),1],color='red')

In[38]:

plot_decision_boundary(x,y,pl1)

In[39]:

#再看下2次特征进行建模:

pr2=plr(degree=2)

In[40]:

pr2.fit(x_train,y_train)

In[41]:

pr2.score(x_train,y_train),pr2.score(x_test,y_test)

#(0.7914285714285715, 0.7866666666666666),分数提升10%

In[43]:

plot_decision_boundary(x,y,pr2)

In[45]:

#如何查看参数情况

pr2.named_steps['logisticregression'].coef_

#array([[-0.81012988, 0.04384694, -0.48583038, 0.02977868, -1.12352417]])

In[46]:

#过拟合倾向实验

pr3=plr(degree=10)

pr3.fit(x_train,y_train)

pr3.score(x_train,y_train),pr3.score(x_test,y_test)

#(0.8314285714285714, 0.78)

In[47]:

plot_decision_boundary(x,y,pr3)

In[48]:

#尝试不同参数下准确率评分

score_l=[]

for degree in range(1,21):

pr_temp=plr(degree=degree)

pr_temp.fit(x_train,y_train)

score_temp=[pr_temp.score(x_train,y_train),pr_temp.score(x_test,y_test)]

score_l.append(score_temp)

In[49]:

np.array(score_l)

In[54]:

#画图看准确率变化

plt.plot(list(range(1,21)),np.array(score_l)[:,0],label='train_acc')

plt.plot(list(range(1,21)),np.array(score_l)[:,1],label='test_acc')

plt.legend(loc=4)#指定图例位置

In[55]:

#手动调参,尝试LL1正则化

pl1=plr(degree=10,penalty='l1',C=1.0)

In[57]:

pl1.set_params(logisticregression__solver='saga')

pl1.fit(x_train,y_train)#直接fit会报错,要改变求解器为saga

In[58]:

pl1.score(x_train,y_train),pl1.score(x_test,y_test)

In[62]:

#尝试枚举搜索参数有degree、C、正则化项

score_l1=[]

for degree in range(1,21):

pr_temp=plr(degree=degree,penalty='l1')

pr_temp.set_params(logisticregression__solver='saga')

pr_temp.fit(x_train,y_train)

score_temp=[pr_temp.score(x_train,y_train),pr_temp.score(x_test,y_test)]

score_l1.append(score_temp)

plt.plot(list(range(1,21)),np.array(score_l1)[:,0],label='train_acc')

plt.plot(list(range(1,21)),np.array(score_l1)[:,1],label='test_acc')

plt.legend(loc=4)#指定图例位置

In[63]:

score_l1#打印发现degree=3是最优解,以此为degree进行后面的搜索

In[64]:

score_l2=[]

for degree in range(1,21):

pr_temp=plr(degree=degree,penalty='l2')

pr_temp.set_params(logisticregression__solver='saga')

pr_temp.fit(x_train,y_train)

score_temp=[pr_temp.score(x_train,y_train),pr_temp.score(x_test,y_test)]

score_l2.append(score_temp)

plt.plot(list(range(1,21)),np.array(score_l2)[:,0],label='train_acc')

plt.plot(list(range(1,21)),np.array(score_l2)[:,1],label='test_acc')

plt.legend(loc=4)#指定图例位置

In[66]:

score_l2#打印发现degree=15是最优解,以此为degree进行后面的搜索

In[72]:

#尝试C的取值

score_l1_3=[]

for c in np.arange(0.5,2,0.1):

pr_temp=plr(degree=3,penalty='l1',C=c)

pr_temp.set_params(logisticregression__solver='saga')

pr_temp.fit(x_train,y_train)

score_temp=[pr_temp.score(x_train,y_train),pr_temp.score(x_test,y_test)]

score_l1_3.append(score_temp)

plt.plot(list(np.arange(0.5,2,0.1)),np.array(score_l1_3)[:,0],label='train_acc')

plt.plot(list(np.arange(0.5,2,0.1)),np.array(score_l1_3)[:,1],label='test_acc')

plt.legend(loc=4)#指定图例位置

In[73]:

score_l1_3#因此准确率最高为0.8左右

相关推荐
智者知已应修善业1 小时前
【51单片机用数码管显示流水灯的种类是按钮控制数码管加一和流水灯】2022-6-14
c语言·经验分享·笔记·单片机·嵌入式硬件·51单片机
m0_751336391 小时前
突破性进展:超短等离子体脉冲实现单电子量子干涉,为飞行量子比特奠定基础
人工智能·深度学习·量子计算·材料科学·光子器件·光子学·无线电电子
拓端研究室2 小时前
视频讲解:门槛效应模型Threshold Effect分析数字金融指数与消费结构数据
前端·算法
随缘而动,随遇而安4 小时前
第八十八篇 大数据中的递归算法:从俄罗斯套娃到分布式计算的奇妙之旅
大数据·数据结构·算法
美狐美颜sdk4 小时前
跨平台直播美颜SDK集成实录:Android/iOS如何适配贴纸功能
android·人工智能·ios·架构·音视频·美颜sdk·第三方美颜sdk
孞㐑¥4 小时前
Linux之Socket 编程 UDP
linux·服务器·c++·经验分享·笔记·网络协议·udp
DeepSeek-大模型系统教程4 小时前
推荐 7 个本周 yyds 的 GitHub 项目。
人工智能·ai·语言模型·大模型·github·ai大模型·大模型学习
郭庆汝4 小时前
pytorch、torchvision与python版本对应关系
人工智能·pytorch·python
IT古董4 小时前
【第二章:机器学习与神经网络概述】03.类算法理论与实践-(3)决策树分类器
神经网络·算法·机器学习
小雷FansUnion6 小时前
深入理解MCP架构:智能服务编排、上下文管理与动态路由实战
人工智能·架构·大模型·mcp