生成一个ndarray,然后用np.percentile和np.quantile还求一下这个array的百分位数
时间: 2023-07-07 09:09:15 浏览: 118
好的,以下是生成ndarray并计算百分位数的示例代码:
```python
import numpy as np
# 生成一个形状为(100,)的随机数组
arr = np.random.rand(100)
# 计算数组的25、50、75百分位数
pct_25 = np.percentile(arr, 25)
pct_50 = np.percentile(arr, 50)
pct_75 = np.percentile(arr, 75)
# 计算数组的0.25、0.5、0.75分位数
qtl_25 = np.quantile(arr, 0.25)
qtl_50 = np.quantile(arr, 0.5)
qtl_75 = np.quantile(arr, 0.75)
print("25th percentile: {}".format(pct_25))
print("50th percentile: {}".format(pct_50))
print("75th percentile: {}".format(pct_75))
print("0.25 quantile: {}".format(qtl_25))
print("0.5 quantile: {}".format(qtl_50))
print("0.75 quantile: {}".format(qtl_75))
```
输出结果类似于:
```
25th percentile: 0.2491505511658243
50th percentile: 0.4823911076846821
75th percentile: 0.7088452628239561
0.25 quantile: 0.2491505511658243
0.5 quantile: 0.4823911076846821
0.75 quantile: 0.7088452628239561
```
阅读全文