2012-02-18 00:58:46 +08:00
|
|
|
#!/usr/bin/python
|
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
=========================================================
|
|
|
|
|
The Iris Dataset
|
|
|
|
|
=========================================================
|
|
|
|
|
This data sets consists of 3 different types of irises'
|
|
|
|
|
(Setosa, Versicolour, and Virginica) petal and sepal
|
|
|
|
|
length, stored in a 150x4 numpy.ndarray
|
|
|
|
|
|
|
|
|
|
The rows being the samples and the columns being:
|
|
|
|
|
Sepal Length, Sepal Width, Petal Length and Petal Width.
|
|
|
|
|
|
|
|
|
|
The below plot uses the first two features.
|
2015-12-03 07:16:40 +08:00
|
|
|
See `here <https://en.wikipedia.org/wiki/Iris_flower_data_set>`_ for more
|
2012-04-28 18:04:36 +08:00
|
|
|
information on this dataset.
|
2012-02-18 00:58:46 +08:00
|
|
|
"""
|
2013-02-01 22:04:03 +08:00
|
|
|
print(__doc__)
|
2012-02-18 00:58:46 +08:00
|
|
|
|
|
|
|
|
|
2013-07-30 18:41:56 +08:00
|
|
|
# Code source: Gaël Varoquaux
|
|
|
|
|
# Modified for documentation by Jaques Grobler
|
2013-04-30 14:23:46 +08:00
|
|
|
# License: BSD 3 clause
|
2012-02-18 00:58:46 +08:00
|
|
|
|
2014-05-15 04:31:03 +08:00
|
|
|
import matplotlib.pyplot as plt
|
2012-09-14 01:37:04 +08:00
|
|
|
from mpl_toolkits.mplot3d import Axes3D
|
2012-04-28 18:04:36 +08:00
|
|
|
from sklearn import datasets
|
2012-09-14 01:37:04 +08:00
|
|
|
from sklearn.decomposition import PCA
|
2012-02-18 00:58:46 +08:00
|
|
|
|
|
|
|
|
# import some data to play with
|
|
|
|
|
iris = datasets.load_iris()
|
2012-04-28 18:04:36 +08:00
|
|
|
X = iris.data[:, :2] # we only take the first two features.
|
2016-11-18 07:24:36 +08:00
|
|
|
y = iris.target
|
2012-02-18 00:58:46 +08:00
|
|
|
|
2012-04-28 18:04:36 +08:00
|
|
|
x_min, x_max = X[:, 0].min() - .5, X[:, 0].max() + .5
|
|
|
|
|
y_min, y_max = X[:, 1].min() - .5, X[:, 1].max() + .5
|
2012-02-18 00:58:46 +08:00
|
|
|
|
2014-05-15 04:31:03 +08:00
|
|
|
plt.figure(2, figsize=(8, 6))
|
|
|
|
|
plt.clf()
|
2012-02-18 00:58:46 +08:00
|
|
|
|
2012-09-14 01:37:04 +08:00
|
|
|
# Plot the training points
|
2017-06-28 20:57:45 +08:00
|
|
|
plt.scatter(X[:, 0], X[:, 1], c=y, cmap=plt.cm.Set1,
|
2017-06-28 20:56:27 +08:00
|
|
|
edgecolor='k')
|
2014-05-15 04:31:03 +08:00
|
|
|
plt.xlabel('Sepal length')
|
|
|
|
|
plt.ylabel('Sepal width')
|
2012-02-18 00:58:46 +08:00
|
|
|
|
2014-05-15 04:31:03 +08:00
|
|
|
plt.xlim(x_min, x_max)
|
|
|
|
|
plt.ylim(y_min, y_max)
|
|
|
|
|
plt.xticks(())
|
|
|
|
|
plt.yticks(())
|
2012-02-18 00:58:46 +08:00
|
|
|
|
2012-09-14 01:37:04 +08:00
|
|
|
# To getter a better understanding of interaction of the dimensions
|
|
|
|
|
# plot the first three PCA dimensions
|
2014-05-15 04:31:03 +08:00
|
|
|
fig = plt.figure(1, figsize=(8, 6))
|
2012-09-15 23:56:46 +08:00
|
|
|
ax = Axes3D(fig, elev=-150, azim=110)
|
2012-09-14 01:37:04 +08:00
|
|
|
X_reduced = PCA(n_components=3).fit_transform(iris.data)
|
2016-11-18 07:24:36 +08:00
|
|
|
ax.scatter(X_reduced[:, 0], X_reduced[:, 1], X_reduced[:, 2], c=y,
|
2017-06-28 20:57:45 +08:00
|
|
|
cmap=plt.cm.Set1, edgecolor='k', s=40)
|
2012-09-15 23:56:46 +08:00
|
|
|
ax.set_title("First three PCA directions")
|
|
|
|
|
ax.set_xlabel("1st eigenvector")
|
2013-02-18 22:30:51 +08:00
|
|
|
ax.w_xaxis.set_ticklabels([])
|
2012-09-15 23:56:46 +08:00
|
|
|
ax.set_ylabel("2nd eigenvector")
|
2013-02-18 22:30:51 +08:00
|
|
|
ax.w_yaxis.set_ticklabels([])
|
2012-09-15 23:56:46 +08:00
|
|
|
ax.set_zlabel("3rd eigenvector")
|
2013-02-18 22:30:51 +08:00
|
|
|
ax.w_zaxis.set_ticklabels([])
|
2012-09-14 01:37:04 +08:00
|
|
|
|
2014-05-15 04:31:03 +08:00
|
|
|
plt.show()
|