Prev: Array construction (cont'd) | Next: Array slicing
import numpy as np
ar = np.linspace(0,10,8, False)
print(ar, '\n')
print(ar[0], ar[2], ar[len(ar)-1], ar[-1], ar[-len(ar)], sep=', ')
import numpy as np
ar = np.arange(20).reshape(2,10)
print(ar)
print(ar[0][len(ar[0])-1])
print(ar[0,len(ar)]) # Remember that len(ar) equals '2' (number of list members)
# three syntactic forms are acceptable
print(ar[1,2], ar[0][5], ar[(1,0)])
import numpy as np
ar = np.array([[1,2,3,4],[5,6,7,8,9,10]]) # ar is now a 'ragged' array due to uneven lists
print(ar.ndim) # strangely its dimension is 1. Why?
print(ar.dtype) # why is dtype 'object' and not integer?
ar = np.concatenate(ar)
print(ar)
print(ar.ndim, ar.dtype)
- about numpy.concatenate
- about array flattening: numpy.flatten and/or numpy.ravel
import numpy as np
ar = np.arange(1,17).reshape(2,2,4)
print(ar,'\n')
print(ar[0,1,2], ar[1,1,1], ar[0][1][3], ar[(1,1,3)])
print()
print(ar[0,1,len(ar)], ar[len(ar)-1,len(ar[1])-1,len(ar[0][1])-1],
ar[0][1][3], ar[(1,1,1)])
- (a) Print the rows one below the other (no indexing)
- (b) Similar to (a) but using single indexing for the rows
- (c) Print the array members (one by the other in a row; no indexing)
- (d) Similar to (c) but using double indices for the single array members
- (e) Using the .format() method.
import numpy as np
ar = np.array([[np.random.randint(1,101) for i in range(5)],
[np.random.randint(1,101) for i in range(5)]])
#(a)
for i in ar:
print(i)
#(b)
for i in [0,1]:
print(ar[i])
#(c)
for i in ar:
for k in i:
print(k, end=' ')
#(d)
for i in [0,1]:
for m in [0,1,2,3,4]:
print(ar[i][m], end=' ')
print()
#(e)
print('{:} {:}'.format(*(ar)))
import numpy as np
ar = np.arange(20).reshape(2,10)
ar[0][0] = 100
ar
#ar[0][0] = 'X' # but this is NOT valid in an array of integers
ar[0][0] = '200' # this is accepted as digit-based string is trasformed to integer
ar
import numpy as np
ar = np.arange(20).reshape(4,5)
print(ar,'\n')
ar[0] = 100 # a scalar is 'broadcast' across the 1D ar[0] array
print(ar)
import numpy as np
ar = np.arange(20).reshape(4,5)
print(ar,'\n')
# this will not work: numpy does not broadcast a 1D array with 3 members on the 5 member ar[0]
#ar[0] = [100,200,300]
# but this will work because shapes are compatible
ar[0] = [100,200,300,400,500]
print(ar)
import numpy as np
ar = np.random.random_integers(0, 100, 10) # See numpy.random documentation for this
print(ar)
mask = (ar%2==0) # mask is array constructed by applying a boolean expression on 'ar'
print(mask)
new_ar = ar[mask] # new_ar is constructed by using mask as array index
print('new_ar =',new_ar) # the new_ar contains the 'True' members of original ar
import numpy as np
ar = np.random.random_integers(0, 100, 10)
print(ar)
alist = [i if i%2==1 else i-1 for i in range(1,11)]
print(alist) # a list of integers is constructed with the required indices
new_ar = ar[alist] # new_ar is constructed by using alist as array index
print('new_ar =',new_ar)
. Free learning material
. See full copyright and disclaimer notice