%pylab inline

import pandas as pd

# load dataset
url = "https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data"
features = ['sepal_length','sepal_width','petal_length','petal_width','target']
df = pd.read_csv(url, names = features)
df.head() # show 5 first entries

"""
Explanation: <h1>Table of Contents<span class="tocSkip"></span></h1>
<div class="toc"><ul class="toc-item"><li><span><a href="#Principal-component-analysis" data-toc-modified-id="Principal-component-analysis-1"><span class="toc-item-num">1&nbsp;&nbsp;</span>Principal component analysis</a></span><ul class="toc-item"><li><span><a href="#Data-standarization" data-toc-modified-id="Data-standarization-1.1"><span class="toc-item-num">1.1&nbsp;&nbsp;</span>Data standarization</a></span></li><li><span><a href="#Principal-component-analysis-into-two-dimensions-" data-toc-modified-id="Principal-component-analysis-into-two-dimensions--1.2"><span class="toc-item-num">1.2&nbsp;&nbsp;</span>Principal component analysis into two dimensions </a></span></li><li><span><a href="#Plot-everything" data-toc-modified-id="Plot-everything-1.3"><span class="toc-item-num">1.3&nbsp;&nbsp;</span>Plot everything</a></span></li></ul></li></ul></div>

<H1>Principal component analysis</H1>
End of explanation
"""


from sklearn.preprocessing import StandardScaler

# extract features
features = ['sepal_length','sepal_width','petal_length','petal_width']
x = df.loc[:, features].values 
y = df.loc[:,['target']].values

# Standarize features
stdx = StandardScaler().fit_transform(x)
stdDf = pd.DataFrame(data = stdx, columns = features)
pd.concat([stdDf, df['target']], axis=1).head()

"""
Explanation: <H2>Data standarization</H2>
<P>The mean of every feature must be zero, and the standard deviation 1</P>
End of explanation
"""


from sklearn.decomposition import PCA

pca = PCA(n_components = 2)
principalComponents = pca.fit_transform(stdx)
pcDf = pd.DataFrame(data = principalComponents, columns =['PC1', 'PC2'])
finalDf = pd.concat([pcDf, df['target']], axis=1)
finalDf.head()

var1, var2 = pca.explained_variance_ratio_
print('The first component contains %2.4f %% of the variance'%var1)
print('The second component containts %2.4f %% of the variance'%var2)
print('Total variance explained %2.4f %% '%(var1+var2))

"""
Explanation: <H2>Principal component analysis into two dimensions </H2>
End of explanation
"""


fig = plt.figure(figsize = (4,4))
ax = fig.add_subplot(111) 
xlabel = 'Component 1 (%2.2f %% $\sigma^2$)'%var1
ylabel = 'Component 2 (%2.2f %% $\sigma^2$)'%var2
ax.set_xlabel(xlabel, fontsize = 12)
ax.set_ylabel(ylabel, fontsize = 12)
ax.set_title('Two component analysis', fontsize = 15)
mytargets = np.unique(df['target'].values).tolist()
colors = ['r', 'g', 'b']
for target, color in zip(mytargets,colors):
    mytarget = finalDf['target'] == target
    ax.scatter(finalDf.loc[mytarget, 'PC1']
               , finalDf.loc[mytarget, 'PC2']
               , c = color
               , s = 10)
ax.legend(mytargets)
#ax.grid()

"""
Explanation: <H2>Plot everything</H2>
End of explanation
"""


