3
$\begingroup$

I have been made a model using both Decision Tree and Random Forest. But, when I tried to test the model on the same DataFrame the output is different. How is this possible?

The data file from my repo:

#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)

I used the decision tree:

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)

With test_score: 0.96. Afterwards, I test this DecisionTree to the same df and the output I got is [424 rows x 11 columns]

and then I tried to use Random Forest algorithm

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)

With test_score: 0.99. Afterwards, I test this RandomForest to the same df and the output I got is [11 rows x 11 columns].

How is this possible? Here is the link to my works: DecisionTree and RandomForest

$\endgroup$
  • 2
    $\begingroup$ Even though the ideas in both models are similar, at the end of the day they are different models, no reason for them to give same the same result. $\endgroup$ – serali Nov 2 '19 at 10:18
  • $\begingroup$ @serali which models are usually superior, is it models from decision tree or random forest? And in this case which models should I believe? $\endgroup$ – ebuzz168 Nov 2 '19 at 10:42
  • 2
    $\begingroup$ I don't think it is possible to say which is superior without experimentation. Here is another post discussing pros and cons of each model in detail: stats.stackexchange.com/questions/285834/… $\endgroup$ – serali Nov 2 '19 at 10:45
  • $\begingroup$ @serali so basically, my works are already right but the different given results need more study to answer the why it's different? $\endgroup$ – ebuzz168 Nov 2 '19 at 12:04
  • $\begingroup$ I don't understand what you mean when you say "the output I got is <shape>"; what output? $\endgroup$ – Ben Reiniger Nov 5 '19 at 20:19
0
$\begingroup$

Your df row count is 14,999

Your test data is 33% ~ 4950

So, your y_predict should be (4950,1) i.e. a binary prediction for all test rows.

This is what I am getting when running your code which you have posted here.

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)
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.