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

Posted

技术标签:

【中文标题】根据决策树算法生成的模型进行预测【英文标题】:Making a Prediction from Model Genereted by Decision Tree Algorithm 【发布时间】:2020-02-22 12:06:58 【问题描述】:

我一直在尝试做出一个预测,该预测由我使用决策树算法创建的模型中的 DataFrame 组成。

我的模型得分为 0.96。然后,我尝试使用该模型对留下但出错的 DataFrame 人进行预测。目标是根据留下的 DataFrame 预测未来将离开公司的人。

如何实现这个目标?

所以我做的是:

    从我的 github 读取 DF 并将它们分成离开和未离开的人
df = pd.read_csv('https://raw.githubusercontent.com/bhaskoro-muthohar/DataScienceLearning/master/HR_comma_sep.csv')

leftdf = df[df['left']==1]
notleftdf =df[df['left']==0]
    为模型生成准备数据
df.salary = df.salary.map('low':0,'medium':1,'high':2)
df.salary
X = df.drop(['left','sales'],axis=1)
y = df['left']
    拆分训练集和测试集
import numpy as np
from sklearn.model_selection import train_test_split


#splitting the train and test sets
X_train, X_test, y_train, y_test= train_test_split(X,y,random_state=0, stratify=y)
    训练它
from sklearn import tree
clftree = tree.DecisionTreeClassifier(max_depth=3)
clftree.fit(X_train,y_train)
    评估模型
y_pred = clftree.predict(X_test)
print("Test set prediction:\n ".format(y_pred))
print("Test set score: :.2f".format(clftree.score(X_test, y_test)))

结果是

测试集分数:0.96

    然后我尝试使用尚未离开公司的人的 DataFrame 进行预测
X_new = notleftdf.drop(['left','sales'],axis=1)

#Map salary to 0,1,2
X_new.salary = X_new.salary.map('low':0,'medium':1,'high':2)
X_new.salary
prediction_will_left = clftree.predict(X_new)
print("Prediction: ".format(prediction_will_left))
print("Predicted target name: ".format(
    notleftdf['left'][prediction_will_left]
))

我得到的错误是:

KeyError: "None of [Int64Index([0, 0, 0, 0, 0, 0, 0, 0, 0, 0,\n            ...\n            0, 0, 0, 0, 0, 0, 1, 0, 0, 0],\n           dtype='int64', length=11428)] are in the [index]"

如何解决?

PS:完整的脚本链接是here

【问题讨论】:

目前尚不清楚您到底想做什么。错误很明显,没有找到 index 的值。但请提供具体细节作为问题的一部分,以便获得快速帮助。 @SupratimHaldar 很抱歉不清楚,我尝试使用该模型对留下但出错的 DataFrame 人进行预测。目标是根据留下的 DataFrame 预测未来将离开公司的人。 请提供Minimal and Reproducible example(即这里的人们可以重现您遇到的错误的最少代码量)。 @Xukrao 已经编辑过先生。 【参考方案1】:

也许您正在寻找类似的东西。 (将the data file 下载到同一目录后的自包含脚本。)

from sklearn import tree
from sklearn.model_selection import train_test_split
import numpy as np
import pandas as pd


def process_df_for_ml(df):
    """
    Process a dataframe for model training/prediction use.

    Returns X/y tensors.
    """

    df = df.copy()
    # Map salary to 0,1,2
    df.salary = df.salary.map("low": 0, "medium": 1, "high": 2)
    # dropping left and sales X for the df, y for the left
    X = 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])

【讨论】:

谢谢!非常感谢。但是,为什么你更喜欢做函数def,而不是直接写呢? 因为你做了两次同样的事情——首先是训练 DF,然后是非左 DF。当您尝试将这样的模型投入生产时,这会派上用场。 能解释一下df = df.copy()的含义吗? @JulioNobre 它获取数据帧的副本并将其分配给相同的名称。我们这样做是因为我们不想改变传入的对象。 嗨@AKX。您是说通过深度克隆覆盖 df 是故意的,以保留 df 名称(我猜,以防止冗长),同时保护原始调用数据帧数据不会被剩余的内部函数操作更改。这是有道理的,尽管在我看来,df_clone = df.copy() 是一个更清晰的选择,特别是对于初学者(比如我)。无论如何,谢谢!

以上是关于根据决策树算法生成的模型进行预测的主要内容,如果未能解决你的问题,请参考以下文章

分类算法 - 随机森林

决策树与随机森林算法

鸢尾花决策树算法选题的目的和意义

「数据挖掘入门系列」数据挖掘模型之分类与预测 - 决策树

sss

决策树算法的介绍