目录
L0范数,L1范数,L2范数
sigmond函数
损失函数
penalty和C参数
逻辑回归中的特征工程
梯度下降
L0范数是指向量中非0的元素的个数。如果我们用L0范数来规则化一个参数矩阵W的话,就是希望W的大部分元素都是0。
L1范数是指向量中各个元素绝对值之和,也有个美称叫“稀疏规则算子”(Lasso regularization)。
任何的规则化算子,如果他在Wi=0的地方不可微,并且可以分解为一个“求和”的形式,那么这个规则化算子就可以实现稀疏。这说是这么说,W的L1范数是绝对值,|w|在w=0处是不可微。
L1范数和L0范数可以实现稀疏,L1因具有比L0更好的优化求解特性而被广泛应用。
L2范数是指向量各元素的平方和然后求平方根。我们让L2范数的规则项||W||2最小,可以使得W的每个元素都很小,都接近于0,但与L1范数不同,它不会让它等于0,而是接近于0,这里是有很大的区别的哦。而越小的参数说明模型越简单,越简单的模型则越不容易产生过拟合现象。
制了参数很小,实际上就限制了多项式某些分量的影响很小,这样就相当于减少参数个数。
损失函数仅仅针对有参数的模型而言,没有参数的模型就不存损失函数,比如KNN,决策树。
#比较L1正则化和L2正则化
from sklearn.linear_model import LogisticRegression as LR
from sklearn.datasets import load_breast_cancer #导入乳腺癌数据集
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
data=load_breast_cancer()
X=data.data
Y=data.target
#实例化
lr_l1=LR(penalty='l1',solver='liblinear',C=0.5,max_iter=1000)
lr_l2=LR(penalty='l2',solver='liblinear',C=0.5,max_iter=1000)
lr_l1=lr_l1.fit(X,Y)
lr_l2=lr_l2.fit(X,Y)
#查看coef_属性 正则化后不为0参数个数
print(lr_l1.coef_)
print((lr_l1.coef_!=0).sum())
print(lr_l2.coef_)
print((lr_l2.coef_!=0).sum())
绘制参数C的学习曲线
#绘制逻辑回归参数C学习曲线
from sklearn.linear_model import LogisticRegression as LR
from sklearn.datasets import load_breast_cancer #导入乳腺癌数据集
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
data = load_breast_cancer()
X = data.data
y = data.target
Xtrain, Xtest, Ytrain, Ytest = train_test_split(X,y,test_size=0.3)
l1=[]
l2=[]
l1test=[]
l2test=[]
for i in np.linspace(0.05,1,19):
lr_l1=LR(penalty='l1',solver='liblinear',C=i,max_iter=1000)
lr_l2=LR(penalty='l2',solver='liblinear',C=i,max_iter=1000)
lr_l1=lr_l1.fit(Xtrain,Ytrain)
l1.append(accuracy_score(lr_l1.predict(Xtrain),Ytrain))
l1test.append(accuracy_score(lr_l1.predict(Xtest),Ytest))
lr_l2=lr_l2.fit(Xtrain,Ytrain)
l2.append(accuracy_score(lr_l2.predict(Xtrain),Ytrain))
l2test.append(accuracy_score(lr_l2.predict(Xtest),Ytest))
graph = [l1,l2,l1test,l2test]
color = ["green","black","lightgreen","red"]
label = ["L1","L2","L1test","L2test"]
plt.figure(figsize=(20,8),dpi=80)
for i in range(len(graph)):
plt.plot(np.linspace(0.05,1,19),graph[i],color[i],label=label[i])
plt.xlabel('C')
plt.ylabel('accurancy')
plt.legend(loc='best')
plt.show()
from sklearn.linear_model import LogisticRegression as LR
from sklearn.datasets import load_breast_cancer
import numpy as np
import matplotlib.pyplot as plt
from sklearn.model_selection import cross_val_score
from sklearn.feature_selection import SelectFromModel
data=load_breast_cancer()
LR_ = LR(solver="liblinear",C=0.8)
score=cross_val_score(LR_,data.data,data.target,cv=10).mean()
X_embedded = SelectFromModel(LR_,norm_order=1).fit_transform(data.data,data.target) #使用L1范数进行筛选
score_=cross_val_score(LR_,X_embedded,data.target,cv=10).mean()
print(score)
print(score_)
print(X_embedded)
from sklearn.linear_model import LogisticRegression as LR
from sklearn.datasets import load_breast_cancer #导入乳腺癌数据集
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
data=load_breast_cancer()
X=data.data
Y=data.target
l1=[]
l1test=[]
Xtrain,Xtest,Ytrain,Ytest=train_test_split(X,Y,test_size=0.3)
for i in np.arange(1,201,20):
lrl2=LR(penalty='l2',solver='liblinear',C=0.9,max_iter=i)
lrl2=lrl2.fit(Xtrain,Ytrain)
l1.append(accuracy_score(lrl2.predict(Xtrain),Ytrain))
l1test.append(accuracy_score(lrl2.predict(Xtest),Ytest))
graph = [l1,l1test]
color = ["black","green"]
label = ["L1","L1test"]
plt.figure(figsize=(20,8),dpi=80)
for i in range(len(graph)):
plt.plot(np.arange(1,201,20),graph[i],c=color[i],label=label[i])
plt.legend(loc=0)
plt.xticks(np.arange(1,201,20))
plt.xlabel('max-iter')
plt.ylabel('accurancy')
plt.show()
中途会遇到警告