美文网首页
机器学习-逻辑回归法实现肿瘤预测案例

机器学习-逻辑回归法实现肿瘤预测案例

作者: 涓涓自然卷 | 来源:发表于2021-03-10 08:35 被阅读0次

    1、分析步骤:
    (1)获取数据:https://archive.ics.uci.edu/ml/machine-learning-databases/breast-cancer-wisconsin/breast-cancer-wisconsin.data
    (2)基本数据处理:缺失值处理、确定特征值,目标值、分割数据
    (3)特征工程
    (4)机器学习 - 模型训练(逻辑回归)
    (5)模型评估:准确率、预测值

    2、所需API:
    import pandas as pd
    import numpy as np
    from sklearn.model_selection import train_test_split
    from sklearn.preprocessing import StandardScaler
    from sklearn.linear_model import LogisticRegression

    3、代码示例🌰:

    import pandas as pd
    import numpy as np
    from sklearn.model_selection import train_test_split
    from sklearn.preprocessing import StandardScaler
    from sklearn.linear_model import LogisticRegression
    import ssl
    
    ssl._create_default_https_context = ssl._create_unverified_context
    
    """
    肿瘤分类分析
    """
    
    # 1、获取数据
    names = ['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=names)
    
    print(data.head())
    
    # 2、基本数据处理
    # 2.1、缺失值处理
    data = data.replace(to_replace="?", value=np.nan)
    data = data.dropna()
    
    # 2.2 确定特征值,目标值
    x = data.iloc[:, 1:10]
    print("x.head():", x.head())
    y = data["Class"]
    print("y.head():\n", y.head())
    
    # 2.3 分割数据
    x_train, x_test, y_train, y_test = train_test_split(x, y, random_state=22, test_size=0.2)
    
    # 3.特征工程(标准化)
    transfer = StandardScaler()
    x_train = transfer.fit_transform(x_train)
    x_test = transfer.fit_transform(x_test)
    
    # 4.机器学习 - 模型训练(逻辑回归)
    estimator = LogisticRegression()
    estimator.fit(x_train, y_train)
    
    # 5.模型评估
    # 5.1 准确率
    ret = estimator.score(x_test, y_test)
    print("准确率为:\n", ret)
    
    # 5.2 预测值
    y_pre = estimator.predict(x_test)
    print("模型预测值为:\n", y_pre)
    
    
    

    4、示例运行结果:

    运行结果.png

    总结:

    • 虽然例子中,运行结果可以看出预测值还是比较高的,但是在很多分类场景当中,我们不一定只关注预测的准确率!!!
      在这个癌症例子里,我们并不关注预测的准确率,而是关注在多有的样本当中,癌症患者有没有被全部预测(检测)出来,可以关注精确率、召回率等。

    • 简单来说:

      • 如果数据中有缺失值,一定要对其进行处理。
      • 准确率并不是衡量分类正确的唯一标准。

    相关文章

      网友评论

          本文标题:机器学习-逻辑回归法实现肿瘤预测案例

          本文链接:https://www.haomeiwen.com/subject/bnlbqltx.html