2012-03-07 10:55:18 +08:00
|
|
|
#!/usr/bin/python
|
|
|
|
|
# -*- 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-02-01 22:04:03 +08:00
|
|
|
print(__doc__)
|
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
|
|
|
from mpl_toolkits.mplot3d import Axes3D
|
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
|
|
centers = [[1, 1], [-1, -1], [1, -1]]
|
|
|
|
|
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()
|
2011-12-18 19:39:53 +08:00
|
|
|
ax = Axes3D(fig, rect=[0, 0, .95, 1], elev=48, azim=134)
|
|
|
|
|
|
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=.5, edgecolor='w', facecolor='w'))
|
2011-12-18 19:39:53 +08:00
|
|
|
# Reorder the labels to have colors matching the cluster results
|
|
|
|
|
y = np.choose(y, [1, 2, 0]).astype(np.float)
|
2017-03-10 01:54:38 +08:00
|
|
|
ax.scatter(X[:, 0], X[:, 1], X[:, 2], c=y, cmap=plt.cm.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()
|