Open In Colab

10: Numpy Tableau ndarray#

import numpy as np

1. ndarray** generators#

  • default generator : ndarray()

  • 1D generator : np.linspace and np.arange()

  • ND generator: np.zeros(), np.ones(), np.random.randn() (these are the most useful)

A = np.array([1, 2, 3]) # default generator, which allows to convert lists (or other objects) into ndarray
A = np.zeros((2, 3)) # array of 0 with dimensions 2x3
B = np.ones((2, 3)) # array of 1 with dimensions 2x3
C = np.random.randn(2, 3) # random array (normal distribution) with dimensions 2x3
D = np.random.rand(2, 3) # random array (uniform distribution)
 E = np.random.randint(0, 10, [2, 3]) # array of random integers from 0 to 10 and of dimension 2x3
A = np.ones((2, 3), dtype=np.float16) # define the type and the space to occupy on the memory
B = np.eye(4, dtype=np.bool) # create an identity matrix and convert the elements into bool type.
/tmp/ipykernel_3860723/3272918426.py:2: DeprecationWarning: `np.bool` is a deprecated alias for the builtin `bool`. To silence this warning, use `bool` by itself. Doing this will not modify any behavior and is safe. If you specifically wanted the numpy scalar type, use `np.bool_` here.
Deprecated in NumPy 1.20; for more details and guidance: https://numpy.org/devdocs/release/1.20.0-notes.html#deprecations
  B = np.eye(4, dtype=np.bool) # create an identity matrix and convert the elements into bool type.
A = np.linspace(1,10, 10)
B = np.arange(0, 10, 10)

2. Important attributes#

  • size

  • shape

A = np.zeros((2, 3)) # create an array of shape (2, 3)
 
print(A.size) # the number of elements in the array A
print(A.shape) # the dimensions of the array A (as a Tuple)
 
print(type(A.shape)) # here is the proof that the shape is a tuple
 
print(A.shape[0]) # the number of elements in the first dimension of A
6
(2, 3)
<class 'tuple'>
2

3. Important methods#

  • reshape() : to resize an array

  • ravel()** : to flatten an array (make it one dimension only)

  • squeeze()** : when a dimension is equal to 1, this dimension disappears

  • concatenate() : assembles 2 arrays together along an axis (also exists in hstack and vstack)

A = np.zeros((2, 3)) # create a shape array (2, 3)
 
A = A.reshape((3, 2)) # resize the array A (3 rows, 2 columns)
A.ravel() # flatten array A (one dimension)
A.squeeze() # eliminate the "1" dimensions of A.
array([[0., 0.],
       [0., 0.],
       [0., 0.]])
A = np.zeros((2, 3)) # create a shape array (2, 3)
B = np.ones((2, 3)) # create a shape array (2, 3)

np.concatenate((A, B), axis=0) # axis 0 : equivalent of np.vstack((A, B))
array([[0., 0., 0.],
       [0., 0., 0.],
       [1., 1., 1.],
       [1., 1., 1.]])
np.concatenate((A, B), axis=1) # axis 1 : equivalent of np.hstack((A, B))
array([[0., 0., 0., 1., 1., 1.],
       [0., 0., 0., 1., 1., 1.]])

4. exercise and solutions#

def initialization(m, n):
    # m : number of rows
    # n : number of columns
    # return a random matrix (m, n+1)
    # with a bias column (filled with "1") on the right

    return X

SOLUTION#

Hide code cell content
def initialization(m, n):
    # m : number of rows
    # n : number of columns
    # return a random matrix (m, n+1)
    # with a bias column (filled with "1") on the right
    X = np.random.randn(m, n)
    X = np.concatenate((X, np.ones((X.shape[0], 1))), axis = 1)

    return X

initialization(3, 4)
array([[ 1.34802861,  0.56557467,  0.07477671, -1.16431132,  1.        ],
       [-0.65413789, -0.29018397,  1.47885623,  0.31252317,  1.        ],
       [-2.82929408,  0.03365898, -0.31968468,  0.67855551,  1.        ]])