0
$\begingroup$

I am getting 100% accuracy on my test set when trained using random forest.

Is there something wrong with my model?

Code:

import pandas as pd 
import numpy as np 
from sklearn.model_selection import train_test_split 
from sklearn.neighbors import KNeighborsClassifier 
from sklearn.ensemble import RandomForestClassifier 
from sklearn.metrics import accuracy_score 
from sklearn import preprocessing 
from sklearn.metrics import classification_report, confusion_matrix 
from sklearn.preprocessing import OneHotEncoder 
from sklearn.compose import ColumnTransformer 
from sklearn.pipeline import Pipeline 
from sklearn.preprocessing import StandardScaler

ds = pd.read_csv('census-income.test(no unk.).csv')

df = pd.read_csv('census-income.data(no unk.).csv')

X = df 
y = df['income']

X_T = ds 
y_T = ds['income']

categorical_preprocessor = Pipeline(steps=[ ("onehot", OneHotEncoder(handle_unknown="ignore")) ])

preprocessor = ColumnTransformer([ ("categorical", categorical_preprocessor, ['workclass','education','martial-status','occupation','relationship','race','sex', 'native-country','income']), ],remainder='passthrough')

pipe = Pipeline(steps=[ ("preprocessor", preprocessor), ("classifier", RandomForestClassifier(n_estimators=128, max_depth=7)) ])

X_train = X 
X_test = X_T 
y_train = y 
y_test = y_T

pipe.fit(X_train, y_train) 
y_pred = pipe.predict(X_test)

print(classification_report(y_test, y_pred, digits=4)) 
print(confusion_matrix(y_test, y_pred)) 

Confusion matrix

[[11360     0]
 [    0  3700]]

Training data enter image description here

Test data enter image description here

$\endgroup$

1 Answer 1

4
$\begingroup$

I think you leak the answer when u defining this variable.

X_T = ds 
y_T = ds['income']

So when u predicting the test set the data perfectly predict 100% accuracy

y_pred = pipe.predict(X_test)

You can try it like this for train and test data:

X_T = ds.drop(['income'],axis=1)
y_T = ds['income']
$\endgroup$
2
  • 2
    $\begingroup$ True, we can also see that OP was not aware that he passed the target income column to ColumnTransformer too. $\endgroup$
    – lpounng
    Commented Dec 14, 2022 at 9:06
  • $\begingroup$ I modified it. Thanks! $\endgroup$
    – hre0
    Commented Dec 15, 2022 at 0:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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