7.逻辑回归实践

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

答:防止过拟合:

通过特征选择,剔除一些不重要的特征,从而降低模型复杂度;检查业务逻辑,判断特征有效性,是否在用结果预测结果等;

如果还过拟合,那就看看是否使用了过度复杂的特征构造工程,比如,某两个特征相乘/除/加等方式构造的特征,不要这样做了,保持原特征。

正则化可以防止过拟合的原因:

通过降低模型复杂度,得到更小的泛化误差,降低过拟合程度。

 

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

源代码:

print("201706120099,郑廷仁,软件1701")
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/LogisticRegression.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('输出模型的分类报告:\n',classification_report(y_test,pre))

 

posted on 2020-04-29 17:07  ztr啊仁  阅读(149)  评论(0编辑  收藏  举报

导航