reshape((-1,))
时间: 2023-08-24 16:09:47 浏览: 52
The `reshape((-1,))` method is used to convert a multi-dimensional array into a one-dimensional array. The `-1` in the method is a placeholder for the number of rows or columns that the new array should have. If `-1` is used for one of the dimensions, NumPy automatically calculates the appropriate number of rows or columns based on the other dimension and the total number of elements in the original array.
For example, consider the following 2D array:
```
import numpy as np
arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
```
The `reshape((-1,))` method can be applied to this array to convert it into a one-dimensional array:
```
new_arr = arr.reshape((-1,))
```
The resulting array `new_arr` will have the following values:
```
array([1, 2, 3, 4, 5, 6, 7, 8, 9])
```
Note that the order of the values in the new array is determined by the order in which the original array was flattened. In this case, the values are arranged in row-major order, meaning that the first row is listed first, followed by the second row, and so on.