Tuesday, September 25, 2012

[Python] package mlpy and matplotlib

import numpy as np # scientific computing package
import mlpy
import matplotlib.pyplot as plt # required for plotting'
iris = np.loadtxt('iris.csv', delimiter=',')  # http://mlpy.sourceforge.net/docs/3.5/_downloads/iris.csv
x, y = iris[:, :4], iris[:, 4].astype(np.int)
x.shape, y.shape

# Principal Component Analysis
pca = mlpy.PCA() # new PCA instance
pca.learn(x) # learn from data
z = pca.transform(x, k=2) # choose 2 principal components

#plot the principal components
plt.set_cmap(plt.cm.Paired)
fig1 = plt.figure(1)
title = plt.title("PCA on iris dataset")
plot = plt.scatter(z[:, 0], z[:, 1], c=y)
labx = plt.xlabel("First component")
laby = plt.ylabel("Second component")
plt.show()

No comments:

Post a Comment