逻辑回归实践
1.逻辑回归是怎么防止过拟合的?为什么正则化可以防止过拟合?(大家用自己的话介绍下)
逻辑回归通过正则化来防止过拟合。
正则化是控制模型空间的一种办法,通过收缩,限制模型变得越来越大,牺牲样本内误差,降低模型的误差。
简单来说就是:以L2正则化为例,正则项会使权重趋于0,就意味着大量权重就和0没什么差别了,此时网络就变得很简单,拟合能力变弱,从高方差往高偏差的方向移动。
激活函数的角度讲:以sigmoid或tanh为例,当w趋于0时(忽略偏置b),激活值趋于0,此时位于激活函数的线性趋于,神经网络就变成一个线性网络,不容易过拟合。
2.用logiftic回归来进行实践操作,数据不限。
from sklearn.datasets import load_breast_cancer from sklearn.linear_model import LogisticRegression from sklearn.model_selection import train_test_split from sklearn.metrics import classification_report data = load_breast_cancer() x = data.data y = data.target #划分训练和测试集 x_train, x_test, y_train, y_test= train_test_split(x , y , test_size=0.8) #构建模型病训练模型 model_LR = LogisticRegression() model_LR.fit(x_train,y_train) #模型预测 y_pre = model_LR.predict(x_test) print('预测:', y_pre) print('真实:', y_test) print('分类报告\n', classification_report(y_test, y_pre)) print('准确率为: {0:.2f}%'.format(model_LR.score(x_test,y_test)*100))




浙公网安备 33010602011771号