Академический Документы
Профессиональный Документы
Культура Документы
import numpy as np
np.full(shape,fill_value, dtype)
e.g
# Create a 3x5 array filled with 3.14
np.full((3, 5), 3.14)
# Create an array of any no. of values evenly spaced between given numbers
np.linspace(start,stop, total_values, dtype)
e.g
# Create an array of five values evenly spaced between 0 and 1
np.linspace(0, 1, 5)
array([ 0. , 0.25, 0.5 , 0.75, 1. ])
x = np.arange(10)
x = array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
Multidimensional Subarrays
x2
array([[12, 5, 2, 4],
[ 7, 6, 8, 8],
[ 1, 6, 7, 7]])
-------------------------------
np.concatenate(list,list,axis)
e.g
x = np.array([1, 2, 3])
y = np.array([3, 2, 1])
np.concatenate([x, y])
array([1, 2, 3, 3, 2, 1])
#For working with arrays of mixed dimensions, it can be clearer to use the
np.vstack
(vertical stack) and np.hstack (horizontal stack) functions:
e.g
x = np.array([1, 2, 3])
grid = np.array([[9, 8, 7],
[6, 5, 4]])
print(lower)
[[ 8 9 10 11]
[12 13 14 15]]
print(right)
[[ 2 3]
[ 6 7]
[10 11]
[14 15]]
----------------------------------
Ufunc Features IN Numpy
np.add(list,list,out = container_name)
np.subtract(array,array)
e.g
x = np.arange(5)
y = np.empty(5)
np.multiply(x, 10, out=y)
print(y)
[ 0. 10. 20. 30. 40.]
y = np.zeros(10)
np.power(2, x, out=y[::2])
print(y)
[ 1. 0. 2. 0. 4. 0. 8. 0. 16. 0.]
-------------------------------
Aggregation of Arrays
x = np.arange(1, 6)
np.add.reduce(x)
Output:15
np.multiply.reduce(x)
Output: 120
np.add.accumulate(x)
Output: array([ 1, 3, 6, 10, 15])
np.multiply.accumulate(x)
Output: array([ 1, 2, 6, 24, 120])
----------------------------------
Functions NaN-safe-Version Description
np.sum np.nansum Compute sum of elements
np.prod np.nanprod Compute product of elements
np.mean np.nanmean Compute median of elements
np.std np.nanstd Compute standard deviation
np.var np.nanvar Compute variance
np.min np.nanmin Find minimum value
np.max np.nanmax Find maximum value
np.argmin np.nanargmin Find index of minimum value
np.argmax np.nanargmax Find index of maximum value
np.median np.nanmedian Compute median of elements
np.percentile np.nanpercentile Compute rank-based statistics of elements
np.any N/A Evaluate whether any elements are true
np.all N/A Evaluate whether all elements are true
x == 3 # equal
Output: array([False, False, True, False, False], dtype=bool)
(2 * x) == (x ** 2)
Output: array([False, True, False, False, False], dtype=bool)
------------------------------
Operator Equivalent ufunc
== np.equal
!= np.not_equal
< np.less
<= np.less_equal
> np.greater
>= np.greater_equal
----------------------------