NumPy array object

1. Recognize NumPy array objects

1
2
3
4
5
6
7
8
9
10
11
12
import numpy as np  # import numpy toolkit

data = np.arange(12).reshape(3, 4) # Create an array with 3 rows and 4 columns

print("data:\n", data, "\n")
print("type(data):\n", type(data), "\n")
print("data.ndim:\n", data.ndim, "\n") # The number of dimensions of the array
print("data.shape:\n", data.shape,
"\n") # The dimension of the array, the output result (3, 4), which means 3 rows and 4 columns
print("data.size:\n", data.size, "\n") # The number of array elements
print("data.dtype:\n", data.dtype, "\n") # The type of array element

2. Create NumPy array objects

1
2
3
4
5
6
7
8
import numpy as np

data1 = np.array([1, 2, 3])
print("data1:\n", data1)

data2 = np.array([[1, 2, 3], [4, 5, 6]])
print("data2:\n", data2)

2.1 zero() function:

type of data is float64.

1
print(np.zeros((3, 4)))

2.2 ones() function:

type of data is float64.

1
print(np.ones((3, 4)))

2.3 empty() function:

type of data is float64.

1
print(np.empty((3, 4)))

2.4 arange() function:

1
print(np.arange(1, 20, 5))


NumPy array object
https://www.hardyhu.cn/2022/02/22/NumPy-array-object/
Author
John Doe
Posted on
February 22, 2022
Licensed under