Array
import numpy as np #Importer numpy en tant que np
np.array([1,2,3,4]) #liste de tableau
array([1, 2, 3, 4])
python_list = (1,2,3,4) #Liste de Python
python_list
(1, 2, 3, 4)
np.array([[1,2,3], [4,5,6],[7,8,9]]) #liste imbriquée de tableaux
array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
[[1,2,3], [4,5,6],[7,8,9]] #Liste des pythons imbriqués
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
arr1 = np.array([[1,2,3], [4,5,6],[7,8,9]])
arr2 = np.array([[1,2,3], [4,5,6],[7,8,9]])
arr1
array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
arr2
array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
arr1 + arr2
array([[ 2, 4, 6],
[ 8, 10, 12],
[14, 16, 18]])
arr1 - arr2
array([[0, 0, 0],
[0, 0, 0],
[0, 0, 0]])
arr1 * arr2
array([[ 1, 4, 9],
[16, 25, 36],
[49, 64, 81]])
arr1 / arr2
array([[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.]])
#Les éléments manquants du tableau[1,2,3]Est automatiquement complété par.(Broadcasting)
arr3= np.array([[1,2,3]])
arr4 = np.array([[1,2,3], [4,5,6],[7,8,9]])
arr3 + arr4
array([[ 2, 4, 6],
[ 5, 7, 9],
[ 8, 10, 12]])
arr3 - arr4
array([[ 0, 0, 0],
[-3, -3, -3],
[-6, -6, -6]])
arr = np.array([[1,2,3], [4,5,6],[7,8,9]])
#Vérifiez la taille de la matrice du tableau
arr.shape
(3, 3)
Indexing
ndarray = np.array([[1,2], [3,4],[5,6]])
ndarray
array([[1, 2],
[3, 4],
[5, 6]])
ndarray[1][0]
3
ndarray[1,0]
3
Slicing
arr = np.array([[1,2,3,4], [2,4,6,4], [3,5,7,4], [3,5,7,4]])
arr
array([[1, 2, 3, 4],
[2, 4, 6, 4],
[3, 5, 7, 4],
[3, 5, 7, 4]])
arr[0:4, 2:4]
array([[3, 4],
[6, 4],
[7, 4],
[7, 4]])
arr[:,2:] #Récupère tous les éléments après la deuxième colonne de toutes les lignes.
array([[3, 4],
[6, 4],
[7, 4],
[7, 4]])
arr = np.array([1,2,3,4,2,4,6,4,5,7,4,5,7,4])
arr[1:6:2] #Trancher en deux
array([2, 4, 4])
np.arange[(start,)stop(,step)]
np.arange(0,5,2) #[start, stop, step] #0~Liste de valeurs où 5 est séparé par 2
array([0, 2, 4])
np.arange(5) #l'étape de démarrage peut être omise. Si omis, 0 et 1 sont entrés par défaut, respectivement.
array([0, 1, 2, 3, 4])
np.arange(0,5,0.1) #l'étape est 0.1 étape est également possible
array([0. , 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1. , 1.1, 1.2,
1.3, 1.4, 1.5, 1.6, 1.7, 1.8, 1.9, 2. , 2.1, 2.2, 2.3, 2.4, 2.5,
2.6, 2.7, 2.8, 2.9, 3. , 3.1, 3.2, 3.3, 3.4, 3.5, 3.6, 3.7, 3.8,
3.9, 4. , 4.1, 4.2, 4.3, 4.4, 4.5, 4.6, 4.7, 4.8, 4.9])
np.linspace(start, stop, num=50)
np.linspace(1, 2, 20) #1~Divisez 2 en 20 parties égales
array([1. , 1.05263158, 1.10526316, 1.15789474, 1.21052632,
1.26315789, 1.31578947, 1.36842105, 1.42105263, 1.47368421,
1.52631579, 1.57894737, 1.63157895, 1.68421053, 1.73684211,
1.78947368, 1.84210526, 1.89473684, 1.94736842, 2. ])
.copy()
arr_copy = arr.copy()
arr_copy
array([0, 1, 2, 3, 4])
ndarray = np.arange(0, 5)
ndarray_copy = ndarray.copy()
print('original array is {}'.format(id(arr)))
print('copied array is {}'.format(id(arr))) #Celui copié est un objet différent de la source de la copie
original array is 140571396284208
copied array is 140571396284208
ndarray[:] = 100
print('original array:\n', ndarray) #Si vous ne copiez pas, la matrice d'origine sera mise à jour
print('copied array:\n', ndarray_copy) #La copie ne met pas à jour la matrice d'origine
original array:
[100 100 100 100 100]
copied array:
[0 1 2 3 4]
def add_world(hello):
hello += ' world'
return hello
h_str = 'hello'
h_list = ['h', 'e', 'l', 'l', 'o']
output_str = add_world(h_str)
output_list = add_world(h_list)
print('output_str: {}'.format(output_str)) #La chaîne est passée par valeur, alors changez-la directement
print('output_list: {}'.format(output_list)) #la liste est passée par référence, donc ne la modifiez pas directement
print('h_str: {}'.format(h_str))
print('h_list: {}'.format(h_list))
output_str: hello world
output_list: ['h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd']
h_str: hello
h_list: ['h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd']
def change_hundred(array):
array[0] = 100 #Si vous ne copiez pas, la matrice d'origine sera mise à jour
return array
def change_hundred_copy(array):
array_copy = array.copy() #La copie ne met pas à jour la matrice d'origine
array_copy[0] = 100
return array_copy
array_1 = np.arange(0, 4)
array_2 = np.arange(0,4)
output_array = change_hundred(array_1)
output_array_copy = change_hundred_copy(array_2)
print('original_array_1/n', array_1)
print('original_array2/n', array_2)
original_array_1/n [100 1 2 3]
original_array2/n [0 1 2 3]
np.zeros(shape)
shape = (2,3,5) #Matrice zéro
zeros = np.zeros(shape)
print(zeros)
[[[0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0.]]
[[0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0.]]]
np.ones(shape)
shape = (2,3,5)
ones_array_1 = np.ones(shape)
ones_array_2 = np.ones(4)
print(ones_array_1)
print(ones_array_2)
[[[1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1.]]
[[1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1.]]]
[1. 1. 1. 1.]
np.eye(N)
np.eye(3) #N *Matrice unitaire de N. Les composantes diagonales sont toutes 1.
array([[1., 0., 0.],
[0., 1., 0.],
[0., 0., 1.]])
np.random.rand()
random_float = np.random.rand() # 0~Renvoie un nombre aléatoire de 1.
random_1d = np.random.rand(3)
random_2d = np.random.rand(3, 4)
print('random_float:{}'.format(random_float))
print('random_1d:{}'.format(random_1d))
print('random_2d:{}'.format(random_2d))
random_float:0.48703321814513356/n
random_1d:[0.27824053 0.60682459 0.22001138]
random_2d:[[0.52285782 0.87272074 0.03123286 0.7921472 ]
[0.80209903 0.26478273 0.91139303 0.63077037]
[0.72151924 0.91234378 0.69355857 0.9969298 ]]
np.random.randn()
random_float = np.random.randn() #Les valeurs sont renvoyées à partir de la distribution normale standard 0-1.
random_1d = np.random.randn(3)
random_2d = np.random.randn(3, 4)
print('random_float:{}'.format(random_float))
print('random_1d:{}'.format(random_1d))
print('random_2d:{}'.format(random_2d))
random_float:-0.013248078417389888
random_1d:[-1.11606544 -0.24693187 0.17212059]
random_2d:[[-0.85896581 0.53993487 0.09458454 1.3505381 ]
[-1.83510873 0.09966918 -0.22293729 0.58218476]
[ 0.37403933 0.83349568 0.73407002 -0.32979339]]
np.random.randint(low[,high][,size])
np.random.randint(10, 50, size=(2,4,3)) #Renvoie un ndarray de la taille spécifiée au hasard à partir d'entiers supérieurs ou égaux à faible et inférieur à élevé. Cette fois, il s'agit de 2D 4 lignes 3 colonnes.
array([[[30, 14, 11],
[33, 24, 48],
[15, 43, 41],
[18, 26, 49]],
[[18, 22, 16],
[15, 10, 43],
[43, 48, 10],
[42, 29, 10]]])
.reshape(shape)
array = np.arange(0, 10)
print('array:{}'.format(array))
new_shape = (2, 5)
reshaped_array = array.reshape(new_shape)
print('reshaped array:{}'.format(reshaped_array))
print("reshaped array's is {}".format(reshaped_array.reshape))
print('original array is NOT changed:{}'.format(array))
array:[0 1 2 3 4 5 6 7 8 9]
reshaped array:[[0 1 2 3 4]
[5 6 7 8 9]]
reshaped array's is <built-in method reshape of numpy.ndarray object at 0x7fd9420d8030>
original array is NOT changed:[0 1 2 3 4 5 6 7 8 9]
normal_dist_mat = np.random.randn(5,5)
print(normal_dist_mat)
[[-1.51861663 -2.09873943 -0.99761607 0.95395101 -0.04577882]
[-0.81944941 0.54339984 0.45265577 -2.56369775 -1.8300719 ]
[ 0.63372482 -0.35763135 0.31683655 1.44185989 -1.2110421 ]
[-1.56200024 1.17061544 1.35721624 -0.46023814 0.3496441 ]
[ 1.09102475 -0.47551934 -0.75747612 0.34564251 1.62400795]]
print('max is')
print(normal_dist_mat.max())
max is
1.6240079549859363
print('argmax is')
print(normal_dist_mat.argmax())
argmax is
24
normal_dist_mat.flatten()[9]
-1.83007190063398
print('min is')
print(normal_dist_mat.min())
min is
-2.5636977453276137
print('argmin is')
print(normal_dist_mat.argmin())
argmax is
8
print('mean is')
print(normal_dist_mat.mean())
mean is
-0.1766919366579146
print('median is')
print(np.median(normal_dist_mat))
median is
-0.04577882007895756
print('standard deviation is')
print(normal_dist_mat.std())
standard deviation is
1.1634432893009636
print(normal_dist_mat.std()) #Toutes les fonctions ci-dessus sont np.Nom de la fonction(ndarray)Peut également être appelé par
1.1634432893009636
print(normal_dist_mat) #Si vous souhaitez rechercher les statistiques d'une ligne ou d'une colonne spécifique, spécifiez l'argument axe.
print('axis=0 > {}'.format(normal_dist_mat.max(axis=0))) #axis=Si 0 est spécifié, les statistiques de chaque colonne,
print('axis=1 > {}'.format(normal_dist_mat.max(axis=1))) #axis=Si 1 est spécifié, les statistiques de chaque ligne sont renvoyées.
[[-1.51861663 -2.09873943 -0.99761607 0.95395101 -0.04577882]
[-0.81944941 0.54339984 0.45265577 -2.56369775 -1.8300719 ]
[ 0.63372482 -0.35763135 0.31683655 1.44185989 -1.2110421 ]
[-1.56200024 1.17061544 1.35721624 -0.46023814 0.3496441 ]
[ 1.09102475 -0.47551934 -0.75747612 0.34564251 1.62400795]]
axis=0 > [1.09102475 1.17061544 1.35721624 1.44185989 1.62400795]
axis=1 > [0.95395101 0.54339984 1.44185989 1.35721624 1.62400795]
np.exp(ndarray)
ndarray = np.linspace(-3,3,10)
expndarray = np.exp(ndarray)
print(ndarray)
print(expndarray)
[-3. -2.33333333 -1.66666667 -1. -0.33333333 0.33333333
1. 1.66666667 2.33333333 3. ]
[ 0.04978707 0.09697197 0.1888756 0.36787944 0.71653131 1.39561243
2.71828183 5.29449005 10.3122585 20.08553692]
np.log(nd.array)
ndarray = np.linspace(-3,3,10)
logndarray = np.log(ndarray)
print(ndarray)
print(logndarray) #Les valeurs négatives ne peuvent pas être logarithmiques, donc nan.
[-3. -2.33333333 -1.66666667 -1. -0.33333333 0.33333333
1. 1.66666667 2.33333333 3. ]
[ nan nan nan nan nan -1.09861229
0. 0.51082562 0.84729786 1.09861229]
/opt/anaconda3/lib/python3.7/site-packages/ipykernel_launcher.py:2: RuntimeWarning: invalid value encountered in log
print(logndarray[0])
print('nan ==None? {}'.format(logndarray[0] is None))
nan
nan ==None? False
np.isnan(logndarray[0])
True
np.e
print(np.e)
print(np.log(np.e))
2.718281828459045
1.0
ndarray = np.array([[1,2,3], [4,5,6], [7,8,9]])
print('array is :{}'.format(ndarray))
print("ndarray's shape is :{}".format(ndarray.shape)) #Le nombre de dimensions est appelé rang.
array is :[[1 2 3]
[4 5 6]
[7 8 9]]
ndarray's shape is :(3, 3)
np.expand_dims(nd.array, axis)
expanded_ndarray = np.expand_dims(ndarray, axis=0) #Ajouter une dimension
expanded_ndarray.shape
(1, 3, 3)
np.squeeze(ndarray)
squeezed_ndarray = np.squeeze(expanded_ndarray) #Éliminez une dimension de forme.
squeezed_ndarray.shape
(3, 3)
.flatten()
flatten_array = ndarray.flatten() #Faire une rangée
print('flatten_array is :{}'.format(flatten_array))
print('ndarray is :{}'.format(ndarray))
flatten_array is :[1 2 3 4 5 6 7 8 9]
ndarray is :[[1 2 3]
[4 5 6]
[7 8 9]]
ndarray = np.array([
[1,2,3,4],
[10,20,30,40],
[100,200,300,400]
])
np.save('saved_numpy', ndarray)
loaded_numpy = np.load('saved_numpy.npy')
loaded_numpy
array([[ 1, 2, 3, 4],
[ 10, 20, 30, 40],
[100, 200, 300, 400]])
Recommended Posts