[Tutor] Help- Regarding python
oscar.j.benjamin at gmail.com
Tue Feb 5 01:21:24 CET 2013
On 4 February 2013 06:24, Gayathri S <gayathri.s112 at gmail.com> wrote:
> Hi All....!
> If i have data set like this means...
PCA only makes sense for multivariate data: your data should be a set
of vectors *all of the same length*. I'll assume that you were just
being lazy when you posted it and that you didn't bother to copy the
first and last lines properly...
> Shall i use the following code for doing PCA on given input? could you tell
This code you posted is all screwed up. It will give you errors if you
try to run it.
Also I don't really know what you mean by "doing PCA". The code below
transforms your data into PCA space and plots a 2D scatter plot using
the first two principal components.
import numpy as np
from matplotlib import pyplot as plt
data = np.array([
# Compute the eigenvalues and vectors of the covariance matrix
C = np.cov(data.T)
eigenvalues, eigenvectors = np.linalg.eig(C)
# 2D PCA - get the two eigenvectors with the largest eigenvalues
v1, v2 = eigenvectors[:,:2].T
# Project the data onto the two principal components
data_pc1 = [np.dot(v1, d) for d in data]
data_pc2 = [np.dot(v2, d) for d in data]
# Scatter plot in PCA space
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
ax.plot(data_pc1, data_pc2, 'x')
More information about the Tutor