In [1]:
%load_ext autoreload
%autoreload 2

%matplotlib inline

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
In [2]:
plt.rcParams['figure.figsize'] = [14, 9]
In [20]:
from sklearn.linear_model import LogisticRegression
from sklearn.datasets import load_iris
In [24]:
data = load_iris()
X, y = data.data, data.target
names = data.feature_names
est = LogisticRegression(solver='lbfgs', multi_class='auto')
est.fit(X, y)
/Users/thomasfan/anaconda3/lib/python3.7/site-packages/sklearn/linear_model/logistic.py:947: ConvergenceWarning: lbfgs failed to converge. Increase the number of iterations.
  "of iterations.", ConvergenceWarning)
Out[24]:
LogisticRegression(C=1.0, class_weight=None, dual=False, fit_intercept=True,
                   intercept_scaling=1, l1_ratio=None, max_iter=100,
                   multi_class='auto', n_jobs=None, penalty='l2',
                   random_state=None, solver='lbfgs', tol=0.0001, verbose=0,
                   warm_start=False)
In [30]:
from sklearn.ensemble import RandomForestClassifier
In [33]:
dtree = RandomForestClassifier(n_estimators=100)
dtree.fit(X, y)
Out[33]:
RandomForestClassifier(bootstrap=True, class_weight=None, criterion='gini',
                       max_depth=None, max_features='auto', max_leaf_nodes=None,
                       min_impurity_decrease=0.0, min_impurity_split=None,
                       min_samples_leaf=1, min_samples_split=2,
                       min_weight_fraction_leaf=0.0, n_estimators=100,
                       n_jobs=None, oob_score=False, random_state=None,
                       verbose=0, warm_start=False)

First Plot

In [34]:
from sklearn_plot_api import plot_partial_dependence
features = [0, 1, 2, 3]
viz = plot_partial_dependence(est, X, features, feature_names=[names[i] for i in features])
In [35]:
from sklearn_plot_api import plot_partial_dependence
features = [0, 1, 2, 3]
viz = plot_partial_dependence(dtree, X, features, feature_names=[names[i] for i in features])

Adjust color of sepal width

In [37]:
viz.artists_['sepal width (cm)'].set_color('green')
viz.figure_
Out[37]:

Add y label to petal length

In [38]:
viz.axes_['petal length (cm)'].set_ylabel('Parital dependence')
viz.figure_
Out[38]:

Passing bounds through axes

In [41]:
from sklearn_plot_api import plot_partial_dependence
fig, (ax1, ax2) = plt.subplots(1, 2)
features = [0, 1, 2, 3]
viz1 = plot_partial_dependence(est, X, features, feature_names=[names[i] for i in features], ax=ax1)
viz2 = plot_partial_dependence(dtree, X, features, feature_names=[names[i] for i in features], ax=ax2)