Lesson 5 ❮ Lesson List ❮ Top Page
❯ 5.1 NumPy Arrays
5.4 Join, Split, and Transpose
⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺
EXPECTED COMPLETION TIME
❲▹❳ Video 7m 22s
☷ Interactive readings 5m
✑ Practice 5.1 (G Colab) 25m
NumPy (Numerical Python) is a library used for working with arrays and math operations. It is usually imported under the np alias.
The array object in NumPy is called ndarray, it is up to 50x faster than lists and provides a lot of support functions.
np.array()
converts a list/tuples into an ndarray.
A dimension is one level of nested arrays.
array.ndim
returns the dimension of array:
0-D array: A number/scalar
1-D array: a list with numbers.
2-D array: a list containing lists, a matrix.
array.shape
return the shape of an array.
(as a tuple, like (2, 6))
array.size
return the number of elements in the array.
Similar to range, NumPy has some functions that make it easy to generate numbers:
np.arange(num)
returns integers from 0 to num-1.
np.arange(minnum, maxnum)
returns integers from minnum to maxnum-1.
np.full(shape, fill_value)
returns a new array of the given shape (e.g., (2,3) or 2) filled with fill_value.
np.empty(shape)
returns a new array of the given shape with no value in it.
You can use round(num) to round a number to num places decimal.
NumPy offers the random module to work with random numbers.
np.random.rand()
returns random float from 0 to 1.
np.random.randint(num)
returns random integers from 0 to num-1.
np.random.randint(minnum, maxnum)
returns random integers from minnum to maxnum-1.
Adding an extra argument size=shape will return a random array of the given shape.