Numpy基础(day2)随机函数及统计函数

Numpy常用random随机函数汇总

在这里插入图片描述
import numpy as np np.random.seed(666)

  • rand(d0,d1,…,dn)
    返回数据在(0,1)之间,均具有均匀分布
    np.random.rand(5)
    np.random.rand(3, 4)
    np.random.rand(3, 4, 5)
  • randn(d0,d1,…,dn)
    返回数据具有标准正态分(均值为0,方差为1)
    np.random.randn(5)
    np.random.randn(3, 4)
    np.random.randn(3, 4, 5)
  • randint(low[,hight,size,dtype])
    生成随机整数,包含low,不包含high
    如果不指定high,则从[0, low]中生成数字
    np.random.randint(3) # 生成0-3之间的数,不包含3
    np.random.randint(1, 10)
    np.random.randn(10,30,size=(2,3,4)) # 10到30之间的整数(分为两块,每块为三行四列)
  • random([size])
    生成[0.0,1.0]的随机数
    np.random.random(5)
    np.random.random(size=(3, 4))
    np.random.random(size=(3, 4, 5))
  • choice(a[,size,replace,p])
    a是一维数组,从它里面生成随机结果
    np.random.choice(5, 3) a是数字,则从range(5)中生成 size为3
    np.random.choice(5, (2,3))
    np.random.choice([2,3,4,5,6,7],3) 从数组里面随机取出数字
  • shuffle(x)
    把一个数组x进行随机排列
import numpy as np
x = np.arrange(10)
np.shuffle(x)
print(x)
a = np.arrange(10).reshape(4, 5)
np.shuffle(a) # 多维数组,则只会在第一维度打乱数据
print(a)
  • permutation(x)
    把一个数组x进行随机排列,或者数字的全排列
    np.random.permutation(10) 随机生成range(10)进行随机排列
    arr = np.arrange.reshape((3,3)) 在第一维度进行打散
    np.random.permutation(arr)创建一个新的copy,不修改原来的
  • normal([loc, scale, size])
    按照平均值loc和方差scale生成高斯分布的数字
    np.random.normal(1,10,10)
    np.random.normal(1,10,(3,4))
  • uniform([low, high, size])
    在[low,high)之间生成均匀分布的数字
    np.random.uniform(1,10,10)
    np.random.uniform(1,10,(3,4))
    demo 对数组加入随机噪声
import matplotlib.pyplot as plt
# 绘制sin曲线
x = np.linspace(-10,10,100) # 指定最小数,最大数,生成的个数
y = np.sin(x)
plt.plot(x,y)
plt.show()

# 加入噪声
x = np.linspace(-10,10,100) # 指定最小数,最大数,生成的个数
y = np.sin(x)+np.random.rand(len(x))
plt.plot(x,y)
plt.show()

加入噪声前
在这里插入图片描述
加入噪声后
在这里插入图片描述

Numpy数学统计函数

  1. 常用统计函数
    在这里插入图片描述
  2. 按不同的axis计算
    以上的函数都有一个参数叫做axis用于指定计算轴还是列,如果不指定,就会计算所有元素结果

数学统计函数实例

import numpy as np

arr = np.arange(12).reshape(3, 4)
print(arr)
print(np.sum(arr))  # 求和 默认作为一维数组处理求和
print(np.prod(arr))  # 乘积
print(np.cumsum(arr))  # 第一位等于第一位,第二位等于第一位加第二位-----同理
print(np.cumprod(arr))  # 第一位等于第一位,第二位等于第一位乘第二位-----同理
print(np.min(arr))  # 最小值
print(np.max(arr))  # 最大值
print(np.percentile(arr, [25, 50, 75]))  # 首先将数据从小到大排列,50 表示取位置为50%的数字,取值为[0-100]
print(np.quantile(arr, [0.25, 0.5, 0.75]))  # 同上,但是取值为[0-1]
print(np.median(arr))  # 中位数
print(np.mean(arr))  # 平均值
print(np.std(arr))  # 标准差
print(np.var(arr))  # 方差
# weight的shape需要和arr一样
weight = np.random.rand(*arr.shape)
print(weight)
np.average(arr, weights=weight)  # 加权平均值

numpy的axis的用途

axis=0代表行、axis=1代表列

  • 按行求和
    arr.sum(axis=0)
  • 按列求和
    arr.sum(axis=1)
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

啊~小 l i

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值