7.逻辑回归实践
1.逻辑回归是怎么防止过拟合的?为什么正则化可以防止过拟合?(大家用自己的话介绍下)
增加样本量
如果数据稀疏,使用L1正则,其他则L2
通过特征选择,剔除一些不重要的特征,从而降低模型复杂度。
检查业务逻辑,判断特征有效性,是否在用结果预测结果
进行离散化处理,所有特征都离散化
2.用logiftic回归来进行实践操作,数据不限。
这里用的数据是老师课堂给的breast-cancer-wisconsin.scv
实验代码:
import pandas as pd import numpy as np from sklearn.linear_model import LogisticRegression from sklearn.model_selection import train_test_split from sklearn.metrics import classification_report from sklearn.preprocessing import StandardScaler def logistic(): data = pd.read_csv("./breast-cancer-wisconsin.csv") data = data.replace(to_replace='?', value=np.nan) data = data.dropna() x = data.iloc[:, 1:10] y = data.iloc[:, 10] x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.3) std = StandardScaler() x_train = std.fit_transform(x_train) x_test = std.fit_transform(x_test) lg = LogisticRegression() lg.fit(x_train, y_train) print(lg.coef_) print("准确率:", lg.score(x_test, y_test)) print("召回率:", classification_report(y_test, lg.predict(x_test))) if __name__ == "__main__": logistic()
实验结果: