Numpy Basics¶
- Fixed Type
- Contiguous Memory
In [ ]:
Copied!
import numpy as np
import numpy as np
The Basics¶
In [ ]:
Copied!
a = np.array([1, 2, 3], dtype=np.int8)
b = np.array([[9, 8, 7, 11, 24], [6, 5, 4, 45, 67], [3, 2, 1, 16, 12]], dtype=np.float64)
a, b
a = np.array([1, 2, 3], dtype=np.int8)
b = np.array([[9, 8, 7, 11, 24], [6, 5, 4, 45, 67], [3, 2, 1, 16, 12]], dtype=np.float64)
a, b
Out[ ]:
(array([1, 2, 3], dtype=int8), array([[ 9., 8., 7., 11., 24.], [ 6., 5., 4., 45., 67.], [ 3., 2., 1., 16., 12.]]))
In [ ]:
Copied!
# Get dimensions
a.ndim, b.ndim
# Get dimensions
a.ndim, b.ndim
Out[ ]:
(1, 2)
In [ ]:
Copied!
# Get shape
a.shape, b.shape
# Get shape
a.shape, b.shape
Out[ ]:
((3,), (3, 5))
In [ ]:
Copied!
# Get type
a.dtype, b.dtype
# Get type
a.dtype, b.dtype
Out[ ]:
(dtype('int8'), dtype('float64'))
In [ ]:
Copied!
# Each item size
a.itemsize, b.itemsize # each item size in Byte
# Each item size
a.itemsize, b.itemsize # each item size in Byte
Out[ ]:
(1, 8)
In [ ]:
Copied!
# Total size
# nbytes = size * itemsize
a.nbytes, b.nbytes
# Total size
# nbytes = size * itemsize
a.nbytes, b.nbytes
Out[ ]:
(3, 120)
In [ ]:
Copied!
# b[i, j] i=row, j=col
# Get specific row
b[1, :]
# b[i, j] i=row, j=col
# Get specific row
b[1, :]
Out[ ]:
array([ 6., 5., 4., 45., 67.])
In [ ]:
Copied!
# Get even indexes started from index 2
b[:, 2::2] = 78
# Get even indexes started from index 2
b[:, 2::2] = 78
In [ ]:
Copied!
# b[0, :] = 78
b[0, :] = [1, 2, 3, 4, 5] # List should be the same size and dimensions
b
# b[0, :] = 78
b[0, :] = [1, 2, 3, 4, 5] # List should be the same size and dimensions
b
Out[ ]:
array([[ 1., 2., 3., 4., 5.], [ 6., 5., 78., 45., 78.], [ 3., 2., 78., 16., 78.]])
3D example¶
In [ ]:
Copied!
c = np.zeros(shape=(2, 3, 4), dtype=np.float16)
c[0, 1, :] = [1, 2, 3, 4]
c
c = np.zeros(shape=(2, 3, 4), dtype=np.float16)
c[0, 1, :] = [1, 2, 3, 4]
c
Out[ ]:
array([[[0., 0., 0., 0.], [1., 2., 3., 4.], [0., 0., 0., 0.]], [[0., 0., 0., 0.], [0., 0., 0., 0.], [0., 0., 0., 0.]]], dtype=float16)
In [ ]:
Copied!
# outside in
c[0, 1, 3]
# outside in
c[0, 1, 3]
Out[ ]:
np.float16(4.0)
Initialize different types of arrays¶
In [ ]:
Copied!
np.zeros((2, 3))
np.zeros((2, 3))
Out[ ]:
array([[0., 0., 0.], [0., 0., 0.]])
In [ ]:
Copied!
np.ones((2, 3), dtype=np.int8)
np.ones((2, 3), dtype=np.int8)
Out[ ]:
array([[1, 1, 1], [1, 1, 1]], dtype=int8)
In [ ]:
Copied!
np.full(shape=(2, 3), fill_value=123)
np.full(shape=(2, 3), fill_value=123)
Out[ ]:
array([[123, 123, 123], [123, 123, 123]])
In [ ]:
Copied!
np.full_like(a, 12)
np.full_like(a, 12)
Out[ ]:
array([12, 12, 12], dtype=int8)
Random¶
In [ ]:
Copied!
# Random int
np.random.randint(low=0, high=12, size=(3, 4))
# Random int
np.random.randint(low=0, high=12, size=(3, 4))
Out[ ]:
array([[ 2, 10, 3, 11], [ 0, 8, 7, 11], [ 8, 6, 7, 2]])
In [ ]:
Copied!
# Random 0-1 just pass size to it
np.random.rand(3, 2)
# Random 0-1 just pass size to it
np.random.rand(3, 2)
Out[ ]:
array([[0.95567647, 0.6325864 ], [0.7441816 , 0.50506503], [0.41507009, 0.04014501]])
In [ ]:
Copied!
# Random 0-1 just pass size to it
np.random.random_sample(size=b.shape)
# Random 0-1 just pass size to it
np.random.random_sample(size=b.shape)
Out[ ]:
array([[0.65321004, 0.93401116, 0.239411 , 0.1699394 , 0.15857918], [0.49180305, 0.47913441, 0.67379083, 0.0516425 , 0.68931425], [0.78729527, 0.07978258, 0.71997382, 0.55340929, 0.1960921 ]])
In [ ]:
Copied!
# Identity matrix
np.identity(6)
# Identity matrix
np.identity(6)
Out[ ]:
array([[1., 0., 0., 0., 0., 0.], [0., 1., 0., 0., 0., 0.], [0., 0., 1., 0., 0., 0.], [0., 0., 0., 1., 0., 0.], [0., 0., 0., 0., 1., 0.], [0., 0., 0., 0., 0., 1.]])
In [ ]:
Copied!
array = np.array([2, 4, 6])
np.repeat(array, 3)
array = np.array([2, 4, 6])
np.repeat(array, 3)
Out[ ]:
array([2, 2, 2, 4, 4, 4, 6, 6, 6])
In [ ]:
Copied!
array = np.array([[2, 4, 6]])
np.repeat(array, 3, axis=0)
array = np.array([[2, 4, 6]])
np.repeat(array, 3, axis=0)
Out[ ]:
array([[2, 4, 6], [2, 4, 6], [2, 4, 6]])
Exercise¶
In [ ]:
Copied!
# Create a zeros array that edges=1 and center=9
# Way 1
array = np.zeros(shape=(11, 11), dtype=np.int8)
r_l, c_l = array.shape
array[r_l // 2, c_l // 2] = 9
array[0::r_l - 1, :] = 1
array[:, 0::c_l - 1] = 1
array
# Create a zeros array that edges=1 and center=9
# Way 1
array = np.zeros(shape=(11, 11), dtype=np.int8)
r_l, c_l = array.shape
array[r_l // 2, c_l // 2] = 9
array[0::r_l - 1, :] = 1
array[:, 0::c_l - 1] = 1
array
Out[ ]:
array([[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1], [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1], [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1], [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1], [1, 0, 0, 0, 0, 9, 0, 0, 0, 0, 1], [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1], [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1], [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1], [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]], dtype=int8)
In [ ]:
Copied!
# Way 2
array = np.ones(shape=(11, 11), dtype=np.int8)
r_l, c_l = array.shape
z = np.zeros(shape=(r_l - 2, c_l -2))
z[(r_l - 2) // 2, (c_l - 2) // 2] = 9
array[1:-1, 1:-1] = z
array
# Way 2
array = np.ones(shape=(11, 11), dtype=np.int8)
r_l, c_l = array.shape
z = np.zeros(shape=(r_l - 2, c_l -2))
z[(r_l - 2) // 2, (c_l - 2) // 2] = 9
array[1:-1, 1:-1] = z
array
Out[ ]:
array([[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1], [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1], [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1], [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1], [1, 0, 0, 0, 0, 9, 0, 0, 0, 0, 1], [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1], [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1], [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1], [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]], dtype=int8)
Mathematics¶
In [ ]:
Copied!
a = np.array([1, 2, 3, 4])
a + 2 # * ** + - / // etc
a = np.array([1, 2, 3, 4])
a + 2 # * ** + - / // etc
Out[ ]:
array([1, 2, 3, 4])
In [ ]:
Copied!
b = np.array([2, 4, 9, 8]),
a + b # * ** +,, - / // etc
b = np.array([2, 4, 9, 8]),
a + b # * ** +,, - / // etc
Out[ ]:
array([[ 3, 6, 12, 12]])
In [ ]:
Copied!
np.sin(b)
np.sin(b)
Out[ ]:
array([[ 0.90929743, -0.7568025 , 0.41211849, 0.98935825]])
Linear Algebra¶
In [ ]:
Copied!
mat_a = np.random.randint(1, 5, (3, 4))
mat_b = np.random.randint(1, 5, (4, 2))
mat_a, mat_b
mat_a = np.random.randint(1, 5, (3, 4))
mat_b = np.random.randint(1, 5, (4, 2))
mat_a, mat_b
Out[ ]:
(array([[2, 2, 2, 2], [4, 4, 2, 1], [4, 3, 4, 2]]), array([[2, 1], [4, 1], [3, 3], [4, 2]]))
In [ ]:
Copied!
# mat_a@mat_b
np.matmul(mat_a, mat_b)
# mat_a@mat_b
np.matmul(mat_a, mat_b)
Out[ ]:
array([[26, 14], [34, 16], [40, 23]])
In [ ]:
Copied!
# Find the determinant
c = np.identity(3)
np.linalg.det(c)
# Find the determinant
c = np.identity(3)
np.linalg.det(c)
Out[ ]:
np.float64(1.0)
Statistics¶
In [ ]:
Copied!
stats = np.array([[1,2,3], [4, 5, 6]])
stats = np.array([[1,2,3], [4, 5, 6]])
In [ ]:
Copied!
np.min(stats, axis=0) # each cols
np.min(stats, axis=0) # each cols
Out[ ]:
array([1, 2, 3])
In [ ]:
Copied!
np.max(stats, axis=1) # each rows
np.max(stats, axis=1) # each rows
Out[ ]:
array([3, 6])
In [ ]:
Copied!
np.mean(stats)
np.mean(stats)
Out[ ]:
np.float64(3.5)
In [ ]:
Copied!
np.sum(stats)
np.sum(stats)
Out[ ]:
np.int64(21)
Reorganizing Arrays¶
In [ ]:
Copied!
stats.reshape((-1,))
stats.reshape((-1,))
Out[ ]:
array([1, 2, 3, 4, 5, 6])
Vertical & Horizontal stacks¶
In [ ]:
Copied!
a = stats.reshape((3, 2))
b = np.random.randint(1, 5, (3, 2))
a = stats.reshape((3, 2))
b = np.random.randint(1, 5, (3, 2))
In [ ]:
Copied!
np.hstack([a, b])
np.hstack([a, b])
Out[ ]:
array([[1, 2, 4, 4], [3, 4, 3, 3], [5, 6, 3, 3]])
In [ ]:
Copied!
np.vstack([a, b])
np.vstack([a, b])
Out[ ]:
array([[1, 2], [3, 4], [5, 6], [4, 4], [3, 3], [3, 3]])
Load Data¶
In [ ]:
Copied!
# np.genfromtxt("file.txt", delimiter=",")
# np.genfromtxt("file.txt", delimiter=",")
Boolean Masking and Advanced Indexing¶
In [ ]:
Copied!
data = np.random.randint(1, 500, (5, 4))
data = np.random.randint(1, 500, (5, 4))
In [ ]:
Copied!
data[data > 100] # return values that match with condition
data[data > 100] # return values that match with condition
Out[ ]:
array([469, 396, 484, 155, 310, 265, 416, 337, 240, 135, 353, 482, 201])
In [ ]:
Copied!
# data > 100
((data > 100) & (data < 200))
# ~((data > 100) & (data < 200)) # not
# data > 100
((data > 100) & (data < 200))
# ~((data > 100) & (data < 200)) # not
Out[ ]:
array([[ True, True, True, True], [ True, False, True, True], [ True, True, True, True], [ True, True, False, True], [ True, True, True, True]])
In [ ]:
Copied!
np.any(data > 469, axis=0) # col
np.any(data > 469, axis=0) # col
Out[ ]:
array([False, True, False, True])
Index with list¶
In [ ]:
Copied!
a = np.array([1,2,3,4,5,6,7,8,9])
a[[1, 2, 8]]
a = np.array([1,2,3,4,5,6,7,8,9])
a[[1, 2, 8]]
Out[ ]:
array([2, 3, 9])
In [ ]:
Copied!