import pandas as pd
import numpy as np
ft_zodiac = pd.read_csv('ft_zodiac.txt')
print(ft_zodiac.shape)
ft_zodiac.head()
15天以上的人为坏,标签为1;5天以内的人为好标签为0;5至15天的标签为2。
zodiac_label = pd.read_csv('zodiac_label.txt')
#这里去除标签为2的数据
ft_label = zodiac_label[zodiac_label.label != 2]
ft_label.head()
data = pd.merge(ft_label,ft_zodiac,on = 'order_id',how = 'inner')
data.head()
#用集合将出现的值存起来
zodiac_list = set(data.zodiac)
chinese_zodiac_list = set(data.chinese_zodiac)
zodiac_list
chinese_zodiac_list
#创建字典,来表示星座及对应的坏账率
zodiac_badrate = {}
for i in zodiac_list:
total = data[data.zodiac == i]
bad = total[total.label == 1]['label'].count()
good = total[total.label == 0]['label'].count()
zodiac_badrate[i] = round(bad/(bad + good),3)
zodiac_badrate
f = zip(zodiac_badrate.keys(),zodiac_badrate.values())
#f = sorted(f,key = lambda x : x[1],reverse = True )
zodiac_badrate = pd.DataFrame(f)
zodiac_badrate.columns = pd.Series(['星座','badrate'])
zodiac_badrate = zodiac_badrate.sort_values('badrate',ascending = False)
这里用了两种方式,一种是matplotlib,一种是pyecharts
matplotlib
import matplotlib.pyplot as plt
%matplotlib inline
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
plt.rcParams['figure.figsize'] = (8, 5)
x = zodiac_badrate['星座']
y = zodiac_badrate['badrate']
fig = plt.figure()
ax = fig.add_subplot(111)
ax.set(title='变量星座与坏账率的关系',ylabel='badrate',xlabel='星座')
ax.set_ylim(0.08,0.16,0.05)
ax.plot(x, y, color='blue', linewidth=3)
plt.show()
pyecharts
from pyecharts.charts import *
from pyecharts import options as opts
from pylab import *
mpl.rcParams['font.sans-serif'] = ['SimHei']
np.set_printoptions(suppress=True)
pd.set_option('display.unicode.ambiguous_as_wide', True)
pd.set_option('display.unicode.east_asian_width', True)
line = (
Line()
.add_xaxis(list(zodiac_badrate['星座']))
.add_yaxis(
"分组坏人占比",
list(zodiac_badrate['badrate']),
yaxis_index=0,
color="red",
)
.set_global_opts(
title_opts=opts.TitleOpts(title="变量星座与坏账率的关系"),
)
)
line.render_notebook()
#生肖
chinese_zodiac_badrate = {}
for x in chinese_zodiac_list:
a = data[data.chinese_zodiac == x]
bad = a[a.label == 1]['label'].count()
good = a[a.label == 0]['label'].count()
chinese_zodiac_badrate[x] = round(bad/(bad+good),3)
chinese_zodiac_badrate
f = zip(chinese_zodiac_badrate.keys(),chinese_zodiac_badrate.values())
f = sorted(f,key = lambda x : x[1],reverse = True )
chinese_zodiac_badrate = pd.DataFrame(f)
chinese_zodiac_badrate.columns = pd.Series(['生肖','badrate'])
chinese_zodiac_badrate
import matplotlib.pyplot as plt
%matplotlib inline
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
plt.rcParams['figure.figsize'] = (8, 5)
x = chinese_zodiac_badrate['生肖']
y = chinese_zodiac_badrate['badrate']
fig = plt.figure()
ax = fig.add_subplot(111)
ax.set(title='变量生肖与坏账率的关系',ylabel='badrate',xlabel='生肖')
ax.set_ylim(0.08,0.16,0.05)
ax.plot(x, y, color='blue', linewidth=3)
plt.show()
from pyecharts.charts import *
from pyecharts import options as opts
from pylab import *
mpl.rcParams['font.sans-serif'] = ['SimHei']
np.set_printoptions(suppress=True)
pd.set_option('display.unicode.ambiguous_as_wide', True)
pd.set_option('display.unicode.east_asian_width', True)
line = (
Line()
.add_xaxis(list(chinese_zodiac_badrate['生肖']))
.add_yaxis(
"分组坏人占比",
list(chinese_zodiac_badrate['badrate']),
yaxis_index=0,
color="red",
)
.set_global_opts(
title_opts=opts.TitleOpts(title="变量生肖与坏账率的关系"),
)
)
line.render_notebook()