2012-03-07 10:55:18 +08:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
"""
|
|
|
|
|
=========================================================
|
|
|
|
|
PCA example with Iris Data-set
|
|
|
|
|
=========================================================
|
|
|
|
|
|
2013-06-06 18:30:42 +08:00
|
|
|
Principal Component Analysis applied to the Iris dataset.
|
|
|
|
|
|
2015-12-03 07:16:40 +08:00
|
|
|
See `here <https://en.wikipedia.org/wiki/Iris_flower_data_set>`_ for more
|
2013-06-06 18:30:42 +08:00
|
|
|
information on this dataset.
|
|
|
|
|
|
2012-03-07 10:55:18 +08:00
|
|
|
"""
|
|
|
|
|
|
2013-07-30 18:41:56 +08:00
|
|
|
# Code source: Gaël Varoquaux
|
2013-04-30 14:23:46 +08:00
|
|
|
# License: BSD 3 clause
|
2012-03-07 10:55:18 +08:00
|
|
|
|
2011-12-18 19:39:53 +08:00
|
|
|
import numpy as np
|
2014-05-15 04:31:03 +08:00
|
|
|
import matplotlib.pyplot as plt
|
2011-12-18 19:39:53 +08:00
|
|
|
|
|
|
|
|
|
2012-03-07 10:55:18 +08:00
|
|
|
from sklearn import decomposition
|
|
|
|
|
from sklearn import datasets
|
2011-12-18 19:39:53 +08:00
|
|
|
|
|
|
|
|
np.random.seed(5)
|
|
|
|
|
|
|
|
|
|
iris = datasets.load_iris()
|
|
|
|
|
X = iris.data
|
|
|
|
|
y = iris.target
|
|
|
|
|
|
2014-05-15 04:31:03 +08:00
|
|
|
fig = plt.figure(1, figsize=(4, 3))
|
|
|
|
|
plt.clf()
|
2022-02-27 21:34:49 +08:00
|
|
|
|
|
|
|
|
ax = fig.add_subplot(111, projection="3d", elev=48, azim=134)
|
|
|
|
|
ax.set_position([0, 0, 0.95, 1])
|
|
|
|
|
|
2011-12-18 19:39:53 +08:00
|
|
|
|
2014-05-15 04:31:03 +08:00
|
|
|
plt.cla()
|
2011-12-18 19:39:53 +08:00
|
|
|
pca = decomposition.PCA(n_components=3)
|
|
|
|
|
pca.fit(X)
|
|
|
|
|
X = pca.transform(X)
|
|
|
|
|
|
2012-04-28 18:04:36 +08:00
|
|
|
for name, label in [("Setosa", 0), ("Versicolour", 1), ("Virginica", 2)]:
|
|
|
|
|
ax.text3D(
|
|
|
|
|
X[y == label, 0].mean(),
|
|
|
|
|
X[y == label, 1].mean() + 1.5,
|
|
|
|
|
X[y == label, 2].mean(),
|
|
|
|
|
name,
|
2011-12-18 19:39:53 +08:00
|
|
|
horizontalalignment="center",
|
2012-12-25 20:16:05 +08:00
|
|
|
bbox=dict(alpha=0.5, edgecolor="w", facecolor="w"),
|
|
|
|
|
)
|
2011-12-18 19:39:53 +08:00
|
|
|
# Reorder the labels to have colors matching the cluster results
|
2020-06-24 22:51:51 +08:00
|
|
|
y = np.choose(y, [1, 2, 0]).astype(float)
|
2018-03-07 13:49:45 +08:00
|
|
|
ax.scatter(X[:, 0], X[:, 1], X[:, 2], c=y, cmap=plt.cm.nipy_spectral, edgecolor="k")
|
2011-12-18 19:39:53 +08:00
|
|
|
|
2012-03-02 00:16:41 +08:00
|
|
|
ax.w_xaxis.set_ticklabels([])
|
|
|
|
|
ax.w_yaxis.set_ticklabels([])
|
|
|
|
|
ax.w_zaxis.set_ticklabels([])
|
|
|
|
|
|
2014-05-15 04:31:03 +08:00
|
|
|
plt.show()
|