请对上面的方法分别举例说明
时间: 2024-03-23 17:43:57 浏览: 48
方法和案例........
好的,下面我就逐个方法给出例子说明:
1. ndim方法:
```python
import numpy as np
a = np.array([[1, 2], [3, 4]])
print(a.ndim) # 输出 2
```
2. shape方法:
```python
import numpy as np
a = np.array([[1, 2], [3, 4]])
print(a.shape) # 输出 (2, 2)
```
3. size方法:
```python
import numpy as np
a = np.array([[1, 2], [3, 4]])
print(a.size) # 输出 4
```
4. dtype方法:
```python
import numpy as np
a = np.array([[1, 2], [3, 4]])
print(a.dtype) # 输出 int64
```
5. itemsize方法:
```python
import numpy as np
a = np.array([[1, 2], [3, 4]])
print(a.itemsize) # 输出 8 (因为int64的字节大小为8)
```
6. data方法:
```python
import numpy as np
a = np.array([[1, 2], [3, 4]])
print(a.data) # 输出 <memory at 0x7f0f84c4e1c0>
```
7. reshape方法:
```python
import numpy as np
a = np.array([[1, 2], [3, 4]])
b = a.reshape((1, 4))
print(b) # 输出 [[1 2 3 4]]
```
8. flatten方法:
```python
import numpy as np
a = np.array([[1, 2], [3, 4]])
b = a.flatten()
print(b) # 输出 [1 2 3 4]
```
9. transpose方法:
```python
import numpy as np
a = np.array([[1, 2], [3, 4]])
b = a.transpose()
print(b) # 输出 [[1 3] [2 4]]
```
10. concatenate方法:
```python
import numpy as np
a = np.array([[1, 2], [3, 4]])
b = np.array([[5, 6], [7, 8]])
c = np.concatenate((a, b), axis=0)
print(c) # 输出 [[1 2] [3 4] [5 6] [7 8]]
```
11. stack方法:
```python
import numpy as np
a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
c = np.stack((a, b), axis=1)
print(c) # 输出 [[1 4] [2 5] [3 6]]
```
12. split方法:
```python
import numpy as np
a = np.array([[1, 2], [3, 4], [5, 6]])
b = np.split(a, 3)
print(b) # 输出 [array([[1, 2]]), array([[3, 4]]), array([[5, 6]])]
```
13. sort方法:
```python
import numpy as np
a = np.array([3, 1, 4, 1, 5, 9, 2, 6, 5])
a.sort()
print(a) # 输出 [1 1 2 3 4 5 5 6 9]
```
14. max/min方法:
```python
import numpy as np
a = np.array([3, 1, 4, 1, 5, 9, 2, 6, 5])
print(np.max(a)) # 输出 9
print(np.min(a)) # 输出 1
```
阅读全文