zoukankan      html  css  js  c++  java
  • 岭回归

    回归算法之岭回归

    具有L2正则化的线性最小二乘法。岭回归是一种专用于共线性数据分析的有偏估计回归方法,实质上是一种改良的最小二乘估计法,通过放弃最小二乘法的无偏性,以损失部分信息、降低精度为代价获得回归系数更为符合实际、更可靠的回归方法,对病态数据的拟合要强于最小二乘法。当数据集中存在共线性的时候,岭回归就会有用。

    from sklearn.linear_model import LogisticRegression
    from sklearn.model_selection import train_test_split
    from sklearn.preprocessing import StandardScaler
    from sklearn.metrics import classification_report
    
    import pandas as pd
    import numpy as np
    
    
    def logistic():
        """
        逻辑回归做二分类进行癌症预测(根据细胞的属性特征)
        :return: NOne
        """
        # 构造列标签名字
        column = ['Sample code number', 'Clump Thickness', 'Uniformity of Cell Size', 'Uniformity of Cell Shape',
                  'Marginal Adhesion', 'Single Epithelial Cell Size', 'Bare Nuclei', 'Bland Chromatin', 'Normal Nucleoli',
                  'Mitoses', 'Class']
    
        # 读取数据
        data = pd.read_csv(
            "https://archive.ics.uci.edu/ml/machine-learning-databases/breast-cancer-wisconsin/breast-cancer-wisconsin.data",
            names=column)
    
        print(data)
    
        # 缺失值进行处理
        data = data.replace(to_replace='?', value=np.nan)
    
        data = data.dropna()
    
        # 进行数据的分割
        x_train, x_test, y_train, y_test = train_test_split(data[column[1:10]], data[column[10]], test_size=0.25)
    
        # 进行标准化处理
        std = StandardScaler()
    
        x_train = std.fit_transform(x_train)
        x_test = std.transform(x_test)
    
        # 逻辑回归预测
        lg = LogisticRegression(C=1.0)
    
        lg.fit(x_train, y_train)
    
        print(lg.coef_)
    
        y_predict = lg.predict(x_test)
    
        print("准确率:", lg.score(x_test, y_test))
    
        print("召回率:", classification_report(y_test, y_predict, labels=[2, 4], target_names=["良性", "恶性"]))
    
        return None
    
    
    if __name__ == "__main__":
        logistic()
    

      

  • 相关阅读:
    文学、哲学段子
    文学、哲学段子
    js技术要点---JS 获取网页源代码
    泛型类,泛型方法,泛型委托的定义方法
    数组元素的逆序数
    stm32 ARM中的RO、RW和ZI DATA
    poj 3040 Allowance 贪心
    schedule()函数的调用时机(周期性调度)
    以JTextPanel为例Swing的鼠标事件详解
    实习生面试总结
  • 原文地址:https://www.cnblogs.com/yoyo1216/p/10361796.html
Copyright © 2011-2022 走看看