randn函数 python_Python numpy matrix randn()用法及代码示例

这篇博客介绍了如何使用numpy.matlib.randn()函数在Python中生成随机值矩阵,包括不同参数的设置方法和代码示例,展示了如何创建均值和方差分别为0和1的标准正态分布矩阵,以及如何通过调整参数生成不同分布的随机矩阵。
摘要由CSDN通过智能技术生成

numpy.matlib.randn()是另一个用于在numpy中进行矩阵运算的函数。它从均值0和方差1的单变量“normal”(高斯)分布返回随机值矩阵。

用法: numpy.matlib.randn(*args)

参数:

* args:[参数]输出矩阵的形状。如果给定为N个整数,则每个整数指定一维的大小。如果以元组形式给出,则该元组将具有完整的形状。如果有多个参数且第一个参数是元组,则其他参数将被忽略。

Return :从标准正态分布中得出的随机值矩阵。

代码1:

# Python program explaining

# numpy.matlib.randn() function

# importing matrix library from numpy

import numpy as geek

import numpy.matlib

# desired 3 x 4 random output matrix

out_mat = geek.matlib.randn((3, 4))

print ("Output matrix:", out_mat)

输出:

Output matrix: [[ 0.78620217 0.41624612 -0.28417131 0.1071018 ]

[ 0.77645105 0.30858858 -1.98901344 1.25977209]

[ 0.26279443 -0.41026178 -0.60834494 2.82552737]]

代码2:

# Python program explaining

# numpy.matlib.randn() function

# importing numpy and matrix library

import numpy as geek

import numpy.matlib

# desired 1 x 5 random output matrix

out_mat = geek.matlib.randn(5)

print ("Output matrix:", out_mat)

输出:

Output matrix: [[ 0.34973625 0.28159132 0.72581405 -1.17511692 1.96773952]]

代码3:

# Python program explaining

# numpy.matlib.randn() function

# importing numpy and matrix library

import numpy as geek

import numpy.matlib

# more than one argument given

out_mat = geek.matlib.randn((5, 3), 4)

print ("Output matrix:", out_mat)

输出:

Output matrix: [[ 0.56784957 0.82980325 1.16683558]

[-1.53444326 -0.27743273 0.65819067]

[ 0.99654573 -1.20399432 -0.25603147]

[ 1.74931585 0.58413453 1.67820029]

[-1.25643231 0.21610229 0.21694595]]

注意:对于来自的随机样本

我们可以用sigma * geek.matlib.randn(...) + mu。

例如,制作一个3 x 3矩阵,其中的样本取自

代码4:

# Python program explaining

# numpy.matlib.randn() function

# importing numpy and matrix library

import numpy as geek

import numpy.matlib

# So, here mu = 3, sigma = 2

out_mat = 2 * geek.matlib.randn((3, 3)) + 3

print ("Output matrix:", out_mat)

输出:

Output matrix: [[ 4.04967121 0.26982021 2.3503067 ]

[ 5.57757131 2.40051874 -0.84588539]

[ 7.43715651 3.84004412 1.40514615]]

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值