pythonnumpyrand函数_Python numpy matrix rand()用法及代码示例

numpy.matlib.rand()是另一个用于在numpy中进行矩阵运算的函数。它从给定形状的[0,1)上的均匀分布返回一个随机值矩阵。

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

参数:

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

Return :随机值矩阵。

代码1:

# Python program explaining

# numpy.matlib.rand() function

# importing matrix library from numpy

import numpy as geek

import numpy.matlib

# desired 3 x 4 random output matrix

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

print ("Output matrix:", out_mat)

输出:

Output matrix: [[ 0.37976085 0.68700838 0.83898103 0.72073804]

[ 0.80577587 0.2508264 0.30179229 0.81376797]

[ 0.70202528 0.17830863 0.61509844 0.27758369]]

代码2:

# Python program explaining

# numpy.matlib.rand() function

# importing numpy and matrix library

import numpy as geek

import numpy.matlib

# desired 1 x 5 random output matrix

out_mat = geek.matlib.rand(5)

print ("Output matrix:", out_mat)

输出:

Output matrix: [[ 0.56138247 0.97881105 0.53380995 0.27486091 0.1603695 ]]

代码3:

# Python program explaining

# numpy.matlib.rand() function

# importing numpy and matrix library

import numpy as geek

import numpy.matlib

# more than one argument given

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

print ("Output matrix:", out_mat)

输出:

Output matrix: [[ 0.86770893 0.35628104 0.19744129]

[ 0.90376689 0.58349554 0.9830152 ]

[ 0.64711739 0.09531791 0.17555793]

[ 0.66141287 0.09164568 0.28818979]

[ 0.92225364 0.56779388 0.58498534]]

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值