[Reprint] Don't bother the python study notes of numpy.array,dtype,empty,zeros,ones,arrange,linspace

Reference link: numpy.empty in Python

array: create an array dtype: specify the data type empty: create data close to 0 zeros: create data all 0ones: create data all 1arrange: create data in a specified range linspace: create line segments

import numpy as np #For the convenience of using numpy, use np abbreviation

 

array = np.array([[1,2,3],[2,3,4]]) #The list is converted to a matrix

print(array)

"""

[[1 2 3]

 [2 3 4]]

"""

 

print('number of dim:',array.ndim) # dimension

# number of dim: 2

 

print('shape :',array.shape) # number of rows and columns

# shape : (2, 3)

 

print('size:',array.size) # number of elements

# size: 6 

a = np.empty((3,4)) # The data is empty, 3 rows and 4 columns, creating a completely empty array, in fact, each value is a number close to zero

"""

array([[  0.00000000e+000,   4.94065646e-324,   9.88131292e-324,

          1.48219694e-323),

       (1.97626258e-323, 2.47032823e-323, 2.96439388e-323,

          3.45845952e-323),

       (3.95252517e-323, 4.44659081e-323, 4.94065646e-323,

          5.43472210e-323]])

""" 

a = np.zeros((3,4)) # data is all 0, 3 rows and 4 columns

"""

array([[ 0.,  0.,  0.,  0.],

       [ 0.,  0.,  0.,  0.],

       [ 0.,  0.,  0.,  0.]])

"""

 

 

a = np.ones((3,4),dtype = np.int) # The data is 1, 3 rows and 4 columns

"""

array([[1, 1, 1, 1],

       [1, 1, 1, 1],

       [1, 1, 1, 1]])

""" 

Pay attention to the difference between arange and linspace  

a = np.arange(1,106,5.5).reshape((5,4)) # Data with interval [1, 106), step size 5.5

>>> a

array([[  1. ,   6.5,  12. ,  17.5],

       [ 23. ,  28.5,  34. ,  39.5],

       [ 45. ,  50.5,  56. ,  61.5],

       [ 67. ,  72.5,  78. ,  83.5],

       [ 89. ,  94.5, 100. , 105.5]])

 

 a = np.linspace(1,105.5,20).reshape((5,4)) # start end 1, end end 105.5, and divide into 20 data, generate line segment

>>> a

array([[  1. ,   6.5,  12. ,  17.5],

       [ 23. ,  28.5,  34. ,  39.5],

       [ 45. ,  50.5,  56. ,  61.5],

       [ 67. ,  72.5,  78. ,  83.5],

       [ 89. ,  94.5, 100. , 105.5]])

Guess you like

Origin blog.csdn.net/u013946150/article/details/112976739