pytorch取中值
时间: 2023-11-20 22:55:28 浏览: 42
可以使用PyTorch中的`quantile()`方法或`median()`方法来计算中值。
使用`quantile()`方法:
```python
import torch
a = torch.tensor([10, 10, 40, 20, 70, 40, 20, 50, 50, 70], dtype=torch.float)
b = torch.tensor([10, 20, 30, 40, 50, 60], dtype=torch.float)
c = torch.tensor([10, 20, 30, 40, 50], dtype=torch.float)
print(a.sort().values, a.quantile(q=0.5).item())
print(b.sort().values, b.quantile(q=0.5).item())
print(c.sort().values, c.quantile(q=0.5).item())
```
使用`median()`方法:
```python
import torch
a = torch.tensor([10, 10, 40, 20, 70, 40, 20, 50, 50, 70], dtype=torch.float)
b = torch.tensor([10, 20, 30, 40, 50, 60], dtype=torch.float)
c = torch.tensor([10, 20, 30, 40, 50], dtype=torch.float)
print(a.sort().values, a.median().item())
print(b.sort().values, b.median().item())
print(c.sort().values, c.median().item())
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)