【python第三方库】Numpy random数学函数 算数函数 副本视图

random

(Legacy random generation — NumPy v2.0 Manual)

  • 初始化种子和状态

seed 设置随机种子 种子相同,随机结果相同

get_state 获取当前的随机数生成器状态

set_state 设置随机数生成器,接受get_state 的返回结果

import numpy as np

np.random.seed(0)
a = np.random.get_state()
print(np.random.randn(6))
np.random.seed(1)
b = np.random.get_state()
print(np.random.randn(6))
np.random.set_state(a)
print(np.random.randn(6))
'''
[ 1.76405235  0.40015721  0.97873798  2.2408932   1.86755799 -0.97727788]
[ 1.62434536 -0.61175641 -0.52817175 -1.07296862  0.86540763 -2.3015387 ]
[ 1.76405235  0.40015721  0.97873798  2.2408932   1.86755799 -0.97727788]
'''
  • 简单随机数据

rand 给定形状的随机值

randn 标准正态分布转化一个或者多个样本

randint 介于两个值之间的随机整数 左闭右开

choice 从给定的一维数组生成随机样本

random_sample返回半开间隔 [0.0, 1.0] 内的随机浮点数

np.random.seed(0)

# print(np.random.rand(3,4))
'''
[[0.5488135  0.71518937 0.60276338 0.54488318]
 [0.4236548  0.64589411 0.43758721 0.891773  ]
 [0.96366276 0.38344152 0.79172504 0.52889492]]
'''

# print(np.random.randn(2,3))
# print(np.random.randn(2))
'''
[[ 1.76405235  0.40015721  0.97873798]
 [ 2.2408932   1.86755799 -0.97727788]]
[ 0.95008842 -0.15135721]
'''
# print(np.random.randint(2,4)) # 2
# print(np.random.random_sample()) # 0.5488135039273248
  • 排列

shuffle 随机打乱序列内容

permutation 返回一个随机排列的副本,而不改变原始数组

# np.random.shuffle(li)
# print(li)# [2, 8, 4, 9, 1, 6, 7, 3, 0, 5]
print(np.random.permutation(li))
print(li)
'''
[2 8 4 9 1 6 7 3 0 5]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
'''
  • 分布

standard_normal 正态分布 均值0 方差1

uniform 均匀分布 指定范围

print(np.random.standard_normal((3,3)))
'''
[[ 1.76405235  0.40015721  0.97873798]
 [ 2.2408932   1.86755799 -0.97727788]
 [ 0.95008842 -0.15135721 -0.10321885]]
'''
print(np.random.uniform(1,10,(5,))) #[8.12552534 5.76005428 6.11240105 9.33036974 1.63932452]

数学函数

  • 三角函数

sin cos tan 传入的参数式弧度值的角度

arcsin arccos arctan 返回给定角度的反三角函数

degrees 弧度转换为角度

# print(np.sin(np.pi/2)) # 1.0
# print(np.cos(np.pi)) # -1.0
# print(np.tan(0))  # 0.0
# print(np.degrees(np.arcsin(1))) # 90
# print(np.degrees(np.arcsin(0))) # 0
  • 舍入函数

around 四舍五入 对于浮点数,如果小数部分大于等于 0.5,则向上取整;小于 0.5,则向下取整。

floor 向下取整

ceil 向上取整

a = 3.5
b = 4.5
c = 3.49
d = 3.51
e = 4.49
f = 4.51
li = [a,b,c,d,e,f]
print([np.around(el) for el in li])
print([np.floor(el) for el in li])
print([np.ceil(el) for el in li])
''''
[4.0, 4.0, 3.0, 4.0, 4.0, 5.0]
[3.0, 4.0, 3.0, 3.0, 4.0, 4.0]
[4.0, 5.0, 4.0, 4.0, 5.0, 5.0]
'''

算术函数

reciprocal 倒数

power 幂运算

mod remainder 求余数

a = np.arange(2,13,2,dtype=np.float32).reshape(2,3) # 运算有浮点数产生,指定数据类型
b = np.arange(3,14,2,dtype=np.float32).reshape(2,3)
'''
[[ 2.  4.  6.]
 [ 8. 10. 12.]]
[[ 3.  5.  7.]
 [ 9. 11. 13.]]
'''
print(np.reciprocal(a))
print(np.reciprocal(b))
print(np.power(a,b))
print(np.mod(a,b))
print(np.remainder(a,b))
'''
[[0.5        0.25       0.16666667]
 [0.125      0.1        0.08333334]]
[[0.33333334 0.2        0.14285715]
 [0.11111111 0.09090909 0.07692308]]
[[8.00000000e+00 1.02400000e+03 2.79936000e+05]
 [1.34217728e+08 9.99999980e+10 1.06993205e+14]]
[[ 2.  4.  6.]
 [ 8. 10. 12.]]
[[ 2.  4.  6.]
 [ 8. 10. 12.]]
'''

统计函数

(NumPy 统计函数 | 菜鸟教程 (runoob.com))

amin 指定维度最小值,未指定就是所有

amax 指定维度最大值

ptp 最大值最小值的差值 可选维度

percentile 百分位数

median 中位数

mean均值

average加权均值

std方差

var 标准差

副本和视图

  • 副本是数据的完整拷贝,对副本修改不会对原数组修改

    • python序列的切片,deepCopy函数
    • ndarray的copy函数
  • 视图是引用,视图进行修改,它会影响到原始数据

    • numpy 切片操作返回原数据的视图
    • view() 函数返回视图
import copy
# li = [i for i in range(10)]
# arr = np.array(li)
# print(li,arr)
# li_Dcopy = copy.deepcopy(li) # 副本
# li_Dcopy[-1] = 100 
# print(li)
# arr2 = arr.copy() # 副本
# arr2[-1] = 1000
# print(arr,arr2)
'''
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9] [0 1 2 3 4 5 6 7 8 9]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[0 1 2 3 4 5 6 7 8 9] [   0    1    2    3    4    5    6    7    8 1000]
'''
# 视图
arr1 = np.arange(6).reshape(2,3)
print(arr1)
arr1[:,0:2] = 3
print(arr1)
arr2 = np.arange(12).reshape(3,4)
view = arr2.view()
print(arr2)
view[:,-1] = 100
print(arr2)
'''
[[0 1 2]
 [3 4 5]]
[[3 3 2]
 [3 3 5]]
[[ 0  1  2  3]
 [ 4  5  6  7]
 [ 8  9 10 11]]
[[  0   1   2 100]
 [  4   5   6 100]
 [  8   9  10 100]]
'''

矩阵

T 矩阵的转置

transpose 转换维度

matlib.empty 返回一个新的矩阵

matlib.zeros 0填充

matlib.ones1填充

matlib.eye 对角线为1 其他为0

matlib.identity 单位矩阵

matlib.rand 随机数据填充

import numpy.matlib as mat

print(mat.eye(3))
print(mat.eye(3,2))
print(mat.eye(2,3))
print(mat.ones((3,2)))
print(mat.zeros((2,3)))
print(mat.empty((3,2)))
print(mat.identity(3))
print(mat.rand(3,2))
''''
[[1. 0. 0.]
 [0. 1. 0.]
 [0. 0. 1.]]
[[1. 0.]
 [0. 1.]
 [0. 0.]]
[[1. 0. 0.]
 [0. 1. 0.]]
[[1. 1.]
 [1. 1.]
 [1. 1.]]
[[0. 0. 0.]
 [0. 0. 0.]]
[[0. 0.]
 [0. 0.]
 [0. 0.]]
[[1. 0. 0.]
 [0. 1. 0.]
 [0. 0. 1.]]
 [[0.5488135  0.71518937]
 [0.60276338 0.54488318]
 [0.4236548  0.64589411]]
'''

线性代数

dot 点积 对应元素向乘

vdot 向量的点积

inner 数组内积

matmul 矩阵相乘

linalg.det 行列式

solve 求解矩阵方程

inv矩阵的逆矩阵

a = np.array([1,2,3])
b = np.array([4,5,6])
A = np.array(
    [[1,2],
    [3,4]]
)
B = np.array(
    [
        [5,6],
        [7,8]
    ]
)
# print(np.dot(a,b))
# print(np.dot(A,B))
'''
32 1x4+2x5+3x6
[[19 22]    1x5+2x7
 [43 50]]
'''
# print(np.vdot(a,b))
# print(np.vdot(A,B))

'''
32  1x4+2x5+3x6
70   1 5   2 6  3 7  4 8 
'''
# print(np.inner(A,B))
''''
[[17 23]  1x5+2x6  1x7+2x8
 [39 53]] 3x5+4x6  3x7+4x8
'''
# print(np.matmul(A,B))  # 同print(np.vdot(A,B))
'''
[[19 22]
 [43 50]]
'''
# print(np.linalg.det(A))  
# -2.0000000000000004  精度影响

# ax = b
'''
[
    [1,0],
    [0,1]
]
[1,
2]
'''
a = np.array(
    [
    [1,0],
    [0,1]
]
)
b = np.array([1,2])
x = np.linalg.solve(a,b)
# print(x)

a = np.array([[1,2],[3,4]])
b = np.linalg.inv(a)
print(b)
print(np.matmul(a,b))
'''
[[-2.   1. ]
 [ 1.5 -0.5]]
[[1.0000000e+00 0.0000000e+00]
 [8.8817842e-16 1.0000000e+00]]
'''

  • 6
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值