决策树和随机森林模型的输出不同?

数据挖掘 机器学习 Python 随机森林 决策树
2022-02-06 12:25:15

我已经使用决策树和随机森林制作了一个模型。但是,当我尝试在同一个 DataFrame 上测试模型时,输出是不同的。这怎么可能?

我的仓库中的数据文件

#This is the function to help me preparing the dataframe
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)

我使用了决策树:

from sklearn import tree
from sklearn.model_selection import train_test_split
# 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_score:0.96。之后,我将这个决策树测试到相同的 df,我得到的输出是 [424 行 x 11 列]

然后我尝试使用随机森林算法

X, y = process_df_for_ml(df)

from sklearn.model_selection import train_test_split
# implementing train-test-split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33, random_state=0, stratify=y)

from sklearn.ensemble import RandomForestClassifier
# random forest model creation
rfc = RandomForestClassifier()
rfc.fit(X_train,y_train)
# predictions
rfc_predict = rfc.predict(X_test)

使用 test_score:0.99。之后,我将这个 RandomForest 测试到相同的 df,我得到的输出是 [11 行 x 11 列]。

这怎么可能?这是我的作品的链接: DecisionTreeRandomForest

2个回答

1. 数据准备

默认情况下,train_test_split如果您没有为test_sizeortrain_size参数声明其他内容,则假定您的 train:test 拆分为 75:25。见这里

对于您的决策树,您没有声明这一点,因此您测试了 25% 的数据,而您在随机森林算法的数据准备中明确声明训练大小应为 33%。

对此进行更正可确保在这种情况下公平比较两种算法。我假设您希望使用test_size=0.33.

2.模型差异

随机森林分类器是一种集成方法。顾名思义,此方法生成许多决策树分类器的“森林”,每个分类器都在训练数据中的不同样本子集和特征上进行训练。,n_estimators参数控制它max_samples见这里然后对每棵树的结果进行平均,目的是提供更稳健的估计器。max_features

为了让两个分类器产生相同的输出,我可以设置相同的random_statemax_depth同时允许在随机森林中使用所有特征(因为默认情况下,所有样本都在生成的每棵树中使用):

tree.DecisionTreeClassifier(random_state=0, max_depth=3)
ensemble.RandomForestClassifier(random_state=0, max_depth=3, max_features=None)

两者现在都实现了相同的测试精度 0.9551,因为两个分类器生成相同的相同树。

删除max_features=None允许随机森林分类器为随机森林中每棵树训练的每个子集随机选择特征:

ensemble.RandomForestClassifier(random_state=0, max_depth=3)

结果,随机森林分类器现在达到了 0.9121 的测试准确度。

希望这可以解释为什么您在决策树和随机森林分类器中得到不同的结果。

您的 df 行数为 14,999

你的测试数据是 33% ~ 4950

因此,您的 y_predict 应该是 (4950,1),即所有测试行的二进制预测。

这就是我在运行您在此处发布的代码时得到的结果。

import pandas as pd
df = pd.read_csv('https://raw.githubusercontent.com/bhaskoro-muthohar/DataScienceLearning/master/HR_comma_sep.csv')

print(df.size)
# 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"]

#splitting the train and test sets
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33, random_state=0, stratify=y)
# Train a decision tree.
from sklearn.tree import DecisionTreeClassifier
clftree = DecisionTreeClassifier(max_depth=3)
clftree.fit(X_train, y_train)
y_pred = clftree.predict(X_test) 
print(y_pred.size)
from sklearn.ensemble import RandomForestClassifier
# random forest model creation
rfc = RandomForestClassifier()
rfc.fit(X_train,y_train)
# predictions
rfc_predict = rfc.predict(X_test)
print(rfc_predict.size)