机器学习实战笔记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左右

相关推荐
望获linux25 分钟前
【实时Linux实战系列】Linux 内核的实时组调度(Real-Time Group Scheduling)
java·linux·服务器·前端·数据库·人工智能·深度学习
程序员大雄学编程30 分钟前
「深度学习笔记4」深度学习优化算法完全指南:从梯度下降到Adam的实战详解
笔记·深度学习·算法·机器学习
Dev7z34 分钟前
河南特色农产品识别系统:让AI守护“中原味道”
人工智能
新子y39 分钟前
【小白笔记】PyTorch 和 Python 基础的这些问题
pytorch·笔记·python
万俟淋曦40 分钟前
【论文速递】2025年第28周(Jul-06-12)(Robotics/Embodied AI/LLM)
人工智能·ai·机器人·大模型·论文·robotics·具身智能
我是李武涯1 小时前
PyTorch DataLoader 高级用法
人工智能·pytorch·python
每月一号准时摆烂1 小时前
PS基本教学(三)——像素与分辨率的关系以及图片的格式
人工智能·计算机视觉
小O的算法实验室1 小时前
2022年ASOC SCI2区TOP,基于竞争与合作策略的金字塔粒子群算法PPSO,深度解析+性能实测,深度解析+性能实测
算法·论文复现·智能算法·智能算法改进
song150265372981 小时前
全自动视觉检测设备
人工智能·计算机视觉·视觉检测
2501_906519671 小时前
大语言模型的幻觉问题:机理、评估与抑制路径探析
人工智能