scikit-learn/examples/feature_selection/plot_f_test_vs_mi.py

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

49 lines
1.6 KiB
Python
Raw Normal View History

2016-01-11 01:01:36 +08:00
"""
===========================================
Comparison of F-test and mutual information
===========================================
This example illustrates the differences between univariate F-test statistics
and mutual information.
We consider 3 features x_1, x_2, x_3 distributed uniformly over [0, 1], the
target depends on them as follows:
[MRG + 1] 18 more examples with matplotlib 2.0 updates (#8983) * updated plot_label_propagation_versus_svm_iris.py plot * updated svm/plot_weighted_samples.py plot * made semi_supervised/plot_label_propagation_versus_svm_iris.py pep8 compliant * modified tree/plot_tree_regression.py [size and edgecolor] * updated tree/plot_tree_regression_multioutput.py [size+color] * fixed examples/semi_supervised/plot_label_propagation_versus_svm_iris.py for backward compatibility * neural_networks/plot_mlp_alpha.py - matplotlib2 update * examples/neural_networks/plot_mlp_alpha.py - pep8 fix * examples/neighbors/plot_nearest_centroid.py - matplotlib2.0 + pep8 fix * neighbors/plot_classification.py - matplotlib2.0 + pep8 fix * examples/neighbors/plot_lof.py - matplotlib2.0 update * examples/model_selection/plot_underfitting_overfitting.py - matplotlib2.0 + pep8 * examples/mixture/plot_concentration_prior.py - matplotlib2.0 + pep8 * examples/linear_model/plot_logistic_multinomial.py - matplotlib2.0 update * linear_model/plot_sgd_iris.py - matplotlib2.0 + pep8 fix * examples/linear_model/plot_sgd_weighted_samples.py - matplotlib2.0 + pep8 * examples/linear_model/plot_sgd_separating_hyperplane.py - matplotlib2.0 update * examples/feature_selection/plot_permutation_test_for_classification.py - matplotlib + pe8 * examples/linear_model/plot_bayesian_ridge.py - matplotlib2.0 update * examples/feature_selection/plot_feature_selection.py - matplotlib2.0 update * examples/feature_selection/plot_f_test_vs_mi.py - matplotlib2.0 + pep8 * examples/feature_selection/plot_f_test_vs_mi.py - matplotlib2.0+ pep8 fix * examples/model_selection/plot_underfitting_overfitting.py - error fixed * blue -> black edgecolor fix for 2 examples
2017-06-07 19:23:12 +08:00
y = x_1 + sin(6 * pi * x_2) + 0.1 * N(0, 1), that is the third features is
completely irrelevant.
2016-01-11 01:01:36 +08:00
The code below plots the dependency of y against individual x_i and normalized
values of univariate F-tests statistics and mutual information.
As F-test captures only linear dependency, it rates x_1 as the most
discriminative feature. On the other hand, mutual information can capture any
kind of dependency between variables and it rates x_2 as the most
discriminative feature, which probably agrees better with our intuitive
perception for this example. Both methods correctly marks x_3 as irrelevant.
2016-01-11 01:01:36 +08:00
"""
import numpy as np
import matplotlib.pyplot as plt
from sklearn.feature_selection import f_regression, mutual_info_regression
2016-01-11 01:01:36 +08:00
np.random.seed(0)
2016-01-11 01:01:36 +08:00
X = np.random.rand(1000, 3)
y = X[:, 0] + np.sin(6 * np.pi * X[:, 1]) + 0.1 * np.random.randn(1000)
f_test, _ = f_regression(X, y)
f_test /= np.max(f_test)
mi = mutual_info_regression(X, y)
2016-01-11 01:01:36 +08:00
mi /= np.max(mi)
plt.figure(figsize=(15, 5))
for i in range(3):
plt.subplot(1, 3, i + 1)
[MRG + 1] 18 more examples with matplotlib 2.0 updates (#8983) * updated plot_label_propagation_versus_svm_iris.py plot * updated svm/plot_weighted_samples.py plot * made semi_supervised/plot_label_propagation_versus_svm_iris.py pep8 compliant * modified tree/plot_tree_regression.py [size and edgecolor] * updated tree/plot_tree_regression_multioutput.py [size+color] * fixed examples/semi_supervised/plot_label_propagation_versus_svm_iris.py for backward compatibility * neural_networks/plot_mlp_alpha.py - matplotlib2 update * examples/neural_networks/plot_mlp_alpha.py - pep8 fix * examples/neighbors/plot_nearest_centroid.py - matplotlib2.0 + pep8 fix * neighbors/plot_classification.py - matplotlib2.0 + pep8 fix * examples/neighbors/plot_lof.py - matplotlib2.0 update * examples/model_selection/plot_underfitting_overfitting.py - matplotlib2.0 + pep8 * examples/mixture/plot_concentration_prior.py - matplotlib2.0 + pep8 * examples/linear_model/plot_logistic_multinomial.py - matplotlib2.0 update * linear_model/plot_sgd_iris.py - matplotlib2.0 + pep8 fix * examples/linear_model/plot_sgd_weighted_samples.py - matplotlib2.0 + pep8 * examples/linear_model/plot_sgd_separating_hyperplane.py - matplotlib2.0 update * examples/feature_selection/plot_permutation_test_for_classification.py - matplotlib + pe8 * examples/linear_model/plot_bayesian_ridge.py - matplotlib2.0 update * examples/feature_selection/plot_feature_selection.py - matplotlib2.0 update * examples/feature_selection/plot_f_test_vs_mi.py - matplotlib2.0 + pep8 * examples/feature_selection/plot_f_test_vs_mi.py - matplotlib2.0+ pep8 fix * examples/model_selection/plot_underfitting_overfitting.py - error fixed * blue -> black edgecolor fix for 2 examples
2017-06-07 19:23:12 +08:00
plt.scatter(X[:, i], y, edgecolor="black", s=20)
2016-01-11 01:01:36 +08:00
plt.xlabel("$x_{}$".format(i + 1), fontsize=14)
if i == 0:
plt.ylabel("$y$", fontsize=14)
plt.title("F-test={:.2f}, MI={:.2f}".format(f_test[i], mi[i]), fontsize=16)
plt.show()