This is the 23rd day of my participation in the August More Text Challenge
The basic properties of a NUMpy array include the shape, size, type, and dimension of the array
1. Array shape
The shape of an array refers to the number of rows and columns in the array. This can be seen by calling the shape property of the array, as shown in the following example. That corresponds to an n by m matrix, which is n rows and m columns
import numpy as np
arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
print(arr)
Copy the code
result:
[1 2 3] [4 5 6] [7 8 9]]
print(arr.shape)
Copy the code
result:
(3, 3)
Copy the code
2. Array size
The size of an array is the total number of elements in the array. This can be seen by calling the size property of the array, as shown in the following example. That corresponds to the nm matrix, which is nm
import numpy as np
arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
print(arr)
Copy the code
result:
[1 2 3] [4 5 6] [7 8 9]]
print(arr.size)
Copy the code
result:
9
Copy the code
3. Array type
The type of an array is the type of the elements that make up the array. This can be seen by calling the dtype property of the array, as shown in the following example.
import numpy as np
arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
print(arr)
Copy the code
result:
[1 2 3] [4 5 6] [7 8 9]]
print(arr.dtype)
Copy the code
result:
int32
Copy the code
4. The dimensions of the array
The dimension of an array is that the array of exponentials is in a multi-dimensional space, and a multi-dimensional space corresponds to an array of dimensions. You can see this by calling the nDIM property of the array directly, as shown in the following example. That corresponds to an n by m matrix, which is the rank of the matrix. The rank is the number of axes, the dimensions of the array, the rank of a one-dimensional array is 1, the rank of a two-dimensional array is 2, and so on.
In NumPy, each linear array is called an axis, or dimensions. For example, a two-dimensional array is equivalent to two one-dimensional arrays, where each element in the first one-dimensional array is in turn a one-dimensional array. So a one-dimensional array is the NumPy axis. The first axis is the underlying array, and the second axis is the array within the underlying array. And the number of axes, the rank, is the dimension of the array.
import numpy as np
arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
print(arr)
Copy the code
result:
[1 2 3] [4 5 6] [7 8 9]]
print(arr.ndim)
Copy the code
result:
2
Copy the code
5. Data types in numpy
The type of an array mainly refers to the types of the elements that make up the array. Numpy has five data types, as shown in the following table.
type | instructions |
---|---|
int | The integer |
float | Floating point Numbers |
object | Python object types |
string_ | It is a string, usually denoted by S, and S10 is a string of length 10 |
Unicode_ | Fixed-length Unicode types, as defined by strings, are often represented by U |