1-机器学习sklearn框架-书上总结的-logistic回归

sklearn的logistic模型训练

准备数据

1 新建exam_missing.ipynb文件,使用该工具能更好得体现思路和过程
:TODO 这里要补充数据的背景,各个字段的含义
2 首先导入相关依赖

import pandas as pd
import numpy as np
import seaborn as sns
from matplotlib import pyplot as plt
%matplotlib inline

这里做了inline设置,让plot图像的输出结果输出到文档中;

3 然后导入excel表,最为panda对象

df = pd.read_excel('default of credit card clients2.xlsx')

此时可以通过df.head()查看数据前五行的情况
在这里插入图片描述
4 然后我们对数据做一些整理

# 首先将表头的字段lower并替换空格为下划线
df.columns = df.columns.str.lower().str.replace('', '_')

然后这里的一些分类的字段用数字进行分类了,例如sex,marriage表示不同的状态,这样的数据没法用,我们需要把这几类都拆开来,比如sex有1和2,那就应该有两列sex_1和sex_2,并构成one-hot的模式来体现数据,例如该用户是男性,那么他的sex_1为1,而sex_2为0;当然我们更希望这些列能够更容易理解,所以首先第一步,我要把这些分类的数值按照码表,替换成更具体的文字;

# 将category的字段替换成字符串
# 主要包括categorical = ['sex', 'marriage', 'pay_0', 'pay_2', 'pay_3', 'pay_4', 'pay_5', 'pay_6']
df['sex'] = df['sex'].replace(1, 'male').replace(2, 'female')
df['education'] = df['education'].replace(0, 'education_0').replace(1, 'graduate_school').replace(2, 'university').replace(3, 'high_school').replace(4, 'others').replace(5, 'education_5').replace(6, 'education_6')

为什么要这样来替换呢?因为logistic回归,会通过每一个数值来找规律,对于像成绩、几点、金额、用时这些度量型的数据,其规律自然能够体现在具体的值上;而对于分类式的字段,像男女,男是1,女是0,你不能找出一个0.5说他的性别是不男不女;因为没有这个维度;因此,常见的方法是把男女两个字段单独分开来,如果是男,则sex_male这一列为1,sex_female为0;如果是女,则sex_male这一列为0,sex_female为1,如此便有利于量化;当然,这一步在后面的特征工程再去做,这里先把数字转码为文字
剩下的其他字段可以参考后面的代码

df['marriage'] = df['marriage'].replace(0,'marriage_0').replace(1,'married').replace(2,'single').replace(3,'others')
df['pay_0'] = df['pay_0'].replace(-2,'pay_2').replace(0,'pay_0').replace(-1, 'pay_duly').replace(1, 'payment_delay_for_one_month').replace(2, 'payment_delay_for_two_month').replace(3, 'payment_delay_for_three_month').replace(4, 'payment_delay_for_four_month').replace(5, 'payment_delay_for_five_month').replace(6, 'payment_delay_for_six_month').replace(7, 'payment_delay_for_seven_month').replace(8, 'payment_delay_for_eight_month')
df['pay_2'] = df['pay_2'<
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值