3
$\begingroup$

I am working on a binary classification problem and am using sklearn's logistic regression model and decision tree classifier.

Somehow I am getting the exact same results and accuracy score on both.

I don't understand how they can output exactly the same result with testing data and have the exact same accuracy score (even after running them multiple times)

Is this something that can happen in machine learning if the dataset is too simple? ( 5 ordinal scale features predicting 1 binary target variable)

EDIT: Here is the code I used:

X = moped[features].values
y = owned_numerical.values

X_train, X_test, y_train, y_test = train_test_split(X, y, 
test_size=0.30, stratify=y)

# LOGISTIC REGRESSION MODEL
logit_model = LogisticRegression(random_state=42)
logit_model.fit(X_train, y_train)
logit_preds = logit_model.predict(X_test)
logit_preds
logit_accuracy = accuracy_score(y_test, logit_preds)
print(f"Logistic Regression Accuracy Score: {logit_accuracy}")

array([1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
1, 1,
   1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
   1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
   1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0,
   1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
   1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1,
   1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
   1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1,
   1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
   1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], dtype=uint8)
Logistic Regression Accuracy Score: 0.8130841121495327

#DECISION TREE MODEL
dt_model = DecisionTreeClassifier(random_state=42)
dt_model.fit(X_train, y_train)
dt_preds = logit_model.predict(X_test)
dt_preds
dt_accuracy = accuracy_score(y_test, dt_preds)
print(f"Decision Tree Accuracy Score: {dt_accuracy}")

array([1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
   1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
   1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
   1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0,
   1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
   1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1,
   1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
   1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1,
   1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
   1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], dtype=uint8)
Decision Tree Accuracy Score: 0.8130841121495327

print(all(dt_preds == logit_preds))
True
$\endgroup$

1 Answer 1

4
$\begingroup$
  1. Running multiple times should indeed have no effect (if you used a random seed). Although sklearn documentation for logistic regression mentions that it is possible to have some variation due to randomization not controlled by a seed.

  2. It has more to do with how many test samples you have than the "simplicity" (the amount of features). If you have 20 test samples then the possibility of the two algorithms predicting the same things is not negligible. If you have a million test samples, and both algorithms predict the exact same results, it is much more probable that you made a mistake along the process than for the algorithms to make the same prediction for each of the test samples.

  3. If your classes are heavily imbalanced (like, 95:05), it is possible that both algorithms made the naive assumption that everything belongs to the big class (this is the extreme case, but heavy bias towards the majority class could explain this in a degree). If this is the case, try and optimize your problem using a metric better suited for this, such as F1 score.

Note that I assumed that "same results" means "both algorithms made the same prediction for each test sample". The same accuracy score follows from this.

$\endgroup$
5
  • $\begingroup$ Thanks! the data is only about 700 observations so i guess this is the most probable scenario as you mentioned in 2). I used stratification to offset the class imbalance with the stratify param in train_test_split which by the way is more like 80:20. And yes by "same results" I meant both models made the same predictions for each test sample. I will add the code to the question but its all boiler plate as far as i can tell $\endgroup$
    – RandomGuy
    Commented Nov 19, 2022 at 11:23
  • 1
    $\begingroup$ Your accuracy is very close to 80% (the percentage of classes), and while not everything is classified in class 1, this (bullet 3) might also be an issue. This method of stratification simply ensures that the test set will have enough data from both classes, it does not prevent any bias from the training. $\endgroup$
    – liakoyras
    Commented Nov 19, 2022 at 11:47
  • 2
    $\begingroup$ Also, what do these 6 data points that are predicted to be in class 0 have in common? Are they actually in the correct class? Maybe your model is actually not very good and can predict only the extreme examples of class 0, while the rest gets lost due to the bias towards class 1 $\endgroup$
    – liakoyras
    Commented Nov 19, 2022 at 11:50
  • 1
    $\begingroup$ Yeah so it was a stupid mistake on my end.. i used logit_model to predict in both cases as you can probably now tell in the code above :)) $\endgroup$
    – RandomGuy
    Commented Nov 21, 2022 at 5:33
  • $\begingroup$ @RandomGuy lol, totally missed it too $\endgroup$
    – liakoyras
    Commented Nov 21, 2022 at 9:35

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.