#%%
#载入数据 、查看相关信息
import pandas as pd
import numpy as np
from sklearn.preprocessing import LabelEncoder print('第一步:加载、查看数据') file_path = r'D:\train\201905data\liwang.csv' band_data = pd.read_csv(file_path,encoding='UTF-8') band_data.info() band_data.shape #%%
#
print('第二步:清洗、处理数据,某些数据可以使用数据库处理数据代替') #数据清洗:缺失值处理:丢去、
#查看缺失值
band_data.isnull().sum band_data = band_data.dropna()
#band_data = band_data.drop(['state'],axis=1)
# 去除空格
band_data['voice_mail_plan'] = band_data['voice_mail_plan'].map(lambda x: x.strip())
band_data['intl_plan'] = band_data['intl_plan'].map(lambda x: x.strip())
band_data['churned'] = band_data['churned'].map(lambda x: x.strip())
band_data['voice_mail_plan'] = band_data['voice_mail_plan'].map({'no':0, 'yes':1})
band_data.intl_plan = band_data.intl_plan.map({'no':0, 'yes':1}) for column in band_data.columns:
if band_data[column].dtype == type(object):
le = LabelEncoder()
band_data[column] = le.fit_transform(band_data[column]) #band_data = band_data.drop(['phone_number'],axis=1)
#band_data['churned'] = band_data['churned'].replace([' True.',' False.'],[1,0])
#band_data['intl_plan'] = band_data['intl_plan'].replace([' yes',' no'],[1,0])
#band_data['voice_mail_plan'] = band_data['voice_mail_plan'].replace([' yes',' no'],[1,0]) #%%
# 模型 [重复、调优]
print('第三步:选择、训练模型') x = band_data.drop(['churned'],axis=1)
y = band_data['churned'] from sklearn import model_selection
train,test,t_train,t_test = model_selection.train_test_split(x,y,test_size=0.3,random_state=1) from sklearn import tree
model = tree.DecisionTreeClassifier(max_depth=2)
model.fit(train,t_train) fea_res = pd.DataFrame(x.columns,columns=['features'])
fea_res['importance'] = model.feature_importances_ t_name= band_data['churned'].value_counts()
t_name.index import graphviz import os
os.environ["PATH"] += os.pathsep + r'D:\software\developmentEnvironment\graphviz-2.38\release\bin' dot_data= tree.export_graphviz(model,out_file=None,feature_names=x.columns,max_depth=2,
class_names=t_name.index.astype(str),
filled=True, rounded=True,
special_characters=False)
graph = graphviz.Source(dot_data)
#graph
graph.render("dtr") #%%
print('第四步:查看、分析模型') #结果预测
res = model.predict(test) #混淆矩阵
from sklearn.metrics import confusion_matrix
confmat = confusion_matrix(t_test,res)
print(confmat) #分类指标 https://blog.csdn.net/akadiao/article/details/78788864
from sklearn.metrics import classification_report
print(classification_report(t_test,res)) #%%
print('第五步:保存模型') from sklearn.externals import joblib
joblib.dump(model,r'D:\train\201905data\mymodel.model') #%%
print('第六步:加载新数据、使用模型')
file_path_do = r'D:\train\201905data\do_liwang.csv' deal_data = pd.read_csv(file_path_do,encoding='UTF-8') #数据清洗:缺失值处理 deal_data = deal_data.dropna()
deal_data['voice_mail_plan'] = deal_data['voice_mail_plan'].map(lambda x: x.strip())
deal_data['intl_plan'] = deal_data['intl_plan'].map(lambda x: x.strip())
deal_data['churned'] = deal_data['churned'].map(lambda x: x.strip())
deal_data['voice_mail_plan'] = deal_data['voice_mail_plan'].map({'no':0, 'yes':1})
deal_data.intl_plan = deal_data.intl_plan.map({'no':0, 'yes':1}) for column in deal_data.columns:
if deal_data[column].dtype == type(object):
le = LabelEncoder()
deal_data[column] = le.fit_transform(deal_data[column])
#数据清洗 #加载模型
model_file_path = r'D:\train\201905data\mymodel.model'
deal_model = joblib.load(model_file_path)
#预测
res = deal_model.predict(deal_data.drop(['churned'],axis=1)) #%%
print('第七步:执行模型,提供数据')
result_file_path = r'D:\train\201905data\result_liwang.csv' deal_data.insert(1,'pre_result',res)
deal_data[['state','pre_result']].to_csv(result_file_path,sep=',',index=True,encoding='UTF-8')

最新文章

  1. Mac入门 (二) 使用VMware Fusion虚拟机
  2. Sql Server系列:Microsoft SQL Server Management Studio模板资源管理器
  3. 1001. 害死人不偿命的(3n+1)猜想 (15)
  4. python splinter
  5. 在Nifi 里 把 HDFS Json 为csv 格式
  6. kernel/module.c
  7. trie树---(插入、删除、查询字符串)
  8. QQ等级图标排名说明_QQ等级表,QQ最高等级(皇冠) qq到一星要5天
  9. Careerup上的简历模板
  10. Php基本语法数据类型操作基础训练
  11. 一个完整的项目中,需要的基本gulp
  12. SSM-SpringMVC-24:SpringMVC异常高级之自定义异常
  13. 20171201 - macOS High Sierra 神级 bug
  14. Java 由浅入深GUI编程实战练习(一)
  15. L319 Zigbee test coding- field test fail-base on EFR32MG1
  16. golang中的 time 常用操作
  17. 牛X的FieldBlur
  18. 【8.19校内测试】【背包】【卡特兰数】【数位dp】
  19. BZOJ4897 THUSC2016成绩单(区间dp)
  20. ios 时间解析 差8个小时

热门文章

  1. [Andoird]Andoird之Log
  2. [LOJ 2190] 「SHOI2014」信号增幅仪
  3. Ubuntu 下修改Tomcat和Jetty默认的JDK和初始内存
  4. (转)Module ngx_http_fastcgi_module
  5. css3弹性伸缩和使用
  6. java构造方法之我见
  7. 《超实用的Node.js代码段》连载一:获取Buffer对象字节长度
  8. robotframework介绍
  9. LeetCode House Robber 家庭劫犯(dp)
  10. python爬虫之路——初识lxml库和xpath语法