Feature Correlation¶
This visualizer calculates Pearson correlation coefficients and mutual information between features and the dependent variable. This visualization can be used in feature selection to identify features with high correlation or large mutual information with the dependent variable.
Pearson Correlation¶
The default calculation is Pearson correlation, which is perform with scipy.stats.pearsonr
.
from sklearn import datasets
from yellowbrick.target import FeatureCorrelation
# Load the regression data set
data = datasets.load_diabetes()
X, y = data['data'], data['target']
feature_names = np.array(data['feature_names'])
visualizer = FeatureCorrelation(labels=feature_names)
visualizer.fit(X, y)
visualizer.poof()
Mutual Information  Regression¶
Mutual information between features and the dependent variable is calculated with sklearn.feature_selection.mutual_info_classif
when method='mutual_infoclassification'
and mutual_info_regression
when method='mutual_inforegression'
.
It is very important to specify discrete features when calculating mutual information because the calculation for continuous and discrete variables are different.
See scikitlearn documentation for more details.
from sklearn import datasets
from yellowbrick.target import FeatureCorrelation
# Load the regression data set
data = datasets.load_diabetes()
X, y = data['data'], data['target']
feature_names = np.array(data['feature_names'])
discrete_features = [False for _ in range(len(feature_names))]
discrete_features[1] = True
visualizer = FeatureCorrelation(method='mutual_inforegression',
labels=feature_names)
visualizer.fit(X, y, discrete_features=discrete_features, random_state=0)
visualizer.poof()
Mutual Information  Classification¶
By fitting with a pandas DataFrame, the feature labels are automatically obtained from the column names. This visualizer also allows sorting of the bar plot according to the calculated mutual information (or Pearson correlation coefficients) and selecting features to plot by specifying the names of the features or the feature index.
from sklearn import datasets
from yellowbrick.target import FeatureCorrelation
# Load the regression data set
data = datasets.load_diabetes()
X, y = data['data'], data['target']
feature_names = np.array(data['feature_names'])
X_pd = pd.DataFrame(X, columns=feature_names)
feature_to_plot = ['alcohol', 'ash', 'hue', 'proline', 'total_phenols']
visualizer = FeatureCorrelation(method='mutual_infoclassification',
feature_names=feature_to_plot, sort=True)
visualizer.fit(X_pd, y, random_state=0)
visualizer.poof()
API Reference¶
Feature Correlation to Dependent Variable Visualizer.

class
yellowbrick.target.feature_correlation.
FeatureCorrelation
(ax=None, method='pearson', labels=None, sort=False, feature_index=None, feature_names=None, **kwargs)[source]¶ Bases:
yellowbrick.target.base.TargetVisualizer
Displays the correlation between features and dependent variables.
This visualizer can be used sidebyside with
yellowbrick.features.JointPlotVisualizer
that plots a feature against the target and shows the distribution of each via a histogram on each axis.Parameters:  ax : matplotlib Axes, default: None
The axis to plot the figure on. If None is passed in the current axes will be used (or generated if required).
 method : str, default: ‘pearson’
The method to calculate correlation between features and target. Options include:
 ‘pearson’, which uses
scipy.stats.pearsonr
 ‘mutual_inforegression’, which uses
mutual_inforegression
fromsklearn.feature_selection
 ‘mutual_infoclassification’, which uses
mutual_info_classif
fromsklearn.feature_selection
 ‘pearson’, which uses
 labels : list, default: None
A list of feature names to use. If a DataFrame is passed to fit and features is None, feature names are selected as the column names.
 sort : boolean, default: False
If false, the features are are not sorted in the plot; otherwise features are sorted in ascending order of correlation.
 feature_index : list,
A list of feature index to include in the plot.
 feature_names : list of feature names
A list of feature names to include in the plot. Must have labels or the fitted data is a DataFrame with column names. If feature_index is provided, feature_names will be ignored.
 kwargs : dict
Keyword arguments that are passed to the base class and may influence the visualization as defined in other Visualizers.
Examples
>>> viz = FeatureCorrelation() >>> viz.fit(X, y) >>> viz.poof()
Attributes:  features_ : np.array
The feature labels
 scores_ : np.array
Correlation between features and dependent variable.

fit
(X, y, **kwargs)[source]¶ Fits the estimator to calculate feature correlation to dependent variable.
Parameters:  X : ndarray or DataFrame of shape n x m
A matrix of n instances with m features
 y : ndarray or Series of length n
An array or series of target or class values
 kwargs : dict
Keyword arguments passed to the fit method of the estimator.
Returns:  self : visualizer
The fit method must always return self to support pipelines.