7.逻辑回归实践

1.逻辑回归是怎么防止过拟合的?为什么正则化可以防止过拟合?(大家用自己的话介绍下)

逻辑回归通过正则化来防止过拟合;正则化可以防止过拟合是因为过拟合的时候,拟合函数的系数往往非常大,而正则化是

通过约束参数的范数使其不要太大,所以可以在一定程度上减少过拟合情况,以L2正则化为例,正则项会使权重趋于0,就

意味着大量权重就和0没什么差别了,此时网络就变得很简单,拟合能力变弱,从高方差往高偏差的方向移动。

2.用logiftic回归来进行实践操作,数据不限。

# (1)加载load_breast_cancer数据集,并划分训练集与测试集。
from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import train_test_split

cancer = load_breast_cancer()
x = cancer.data
y = cancer.target
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.2, random_state=5)

# (2)训练模型,并计算训练数据集的评分数据和测试数据集的评分数据,以及查看测试样本中预测正确的个数。
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import classification_report

model = LogisticRegression()
model.fit(x_train, y_train)
y_pre = model.predict(x_test)
print('训练数据集的评分:', model.score(x_train, y_train))
print('测试数据集的评分:', model.score(x_test, y_test))
print('预测个数:', x_test.shape[0])
print('预测正确个数:', x_test.shape[0] * model.score(x_test, y_test))
print("召回率", classification_report(y_test, y_pre))

原文地址:https://www.cnblogs.com/sgczw/p/12788015.html