Create array
1 | a = np.array([[1,2,3,4],[5,6,7,8]]) |
- ndarray.ndim: show the dimensionality of array
1 | a.ndim |
- ndarray.shape: show the length of array in every direction
1 | a.shape |
- ndarray.size: show the size of array(the value = the product of every item in shape)
1 | a.size |
- ndarray.dtype: assign the data type
1 | a.dtype |
ndarray.itemsize: calculate the size of every item in the memory. e.g. a float64 type of data occupies 8 bit in computer memory.
ndarray.data: show the address of this array in memory.
Create array with assigned data
- int
1 | a = np.array([1,2,3,4],dtype = np.int) #(int8,int16,int64......) |
- float
1 | a = np.array([1,2,3,4],dtype = np.float)#(float32......) |
Create the specific array
1 | a = np.array([1,2,3,4],[5,6,7,8]) |
Create the zero array
- all zero
1 | a = np.zeros((3,4)) #3 row 4 line |
- create array and assign data type
1 | a = np.ones((3,4),dtype = np.int) |
- create all empty array(every value is close to zero)
1 | a = np.empty((3,4)) #the value is close to zero, 3 row 4 line |
The “arange() “ function
arange() has three parameter, arange( , , * )
- first
1 | np.arange(6) |
- second
1 | np.arange(0,6) #the former is the initial value, the latter is the end value |
- third
1 | np.arange(1,3,0.5)# the third parameter is the interval value. |
The “linspace()“ function
linspace() can generate an well-proportion array in assigned interval range.
1 | np.linspace(0,10,20) |
rand() function generate array in range of [ 0,1 )
1 | np.random.rand(2,3,3) |