Tableau à 1 dimension
import numpy as np
sample_ndarray_1d = np.array([1,2,3,4,5])
print("sample_ndarray_1d: ", sample_ndarray_1d)
sample_ndarray_1d: [1 2 3 4 5]
Un tableau bidimensionnel
import numpy as np
sample_ndarray_2d = np.array([[1,2,3,4,5],[6,7,8,9,10]])
print("sample_ndarray_2d: ", sample_ndarray_2d)
sample_ndarray_2d: [[ 1 2 3 4 5]
Tableau à 1 dimension
import numpy as np
sample_ndarray_1d = np.array([1,2,3,4,5])
print("sample_ndarray_1d[0]: ", sample_ndarray_1d[0])
print("sample_ndarray_1d[1]: ", sample_ndarray_1d[1])
print("sample_ndarray_1d[2]: ", sample_ndarray_1d[2])
print("sample_ndarray_1d[3]: ", sample_ndarray_1d[3])
print("sample_ndarray_1d[4]: ", sample_ndarray_1d[4])
print("sample_ndarray_1d[-1]: ", sample_ndarray_1d[-1])
sample_ndarray_1d[0]: 1
sample_ndarray_1d[1]: 2
sample_ndarray_1d[2]: 3
sample_ndarray_1d[3]: 4
sample_ndarray_1d[4]: 5
sample_ndarray_1d[-1]: 5
Un tableau bidimensionnel
import numpy as np
sample_ndarray_2d = np.array([[1,2,3,4,5],[6,7,8,9,10]])
print("sample_ndarray_2d[0,0]: ", sample_ndarray_2d[0,0])
print("sample_ndarray_2d[0,1]: ", sample_ndarray_2d[0,1])
print("sample_ndarray_2d[0,2]: ", sample_ndarray_2d[0,2])
print("sample_ndarray_2d[0,3]: ", sample_ndarray_2d[0,3])
print("sample_ndarray_2d[0,4]: ", sample_ndarray_2d[0,4])
print("sample_ndarray_2d[1,0]: ", sample_ndarray_2d[1,0])
print("sample_ndarray_2d[1,1]: ", sample_ndarray_2d[1,1])
print("sample_ndarray_2d[1,2]: ", sample_ndarray_2d[1,2])
print("sample_ndarray_2d[1,3]: ", sample_ndarray_2d[1,3])
print("sample_ndarray_2d[1,4]: ", sample_ndarray_2d[1,4])
sample_ndarray_2d[0,0]: 1
sample_ndarray_2d[0,1]: 2
sample_ndarray_2d[0,2]: 3
sample_ndarray_2d[0,3]: 4
sample_ndarray_2d[0,4]: 5
sample_ndarray_2d[1,0]: 6
sample_ndarray_2d[1,1]: 7
sample_ndarray_2d[1,2]: 8
sample_ndarray_2d[1,3]: 9
sample_ndarray_2d[1,4]: 10
Tableau à 1 dimension
import numpy as np
sample_ndarray_1d = np.array([1,2,3,4,5])
print("sample_ndarray_1d: ", sample_ndarray_1d)
sample_ndarray_1d: [1 2 3 4 5]
start_id = 1
end_id = 3
print("Numéros d'article "1" à "3":",sample_ndarray_1d[start_id:end_id+1])
print("Numéro d'article "Premier" à "3":",sample_ndarray_1d[:end_id+1])
print("Numéro d'article "1" à "dernier":",sample_ndarray_1d[start_id:])
print("Tous les éléments:",sample_ndarray_1d[:])
Numéros d'article "1" à "3":[2 3 4]
Numéro d'article "Premier" à "3":[1 2 3 4]
Numéro d'article "1" à "dernier":[2 3 4 5]
Tous les éléments:[1 2 3 4 5]
Tableau à 1 dimension
import numpy as np
sample_ndarray_1d = np.array([1,2,3,4,5])
print("Longueur du tableau:",len(sample_ndarray_1d))
Longueur du tableau: 5
Un tableau bidimensionnel
import numpy as np
sample_ndarray_2d = np.array([[1,2,3,4,5],[6,7,8,9,10]])
print("Longueur de la liste des parents:",len(sample_ndarray_2d)) ###Liste des parents
print("Longueur de la première liste enfant:",len(sample_ndarray_2d[0])) ###Première liste d'enfants
Longueur de la liste des parents: 2
Longueur de la première liste enfant: 5
Tableau à 1 dimension
import numpy as np
sample_ndarray_1d = np.array([1,2,3,4,5])
print("Taille du tableau:",sample_ndarray_1d.shape)
Taille du tableau:(5,)
Un tableau bidimensionnel
import numpy as np
sample_ndarray_2d = np.array([[1,2,3,4,5],[6,7,8,9,10]])
print("Taille du tableau:",sample_ndarray_2d.shape)
Taille du tableau:(2, 5)
Tableau à 1 dimension
import numpy as np
sample_ndarray_1d = np.array([1,2,3,4,5])
print("Nombre de dimensions:",sample_ndarray_1d.ndim)
Nombre de dimensions: 1
Un tableau bidimensionnel
import numpy as np
sample_ndarray_2d = np.array([[1,2,3,4,5],[6,7,8,9,10]])
print("Nombre de dimensions:",sample_ndarray_2d.ndim)
Nombre de dimensions: 2
Tableau à 1 dimension
import numpy as np
sample_ndarray_1d = np.array([1,2,3,4,5])
print("Nombre d'éléments:",sample_ndarray_1d.size)
Nombre d'éléments: 5
Un tableau bidimensionnel
import numpy as np
sample_ndarray_2d = np.array([[1,2,3,4,5],[6,7,8,9,10]])
print("Nombre d'éléments:",sample_ndarray_2d.size)
Nombre d'éléments: 10
Tableau à 1 dimension
import numpy as np
sample_ndarray = np.array([1,2,3,4,5])
print("Type de stockage:",type(sample_ndarray))
Type de stockage:<class 'numpy.ndarray'>
Tableau à 1 dimension
import numpy as np
sample_ndarray = np.array([1,2,3,4,5])
print("Type de données d'élément:",sample_ndarray.dtype)
Type de données d'élément: int32
Tableau à 1 dimension
import numpy as np
sample_ndarray_1d = np.array([1,2,3,4,5])
print("sample_ndarray_1d: ",sample_ndarray_1d)
sample_ndarray_1d: [1 2 3 4 5]
deleted_ndarray_1d = np.delete(sample_ndarray_1d,2,0)
print("deleted_ndarray_1d: ",deleted_ndarray_1d)
deleted_ndarray_1d: [1 2 4 5]
Un tableau bidimensionnel[Supprimer: ligne]
import numpy as np
sample_ndarray_2d = np.array([[1,2,3,4,5],[6,7,8,9,10]])
print("sample_ndarray_2d: ", sample_ndarray_2d)
sample_ndarray_2d: [[ 1 2 3 4 5]
[ 6 7 8 9 10]]
deleted_ndarray_2d = np.delete(sample_ndarray_2d,0,0)
print("deleted_ndarray_2d: ",deleted_ndarray_2d)
deleted_ndarray_2d: [[ 6 7 8 9 10]]
Un tableau bidimensionnel[Supprimer: Colonne]
import numpy as np
sample_ndarray_2d = np.array([[1,2,3,4,5],[6,7,8,9,10]])
print("sample_ndarray_2d: ", sample_ndarray_2d)
sample_ndarray_2d: [[ 1 2 3 4 5]
[ 6 7 8 9 10]]
deleted_ndarray_2d = np.delete(sample_ndarray_2d,2,1)
print("deleted_ndarray_2d: ",deleted_ndarray_2d)
deleted_ndarray_2d: [[ 1 2 4 5]
[ 6 7 9 10]]
Tableau à 1 dimension
import numpy as np
A = np.array([1,2,3])
B = np.array([4,5,6])
C = np.concatenate((A,B),axis=0)
print("joint ndarray (Horizontal): ", C)
joint ndarray (Horizontal): [1 2 3 4 5 6]
Un tableau bidimensionnel
import numpy as np
A = np.array([[1,2,3],[4,5,6]])
B = np.array([[7,8,9],[10,11,12]])
C = np.concatenate((A,B),axis=0)
print("joint ndarray (Vertical): ", C)
joint ndarray (Vertical): [[ 1 2 3]
[ 4 5 6]
[ 7 8 9]
[10 11 12]]
D = np.concatenate((A,B),axis=1)
print("joint ndarray (Horizontal): ", D)
joint ndarray (Horizontal): [[ 1 2 3 7 8 9]
[ 4 5 6 10 11 12]]
Tableau à 1 dimension
import numpy as np
A = np.array([1,2,3])
B = np.array([4,5,6])
C = np.block([[A,B]])
print("joint ndarray (Vertical): ", C)
joint ndarray (Vertical): [[1 2 3]
[4 5 6]]
D = np.block([[A],[B]])
print("joint ndarray (Horizontal): ", D)
joint ndarray (Horizontal): [1 2 3 4 5 6]
Un tableau bidimensionnel
import numpy as np
A = np.array([[1,2,3],[4,5,6]])
B = np.array([[7,8,9],[10,11,12]])
C = np.block([[A],[B]])
print("joint ndarray (Vertical): ", C)
joint ndarray (Vertical): [[ 1 2 3]
[ 4 5 6]
[ 7 8 9]
[10 11 12]]
D = np.block([[A,B]])
print("joint ndarray (Horizontal): ", D)
joint ndarray (Horizontal): [[ 1 2 3 7 8 9]
[ 4 5 6 10 11 12]]
Tableau à 1 dimension
import numpy as np
A = np.array([1,2,3])
B = np.array([4,5,6])
C = np.vstack((A,B))
print("joint ndarray (Vertical): ", C)
joint ndarray (Vertical): [[1 2 3]
[4 5 6]]
D = np.hstack((A,B))
print("joint ndarray (Horizontal): ", D)
joint ndarray (Horizontal): [1 2 3 4 5 6]
Un tableau bidimensionnel
import numpy as np
A = np.array([[1,2,3],[4,5,6]])
B = np.array([[7,8,9],[10,11,12]])
C = np.vstack((A,B))
print("joint ndarray (Vertical): ", C)
joint ndarray (Vertical): [[ 1 2 3]
[ 4 5 6]
[ 7 8 9]
[10 11 12]]
D = np.hstack((A,B))
print("joint ndarray (Horizontal): ", D)
joint ndarray (Horizontal): [[ 1 2 3 7 8 9]
[ 4 5 6 10 11 12]]
Un tableau bidimensionnel
import numpy as np
A = np.array([[1,2,3],[4,5,6]])
B = np.array([[7,8,9],[10,11,12]])
C = np.dstack((A,B))
print("stack ndarray: ", C)
stack ndarray: [[[ 1 7]
[ 2 8]
[ 3 9]]
[[ 4 10]
[ 5 11]
[ 6 12]]]
print("Taille du tableau:",C.shape)
print("Nombre de dimensions:",C.ndim)
Taille du tableau:(2, 3, 2)
Nombre de dimensions: 3
Un tableau bidimensionnel
import numpy as np
A = np.array([[1,2,3],[4,5,6]])
B = np.array([[7,8,9],[10,11,12]])
C = np.stack((A,B))
print("stack ndarray: ", C)
stack ndarray: [[[ 1 2 3]
[ 4 5 6]]
[[ 7 8 9]
[10 11 12]]]
print("Taille du tableau:",C.shape)
print("Nombre de dimensions:",C.ndim)
Taille du tableau:(2, 2, 3)
Nombre de dimensions: 3
Tableau à 1 dimension
import numpy as np
sample_ndarray_1d = np.array([1,2,3,4,5,6])
print("sample_ndarray_1d: ",sample_ndarray_1d)
sample_ndarray_1d: [1 2 3 4 5 6]
split_ndarray_1d = np.split(sample_ndarray_1d,2)
print("split_ndarray_1d[0]: ",split_ndarray_1d[0])
print("split_ndarray_1d[1]: ",split_ndarray_1d[1])
split_ndarray_1d[0]: [1 2 3]
split_ndarray_1d[1]: [4 5 6]
Un tableau bidimensionnel[Split: ligne]
import numpy as np
sample_ndarray_2d = np.array([[1,2,3,4,5,6],[7,8,9,10,11,12]])
print("sample_ndarray_2d: ",sample_ndarray_2d)
sample_ndarray_2d: [[ 1 2 3 4 5 6]
[ 7 8 9 10 11 12]]
split_ndarray_2d = np.split(sample_ndarray_2d,2,axis=0)
print("split_ndarray_2d[0]: ",split_ndarray_2d[0])
print("split_ndarray_2d[1]: ",split_ndarray_2d[1])
split_ndarray_2d[0]: [[1 2 3 4 5 6]]
split_ndarray_2d[1]: [[ 7 8 9 10 11 12]]
Un tableau bidimensionnel[Split: Colonne]
import numpy as np
sample_ndarray_2d = np.array([[1,2,3,4,5,6],[7,8,9,10,11,12]])
print("sample_ndarray_2d: ",sample_ndarray_2d)
sample_ndarray_2d: [[ 1 2 3 4 5 6]
[ 7 8 9 10 11 12]]
split_ndarray_2d = np.split(sample_ndarray_2d,2,axis=1)
print("split_ndarray_2d[0]: ",split_ndarray_2d[0])
print("split_ndarray_2d[1]: ",split_ndarray_2d[1])
split_ndarray_2d[0]: [[1 2 3]
[7 8 9]]
split_ndarray_2d[1]: [[ 4 5 6]
[10 11 12]]
Un tableau bidimensionnel[Split: ligne]
import numpy as np
sample_ndarray_2d = np.array([[1,2,3,4,5,6],[7,8,9,10,11,12]])
print("sample_ndarray_2d: ",sample_ndarray_2d)
sample_ndarray_2d: [[ 1 2 3 4 5 6]
[ 7 8 9 10 11 12]]
split_ndarray_2d = np.vsplit(sample_ndarray_2d,2)
print("split_ndarray_2d[0]: ",split_ndarray_2d[0])
print("split_ndarray_2d[1]: ",split_ndarray_2d[1])
split_ndarray_2d[0]: [[1 2 3 4 5 6]]
split_ndarray_2d[1]: [[ 7 8 9 10 11 12]]
Un tableau bidimensionnel[Split: Colonne]
import numpy as np
sample_ndarray_2d = np.array([[1,2,3,4,5,6],[7,8,9,10,11,12]])
print("sample_ndarray_2d: ",sample_ndarray_2d)
sample_ndarray_2d: [[ 1 2 3 4 5 6]
[ 7 8 9 10 11 12]]
split_ndarray_2d = np.hsplit(sample_ndarray_2d,2)
print("split_ndarray_2d[0]: ",split_ndarray_2d[0])
print("split_ndarray_2d[1]: ",split_ndarray_2d[1])
split_ndarray_2d[0]: [[1 2 3]
[7 8 9]]
split_ndarray_2d[1]: [[ 4 5 6]
[10 11 12]]
Matrice 3D
import numpy as np
sample_ndarray_3d = np.array([[[1,2],[3,4]],[[5,6],[7,8]]])
print("sample_ndarray_3d: ",sample_ndarray_3d)
sample_ndarray_3d: [[[1 2]
[3 4]]
[[5 6]
[7 8]]]
split_ndarray_3d = np.dsplit(sample_ndarray_3d,2)
print("split_ndarray_3d[0]: ",split_ndarray_3d[0])
print("split_ndarray_3d[1]: ",split_ndarray_3d[1])
split_ndarray_3d[0]: [[[1]
[3]]
[[5]
[7]]]
split_ndarray_3d[1]: [[[2]
[4]]
[[6]
[8]]]
Tableau à 1 dimension
import numpy as np
zero_ndarray_1d = np.zeros(5)
print("zero_ndarray_1d: ",zero_ndarray_1d)
zero_ndarray_1d: [0. 0. 0. 0. 0.]
Un tableau bidimensionnel
import numpy as np
zero_ndarray_2d = np.zeros((2,3))
print("zero_ndarray_2d: ",zero_ndarray_2d)
zero_ndarray_2d: [[0. 0. 0.]
[0. 0. 0.]]
Copie de taille de tableau
import numpy as np
sample_ndarray = np.array([[1,2,3,4,5],[6,7,8,9,10]])
zero_ndarray = np.zeros_like(sample_ndarray)
print("zero ndarray: ",zero_ndarray)
zero ndarray: [[0 0 0 0 0]
[0 0 0 0 0]]
Réseau d'unités[Décalage: aucun]
import numpy as np
identity_ndarray = np.eye(3)
print("identity ndarray: ",identity_ndarray)
identity ndarray: [[1. 0. 0.]
[0. 1. 0.]
[0. 0. 1.]]
Réseau d'unités[Offset: Oui]
import numpy as np
offset = 1
identity_ndarray = np.eye(3,k=offset)
print("identity ndarray: ",identity_ndarray)
identity ndarray: [[0. 1. 0.]
[0. 0. 1.]
[0. 0. 0.]]
import numpy as np
offset = -1
identity_ndarray = np.eye(3,k=offset)
print("identity ndarray: ",identity_ndarray)
identity ndarray: [[0. 0. 0.]
[1. 0. 0.]
[0. 1. 0.]]
Réseau d'unités[Décalage: aucun]
import numpy as np
identity_ndarray = np.identity(3)
print("identity ndarray: ",identity_ndarray)
Tableau à 1 dimension
import numpy as np
empty_ndarray_1d = np.empty(5)
print("empty_ndarray_1d: ",empty_ndarray_1d)
empty_ndarray_1d: [4.94065646e-324 4.94065646e-324 0.00000000e+000 0.00000000e+000
1.32373351e+079]
Un tableau bidimensionnel
import numpy as np
empty_ndarray_2d = np.empty((2,3))
print("empty_ndarray_2d: ",empty_ndarray_2d)
empty_ndarray_2d: [[0. 0. 0.]
[0. 0. 0.]]
Tableau à 1 dimension
import numpy as np
specified_value = 10
array_size = 5
specified_value_ndarray = np.empty(array_size)
specified_value_ndarray.fill(specified_value)
print("specified_value_ndarray: ",specified_value_ndarray)
specified_value_ndarray: [10. 10. 10. 10. 10.]
Un tableau bidimensionnel
import numpy as np
specified_value = 3
array_size = (2,3)
specified_value_ndarray = np.empty(array_size)
specified_value_ndarray.fill(specified_value)
print("specified_value_ndarray: ",specified_value_ndarray)
specified_value_ndarray: [[3. 3. 3.]
[3. 3. 3.]]
Tableau à 1 dimension
start_num = 1
end_num = 10
difference = 1
arithmetic_sequence_ndarray = np.array(range(start_num, end_num + 1, difference))
print("arithmetic sequence ndarray: ",arithmetic_sequence_ndarray)
Recommended Posts