要修改NumPy數組的元素,可以通過索引和切片操作來實現。下面是一些示例代碼:
import numpy as np
arr = np.array([1, 2, 3, 4, 5])
arr[0] = 10
print(arr) # 輸出: [10, 2, 3, 4, 5]
import numpy as np
arr = np.array([1, 2, 3, 4, 5])
arr[1:3] = 10
print(arr) # 輸出: [1, 10, 10, 4, 5]
import numpy as np
arr = np.array([[1, 2, 3], [4, 5, 6]])
arr[0, 1] = 10
print(arr) # 輸出: [[1, 10, 3], [4, 5, 6]]
請注意,NumPy數組的索引是從0開始的。