Numpy

import numpy as np
matrice = [ [1,2,3] , [4,5,6] , [7,8,9] ]
np.array(matrice) # Affichage de la matrice
np.arange(0, 10) # array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
np.arange(0,11,2) # array([ 0,  2,  4,  6,  8, 10]) -> de 2 en 2
np.zeros(0) # array([], dtype=float64)
np.zeros(3) # array([0., 0., 0.])
np.zeros((2, 2)) # array( [ [0., 0.], [0., 0.] ] )
np.ones((2,2)) # array( [ [1., 1.], [1., 1.] ] )
np.ones(10) * 5 # array([5., 5., 5., 5., 5., 5., 5., 5., 5., 5.])
new_arr = np.ones(10)
new_arr *= 5 # array([5., 5., 5., 5., 5., 5., 5., 5., 5., 5.])
np.linspace(0, 10, 50)
# array([ 0.        ,  0.20408163,  0.40816327,  0.6122449 ,  0.81632653,
        # 1.02040816,  1.2244898 ,  1.42857143,  1.63265306,  1.83673469,
        # 2.04081633,  2.24489796,  2.44897959,  2.65306122,  2.85714286,
        # 3.06122449,  3.26530612,  3.46938776,  3.67346939,  3.87755102,
        # 4.08163265,  4.28571429,  4.48979592,  4.69387755,  4.89795918,
        # 5.10204082,  5.30612245,  5.51020408,  5.71428571,  5.91836735,
        # 6.12244898,  6.32653061,  6.53061224,  6.73469388,  6.93877551,
        # 7.14285714,  7.34693878,  7.55102041,  7.75510204,  7.95918367,
        # 8.16326531,  8.36734694,  8.57142857,  8.7755102 ,  8.97959184,
        # 9.18367347,  9.3877551 ,  9.59183673,  9.79591837, 10.        ])
np.eye(5)
# array([[1., 0., 0., 0., 0.],
    #    [0., 1., 0., 0., 0.],
    #    [0., 0., 1., 0., 0.],
    #    [0., 0., 0., 1., 0.],
    #    [0., 0., 0., 0., 1.]])
np.random.rand(5,5)
# array([[0.18555846, 0.35525655, 0.37255103, 0.0754025 , 0.92135755],
    #    [0.49103305, 0.83663558, 0.77959934, 0.92780553, 0.73860928],
    #    [0.65173983, 0.51333725, 0.54339623, 0.64512514, 0.52536205],
    #    [0.03078573, 0.54962413, 0.71531291, 0.41253377, 0.65652534],
    #    [0.54361858, 0.14206775, 0.97034294, 0.06173417, 0.42714206]])
np.random.randint(1, 100) # 56
arr = np.arange(25) # array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24])
arr.reshape(5, 5) 
# array([[ 0,  1,  2,  3,  4],
    #    [ 5,  6,  7,  8,  9],
    #    [10, 11, 12, 13, 14],
    #    [15, 16, 17, 18, 19],
    #    [20, 21, 22, 23, 24]])
arr.shape # (25,) (count)
arr.reshape(1, 25) # array([[ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24]])
arr.reshape(1, 25).shape # (1, 25)
arr.dtype # dtype('int64')
ranarr = np.random.randint(1, 50, 10) # array([27, 31, 12,  5, 36, 31, 17, 21, 19, 35])
ranarr.max() # 36 (value)
ranarr.min() # 5 (value)
ranarr.argmax() # 4 (index)
arr = np.arange(0, 11) # array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10])
arr[0:5] = 100 # array([100, 100, 100, 100, 100,   5,   6,   7,   8,   9,  10])
arr > 4 # array([ True,  True,  True,  True,  True,  True,  True,  True,  True, True,  True])
arr < 4 # array([False, False, False, False, False, False, False, False, False, False, False])
bool_array = arr < 100 # array([False, False, False, False, False,  True,  True,  True,  True, True,  True])
arr[bool_array] # rray([ 5,  6,  7,  8,  9, 10])
arr[arr > 2] # array([100, 100, 100, 100, 100,   5,   6,   7,   8,   9,  10])
arr = np.arange(0, 10) # array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
arr + arr # array([ 0,  2,  4,  6,  8, 10, 12, 14, 16, 18])
arr * arr # array([ 0,  1,  4,  9, 16, 25, 36, 49, 64, 81])
arr - arr # array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
arr ** 3 # array([  0,   1,   8,  27,  64, 125, 216, 343, 512, 729])
np.max(arr) # 9
np.sin(arr)
# array([ 0.        ,  0.84147098,  0.90929743,  0.14112001, -0.7568025 ,
    #    -0.95892427, -0.2794155 ,  0.6569866 ,  0.98935825,  0.41211849])
mat = np.arange(1, 26).reshape(5, 5)
mat.sum() # 325
mat.std() # 7.211102550927978
mat.sum(axis=0) # Somme par colonne