专栏首页python3Python 建模步骤

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')

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

我来说两句

0 条评论
登录 后参与评论

相关文章

  • Python_冒泡排序

    从小到大的排序:(最前面的数和一步步和后面的数比较,如果大于则交换,如果不大于则继续循环)

    py3study
  • python(实操4):录音文件的读取、

    py3study
  • Python算法题----逆序列表

        有这样一个列表[1, 2, 3, 4, 5, 6, 7, 8, 9]编程实现该列表逆序排列,将其变为[9, 8, 7, 6, 5, 4, 3, 2, 1...

    py3study
  • 上映4天,票房7.4亿的《海王》,用Python分析数据看大片!

    《海王》一部电影带你重温《驯龙高手》《变形金刚》《星球大战》《星河战队》《铁血战士》《安德的游戏》《异形》可能还借鉴了对手的《钢铁侠》与《黑豹》剧情,再稍稍带一...

    周三不加班
  • 使用 Python 实现几种常见的排序算法

    冒泡排序是最为基础的排序算法,其核心思想就是相邻元素两两比较,把较大的元素放到后面,在一轮比较完成之后,最大的元素就位于最后一个位置了,就好像是气泡,慢慢的浮出...

    周萝卜
  • pandas的一些小知识

    生信编程日常
  • 【go】剑指offer:常见排序算法

    冒泡排序是比较简单的排序算法,它的关键思想是移动指针不断的进行两两比较,将最大的数字不断的进行更换位置,直至到最后,即完成一趟比较,都会寻找到最大的数字,且最大...

    陌无崖
  • Python_冒泡排序

    从小到大的排序:(最前面的数和一步步和后面的数比较,如果大于则交换,如果不大于则继续循环)

    py3study
  • 使用脚手架应用做单元测试

    版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。 ...

    Jerry Wang
  • 通过空气质量指数AQI学习统计分析并进行预测(上)

    AQI(空气质量指数),用来衡量空气清洁或者污染的程度。值越小,表示空气质量越好。近年来,因为环境问题,空气质量也越来越受到人们的重视。

    朱小五

扫码关注云+社区

领取腾讯云代金券