rasbt / rasbt/mlxtend

Is it possible to use `plot_decision_regions` function with model fit on a Pandas DataFrame?

Open
#335 14 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
5.2k
Forks
916
Avg merge
17h 31m
Merged PRs (30d)
4

Description

question originally posted at: https://stackoverflow.com/q/49167961/1870832
reproduced below...

With plot_decision_regions, X and y have to be numpy arrays, and clf needs to have a predict() method. My problem is that in my case, the model clf object I would like to visualize has already been fitted on a Pandas DataFrame...

import numpy as np
import pandas as pd
import xgboost as xgb

import matplotlib
matplotlib.use('Agg')
from mlxtend.plotting import plot_decision_regions
import matplotlib.pyplot as plt


# Create arbitrary dataset for example
df = pd.DataFrame({'Planned_End': np.random.uniform(low=-5, high=5, size=50),
                   'Actual_End':  np.random.uniform(low=-1, high=1, size=50),
                   'Late':        np.random.random_integers(low=0,  high=2, size=50)}
)

# Fit a Classifier to the data
# This classifier is fit on the data as a Pandas DataFrame
X = df[['Planned_End', 'Actual_End']]
y = df['Late']

clf = xgb.XGBClassifier()
clf.fit(X, y)

So now when I try to use plot_decision_regions passing X/y as numpy arrays...

# Plot Decision Region using mlxtend's awesome plotting function
plot_decision_regions(X=X.values,
                      y=y.values,
                      clf=clf,
                      legend=2)

I (understandably) get an error that the model can't find the column names of the dataset it was trained on

ValueError: feature_names mismatch: ['Planned_End', 'Actual_End'] ['f0', 'f1']
expected Planned_End, Actual_End in input data
training data did not have the following fields: f1, f0

In my actual case, it would be a big deal to avoid training our model on Pandas DataFrames. Is there a way to still produce decision_regions plots for a classifier trained on a Pandas DataFrame?

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at the plot_decision_regions entry point and reproduce the issue with the provided Pandas DataFrame, NumPy arrays, and XGBClassifier example. Trace how the plotting function passes features to clf.predict; done means a classifier trained on the DataFrame can produce the decision-regions plot without the feature-name mismatch.

Written by the indexing model from the issue text.

Assessment

Tech stack
numpy, pandas, python
Domain
data-visualization, machine-learning
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.