arr = np.arange(10)
arr[5]
>>>5
arr[5:8]
>>>array([5, 6, 7])
arr[5:8] = 12
arr
>>>array([ 0, 1, 2, 3, 4, 12, 12, 12, 8, 9])
arr_slice = arr[5:8]
arr_slice[1]=12345
arr
>>>array([ 0, 1, 2, 3, 4, 12, 12345, 12, 8,
9])
arr_slice[:] = 64
arr
>>>array([ 0, 1, 2, 3, 4, 64, 64, 64, 8, 9])
arr2d = np.array([[1,2,3],[4,5,6],[7,8,9]])
arr2d[2]
>>>array([7, 8, 9])
arr2d[0][2]
>>>3
arr2d[0,2]
>>>3
arr3d = np.array([[[1,2,3],[4,5,6]],[[7,8,9],[10,11,12]]])
arr3d
>>> array([[[ 1, 2, 3],
[ 4, 5, 6]],
[[ 7, 8, 9],
[10, 11, 12]]])
arr3d[0]
>>>array([[1, 2, 3],
[4, 5, 6]])
old_values = arr3d[0].copy()
arr3d[0] = 42
arr3d
>>>array([[[42, 42, 42],
[42, 42, 42]],
[[ 7, 8, 9],
[10, 11, 12]]])
arr3d[0] = old_values
arr3d
>>>array([[[ 1, 2, 3],
[ 4, 5, 6]],
[[ 7, 8, 9],
[10, 11, 12]]])
arr3d[1,0]
>>>array([7, 8, 9])
arr[1:6]
>>>array([7, 8, 9])
arr2d
>>>array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
arr2d[:2]
>>>array([[1, 2, 3],
[4, 5, 6]])
arr2d[:2,1:]
>>>array([[2, 3],
[5, 6]])
arr2d[1,:2]
>>>array([4, 5])
arr2d[:,:1]
>>>array([[1],
[4],
[7]])
names = np.array(['Bob','Joe','Will','Bob','Will','Joe','Joe'])
data = np.random.randn(7,4)
names
>>>array(['Bob', 'Joe', 'Will', 'Bob', 'Will', 'Joe', 'Joe'], dtype='<U4')
data
>>>array([[-0.54093662, 0.98533044, 0.82009084, 1.20798381],
[ 1.82829901, 0.17411663, -1.51303825, 0.22804367],
[-0.65078152, 0.08313449, 0.83880963, -1.78276309],
[ 0.61207065, 1.15364584, -0.12602078, -1.09535793],
[ 0.45050638, 1.58353401, -0.46624268, -0.25089213],
[-0.14677405, 0.26151823, 1.13177225, -1.47578361],
[-0.80152908, 0.49052361, 0.22747227, -0.7811803 ]])
names == 'Bob'
>>>array([ True, False, False, True, False, False, False])
data[names == 'Bob']
>>>array([[-0.54093662, 0.98533044, 0.82009084, 1.20798381],
[ 0.61207065, 1.15364584, -0.12602078, -1.09535793]])
data[names == 'Bob',2:]
>>>array([[ 0.82009084, 1.20798381],
[-0.12602078, -1.09535793]])
names != 'Bob'
>>>array([False, True, True, False, True, True, True])
data[~(names=='Bob')]
>>>array([[ 1.82829901, 0.17411663, -1.51303825, 0.22804367],
[-0.65078152, 0.08313449, 0.83880963, -1.78276309],
[ 0.45050638, 1.58353401, -0.46624268, -0.25089213],
[-0.14677405, 0.26151823, 1.13177225, -1.47578361],
[-0.80152908, 0.49052361, 0.22747227, -0.7811803 ]])
mask = (names == 'Bob') | (names == 'Will')
mask
>>>array([ True, False, True, True, True, False, False])
data[mask]
array([[-0.54093662, 0.98533044, 0.82009084, 1.20798381],
[-0.65078152, 0.08313449, 0.83880963, -1.78276309],
[ 0.61207065, 1.15364584, -0.12602078, -1.09535793],
[ 0.45050638, 1.58353401, -0.46624268, -0.25089213]])
data[data<0] = 0
data
>>>array([[0. , 0.98533044, 0.82009084, 1.20798381],
[1.82829901, 0.17411663, 0. , 0.22804367],
[0. , 0.08313449, 0.83880963, 0. ],
[0.61207065, 1.15364584, 0. , 0. ],
[0.45050638, 1.58353401, 0. , 0. ],
[0. , 0.26151823, 1.13177225, 0. ],
[0. , 0.49052361, 0.22747227, 0. ]])
data[names != 'Joe'] = 7
data
>>>array([[7. , 7. , 7. , 7. ],
[1.82829901, 0.17411663, 0. , 0.22804367],
[7. , 7. , 7. , 7. ],
[7. , 7. , 7. , 7. ],
[7. , 7. , 7. , 7. ],
[0. , 0.26151823, 1.13177225, 0. ],
[0. , 0.49052361, 0.22747227, 0. ]])
import numpy as np
arr = np.empty((8,4))
for i in range(8):
arr[i] = i
arr
>>>array([[0., 0., 0., 0.],
[1., 1., 1., 1.],
[2., 2., 2., 2.],
[3., 3., 3., 3.],
[4., 4., 4., 4.],
[5., 5., 5., 5.],
[6., 6., 6., 6.],
[7., 7., 7., 7.]])
arr[[4,3,0,6]]
>>>array([[4., 4., 4., 4.],
[3., 3., 3., 3.],
[0., 0., 0., 0.],
[6., 6., 6., 6.]])
arr[[-3,-5,-7]]
>>>array([[5., 5., 5., 5.],
[3., 3., 3., 3.],
[1., 1., 1., 1.]])
arr = np.arange(32).reshape((8,4))
arr
>>>array([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11],
[12, 13, 14, 15],
[16, 17, 18, 19],
[20, 21, 22, 23],
[24, 25, 26, 27],
[28, 29, 30, 31]])
arr[[1,5,7,2],[0,3,1,2]]
>>>array([ 4, 23, 29, 10])
arr[[1,5,7,2],[0,3,1,2]]
>>>array([ 4, 23, 29, 10])
arr[[1,5,7,2]][:,[0,3,1,2]]
>>>array([[ 4, 7, 5, 6],
[20, 23, 21, 22],
[28, 31, 29, 30],
[ 8, 11, 9, 10]])
arr[np.ix_([1,5,7,2],[0,3,1,2])]
>>>array([[ 4, 7, 5, 6],
[20, 23, 21, 22],
[28, 31, 29, 30],
[ 8, 11, 9, 10]])