Import library

import numpy as np

Creating

Random numbers

# random int number between [1, 100]
np.random.randint(1, 100 + 1)

# random float number between [0, 1)
np.random.random()
# array of random between [0, 1)
np.random.random_sample((5,)) # size: 5x1

# betweeen (a, b)
(b - a) * random_sample() + a

Equal size

Create evenly spaced numbers over a specified interval[ref]

x = np.linspace(0, 3.5, num=20) # default num = 50

Range of int numbers

np.arange(0, 5)
np.arange(0, 31, 5)
array([0, 1, 2, 3, 4])
array([ 0,  5, 10, 15, 20, 25, 30])

Indexes and values in other arrays

Create an array from nested arrays. Values in array_2 are indexes in array_1 and we create a new array take values in array_1 which is corresponding to its indexes showed in array_2.

array_1 = np.array([ [0,0,0], [1,1,1], [2,2,2] ])
array_2 = np.array([1,0,2,1,0,2,1]) # indexes in array_1
array_3 = array_1[array_2]

print(array_1)
print(array_2)
print(array_3)
[[0 0 0]
 [1 1 1]
 [2 2 2]]

[1 0 2 1 0]

[[1 1 1]
 [0 0 0]
 [2 2 2]
 [1 1 1]
 [0 0 0]]

Array of NaN values

# single array
np.repeat(np.nan, 5)

# multi dimensional arrays
a = np.empty((2,3))
a[:] = np.nan
# other way
np.repeat([np.repeat(np.nan, 3)], 2, axis=0)
array([nan, nan, nan, nan, nan])

array([[nan, nan, nan],
       [nan, nan, nan]])

Deleting

# DELETE POSITIONS
arr = np.arange(6)
np.delete(arr, [3,4])
array([0, 1, 2, 3, 4, 5])
array([0, 1, 2, 5])