【python】numpy.percentile()函数

2023-11-18

numpy.percentile()

1.函数

百分位数是统计中使用的度量,表示小于这个值的观察值的百分比。 函数numpy.percentile()接受以下参数。

np.percentile(a, q, axis=None, out=None, overwrite_input=False, interpolation='linear', keepdims=False) 
2.参数说明:
  • a: 输入数组
  • q: 要计算的百分位数,在 0 ~ 100 之间
  • axis: 沿着它计算百分位数的轴
  • keepdims :bool是否保持维度不变
  • 首先明确百分位数:第 p 个百分位数是这样一个值,它使得至少有 p% 的数据项小于或等于这个值,且至少有 (100-p)% 的数据项大于或等于这个值。

【注】举个例子:高等院校的入学考试成绩经常以百分位数的形式报告。比如,假设某个考生在入学考试中的语文部分的原始分数为 54 分。相对于参加同一考试的其他学生来说,他的成绩如何并不容易知道。但是如果原始分数54分恰好对应的是第70百分位数,我们就能知道大约70%的学生的考分比他低,而约30%的学生考分比他高。这里的 p = 70。

  • a : array_like
    Input array or object that can be converted to an array.
  • q : array_like of float
    Percentile or sequence of percentiles to compute, which must be between 0 and 100 inclusive.
  • axis : {int, tuple of int, None}, optional
    Axis or axes along which the percentiles are computed. The default is to compute the percentile(s) along a flattened version of the array.
    Changed in version 1.9.0: A tuple of axes is supported
  • out : ndarray, optional
    Alternative output array in which to place the result. It must have the same shape and buffer length as the expected output, but the type (of the output) will be cast if necessary.
  • overwrite_input : bool, optional
    If True, then allow the input array a to be modified by intermediate calculations, to save memory. In this case, the contents of the input a after this function completes is undefined.
  • interpolation : {‘linear’, ‘lower’, ‘higher’, ‘midpoint’, ‘nearest’}
    This optional parameter specifies the interpolation method to use when the desired percentile lies between two data points i < j:
    ‘linear’: i + (j - i) * fraction, where fraction is the fractional part of the index surrounded by i and j.
    ‘lower’: i.
    ‘higher’: j.
    ‘nearest’: i or j, whichever is nearest.
    ‘midpoint’: (i + j) / 2.
    New in version 1.9.0.
  • keepdims : bool, optional
    If this is set to True, the axes which are reduced are left in the result as dimensions with size one. With this option, the result will broadcast correctly against the original array a.
3.实例分析
import numpy as np 
 
a = np.array([[10, 7, 4], [3, 2, 1]])
print ('我们的数组是:')
print (a)
 
print ('调用 percentile() 函数:')
# 50% 的分位数,就是 a 里排序之后的中位数
print (np.percentile(a, 50)) 
 
# axis 为 0,在纵列上求
print (np.percentile(a, 50, axis=0)) 
 
# axis 为 1,在横行上求
print (np.percentile(a, 50, axis=1)) 
 
# 保持维度不变
print (np.percentile(a, 50, axis=1, keepdims=True))

输出结果

我们的数组是:
[[10  7  4]
 [ 3  2  1]]
调用 percentile() 函数:
3.5
[6.5 4.5 2.5]
[7. 2.]
[[7.]
 [2.]]
4.更多例子
>> a = np.array([[10, 7, 4], [3, 2, 1]])
>>> a
array([[10,  7,  4],
      [ 3,  2,  1]])
>>> np.percentile(a, 50)
3.5
>>> np.percentile(a, 50, axis=0)
array([6.5, 4.5, 2.5])
>>> np.percentile(a, 50, axis=1)
array([7.,  2.])
>>> np.percentile(a, 50, axis=1, keepdims=True)
array([[7.],
      [2.]])
>>> m = np.percentile(a, 50, axis=0)
>>> out = np.zeros_like(m)
>>> np.percentile(a, 50, axis=0, out=out)
array([6.5, 4.5, 2.5])
>>> m
array([6.5, 4.5, 2.5])
>>> b = a.copy()
>>> np.percentile(b, 50, axis=1, overwrite_input=True)
array([7.,  2.])
>>> assert not np.all(a == b)

参考:
1.https://docs.scipy.org/doc/numpy/reference/generated/numpy.percentile.html
2.https://www.runoob.com/numpy/numpy-statistical-functions.html

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

【python】numpy.percentile()函数 的相关文章

随机推荐

  • pytorch入门day5-卷积神经网络实战

    目录 LeNet网络实战 ResNet 训练函数 LeNet网络实战 import torch from torch utils data import DataLoader from torchvision import datasets
  • IDEA集成Git及相关操作详解

    IDEA集成Git 一 环境准备 1 配置Git忽略文件 2 Git程序定位 二 Git集成及相关操作 1 Git本地库初始化 2 文件 或目录 添加暂存区及添加本地库 3 查看历史版本及版本切换 4 创建及切换分支 5 合并分支 一 环境
  • JavaScript 设计模式 – 通过示例进行解释

    大家好 在本文中 我将解释什么是设计模式以及它们为何有用 目录 什么是设计模式 创意设计模式 单例模式 工厂方法模式 抽象工厂模式
  • CORS on Nginx

    https enable cors org server nginx html Wide open CORS config for nginx location if request method OPTIONS add header Ac
  • 麒麟V10编译安装GCC9.3

    在麒麟V10 arm64架构 桌面版系统上编译安装gcc9 3版本 麒麟V10自带的gcc版本是5 4 根据以下步骤顺序执行即可安装成功 1 下载解压gcc wget https mirrors tuna tsinghua edu cn g
  • 软件外包开发的原型图工具

    在软件开发中需要用到原型图工具来将需求转化为图形界面 这样可以更好更准确的表达需求的实现方式 与传统的需求文档相比 原型图的表达更直接 不但可以画出UI 也支持UI之间的跳转连接 与最终的实现效果基本是一样的 今天和大家分享几个原型图工具
  • python-jinja2模板引擎

    文章目录 1 什么是Jinja2模板引擎 2 语法 1 Jinja2变量显示语法 变量名 过滤器 如何自定义过滤器 2 for循环 3 if语句 4 宏的操作 相当于函数 5 include包含操作 6 模板的继承 一般网站的导航栏和底部不
  • Array.splice()--删除数组中重复的数据

    Array splice 删除数组中重复的数据 splice方法从一个数组中移除一个或多个元素 如果必要 在所移除元素的位置上插入新元素 返回所移除的元素 用法 my array splice start deleteCount value
  • Seata1.4.2+Nacos搭建使用

    Seata1 4 2 Nacos搭建使用 前言 一 搭建seata1 4 2服务端 1 下载seata1 4 2 2 创建相关数据库和表 3 配置Seata 1 4 2 4 启动seata1 4 2 二 客户端使用seata1 4 2 1
  • 蓝桥杯题目练习(星际交流)

    算法训练VIP 星际交流 原题链接 星际交流 题目描述 人类终于登上了火星的土地并且见到了神秘的火 星人 人类和火星人都无法理解对方的语言 但是我们的科学家发明了一种用数字交流的方法 这种交流方法是这样 的 首先 火星人把一个非常大的数字告
  • 深度CV基础——图像噪声和滤波

    一 图像噪声 1 图像噪声的概念 图像噪声是图像在获取或是传输过程中受到随机信号干扰 妨碍人们对图像理解及分析处理的信号 很多时候将图像噪声看做多维随机过程 因而描述噪声的方法完全可以借用随机过程的描述 也就是使用随机过程的描述 也就是用它
  • SQL Server 修改字段属性信息报错

    当表创建好以后再次修改表信息保存会出现 选择Tools gt Options 选中勾掉点OK就可以了
  • docker-compose单机容器编排的神器

    docker compose和docker兼容表 简介 docker所制作的容器多半需要大量的依赖 有些可能依赖于其他容器的启动 比方说一个springboot的项目 它需要rabbitMQ和esearch什么的 有些没装数据库的机器需要m
  • STM32 GPIO的基础实现

    什么是GPIO GPIO 英文全称 general porpose intput output 即 通用输入输出端口 顾名思义 芯片最基本的输入输出接口 STM32或其它单片机芯片的GPIO引脚可以与外部设备连接起来 可以实现与外部设备通讯
  • Spring系列之@Value【用法、数据来源、动态刷新】

    面试官 Spring中的 Value用过么 介绍一下 我 Value可以标注在字段上面 可以将外部配置文件中的数据 比如可以将数据库的一些配置信息放在配置文件中 然后通过 Value的方式将其注入到bean的一些字段中 面试官 那就是说 V
  • 关于DYNPRO程序的系统迁移与版本不匹配问题之一

    前段时间公司做的一个项目 这两天在将项目程序导入公司 出问题了 搞了半天才发现是系统版本问题 但是还是不知道怎么解决 纠结ING DYNRPO程序在创建 或是首次运行 的时候会自动生成一个DYNRPO组件程序的类 这个程序是后台解析DYNP
  • 优惠券秒杀(一)

    L1296686146 冗谪 2023 07 20 08 18 发表于陕西 收录于合集 redis7个 优惠券秒杀 数据表 优惠券的表 优惠券的基本信息 优惠金额 使用规则等 CREATE TABLE tb voucher id bigin
  • RESTful API介绍

    1 什么是RESTful API 一种流行的API设计风格 2 为什么使用RESTful架构 REST 是 Representational State Transfer 的缩写 如果一个架构符合 REST 原则 就称它为 RESTful
  • 服务器老是提示内存不足

    jps mlvV 查进程 jmap heap 进程ID 查看内存是多少 JAVA OPTS Xms1536m Xmx1536m XX PermSize 512m XX MaxPermSize 512m
  • 【python】numpy.percentile()函数

    numpy percentile 1 函数 百分位数是统计中使用的度量 表示小于这个值的观察值的百分比 函数numpy percentile 接受以下参数 np percentile a q axis None out None overw