使用决策树算法生成的模型进行预测

我一直在尝试使用决策树算法创建的模型,从一个DataFrame中进行预测。

我已经得到了模型的分数,为0.96。接着,我尝试使用模型对留下来的人的DataFrame进行预测,但遇到了错误。目标是根据留下来的人的DataFrame预测未来会离开公司的人。

如何实现这个目标?

所以我所做的步骤如下:

  1. 从我的GitHub读取DataFrame,并将其分为已离开和未离开的人
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]
  1. 准备数据以生成模型
df.salary = df.salary.map({'low':0,'medium':1,'high':2})df.salary
X = df.drop(['left','sales'],axis=1)y = df['left']
  1. 划分训练集和测试集
import numpy as npfrom sklearn.model_selection import train_test_split#划分训练集和测试集X_train, X_test, y_train, y_test= train_test_split(X,y,random_state=0, stratify=y)
  1. 训练模型
from sklearn import treeclftree = tree.DecisionTreeClassifier(max_depth=3)clftree.fit(X_train,y_train)
  1. 评估模型
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)))

结果是

Test set score: 0.96

  1. 然后我尝试使用未离开公司的人的DataFrame进行预测
X_new = notleftdf.drop(['left','sales'],axis=1)#将薪水映射到0,1,2X_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: 完整脚本的链接在这里


回答:

也许你正在寻找这样的东西。(一旦你将数据文件下载到同一目录下,这是一个自包含的脚本。)

from sklearn import treefrom sklearn.model_selection import train_test_splitimport numpy as npimport pandas as pddef process_df_for_ml(df):    """    处理DataFrame以供模型训练/预测使用。    返回X/y张量。    """    df = df.copy()    # 将薪水映射到0,1,2    df.salary = df.salary.map({"low": 0, "medium": 1, "high": 2})    # 丢弃left和sales列,X为DataFrame,y为left    X = df.drop(["left", "sales"], axis=1)    y = df["left"]    return (X, y)# 读取并重新索引CSV文件。df = pd.read_csv("HR_comma_sep.csv")df = df.reindex()# 训练决策树。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)# 在尚未离开的人身上测试决策树。notleftdf = df[df["left"] == 0].copy()X, y = process_df_for_ml(notleftdf)# 添加一个新列,包含预测的零和一。notleftdf["will_leave"] = clftree.predict(X)# 打印出标记为将要离开的人。print(notleftdf[notleftdf["will_leave"] == 1])

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注