Python 建模步骤
#%%
#载入数据 、查看相关信息
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')
Python 建模步骤的更多相关文章
- Python学习步骤如何安排?
一.清楚学习目标 无论是学习什么知识,都要有一个对学习目标的清楚认识. 只有这样才能朝着目标持续前进,少走弯路,从学习中得到不断的提升,享受python学习计划的过程. 二.基本python 知识学习 ...
- Linux系统下升级Python版本步骤(suse系统)
Linux系统下升级Python版本步骤(suse系统) http://blog.csdn.net/lifengling1234/article/details/53536493
- 决策树python建模中的坑 :ValueError: Expected 2D array, got 1D array instead:
决策树python建模中的坑 代码 #coding=utf-8 from sklearn.feature_extraction import DictVectorizerimport csvfrom ...
- odoo 14 python 单元测试步骤
# odoo 14 python 单元测试步骤 # 一.在模块根目录创建tests目录 # 二.在tests目录下创建__init__.py文件 # 三.继承TransactionCase(Singl ...
- 逻辑回归--美国挑战者号飞船事故_同盾分数与多头借贷Python建模实战
python信用评分卡(附代码,博主录制) https://study.163.com/course/introduction.htm?courseId=1005214003&utm_camp ...
- Python机器学习步骤
推荐学习顺序 学习机器学习得有个步骤, 下面大家就能按照自己所需, 来探索这个网站. 图中请找到 "Start", 然后依次沿着箭头, 看看有没有不了解/没学过的地方, 接着, 就 ...
- 正态分布-python建模
sklearn实战-乳腺癌细胞数据挖掘 https://study.163.com/course/introduction.htm?courseId=1005269003&utm_campai ...
- T分布在医药领域应用-python建模
sklearn实战-乳腺癌细胞数据挖掘 https://study.163.com/course/introduction.htm?courseId=1005269003&utm_campai ...
- 下载及安装Python详细步骤
安装python分三个步骤: *下载python *安装python *检查是否安装成功 1.下载Python (1)python下载地址https://www.python.org/download ...
随机推荐
- Net Core微服务
Net Core微服务 http://www.cnblogs.com/qhbm/category/1235971.html 开发工具:VS2017 .Net Core 2.1 什么是微服务? 单体结构 ...
- net core 2.0 web api + Identity Server 4 + angular 5
net core 2.0 web api + Identity Server 4 + angular 5前台使用angular 5, 后台是asp.net core 2.0 web api + ide ...
- 牛客网Java刷题知识点之为什么static成员方法不能是抽象方法,其必须实现
不多说,直接上干货! static修饰的方法我们称之为静态方法,我们通过类名对其进行直接调用.由于它在类加载的时候就存在了,它不依赖于任何实例,所以static方法必须实现,也就是说它不能是抽象方法.
- SpriingMVC执行流程结构
SpringMVC也叫spring web mvc,属于表现层的框架,是Spring框架的一部分. Spring MVC请求流程图: request-------->DispatcherSer ...
- Java并发(二):基础概念
并发编程的第二部分,先来谈谈发布(Publish)与逸出(Escape); 发布是指:对象能够在当前作用域之外的代码中使用,例如:将对象的引用传递到其他类的方法中,对象的引用保存在其他类可以访问的地方 ...
- mitmweb的使用
安装mitmproxy时带有mitmweb,可直接在命令行输入命令:mitmweb 此时可打开web界面.
- IOS使用固定定位遇到的问题
近日需要实现移动端页面额外功能按钮,即点击加号弹出点赞与留言功能,通常这个按钮都会固定于页面的右下角,首先就想到使用固定定位来实现. 但是在测试时我们发现,在IOS中,当系统键盘弹出时,fixed会失 ...
- class类型重定义,防止头文件重复加载
今天调用自己写的一个类,出现了class类型重定义问题,上网查了相关资料,发现是头文件重复include引起的问题. 防止头文件重复加载: 系统那些头文件,无论怎么include都没事,因为一般都用了 ...
- -oN ,-oX,-oG
-oN ,正常输出 -oX, xml输出 nmap 192.168.9.12 -oX TEST.xml -oG grep输出 html文件可读性比xml文件要好,将xml转换成html xs ...
- Java 反射机制(一)
阅读<Core Java Volume I --- Fundamentals>反射部分,总觉得许多概念艰涩难懂.模棱两可.我想造成这个结果的主要原因可能是Cay S. Horstmann和 ...