Please clarify the following Python NumPy array initialization and splicing examples -


i using python version 2.6 , learning numpy version 1.3.

i have tried out several numpy array initialization , column splicing examples below, , added inline questions comments , list of findings in end. can explain me behind differences in behaviors. lots of inter-related questions , rather long post, each example small, feel free answer 1 or couple.

import numpy np  print "initializing number of numpy arrays:\n" 

a) initialize list of tuples

a = np.zeros((3,),dtype=('i4,i4,a1')) a[:] = [(1, 2, 'a'), (3, 4, 'b'),(5, 6, 'a')] print "a: " print         # print => [(1, 2, 'a') (3, 4, 'b') (5, 6, 'a')] print repr(a)   # print => array([(1, 2, 'a'), (3, 4, 'b'), (5, 6, 'a')],                 #     dtype=[('f0', '<i4'), ('f1', '<i4'), ('f2', '|s1')] print '\n' 

b) normal list of tuples

b = []; b[:] = [(1, 2, 'a'), (3, 4, 'b'),(5, 6, 'a')] print "b: " print b         # print => [(1, 2, 'a'), (3, 4, 'b'), (5, 6, 'a') print '\n' 

question 1: a) looks list of tuples print, except without comma between tuples. if print repr(a), has commas. so, should no longer considered same b), correct?

c) fail: try initialize array returned np.zeroes list of list

question 2: below failing because dtype not match list passed in?

c = np.zeros((3,),dtype=('i4,i4,a1')) #c[:] = [[1, 2, 'a'], [3, 4, 'b'],[5, 6, 'a']] # typeerror: expected readable buffer object print '\n' 

d) fail: same c) try set dtype list

question 3: below failing, because not allowed specify dtype list?

#d = np.zeros((3,),dtype=['i4,i4,a1']) # typeerror: data type not understood #d[:] = [[1, 2, 'a'], [3, 4, 'b'],[5, 6, 'a']] print '\n' 

e) try initialize array using np.array list of list

question 4: why e) below list of list work, d) fail?

e = np.array( [[1, 2, 'a'], [3, 4, 'b'],[5, 6, 'a']] ) print "e: " print e     # print =>  [['1' '2' 'a']             #   ['3' '4' 'b']             #   ['5' '6' 'a']] print '\n' 

f) try initialize array using np.array list of tuples

question 5: same example e), time initializing list of tuples print out of f) identical e), initializing list of tuples , list of list identical then?

f = np.array( [(1, 2, 'a'), (3, 4, 'b'),(5, 6, 'a')] ) print "f: " print f     # print =>  [['1' '2' 'a']             #   ['3' '4' 'b']             #   ['5' '6' 'a']] print '\n' 

g) try initialize array using np.array csv file

question 6: same example e , f, time initializing file minor difference in quoting print out. there should no difference # between array generated , e) , f) right?

from stringio import stringio data = stringio( """ 1, 2, 3, 4, b 5, 6, """.strip()) g = np.genfromtxt(data, dtype=object, delimiter=',') print "g: " print g     # print =>  [[1 2 a]             #   [3 4 b]             #   [5 6 a]] print '\n' 

h) splicing numpy arrays column

#print "a: " #print a[:,2]   # indexerror: invalid index print "a: " print a['f2']   # ok though  # splicing normal list of tuples if not expected work #print "b: " #print b[:,2]   # indexerror: invalid index 

question 7 why splicing e below work, fail above index error same syntax?

print "e: " print e[:,2]    # print => ['a' 'b' 'a']  print "f: " print f[:,2]    # print => ['a' 'b' 'a']  print "g: " print g[:,2]    # print => [a b a] 

finding 1: initializing numpy.ndarray using nd.array , list of tuples, list of list, or csv file identical. maybe contrary other answer viewed says np.array expects list of tuples, stack overflow question define dtypes in numpy using list?.

finding 2: initializing numpy.ndarray using np.zeroes, unable initialize ndarray list of list.

finding 3: column splicing, initializing numpy.ndarray using nd.array, column splice (that is, e[:,2], syntax of splicing, using np.zeroes initialization method different a['f2']. normal list of tuples cannot spliced.

question 1

a) looks list of tuples print, except without comma between tuples. if print repr(a), has commas. so, should no longer considered same b) correct?

absolutely. a , b have different types: type(a) numpy.ndarray, type(b) list

question 2

is below failing because dtype not match list passed in?

no - issue you're trying fill list of lists, rather list of tuples did a. see here. i'm not totally sure deep reason behaviour, suspect has tuples being immutable objects, whereas lists (and arrays) mutable.

question 3

is below failing because not allowed specify dtype list?

yes, , furthermore fail fill d list of lists (see previous answer).

question 4

why e) below list of list work, d) fail?

look @ dtype of e - |s1, i.e. every element in array string of length 1. if don't specify dtype array constructor, type determined minimum type required hold all of objects in sequence. in case, since handed sequence containing strings, upcast integers strings.

question 5

same example e), time initializing list of tuples print out of f) identical e), initializing list of tuples , list of list identical then?

again, since don't give constructor dtype, upcast |s1.

question 6

same example e , f, time initializing file minor difference in quoting print out. there should no difference # between array generated , e) , f) right?

no, you're telling constructor create array dtype=object, whereas e , f have dtype=|s1.

question 7

why splicing e below work, fail above index error same syntax?

look @ a.shape - you'll see it's (3,), i.e. a 1d vector of length 3. although have fields can index by, has no second dimension index into. contrast, e.shape (3,3), can index column.


Comments

Popular posts from this blog

javascript - Count length of each class -

What design pattern is this code in Javascript? -

hadoop - Restrict secondarynamenode to be installed and run on any other node in the cluster -