Machine Learning Tutorial, Iris Dataset

Machine Learning – Iris Dataset


#python code iris dataset 

import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from sklearn import datasets
from sklearn.decomposition import PCA

#import some data to play with
iris=datasets.load_iris()
#print(iris)
x=iris.data[:,:2] #we only tale the first two features.
print(x)
print(iris.data)
y=iris.target
print(iris.target)

x_min,x_max=x[:,0].min() - .5, x[:,0].max() + .5
y_min,y_max=x[:,1].min() - .5, x[:,1].max() + .5

plt.figure(2,figsize=(8,6))
plt.clf()

#plot the training points
plt.scatter(x[:,0],x[:,1],c=y,edgecolor='k')
plt.xlabel('Sepal length')
plt.ylabel('Sepal width')

plt.xlim(x_min,x_max)
plt.ylim(y_min,y_max)
plt.xticks(())
plt.yticks(())


#To getter a better understanding of interaction of the dimensions
#plot the first three PCA dimensions

fig=plt.figure(1,figsize=(8,10))
ax=Axes3D(fig,elev=-160,azim=140)
x_reduced=PCA(n_components=3).fit_transform(iris.data)
ax.scatter(x_reduced[:,0],x_reduced[:,1],x_reduced[:,2],c=y,cmap=plt.cm.Set1,edgecolor='k',s=40)
ax.set_title("First three PCA Directions")
ax.set_xlabel("1st eigenvector")
ax.w_xaxis.set_ticklabels([])
ax.set_ylabel("2nd eigenvector")
ax.w_yaxis.set_ticklabels([])
ax.set_zlabel("3rd eigenvector")
ax.w_zaxis.set_ticklabels([])
plt.show()

Output :

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top