[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Numpy array

On 05/18/2018 09:50 PM, Sharan Basappa wrote:
> This is regarding numpy array. I am a bit confused how parts of the array are being accessed in the example below.
> 1 import scipy as sp
> 2 data = sp.genfromtxt("web_traffic.tsv", delimiter="\t")
> 3 print(data[:10])
> 4 x = data[:,0]
> 5 y = data[:,1]
> Apparently, line 3 prints the first 10 entries in the array
> line 4 & 5 is to extract all rows but only 1st and second columns alone for x and y respectively.
> I am confused as to how data[:10] gives the first 10 rows while data[:,0] gives all rows

Numpy ordering is [rows,columns,...].  Things you don't provide an 
explicit answer for are assumed to be "all", which in Python syntax is 
":".  The reason for THAT goes into how slices are constructed.

So your example 3 could also be written print(data[0:10,:]), which sets 
rows to the range starting with 0 and ending before 10, and columns to 

Rob Gaddi, Highland Technology -- www.highlandtechnology.com
Email address domain is currently out of order.  See above to fix.