La méthode d'opération de l'objet de tableau à N dimensions (ci-après, ndarray) fourni par la bibliothèque de calcul numérique de Python "Numpy" est organisée. Python est lent à calculer en raison du manque de prise en charge des types de données, et il est standard d'utiliser ndarray lors du calcul avec une grande quantité de données.
import numpy as np
Générez un ndarray.
【array(object, dtype=None, copy=True, order='K', subok=False, ndmin=0)
】
y = np.array([1,2,3])
X = np.array([[1,2,3], [4,5,6]], dtype=np.int64)
【zeros(shape, dtype=float, order='C')
】
y = np.zeros(3)
# array([0., 0., 0.])
X = np.zeros((2,3))
# array([[0., 0., 0.],
# [0., 0., 0.]])
【ones(shape, dtype=None, order='C')
】
y = np.ones(3)
# array([1., 1., 1.])
X = np.ones((2,3))
# array([[1., 1., 1.],
# [1., 1., 1.]])
【empty(shape, dtype=None, order='C')
】
y = np.empty(3)
# array([0.39215686, 0.70980392, 0.80392157])
X = np.empty((2,3))
# array([[2.6885677e-316, 0.0000000e+000, 0.0000000e+000],
# [0.0000000e+000, 0.0000000e+000, 0.0000000e+000]])
【identity(n, dtype=None)
】
X = np.identity(3)
# array([[1., 0., 0.],
# [0., 1., 0.],
# [0., 0., 1.]])
【arange([start,] stop[, step,], dtype=None)
】
y = np.arange(5)
# array([0, 1, 2, 3, 4])
y = np.arange(2,5)
# array([2, 3, 4])
y = np.arange(5,2,-1)
# array([5, 4, 3])
【linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None)
】
y = np.linspace(2,10,3)
# array([ 2., 6., 10.])
y = np.linspace(2,10,5)
# array([ 2., 4., 6., 8., 10.])
【unique(ar, return_index=False, return_inverse=False, return_counts=False, axis=None)
】
y = np.unique(['C', 'E', 'C', 'A', 'B', 'E', 'D', 'C'])
# array(['A', 'B', 'C', 'D', 'E'], dtype='<U1')
Les éléments de ndarray peuvent être obtenus de la même manière que Get List Elements in Python. Cependant, notez que la méthode de spécification change pour les tableaux multidimensionnels.
X = [[1,2,3],[4,5,6]]
print(X[1][2])
# 6
X_numpy = np.array(X)
print(X_numpy[1,2])
# 6
En plus de la méthode d'acquisition habituelle, il peut également être acquis par la méthode suivante.
X = np.array([[1,2,3],[4,5,6]])
print(X[[0,1,-1]])
# [[1 2 3],
# [4 5 6],
# [4 5 6]]
print(X[[0,1,-1], 0])
# [1, 4, 4]
print(X[[0,1,-1], ::-1])
# [[3 2 1],
# [6 5 4],
# [6 5 4]]
print(X[[0,1,-1],[1,0,-1]])
# [2, 4, 6]
"""
* Cette désignation a le même résultat que la suivante.
np.array([X[0,1], X[1,0], X[-1,-1]])
"""
X = np.array([[1,2,3],[4,5,6]])
print(X > 4)
# [[False, False, False],
# [False, True, True]]
print(np.where(X > 4))
# (array([1, 1]), array([1, 2]))
print(X[X > 4])
# [5, 6]
print(X[np.where(X > 4)])
# [5, 6]
Obtenez des informations sur ndarray.
【shape
】
X = np.array([[1,2,3], [4,5,6]])
X.shape
# (2, 3)
【dtype
】
X = np.array([[1,2,3], [4,5,6]])
X.dtype
# dtype('int64')
【ndim
】
X = np.array([[1,2,3], [4,5,6]])
X.ndim
# 2
【size
】
X = np.array([[1,2,3], [4,5,6]])
X.size
# 6
Manipulez le ndarray.
【<ndarray>.flatten(order='C')
】
X = np.array([[1,2,3], [4,5,6]])
print(X.flatten())
# [1, 2, 3, 4, 5, 6]
【<ndarray>.reshape(shape, order='C')
】
X = np.array([1,2,3,4,5,6])
print(X.reshape((3,2)))
# [[1, 2],
# [3, 4],
# [5, 6]]
【sort(a, axis=-1, kind='quicksort', order=None)
】
X1 = np.array([[5,4,6], [3,1,2]])
X2 = np.sort(X1)
print(X1)
# [[5 4 6]
# [3 1 2]]
print(X2)
# [[4 5 6]
# [1 2 3]]
X3 = np.sort(X1, axis=0)
print(X3)
# [[3 1 2]
# [5 4 6]]
【<ndarray>.sort(axis=-1, kind='quicksort', order=None)
】
X = np.array([[5,4,6], [3,1,2]])
X.sort()
print(X)
# [4 5 6]
# [1 2 3]]
X.sort(axis=0)
print(X)
# [[3 1 2]
# [5 4 6]]
【<ndarray>.argsort(axis=-1, kind='quicksort', order=None)
】
X = np.array([[5,4,6], [3,1,2]])
idxs = X.argsort()
print(idxs)
# [[1 0 2]
# [1 2 0]]
idxs = X.argsort(axis=0)
print(idxs)
# [[1 1 1]
# [0 0 0]]
【<ndarray>.astype(dtype, order='K', casting='unsafe', subok=True, copy=True)
】
X = np.array([[1,2,3], [4,5,6]])
print(X.dtype)
# int64
print(X)
# [[1 2 3]
# [4 5 6]]
X2 = X.astype(np.float64)
print(X2.dtype)
# float64
print(X2)
# [[1. 2. 3.]
# [4. 5. 6.]]
Effectuez des opérations matricielles avec ndarray.
【<ndarray>.T
】
X = np.array([[1,2,3], [4,5,6]])
print(X.T)
# [[1, 4],
# [2, 5],
# [3, 6]]
【numpy.dot
】【<ndarray>.dot
】
X1 = np.array([[1, 2, 3], [4, 5, 6]])
X2 = np.array([[10, 20], [100, 200], [1000, 2000]])
print(np.dot(X1, X2))
# [[ 3210 6420]
# [ 6540 13080]]
print(X1.dot(X2))
# [[ 3210 6420]
# [ 6540 13080]]
【numpy.trace
】【<ndarray>.trace
】
X = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
print(np.trace(X))
# 15
print(X.trace())
# 15
【numpy.linalg.det
】
X1 = np.array([[3., 2.], [5., 4.]])
print(np.linalg.det(X1))
# 2.0000000000000013
【numpy.linalg.eig
】
X = np.array([[2, 0], [0, 0.25]])
print(np.linalg.eig(X))
# (array([2. , 0.25]), array([[1., 0.], [0., 1.]]))
【numpy.linalg.svd
】
X = np.array([[2, 0], [0, 0.25], [0, 1]])
print(np.linalg.svd(X))
# (array([[ 1., 0. , 0. ],
# [ 0., -0.24253563, -0.9701425 ],
# [ 0., -0.9701425 , 0.24253563]]),
# array([2., 1.03077641]),
# array([[ 1., 0.],
# [-0., -1.]]))
【numpy.linalg.inv
】
X = np.array([[1, 1], [1, -1]])
print(np.linalg.inv(X))
# [[ 0.5 0.5]
# [ 0.5 -0.5]]
【numpy.linalg.pinv
】
X = np.array([[1, 1], [1, -1], [1, 0]])
print(np.linalg.pinv(X))
# [[ 0.33333333 0.33333333 0.33333333]
# [ 0.5 -0.5 0. ]]
【numpy.linalg.qr
】
X = np.array([[1, 2], [3, 4], [5, 6]])
print(np.linalg.qr(X))
# (array([[-0.16903085, 0.89708523],
# [-0.50709255, 0.27602622],
# [-0.84515425, -0.34503278]]),
# array([[-5.91607978, -7.43735744],
# [ 0. , 0.82807867]]))
Les opérations arithmétiques sont effectuées entre deux ndarrays.
【add
】
a = np.array([[1, 2, 3], [4, 5, 6]])
print(np.add(a, 10))
# [[11 12 13]
# [14 15 16]]
print(a + 10)
# [[11 12 13]
# [14 15 16]]
print(np.add(a, [10, 20, 30]))
# [[11 22 33]
# [14 25 36]]
print(a + [10, 20, 30])
# [[11 22 33]
# [14 25 36]]
print(np.add(a, np.array([[10, 20, 30], [40, 50, 60]])))
# [[11 22 33]
# [44 55 66]]
print(a + np.array([[10, 20, 30], [40, 50, 60]]))
# [[11 22 33]
# [44 55 66]]
【subtract
】
a = np.array([[11, 22, 33], [44, 55, 66]])
print(np.subtract(a, 10))
# [[ 1 12 23]
# [34 45 56]]
print(a - 10)
# [[ 1 12 23]
# [34 45 56]]
print(np.subtract(a, [10, 20, 30]))
# [[ 1 2 3]
# [34 35 36]]
print(a - [10, 20, 30])
# [[ 1 2 3]
# [34 35 36]]
print(np.subtract(a, np.array([[10, 20, 30], [40, 50, 60]])))
# [[1 2 3]
# [4 5 6]]
print(a - np.array([[10, 20, 30], [40, 50, 60]]))
# [[1 2 3]
# [4 5 6]]
【multiply
】
a = np.array([[1, 2, 3], [4, 5, 6]])
print(np.multiply(a, 10))
# [[10 20 30]
# [40 50 60]]
print(a * 10)
# [[10 20 30]
# [40 50 60]]
print(np.multiply(a, [10, 20, 30]))
# [[ 10 40 90]
# [ 40 100 180]]
print(a * [10, 20, 30])
# [[ 10 40 90]
# [ 40 100 180]]
print(np.multiply(a, np.array([[10, 20, 30], [40, 50, 60]])))
# [[ 10 40 90]
# [160 250 360]]
print(a * np.array([[10, 20, 30], [40, 50, 60]]))
# [[ 10 40 90]
# [160 250 360]]
【divide
】
a = np.array([[10, 20, 30], [40, 50, 60]])
print(np.divide(a, 10))
# [[1. 2. 3.]
# [4. 5. 6.]]
print(a / 10)
# [[1. 2. 3.]
# [4. 5. 6.]]
print(np.divide(a, [10, 20, 30]))
# [[1. 1. 1. ]
# [4. 2.5 2. ]]
print(a / [10, 20, 30])
# [[1. 1. 1. ]
# [4. 2.5 2. ]]
print(np.divide(a, np.array([[10, 20, 30], [40, 50, 60]])))
# [[1. 1. 1.]
# [1. 1. 1.]]
print(a / np.array([[10, 20, 30], [40, 50, 60]]))
# [[1. 1. 1.]
# [1. 1. 1.]]
【floor_divide
】
a = np.array([[22, 33, 44], [45, 67, 89]])
print(np.floor_divide(a, 2))
# [[11 16 22]
# [22 33 44]]
print(a // 2)
# [[11 16 22]
# [22 33 44]]
print(np.floor_divide(a, [2, 3, 4]))
# [[11 11 11]
# [22 22 22]]
print(a // [2, 3, 4])
# [[11 11 11]
# [22 22 22]]
print(np.floor_divide(a, np.array([[2, 3, 4], [4, 6, 8]])))
# [[11 11 11]
# [11 11 11]]
print(a // np.array([[[2, 3, 4], [4, 6, 8]]]))
# [[11 11 11]
# [11 11 11]]
【mod
】
a = np.array([[22, 33, 44], [45, 67, 89]])
print(np.mod(a, 2))
# [[0 1 0]
# [1 1 1]]
print(a % 2)
# [[0 1 0]
# [1 1 1]]
print(np.mod(a, [2, 3, 4]))
# [[0 0 0]
# [1 1 1]]
print(a % [2, 3, 4])
# [[0 0 0]
# [1 1 1]]
print(np.mod(a, np.array([[2, 3, 4], [4, 6, 8]])))
# [[0 0 0]
# [1 1 1]]
print(a % np.array([[[2, 3, 4], [4, 6, 8]]]))
# [[0 0 0]
# [1 1 1]]
【power
】
a = np.array([[1, 2, 3], [4, 5, 6]])
print(np.power(a, 2))
# [[ 1 4 9]
# [16 25 36]]
print(a ** 2)
# [[ 1 4 9]
# [16 25 36]]
print(np.power(a, [2, 3, 4]))
# [[ 1 8 81]
# [ 16 125 1296]]
print(a ** [2, 3, 4])
# [[ 1 8 81]
# [ 16 125 1296]]
print(np.power(a, np.array([[2, 3, 4], [1/2, 1/3, 1/4]])))
# [[ 1. 8. 81. ]
# [ 2. 1.70997595 1.56508458]]
print(a ** np.array([[2, 3, 4], [1/2, 1/3, 1/4]]))
# [[ 1. 8. 81. ]
# [ 2. 1.70997595 1.56508458]]
Comparez deux ndarrays.
【greater
】
a1 = np.array([10, 10, 10, 10, 10])
a2 = np.array([9, 9.9, 10, 10.1, 11])
print(np.greater(a1, a2))
# [ True True False False False]
print(a1 > a2)
# [ True True False False False]
【greater_equal
】
a1 = np.array([10, 10, 10, 10, 10])
a2 = np.array([9, 9.9, 10, 10.1, 11])
print(np.greater_equal(a1, a2))
# [ True True True False False]
print(a1 >= a2)
# [ True True True False False]
【less
】
a1 = np.array([10, 10, 10, 10, 10])
a2 = np.array([9, 9.9, 10, 10.1, 11])
print(np.less(a1, a2))
# [False False False True True]
print(a1 < a2)
# [False False False True True]
【less_equal
】
a1 = np.array([10, 10, 10, 10, 10])
a2 = np.array([9, 9.9, 10, 10.1, 11])
print(np.less_equal(a1, a2))
# [False False True True True]
print(a1 <= a2)
# [False False True True True]
【equal
】
a1 = np.array([10, 10, 10, 10, 10])
a2 = np.array([9, 9.9, 10, 10.1, 11])
print(np.equal(a1, a2))
# [False False True False False]
print(a1 == a2)
# [False False True False False]
【not_equal
】
a1 = np.array([10, 10, 10, 10, 10])
a2 = np.array([9, 9.9, 10, 10.1, 11])
print(np.not_equal(a1, a2))
# [ True True False True True]
print(a1 != a2)
# [ True True False True True]
Effectuez des opérations logiques sur deux ndarrays.
【logical_and
】
a1 = np.array([True, False, True, False])
a2 = np.array([True, False, False, True])
print(np.logical_and(a1, a2))
# [ True False False False]
【logical_or
】
a1 = np.array([True, False, True, False])
a2 = np.array([True, False, False, True])
print(np.logical_or(a1, a2))
# [ True False True True]
【logical_xor
】
a1 = np.array([True, False, True, False])
a2 = np.array([True, False, False, True])
print(np.logical_xor(a1, a2))
# [False False True True]
【logical_not
】
a = np.array([True, False, True, False])
print(np.logical_not(a))
# [False True False True]
【<ndarray>.any()
】
a1 = np.array([0, 1, 0, 0])
print(a1.any())
# True
a2 = np.array([1, 1, 1, 1])
print(a2.any())
# True
a3 = np.array([0, 0, 0, 0])
print(a3.any())
# False
【<ndarray>.all()
】
a1 = np.array([0, 1, 0, 0])
print(a1.all())
# False
a2 = np.array([1, 1, 1, 1])
print(a2.all())
# True
a3 = np.array([0, 0, 0, 0])
print(a3.all())
# False
Effectuez une opération de définition avec ndarray.
【union1d(ar1, ar2)
】
X = np.unique([1,2,3,4,5,6])
Y = np.unique([4,5,6,7,8,9])
Z = np.union1d(X,Y)
print(Z)
# [1 2 3 4 5 6 7 8 9]
【intersect1d(ar1, ar2)
】
X = np.unique([1,2,3,4,5,6])
Y = np.unique([4,5,6,7,8,9])
Z = np.intersect1d(X,Y)
print(Z)
# [4 5 6]
【setxor1d(ar1, ar2)
】
X = np.unique([1,2,3,4,5,6])
Y = np.unique([4,5,6,7,8,9])
Z = np.setxor1d(X,Y)
print(Z)
# [1 2 3 7 8 9]
【setdiff1d(ar1, ar2)
】
X = np.unique([1,2,3,4,5,6])
Y = np.unique([4,5,6,7,8,9])
Z = np.setdiff1d(X,Y)
print(Z)
# [1 2 3]
【in1d(ar1, ar2)
】
X = np.unique([1,2,3,4,5])
Y = np.unique([1,2,3])
Z = np.in1d(X,Y)
print(Z)
# [ True True True False False]
Extrayez la valeur maximale, la valeur minimale et le code de ndarray.
【maximum
】
a1 = np.array([4, 2, np.nan])
a2 = np.array([3, 6, 5])
print(np.maximum(a1, a2))
# [ 4. 6. nan]
【fmax
】
a1 = np.array([4, 2, np.nan])
a2 = np.array([3, 6, 5])
print(np.fmax(a1, a2))
# [4. 6. 5.]
【minimum
】
a1 = np.array([4, 2, np.nan])
a2 = np.array([3, 6, 5])
print(np.minimum(a1, a2))
# [ 3. 2. nan]
【fmin
】
a1 = np.array([4, 2, np.nan])
a2 = np.array([3, 6, 5])
print(np.fmin(a1, a2))
# [3. 2. 5.]
【copysign
】
a1 = np.array([1, 2, 3, 4, 5])
a2 = np.array([0, -1, 1, 2, -2])
print(np.copysign(a1, a2))
# [ 1. -2. 3. 4. -5.]
Pour chaque valeur de ndarray, traitez la valeur après la virgule décimale.
【ceil(ndarray)
】
y = np.array([-5.6, -5.5, -5.4, 0, 5.4, 5.5, 5.6])
z = np.ceil(y)
print(z)
# [-5. -5. -5. 0. 6. 6. 6.]
【floor(ndarray)
】
y = np.array([-5.6, -5.5, -5.4, 0, 5.4, 5.5, 5.6])
z = np.floor(y)
print(z)
# [-6. -6. -6. 0. 5. 5. 5.]
【rint(ndarray)
】
y = np.array([-5.6, -5.5, -5.4, 0, 5.4, 5.5, 5.6])
z = np.rint(y)
print(z)
# [-6. -6. -5. 0. 5. 6. 6.]
【modf(ndarray)
】
y = np.array([-3.6, -2.5, -1.4, 0, 1.4, 2.5, 3.6])
z = np.modf(y)
print(z)
# (array([-0.6, -0.5, -0.4, 0. , 0.4, 0.5, 0.6]), array([-3., -2., -1., 0., 1., 2., 3.]))
Calculez le carré, la racine carrée et la valeur absolue de chaque valeur de ndarray.
【square(ndarray)
】
y = np.arange(1,6)
z = np.square(y)
print(z)
# [ 1 4 9 16 25]
z = y ** 2
print(z)
# [ 1 4 9 16 25]
【sqrt(ndarray)
】
y = np.array([1,4,9,16,25])
z = np.sqrt(y)
print(z)
# [1. 2. 3. 4. 5.]
z = y ** 0.5
print(z)
# [1. 2. 3. 4. 5.]
【abs(ndarray)
】
y = np.array([-1,-2,3,4,5j])
z = np.abs(y)
print(z)
# [1, 2, 3, 4, 5]
【fabs(ndarray)
】
y = np.array([-1,-2,3,4,-5])
z = np.fabs(y)
print(z)
# [1, 2, 3, 4, 5]
Calculez l'exposant et la valeur logarithmique pour chaque valeur de ndarray.
【exp(ndarray)
】
y = np.arange(1,6)
z = np.exp(y)
print(z)
# [ 2.71828183 7.3890561 20.08553692 54.59815003 148.4131591 ]
print(np.e)
# 2.718281828459045
【log(ndarray)
】
y = np.e ** np.array([1, 2, 3, 4, 5])
z = np.log(y)
print(z)
# [1. 2. 3. 4. 5.]
【log10(ndarray)
】
y = 10 ** np.array([1, 2, 3, 4, 5])
z = np.log10(y)
print(z)
# [1. 2. 3. 4. 5.]
【log2(ndarray)
】
y = 2 ** np.array([1, 2, 3, 4, 5])
z = np.log2(y)
print(z)
# [1. 2. 3. 4. 5.]
Appliquez une fonction triangulaire à chaque valeur de ndarray.
【sin(ndarray)
】
y = np.linspace(-1, 1, 5) * np.pi
print(np.sin(y))
# [-1.2246468e-16 -1.0000000e+00 0.0000000e+00 1.0000000e+00 1.2246468e-16]
【cos(ndarray)
】
y = np.linspace(-1, 1, 5) * np.pi
print(np.cos(y))
# [-1.000000e+00 6.123234e-17 1.000000e+00 6.123234e-17 -1.000000e+00]
【tan(ndarray)
】
y = np.linspace(-1, 1, 5) * np.pi
print(np.tan(y))
# [ 1.22464680e-16 -1.63312394e+16 0.00000000e+00 1.63312394e+16 -1.22464680e-16]
Appliquez une fonction bicurve à chaque valeur de ndarray.
【sinh(ndarray)
】
y = np.array([-np.inf, -2, -1, 0, 1, 2, np.inf])
print(np.sinh(y))
# [-inf -3.62686041 -1.17520119 0. 1.17520119 3.62686041 inf]
【cosh(ndarray)
】
y = np.array([-np.inf, -2, -1, 0, 1, 2, np.inf])
print(np.cosh(y))
# [inf 3.76219569 1.54308063 1. 1.54308063 3.76219569 inf]
【tanh(ndarray)
】
y = np.array([-np.inf, -2, -1, 0, 1, 2, np.inf])
print(np.tanh(y))
# [-1. -0.96402758 -0.76159416 0. 0.76159416 0.96402758 1.]
Appliquez la fonction de triangle inverse à chaque valeur de ndarray.
【arcsin(ndarray)
】
y = np.linspace(-1, 1, 5)
print(np.arcsin(y))
# [-1.57079633 -0.52359878 0. 0.52359878 1.57079633]
【arccos(ndarray)
】
y = np.linspace(-1, 1, 5)
print(np.arccos(y))
# [3.14159265 2.0943951 1.57079633 1.04719755 0.]
【arctan(ndarray)
】
y = np.linspace(-1, 1, 5)
print(np.arctan(y))
# [-0.78539816 -0.46364761 0. 0.46364761 0.78539816]
Appliquez la fonction bicurve inverse à chaque valeur de ndarray.
【arcsinh(ndarray)
】
y = np.array([-np.inf, -3.62686041, -1.17520119, 0., 1.17520119, 3.62686041, np.inf])
print(np.arcsinh(y))
# [-inf -2. -1. 0. 1. 2. inf]
【arccosh(ndarray)
】
y = np.array([1., 1.54308063, 3.76219569, np.inf])
print(np.arccosh(y))
# [ 0. 1. 2. inf]
【arctanh(ndarray)
】
y = np.array([-1., -0.96402758, -0.76159416, 0., 0.76159416, 0.96402758, 1.])
print(np.arctanh(y))
# [-inf -2. -1.00000001 0. 1.00000001 2. inf]
Générez des nombres aléatoires avec Numpy.
【numpy.random.rand(d0, d1, ..., dn)
】
print(np.random.rand())
# 0.5504876218756463
print(np.random.rand(2))
# [0.70029403 0.48969378]
print(np.random.rand(2, 2))
# [[0.98181874 0.47001957]
# [0.79277853 0.12536121]]
【numpy.random.randint(low, high=None, size=None, dtype='l')
】
print(np.random.randint(1,4))
# 2
print(np.random.randint(1,4, 2))
# [2 2]
print(np.random.randint(1,4, (2, 2)))
# [[3 2]
# [3 1]]
【numpy.random.uniform(low=0.0, high=1.0, size=None)
】
print(np.random.uniform(1,4))
# 3.5515484791542056
print(np.random.uniform(1,4, 2))
# [1.51270014 3.02435494]
print(np.random.uniform(1,4, (2, 2)))
# [[3.47188029 1.17177563]
# [3.87198389 3.91458379]]
【numpy.random.randn(d0, d1, ..., dn)
】
print(np.random.randn())
# -0.5775065521096695
print(np.random.randn(2))
# [-1.50501689 1.46743032]
print(np.random.randn(2, 2))
# [[ 1.16357112 -0.24601519]
# [ 2.07269576 -0.39272309]]
【numpy.random.normal(loc=0.0, scale=1.0, size=None)
】
print(np.random.normal(50, 10))
# 63.47995333571061
print(np.random.normal(50, 10, (2, 2)))
# [[56.02364177 55.25423891]
# [45.44840171 29.8303964 ]]
print(np.random.normal((50, 0), (10, 1), (5, 2)))
# [[45.48754234 -0.74792452]
# [60.84696747 1.01209036]
# [42.38844146 -0.10453915]
# [39.77544056 1.22264549]
# [41.60250782 1.64150462]]
【numpy.random.binomial(n, p, size=None)
】
print(np.random.binomial(100, 0.5))
# 53
print(np.random.binomial(100, 0.5, (2, 2)))
# [[53 53]
# [48 50]]
print(np.random.binomial((100, 1000), (0.3, 0.7), (5, 2)))
# [[ 33 699]
# [ 30 660]
# [ 34 698]
# [ 26 688]
# [ 25 683]]
【numpy.random.beta(a, b, size=None)
】
print(np.random.beta(3, 5))
# 0.09838262724430759
print(np.random.beta(8, 2, (2, 2)))
# [[0.92800788 0.86391443]
# [0.67249524 0.97299346]]
print(np.random.beta((3, 8), (5, 2), (5, 2)))
# [[0.11825463 0.74320634]
# [0.24992266 0.79029969]
# [0.13345269 0.57807883]
# [0.32374525 0.92666103]
# [0.64669681 0.84867388]]
【numpy.random.chisquare(df, size=None)
print(np.random.chisquare(1))
# 0.05074259859574817
print(np.random.chisquare(5, (2, 2)))
# [[ 6.15617206 5.54859677]
# [ 2.60704305 10.35079434]]
print(np.random.chisquare((1, 5), (5, 2)))
# [[2.3942405 6.43803251]
# [1.97544231 2.73456762]
# [0.63389405 7.81526263]
# [0.05035459 7.8224598 ]
# [1.01597309 1.46098368]]
【numpy.random.gamma(shape, scale=1.0, size=None)
】
print(np.random.gamma(1, 2))
# 0.48471788864900295
print(np.random.gamma(9, 1, (2, 2)))
# [[10.71101589 16.68686166]
# [ 5.22150652 5.87160223]]
print(np.random.gamma((1, 9), (2, 1), (5, 2)))
# [[ 3.4102224 6.31938602]
# [ 0.03882968 7.71108072]
# [ 2.62781738 10.70853193]
# [ 5.07929584 5.83489052]
# [ 1.50577929 11.21572879]]
Calculez les statistiques de base à partir de la valeur de ndarray.
【max
】
a = np.array([10, 20, 30, 40, 50])
print(np.max(a))
# 50
【argmax
】
a = np.array([10, 20, 30, 40, 50])
print(np.argmax(a))
# 4
【min
】
a = np.array([10, 20, 30, 40, 50])
print(np.min(a))
# 10
【argmin
】
a = np.array([10, 20, 30, 40, 50])
print(np.argmin(a))
# 0
【sum
】
a = np.array([10, 20, 30, 40, 50])
print(np.sum(a))
# 150
【mean
】
a = np.array([10, 20, 30, 40, 50])
print(np.mean(a))
# 30.0
【var
】
a = np.array([10, 20, 30, 40, 50])
print(np.var(a))
# 200.0
【std
】
a = np.array([10, 20, 30, 40, 50])
print(np.std(a))
# 14.142135623730951
【cumsum
】
a = np.array([10, 20, 30, 40, 50])
print(np.cumsum(a))
# [ 10 30 60 100 150]
【cumsum
】
a = np.array([10, 20, 30, 40, 50])
print(np.cumprod(a))
# [ 10 200 6000 240000 12000000]
Effectue le traitement de vérification de la valeur.
【isnan(ndarray)
】
y = np.array([np.nan, np.inf, 0, 1])
print(np.isnan(y))
# [ True False False False]
【isfinite(ndarray)
】
y = np.array([np.nan, np.inf, -np.inf, 0, 1])
print(np.isfinite(y))
# [False False False True True]
【isfin(ndarray)
】
y = np.array([np.nan, np.inf, -np.inf, 0, 1])
print(np.isinf(y))
# [False True True False False]
【sign(ndarray)
】
y = np.array([-np.inf, -5, 0, 5, np.inf])
print(np.sign(y))
# [-1. -1. 0. 1. 1.]
Cet article est un article sur la migration du blog "Notes techniques pour les ingénieurs de tâches". Le blog précédent sera supprimé.
Recommended Posts