1、目的
在逻辑回归算法中需要有一个正则项,防止过拟合。那么我们如何确定正则项的强度呢,如何调整这个参数呢?可以通过一个代码来实现,下面我们分析一下该参数对L1正则和L2正则的影响
2、代码
from sklearn import datasets
import numpy as np
from sklearn.linear_model import LogisticRegression as LG
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
from matplotlib import pyplot as plt
data=datasets.load_breast_cancer()
X_train,X_test,y_train,y_test = train_test_split(data.data,data.target,test_size=0.25,random_state=123)
C_list=np.linspace(0.1,1,19)
LG1_ac=[]
LG2_ac=[]
for i in C_list:
LG_1=LG(penalty='l1',C=i,solver='liblinear',max_iter=1000)
LG_2=LG(penalty='l2',C=i,solver='liblinear',max_iter=1000)
LG_1.fit(X_train,y_train)
LG_2.fit(X_train,y_train)
LG1_ac.append(accuracy_score(LG_1.predict(X_test),y_test))
LG2_ac