python决策树预测模型_根据决策树算法生成的模型进行预测

fromsklearnimporttreefromsklearn.model_selectionimporttrain_test_splitimportnumpyasnpimportpandasaspddefprocess_df_for_ml(df):"""

Process a dataframe for model training/prediction use.

Returns X/y tensors.

"""df=df.copy()# Map salary to 0,1,2df.salary=df.salary.map({"low":0,"medium":1,"high":2})# dropping left and sales X for the df, y for the leftX=df.drop(["left","sales"],axis=1)y=df["left"]return(X,y)# Read and reindex CSV.df=pd.read_csv("HR_comma_sep.csv")df=df.reindex()# Train a decision tree.X,y=process_df_for_ml(df)X_train,X_test,y_train,y_test=train_test_split(X,y,random_state=0,stratify=y)clftree=tree.DecisionTreeClassifier(max_depth=3)clftree.fit(X_train,y_train)# Test the decision tree on people who haven't left yet.notleftdf=df[df["left"]==0].copy()X,y=process_df_for_ml(notleftdf)# Plug in a new column with ones and zeroes from the prediction.notleftdf["will_leave"]=clftree.predict(X)# Print those with the will-leave flag on.print(notleftdf[notleftdf["will_leave"]==1])

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值