Create an array¶

In [ ]:
import numpy as np
arr = np.array([1, 2, 3, 4])
print(arr)
[1 2 3 4]

Construct a 2d array¶

In [ ]:
arr_2d = np.array([[1, 2, 3], [4, 5, 6]])
print(arr_2d)
[[1 2 3]
 [4 5 6]]

Construct a 3d array¶

In [ ]:
arr_3d = np.array([[[1, 2, 3],
          [4, 5, 6]],
           [[7, 8, 9],
          [10, 11, 12]],
           [[13, 14, 15],
          [16, 17, 18]]])

print(arr_3d)
[[[ 1  2  3]
  [ 4  5  6]]

 [[ 7  8  9]
  [10 11 12]]

 [[13 14 15]
  [16 17 18]]]

Create an array fill with zeros¶

In [ ]:
arr = np.zeros((3, 3))
print(arr)
[[0. 0. 0.]
 [0. 0. 0.]
 [0. 0. 0.]]

Create an array fill with ones¶

In [ ]:
arr = np.ones((2, 4))
print(arr)
[[1. 1. 1. 1.]
 [1. 1. 1. 1.]]

Check the dimension of an array¶

In [ ]:
arr = np.ones((2, 4))
print(arr)
arr.shape
[[1. 1. 1. 1.]
 [1. 1. 1. 1.]]
Out[ ]:
(2, 4)

Check the size of an array, it returns total number of elements¶

In [ ]:
arr_2d = np.array([[1, 2, 3], [4, 5, 6]])
arr_2d.size
Out[ ]:
6

Get element from an array by index¶

In [ ]:
arr = np.array([1, 2, 3, 4])
arr[1]
Out[ ]:
2

Get element from a 2d array by index¶

In [ ]:
arr_2d = np.array([[1, 2, 3], [4, 5, 6]])
print(arr_2d[0])
print(arr_2d[0][1])
[1 2 3]
2

Slice an array¶

In [ ]:
arr = np.array([1, 2, 3, 4, 5, 6])
print(arr)
print('===============')
print(arr[1:4])
[1 2 3 4 5 6]
===============
[2 3 4]

Change the dimension of an array. (Reshape)¶

In [ ]:
arr = np.array([1, 2, 3, 4])
reshaped_arr = arr.reshape((2, 2))
print(arr)
print('-----------')
print(reshaped_arr)
[1 2 3 4]
-----------
[[1 2]
 [3 4]]

Let number of elements decide dimension. (.reshape(-1))¶

In [ ]:
arr = np.array([1, 2, 3, 4])
reshaped_arr = arr.reshape((2, 2))

reshaped_back_arr = reshaped_arr.reshape(-1)
print(arr)
print('-----------')
print(reshaped_arr)
print('-----------')
print(reshaped_back_arr)
[1 2 3 4]
-----------
[[1 2]
 [3 4]]
-----------
[1 2 3 4]

Plus of two arrays¶

In [ ]:
arr1 = np.ones((3,3))
arr2 = np.eye(3)

arr = arr1 + arr2
print(arr1)
print('==============')
print(arr2)
print('==============')
print(arr)
[[1. 1. 1.]
 [1. 1. 1.]
 [1. 1. 1.]]
==============
[[1. 0. 0.]
 [0. 1. 0.]
 [0. 0. 1.]]
==============
[[2. 1. 1.]
 [1. 2. 1.]
 [1. 1. 2.]]

Element-wise operation.¶

Broadcasting of an array with a number¶

In [ ]:
arr1 = np.array([2,4])
arr = arr1 + 5

print(arr1)
print('==============')
print(arr)
[2 4]
==============
[7 9]

Element-wise operation¶

2d array to 1d array¶

In [ ]:
arr1 = np.arange(0, 6).reshape(2,3)
arr2 = np.array([2,2,2])
arr = arr1 * arr2
print(arr1)
print('==============')
print(arr2)
print('==============')
print(arr)
[[0 1 2]
 [3 4 5]]
==============
[2 2 2]
==============
[[ 0  2  4]
 [ 6  8 10]]

Element-wise operation¶

2d array to 1d array (different axes)¶

In [ ]:
arr1 = np.arange(0, 6).reshape(2,3)
arr2 = np.array([[2],[2]])
arr = arr1 * arr2
print(arr1)
print('==============')
print(arr2)
print('==============')
print(arr)
[[0 1 2]
 [3 4 5]]
==============
[[2]
 [2]]
==============
[[ 0  2  4]
 [ 6  8 10]]

Vecter-wise operation¶

dot operation¶

In [ ]:
a = np.array([[1,2,3],[4,8,16]])
b = np.array([5,6,11]).reshape(-1,1)
c = np.dot(a,b)
print(a)
print('shape of a is:', a.shape)
print('==============')
print(b)
print('shape of b is:', b.shape)
print('==============')
print(c)
print('shape of c is:', c.shape)
[[ 1  2  3]
 [ 4  8 16]]
shape of a is: (2, 3)
==============
[[ 5]
 [ 6]
 [11]]
shape of b is: (3, 1)
==============
[[ 50]
 [244]]
shape of c is: (2, 1)

Vecter-wise operation¶

matmul operation (multiply two arrays)¶

In [ ]:
a = np.array([[1,2,3],[4,8,16]])
b = np.array([[1,2],[3,4],[5,6]])
c = np.matmul(a,b)
print(a)
print('shape of a is:', a.shape)
print('==============')
print(b)
print('shape of b is:', b.shape)
print('==============')
print(c)
print('shape of c is:', c.shape)
[[ 1  2  3]
 [ 4  8 16]]
shape of a is: (2, 3)
==============
[[1 2]
 [3 4]
 [5 6]]
shape of b is: (3, 2)
==============
[[ 22  28]
 [108 136]]
shape of c is: (2, 2)

Linear Algebra¶

np.linalg.det¶

determine of an array¶

In [ ]:
a = np.array([[2,1],[4,8]])
c = np.linalg.det(a) # linagl is for linear algebra
print(a)
print('==============')
print('Determine of array a is',c)
[[2 1]
 [4 8]]
==============
Determine of array a is 12.0

Basic statistics, sum, mean, min, max¶

In [ ]:
# some basic statistics
arr = np.array([1, 2, 3, 4])
print('Mean is ',np.mean(arr))
print('Min is ',np.min(arr))
print('Max is ',np.max(arr))
print('Sum is ',np.sum(arr))
Mean is  2.5
Min is  1
Max is  4
Sum is  10

find elements which meet condition¶

In [ ]:
# Find elements meet some conditions.
arr = np.array([1, 2, 3, 4, 5])
print(np.where(arr > 3))
(array([3, 4]),)

Generates values between a start and stop with a step.¶

In [ ]:
arr = np.arange(0, 10, 2) # (start, end, step)
print(arr)
[0 2 4 6 8]

Generates evenly spaced numbers over a range.¶

In [ ]:
arr = np.linspace(0, 5, 4) # (start, end, number of ends)
print(arr)
[0.         1.66666667 3.33333333 5.        ]

Generates random numbers from a uniform distribution [0, 1].¶

In [ ]:
arr = np.random.rand(3, 3)
print(arr)
[[0.76297454 0.41257014 0.01460657]
 [0.471247   0.25432555 0.00147957]
 [0.99812922 0.46172553 0.11443597]]

Generates random numbers from a normal distribution. [-inf, inf]¶

In [ ]:
arr = np.random.randn(3, 3)
print(arr)
[[ 1.18465094 -1.33483889  0.13211513]
 [ 0.67217354 -0.22472323 -0.20738979]
 [-1.44679146 -0.21920966  0.04974882]]

Generates random integers between a specified range.¶

In [ ]:
arr = np.random.randint(1, 10, (2, 3)) # (begin, end, shape of the array)
print(arr)
[[5 9 7]
 [7 4 6]]