7.逻辑回归实践

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

1)减少数量特征或正则化

2)正则化不需减少数量特征,只需要通过减小特征变量的数量级使他们接近于0.这样子就可以形成一个类似二元的多元函数。

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

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()

原文地址:https://www.cnblogs.com/hoioh/p/12785741.html