Fancy indexing
import numpy as np
x=np.arange(16)
x
>>>array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15])
x[3:9:2]
>>>array([3, 5, 7])
ind=[3,5,8]
x[ind]
>>>array([3, 5, 8])
ind1=np.array([[0,2],[1,3]])
x[ind1]
>>>array([[0, 2],
[1, 3]])
ind2=np.array([[1,2],[0,3]])
x[ind2]
>>>array([[1, 2],
[0, 3]])
x[5]=6
x
>>>array([ 0, 1, 2, 3, 4, 6, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15])
x[::-1]
>>>array([15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 6, 4, 3, 2, 1, 0])
ind3=np.array([[1,2],[0,3]])
x[ind3]
>>>array([[1, 2],
[0, 3]])
x[::-1]
x.reshape(4,-1)
x
>>>array([ 0, 1, 2, 3, 4, 6, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15])
xx=x.reshape(4,-1)
xx
>>>array([[ 0, 1, 2, 3],
[ 4, 6, 6, 7],
[ 8, 9, 10, 11],
[12, 13, 14, 15]])
row=np.array([0,1,2])
col=np.array([1,2,3])
xx[row,col]
>>>array([ 1, 6, 11]) # xx[0,1],xx[1,2],xx[2,3]
xx[:2,col]
>>>array([[1, 2, 3],
[6, 6, 7]])
col=[True,False,True,True]
xx[1:3,col]
>>>array([[ 4, 6, 7],
[ 8, 10, 11]])
x
>>>array([ 0, 1, 2, 3, 4, 6, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15])
x<3
>>>array([ True, True, True, False, False, False, False, False, False,
False, False, False, False, False, False, False])
x>3
>>>array([False, False, False, False, True, True, True, True, True,
True, True, True, True, True, True, True])
2*4==12-x
>>>array([False, False, False, False, True, False, False, False, False,
False, False, False, False, False, False, False])
xx<6
>>>array([[ True, True, True, True],
[ True, False, False, False],
[False, False, False, False],
[False, False, False, False]])
x
>>>array([ 0, 1, 2, 3, 4, 6, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15])
x[5]=5
x
>>>array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15])
np.sum(x<=3)
>>>4
np.count_nonzero(x<=5)
>>>6
np.any(x==0)
>>>True
np.all(x>0)
>>>False
xx
>>>array([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11],
[12, 13, 14, 15]])
np.sum(xx%2==0)
>>>8
np.sum(xx%2==0,axis=1)
>>>array([2, 2, 2, 2])
np.sum(xx%2==0,axis=0)
>>>array([4, 0, 4, 0])
np.all(xx>0,axis=1)
>>>array([False, True, True, True])
np.sum((x>3)&(x<10))
>>>6
np.sum(~(x==0))
>>>15
x[x<6]
>>>array([0, 1, 2, 3, 4, 5])
xx[xx[:,3]%3==0,:]
>>>array([[ 0, 1, 2, 3],
[12, 13, 14, 15]])
xx[xx[:,3]%3==0]
>>>array([[ 0, 1, 2, 3],
[12, 13, 14, 15]])