📅  最后修改于: 2023-12-03 15:33:12.372000             🧑  作者: Mango
The np.array([(1,2),(3,4)], dtype)
creates a 2-dimensional NumPy array with two rows and two columns. The elements of the array are integer values 1, 2, 3, and 4.
import numpy as np
arr = np.array([(1,2),(3,4)], dtype=int)
print(arr)
Output:
[[1 2]
[3 4]]
The dtype
argument specifies the data type of the array. In this case, we have specified int
as the data type.
Some other important properties of this array are:
shape
: The shape property returns the dimensions of the array.size
: The size property returns the total number of elements in the array.ndim
: The ndim property returns the number of dimensions of the array.dtype
: The dtype property returns the data type of the array.print(arr.shape)
print(arr.size)
print(arr.ndim)
print(arr.dtype)
Output:
(2, 2)
4
2
int64
We can also perform various operations on this array, such as indexing, slicing, and arithmetic operations.
print(arr[0,0]) # 1
print(arr[:,1]) # [2 4]
print(arr + 1) # [[2 3]
# [4 5]]
Overall, the np.array([(1,2),(3,4)], dtype)
creates a useful 2-dimensional NumPy array for performing data analysis and other scientific computations.