作业7 逻辑回归实践

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

 答:逻辑回归通过正则化可以防止过拟合。

因为过拟合的时候,拟合函数的系数往往非常大,过拟合,就是拟合函数需要顾忌每一个点,最终形成的拟合函数波动很大。在某些很小的区间里,函数值的变化很剧烈。这就意味着函数在某些小区间里的导数值(绝对值)非常大,由于自变量值可大可小,所以只有系数足够大,才能保证导数值很大。正则化是通过约束参数的范数使其不要太大,所以可以在一定程度上减少过拟合情况。

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

# 使用逻辑回归算法预测研究生入学考试是否会被录取
import pandas as pd
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split
from sklearn.metrics import classification_report
#(1)数据读取与预处理
data = pd.read_csv('./data/examination.csv')
x = data.iloc[: ,1:]
y = data.iloc[:,0]
x_train,x_test,y_train,y_test = train_test_split(x,y,test_size=0.2,random_state=5)
#(2)构建模型
LR_model = LogisticRegression()
#(3)训练模型
LR_model.fit(x_train,y_train)
#(4)预测模型
pre = LR_model.predict(x_test)
print('模型的准确率:',LR_model.score(x_test,y_test))
print('模型的召回率:',classification_report(y_test,pre))

运行结果:

 

原文地址:https://www.cnblogs.com/carmen-/p/12779852.html