逻辑回归预测模型代码复现

1、逻辑回归代码复现

import numpy as np
import numpy.random
import pandas as pd
from matplotlib import pyplot as plt
from sklearn.preprocessing import StandardScaler
from sklearn import metrics
from sklearn import preprocessing as pp
from sklearn.metrics import classification_report, roc_auc_score
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression, SGDRegressor, LogisticRegression

def logistic():
    #path = r'D:\小美元\代码\ml-机器学习\data\breast-cancer-wisconsin.csv'#更改此处地址
    path = r'E:\思特奇\xxxx\模型2\min_max.csv'#更改此处地址
    #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']

    column = ['type1','type2','type3','type4','type5','type6','type7','type8','type9','type10','type11','type12','type13','type14','type15','type16','type17','type18','type19','type20','type21','type22','type23','type24','type25','type26','type27','type28','type29','type30','type31','type32','type33','type34','type35','type36','type37','type38','type39','type40','type41','type42','type43','type44','type45','type46','type47','type48','type49','type50','type51','type52','type53','type54','type55','type56','type57','type58','type59','type60','type61','type62','type63','type64','type65','type66','type67','type68','type69','type70','type71','type72','type73','type74','type75','type76','type77','type78','type79','type80','type81','type82','type83','type84','type85','type86','type87','type88','type89','type90','Class']
    data = pd.read_csv(path, names=column)
    print(data)
    # 进行数据的分割
    #train_test_split函数可以将原始数据集按照一定比例划分训练集和测试集对模型进行训练
    #x是训练的数据,y是数据对应的标签
    #test_size=0.2
    # 那就是训练集和测试集这里设置需要调整
    # 把 1号2号 3号的数据作为训练集合
    # x_train y_train 训练集 x_test y_test测试集
    # 正负样本的比例大约是100:1
    x_train, x_test, y_train, y_test = train_test_split(data[column[1:90]], data[column[90]], test_size=0.25)
    std = StandardScaler() #数据标准化

    print('-----x_train------')#特征
    print(x_train)

    print('-----x_test------')#特征
    print(x_test)

    print('-----y_train------')#标签
    print(y_train)

    print('-----y_test------') #标签
    print(y_test)

    x_train = std.fit_transform(x_train)
    x_test = std.transform(x_test)

    print('After_x_train',x_train)
    print('After_x_test',x_test)

    # 逻辑回归开始预测
    lg = LogisticRegression(C=1.0)
    lg.fit(x_train, y_train)
    print(lg.coef_)  # 特征值的参数
    y_predict = lg.predict(x_test)

    # 准确率(accuracy) = 预测对的/所有 = (TP+TN)/(TP+FN+FP+TN) = 70%
    # 正确率 = 提取出的正确信息条数 / 提取出的信息条数
    print("准确率:", lg.score(x_test, y_test))

    # 召回率 = 提取出的正确信息条数 / 样本中的信息条数
    print("召回率:", classification_report(y_test, y_predict, labels=[0, 1], target_names=["未停机", "停机"]))

    #AUC指标是由ROC曲线中得到来的,就是下面这部分的面积
    print("---AUC曲线--------")
    print("AUC指标:", roc_auc_score(y_test, y_predict))

    #AUC面积
    fpr, tpr, threshold = metrics.roc_curve(y_test, y_predict)
    roc_auc = metrics.auc(fpr, tpr)
    plt.figure(figsize=(6, 6))
    #plt.title('Validation ROC')
    plt.plot(fpr, tpr, 'b',roc_auc)
    plt.legend(loc='lower right')
    plt.plot([0, 1], [0, 1], 'r--')
    plt.xlim([0, 1])
    plt.ylim([0, 1])
    plt.ylabel('True Positive Rate')
    plt.xlabel('False Positive Rate')
    plt.show()



    return None

if __name__ == "__main__":
    logistic()
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值