This paper mainly introduces NumPy The creation of multidimensional arrays , more Python Advanced Series , Please refer to Python Advanced learning Play data series
Summary :
numpy.empty Method to create a specified shape (shape)、 data type (dtype) And uninitialized array
empty(shape, dtype=float, order=‘C’)
give an example :
import numpy as np
array_default = np.empty (shape = (2,3))
array_default_int = np.empty (shape = (2,3), dtype=int)
array_float_16 = np.empty (shape = (2,3), dtype = np.float16, order = 'F')
array_float = np.empty (shape = (2,3), dtype = float, order = 'F')
print("array_default:{}\n".format(array_default))
print("array_default_int:{}\n".format(array_default_int))
print("array_float_16:{}\n".format(array_float_16))
print("array_float:{}\n".format(array_float))
Output :
Be careful
, Array elements are random values , Because they are not initialized , So the value will be different every time , That is to say, you need to set the value manually .
array_default:[[9.34609111e-307 3.56043054e-307 1.11261027e-306]
[2.33646845e-307 3.44898841e-307 3.22646744e-307]]
array_default_int:[[ 924184752 32765 924189296]
[ 32765 537536802 1042292768]]
array_float_16:[[0.000e+00 1.132e-06 3.731e-05]
[1.229e+04 0.000e+00 0.000e+00]]
array_float:[[1.24610723e-306 1.29060871e-306 9.34604358e-307]
[1.37962320e-306 1.11258446e-306 1.44635488e-307]]
The storage of a multidimensional array in memory is one-dimensional linear continuous storage :
● Fortran-style (column-wise): The cache is optimized by Column
Access priority , Press Column
Sequential storage of data
● C-style (row-major): The cache is optimized by That's ok
Access priority , Press That's ok
Sequential storage of data
Performance impact :
Choosing different storage methods will affect the quantitative calculation performance of elements in the array .
for example : Calculate an array Each row Sum of elements of
Then press That's ok The performance of storage must be better than that of pressing Column Storage performance
for example : Calculate an array Each column Sum of elements of
Then press Column The performance of storage must be better than that of pressing That's ok Storage performance
N
-> Row number ; M
-> Number of columns ; k
-> Diagonal or diagonal index;np.arange(start, stop, step, dtype) Use arange Function to create a numeric range ; start
: Starting value , The default is 0; stop
: Termination value ( It doesn't contain );step
: step , The default is 1;dtype
: return ndarray Data type of , If not provided , The type of input data will be used .np.linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None) Create a one-dimensional array , An array is a sequence of arithmetical numbers . start
: The starting value of the sequence ; stop
: The end value of the sequence ; num
: Number of equal step samples to generate , The default is 50;endpoint
: The value is true when , The sequence contains stop value , On the contrary, it does not include , The default is True;retstep
: If True when , The resulting array shows the spacing , On the contrary, it does not show ;dtype
:ndarray Data type of give an example :
import numpy as np
aray_zeros = np.zeros(shape = (2,5), dtype = int, order = 'F')
array_ones = np.ones(shape = (2,5), dtype = float, order = 'C')
array_full = np.full(shape = (3, 5), fill_value=3.14, dtype = float, order = 'F')
array_eye_1 = np.eye(3)
array_eye_2 = np.eye(N=4, M=5, k=1, dtype=int, order='C')
array_arange_1 = np.arange(5)
array_arange_2 = np.arange(1, 10, 2)
array_lines_1 = np.linspace(2.0, 3.0, num=5)
array_lines_2 = np.linspace(2.0, 3.0, num=5, retstep=True)
print("aray_zeros:{}\n".format(aray_zeros))
print("array_ones:{}\n".format(array_ones))
print("array_full:{}\n".format(array_full))
print("array_eye_1:{}\n".format(array_eye_1))
print("array_eye_2:{}\n".format(array_eye_2))
print("array_arange_1:{}\n".format(array_arange_1))
print("array_arange_2:{}\n".format(array_arange_2))
print("array_lines_1:{}\n".format(array_lines_1))
print("array_lines_2:{}\n".format(array_lines_2))
Output :
aray_zeros:[[0 0 0 0 0]
[0 0 0 0 0]]
array_ones:[[1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1.]]
array_full:[[3.14 3.14 3.14 3.14 3.14]
[3.14 3.14 3.14 3.14 3.14]
[3.14 3.14 3.14 3.14 3.14]]
array_eye_1:[[1. 0. 0.]
[0. 1. 0.]
[0. 0. 1.]]
array_eye_2:[[0 1 0 0 0]
[0 0 1 0 0]
[0 0 0 1 0]
[0 0 0 0 1]]
array_arange_1:[0 1 2 3 4]
array_arange_2:[1 3 5 7 9]
array_lines_1:[2. 2.25 2.5 2.75 3. ]
array_lines_2:(array([2. , 2.25, 2.5 , 2.75, 3. ]), 0.25)
give an example :
import numpy as np
array_from_list_1 = np.array ( ['NumPy', 'supports', 'vectorized', 'operations'])
array_from_list_2 = np.array ([[1, 2, 3], [4, 5, 6]])
array_from_list_3 = np.array ( [range(i, i+3) for i in [2, 4, 6]])
array_from_tuple = np.array((1, 2, 3))
array_from_set = np.array({
1, 2, 2, 3})
array_from_dict = np.array({
'language' : 'Python', "Hobby":"Reading"})
print("array_from_list_1:{}\n".format(array_from_list_1))
print("array_from_list_2:{}\n".format(array_from_list_2))
print("array_from_list_3:{}\n".format(array_from_list_3))
print("array_from_tuple:{}\n".format(array_from_tuple))
print("array_from_set:{}\n".format(array_from_set))
print("array_from_set attribute: shape:{}; ndim:{}; size:{}\n".format(array_from_set.shape, array_from_set.ndim, array_from_set.size))
print("array_from_dict:{}\n".format(array_from_dict))
print("array_from_dict attribute: shape:{}; ndim:{}; size:{}\n".format(array_from_dict.shape, array_from_dict.ndim, array_from_dict.size))
Output : Be careful : from set,dict Created in numpy Array , yes 0 Dimension group ,size yes 1
array_from_list_1:['NumPy' 'supports' 'vectorized' 'operations']
array_from_list_2:[[1 2 3]
[4 5 6]]
array_from_list_3:[[2 3 4]
[4 5 6]
[6 7 8]]
array_from_tuple:[1 2 3]
array_from_set:{
1, 2, 3}
array_from_set attribute: shape:(); ndim:0; size:1
array_from_dict:{
'language': 'Python', 'Hobby': 'Reading'}
array_from_dict attribute: shape:(); ndim:0; size:1
We know NumPy The array element types in must be consistent , When the element types in the array are inconsistent ,NumPy Will force up type conversions whenever possible , Unless the data type is specified .
for example :
np.array ( [3.14, 2, 3] ) -> Cast to floating point type array
( [ 3.14, 2.0, 3.0 ] )
np.array ( [2, 3.14, 3] ) -> Even though the 1 The first element is an integer , Cast to floating point type
array ( [ 2.0, 3.14, 3.0 ] )
np.array ([‘1’, 2, 3]) -> Cast to character
array([‘1’, ‘2’, ‘3’], dtype=’<U1’)
np.array ([‘1’, 2, 3], dtype = ‘int8’) -> Convert to an explicitly given data type
array([1, 2, 3], dtype=int8)
array_random_random = np.random.random(size=(3,5))
array_random_normal = np.random.normal(loc = 2, scale = 1, size = (3,5))
array_random_int = np.random.randint(low = 0, high = 10, size = (2, 3))
print("array_random_random:{}\n".format(array_random_random))
print("array_random_normal:{}\n".format(array_random_normal))
print("array_random_int:{}\n".format(array_random_int))
Output :
array_random_random:[[0.02164541 0.72035649 0.69718817 0.91684897 0.36921595]
[0.32850542 0.47149381 0.04184023 0.39958435 0.62943443]
[0.62590949 0.42814604 0.11767369 0.71687164 0.51742262]]
array_random_normal:[[1.886419 2.32612002 2.31179881 1.24693448 1.37626366]
[1.54387266 2.0334366 3.68800144 1.45346514 3.36354338]
[3.08620338 1.0974769 3.18664 3.64974797 2.27638157]]
array_random_int:[[9 7 0]
[7 7 7]]