python建模的步骤_python基础教程之Python 建模步骤|python基础教程|python入门|python教程...
#%%#載入數(shù)據(jù) 、查看相關(guān)信息
importpandas as pdimportnumpy as npfrom sklearn.preprocessing importLabelEncoderprint('第一步:加載、查看數(shù)據(jù)')
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('第二步:清洗、處理數(shù)據(jù),某些數(shù)據(jù)可以使用數(shù)據(jù)庫處理數(shù)據(jù)代替')#數(shù)據(jù)清洗:缺失值處理:丟去、#查看缺失值
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(lambdax: x.strip())
band_data['intl_plan'] = band_data['intl_plan'].map(lambdax: x.strip())
band_data['churned'] = band_data['churned'].map(lambdax: 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 inband_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])
#%%#模型 [重復(fù)、調(diào)優(yōu)]
print('第三步:選擇、訓(xùn)練模型')
x= band_data.drop(['churned'],axis=1)
y= band_data['churned']from sklearn importmodel_selection
train,test,t_train,t_test= model_selection.train_test_split(x,y,test_size=0.3,random_state=1)from sklearn importtree
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.indeximportgraphvizimportos
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('第四步:查看、分析模型')#結(jié)果預(yù)測
res =model.predict(test)#混淆矩陣
from sklearn.metrics importconfusion_matrix
confmat=confusion_matrix(t_test,res)print(confmat)#分類指標(biāo) https://blog.csdn.net/akadiao/article/details/78788864
from sklearn.metrics importclassification_reportprint(classification_report(t_test,res))#%%
print('第五步:保存模型')from sklearn.externals importjoblib
joblib.dump(model,r'D:\train\201905data\mymodel.model')#%%
print('第六步:加載新數(shù)據(jù)、使用模型')
file_path_do= r'D:\train\201905data\do_liwang.csv'deal_data= pd.read_csv(file_path_do,encoding='UTF-8')#數(shù)據(jù)清洗:缺失值處理
deal_data=deal_data.dropna()
deal_data['voice_mail_plan'] = deal_data['voice_mail_plan'].map(lambdax: x.strip())
deal_data['intl_plan'] = deal_data['intl_plan'].map(lambdax: x.strip())
deal_data['churned'] = deal_data['churned'].map(lambdax: 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 indeal_data.columns:if deal_data[column].dtype ==type(object):
le=LabelEncoder()
deal_data[column]=le.fit_transform(deal_data[column])#數(shù)據(jù)清洗
#加載模型
model_file_path = r'D:\train\201905data\mymodel.model'deal_model=joblib.load(model_file_path)#預(yù)測
res = deal_model.predict(deal_data.drop(['churned'],axis=1))#%%
print('第七步:執(zhí)行模型,提供數(shù)據(jù)')
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')
總結(jié)
以上是生活随笔為你收集整理的python建模的步骤_python基础教程之Python 建模步骤|python基础教程|python入门|python教程...的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。