-1

I am a newbie in Python, and following this https://stackoverflow.com/a/13224566/

import numpy as np
import matplotlib.pyplot as plt

from matplotlib.mlab import PCA

pcadata = array(1,2,3)
pcaresults = PCA(pcadata)

print(pcaresults)

it is giving error: pcadata = array(1,2,3) NameError: name 'array' is not defined Background: I am using Python in Pydev Eclipse in Windows Environment

Community
  • 1
  • 1
Jack
  • 41
  • 2
  • 7
  • read the comments... copied from the link... "I think you want to add and change the following commands @user2988577: import numpy as np and data = np.array(np.random.randint(10,size=(10,3))). Then I would suggest following this tutorial to help you see how to plot blog.nextgenetics.net/?e=42" – NaN Nov 25 '16 at 23:11

1 Answers1

0

Arrays are not implemented in python itself, but in the module numpy. Use np.array([1,2,3]) to generate a one dimensional array containing the integers [1,2,3].

In order to do a principal component analysis, you must have more vectors than degrees of freedom (ie - you must have more rows than columns). Thus you should set pcadata to have at least three rows.

 `pcadata = np.array([[1,2,3],[4,5,6],[6,7,8]])` 

You asked in the comments below, how do I access pcaresults values? You can access the normalized centered version of the input with print(pcaresults.a). If you want some of the other attributes of pcaresults, you should try help(pcaresults) to see what else is contained in pcaresults.

Mark Hannel
  • 767
  • 5
  • 12
  • thanks @mark but now error is : pcadata = np.array(1,2,3) ValueError: only 2 non-keyword arguments accepted – Jack Nov 26 '16 at 01:51
  • Np @ Jack. If you look at the solution above, there are a set of square brackets around 1,2,3. Square brackets in python denote a list. Why do you need to give np.array a list? numpy's function array takes a single, list-like argument and turns it into an array. Currently, you are giving it three arguments each of which is not list-like. – Mark Hannel Nov 26 '16 at 02:50
  • oh my bad @Mark, thanks, for the correction I got it also rows must be greater than columns when i am printing result print(type(pcaresults)) print(pcaresults) it says how can i get its values? – Jack Nov 26 '16 at 04:30