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]]
相關用法
- Python numpy matrix eye()用法及代碼示例
- Python numpy matrix ones()用法及代碼示例
- Python numpy matrix repmat()用法及代碼示例
- Python numpy matrix zeros()用法及代碼示例
- Python numpy matrix identity()用法及代碼示例
- Python numpy matrix empty()用法及代碼示例
- Python numpy matrix randn()用法及代碼示例
- Python numpy.random.rand()用法及代碼示例
- Python Numpy Numpy.random用法及代碼示例
- Numpy string less()用法及代碼示例
- Numpy string isupper()用法及代碼示例
- Numpy string isnumeric()用法及代碼示例
- Numpy string isdigit()用法及代碼示例
- Numpy string isalpha()用法及代碼示例
- Numpy string lstrip()用法及代碼示例
注:本文由純淨天空篩選整理自jana_sayantan大神的英文原創作品 numpy matrix operations | rand() function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。