scikit-learn/examples/neighbors/plot_classification.py

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

63 lines
1.6 KiB
Python
Raw Normal View History

"""
================================
Nearest Neighbors Classification
================================
Sample usage of Nearest Neighbors classification.
It will plot the decision boundaries for each class.
"""
import matplotlib.pyplot as plt
import seaborn as sns
from matplotlib.colors import ListedColormap
from sklearn import neighbors, datasets
from sklearn.inspection import DecisionBoundaryDisplay
n_neighbors = 15
# import some data to play with
iris = datasets.load_iris()
[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
# we only take the first two features. We could avoid this ugly
# slicing by using a two-dim dataset
X = iris.data[:, :2]
y = iris.target
# Create color maps
cmap_light = ListedColormap(["orange", "cyan", "cornflowerblue"])
cmap_bold = ["darkorange", "c", "darkblue"]
for weights in ["uniform", "distance"]:
# we create an instance of Neighbours Classifier and fit the data.
clf = neighbors.KNeighborsClassifier(n_neighbors, weights=weights)
clf.fit(X, y)
_, ax = plt.subplots()
DecisionBoundaryDisplay.from_estimator(
clf,
X,
cmap=cmap_light,
ax=ax,
response_method="predict",
plot_method="pcolormesh",
xlabel=iris.feature_names[0],
ylabel=iris.feature_names[1],
shading="auto",
)
2011-09-16 17:29:26 +08:00
# Plot also the training points
sns.scatterplot(
x=X[:, 0],
y=X[:, 1],
hue=iris.target_names[y],
palette=cmap_bold,
alpha=1.0,
edgecolor="black",
)
plt.title(
"3-Class classification (k = %i, weights = '%s')" % (n_neighbors, weights)
)
2011-09-16 17:29:26 +08:00
plt.show()