PCA with the iris data set¶

Preparing the Iris Dataset

For the following tutorial, we will be working with the famous "Iris" dataset that has been deposited on the UCI machine learning repository (https://archive.ics.uci.edu/ml/datasets/Iris).

The iris dataset contains measurements for 150 iris flowers from three different species.

The three classes in the Iris dataset are:

  • Iris-setosa (n=50)
  • Iris-versicolor (n=50)
  • Iris-virginica (n=50)

And the four features of in Iris dataset are:

  • sepal length in cm
  • sepal width in cm
  • petal length in cm
  • petal width in cm

Loading the Dataset¶

In order to load the Iris data directly from the UCI repository, we are going to use the superb pandas library.

In [5]:
import numpy as np # linear algebra
import pandas as pd
from sklearn.decomposition import PCA
from sklearn import datasets

import seaborn as sns
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
plt.style.use('seaborn-v0_8')
%matplotlib inline
---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
<ipython-input-5-f3a404031699> in <module>()
      4 from sklearn import datasets
      5 
----> 6 import seaborn as sns
      7 import matplotlib.pyplot as plt
      8 from mpl_toolkits.mplot3d import Axes3D

/home/simon/.local/lib/python3.6/site-packages/seaborn/__init__.py in <module>()
      1 # Import seaborn objects
----> 2 from .rcmod import *  # noqa: F401,F403
      3 from .utils import *  # noqa: F401,F403
      4 from .palettes import *  # noqa: F401,F403
      5 from .relational import *  # noqa: F401,F403

/home/simon/.local/lib/python3.6/site-packages/seaborn/rcmod.py in <module>()
      5 import matplotlib as mpl
      6 from cycler import cycler
----> 7 from . import palettes
      8 
      9 

/home/simon/.local/lib/python3.6/site-packages/seaborn/palettes.py in <module>()
      7 from .external import husl
      8 
----> 9 from .utils import desaturate, get_color_cycle
     10 from .colors import xkcd_rgb, crayons
     11 

/home/simon/.local/lib/python3.6/site-packages/seaborn/utils.py in <module>()
     12 import matplotlib as mpl
     13 import matplotlib.colors as mplcol
---> 14 import matplotlib.pyplot as plt
     15 from matplotlib.cbook import normalize_kwargs
     16 

/home/simon/.local/lib/python3.6/site-packages/matplotlib/pyplot.py in <module>()
     41 from matplotlib import docstring
     42 from matplotlib.backend_bases import FigureCanvasBase, MouseButton
---> 43 from matplotlib.figure import Figure, figaspect
     44 from matplotlib.gridspec import GridSpec
     45 from matplotlib import rcParams, rcParamsDefault, get_backend, rcParamsOrig

/home/simon/.local/lib/python3.6/site-packages/matplotlib/figure.py in <module>()
     16 
     17 import matplotlib as mpl
---> 18 from matplotlib import docstring, projections
     19 from matplotlib import __version__ as _mpl_version
     20 

/home/simon/.local/lib/python3.6/site-packages/matplotlib/projections/__init__.py in <module>()
      2 from .geo import AitoffAxes, HammerAxes, LambertAxes, MollweideAxes
      3 from .polar import PolarAxes
----> 4 from mpl_toolkits.mplot3d import Axes3D
      5 
      6 

/usr/lib/python3/dist-packages/mpl_toolkits/mplot3d/__init__.py in <module>()
      4 import six
      5 
----> 6 from .axes3d import Axes3D

/usr/lib/python3/dist-packages/mpl_toolkits/mplot3d/axes3d.py in <module>()
     30 import matplotlib.transforms as mtransforms
     31 from matplotlib.axes import Axes, rcParams
---> 32 from matplotlib.cbook import _backports
     33 from matplotlib.colors import Normalize, LightSource
     34 from matplotlib.transforms import Bbox

ImportError: cannot import name '_backports'

Principal compomenet analysis¶

Import iris dataset and view as a dataframe¶

In [2]:
# import some data to play with
iris = datasets.load_iris()
df = pd.DataFrame(data=iris['data'],columns=iris['feature_names'])
df['species'] = pd.Categorical.from_codes(iris.target, iris.target_names)
df.head()
Out[2]:
sepal length (cm) sepal width (cm) petal length (cm) petal width (cm) species
0 5.1 3.5 1.4 0.2 setosa
1 4.9 3.0 1.4 0.2 setosa
2 4.7 3.2 1.3 0.2 setosa
3 4.6 3.1 1.5 0.2 setosa
4 5.0 3.6 1.4 0.2 setosa

Describe Features¶

In [3]:
sns.pairplot(data=df,kind='scatter', hue='species')
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-3-08d3de81a4d6> in <module>()
----> 1 sns.pairplot(data=df,kind='scatter', hue='species')

NameError: name 'sns' is not defined

Conclusion¶

Three features describes relally good the classes so we could reduce the number of features to 3

In [17]:
reduced = PCA(n_components=3).fit_transform(df.iloc[:,0:4])
In [18]:
# plot
fig = plt.figure(figsize=(18, 10))
ax = fig.add_subplot(111, projection='3d')
ax.scatter(reduced[:,0], reduced[:,1],reduced[:,2], c=iris.target, s=100)
ax.view_init(20, 90)
plt.show()
No description has been provided for this image
In [ ]: