網站首頁 編程語言 正文
在矩陣應用的過程中,經常需要使用隨機數,那么怎么使用numpy 產生隨機數呢 ,為此專門做一個總結。
random模塊用于生成隨機數,下面是一些常用的函數用法:
numpy.random.seed(n) 其中n為任意指定
當我們設置相同的seed,每次生成的隨機數相同。如果不設置seed,則每次會生成不同的隨機數
numpy.random.seed(0)
np.random.seed(0)
a = np.random.rand(4)
a
array([0.5488135 , 0.71518937, 0.60276338, 0.54488318])
主要介紹了生成符合均勻分布,正態分布等數組和隨機選取數以及打亂數組順序的方法。
1.np.random.rand 生成一個0到1之間的均勻分布
import numpy as np
a = np.random.rand(2,3,4)
print(a,a.shape)
[[[0.18000344 0.03724064 0.15040061 0.93007827]
? [0.59423019 0.35439936 0.49193457 0.37633185]
? [0.83924196 0.4908405 ?0.49387427 0.98718216]]?[[0.20072849 0.90163245 0.36710883 0.56668257]
? [0.61402791 0.46602958 0.56086072 0.83099671]
? [0.85196098 0.62774727 0.62826083 0.41739078]]] (2, 3, 4)
2.np.random.randn 返回一個符合標準正態分布的數組。
a = np.random.randn(2,3,4)
print(a,a.shape)
[[[ 0.32062268 ?0.08867553 -0.83741647 -0.21917891]
? [-0.06516898 -1.17123767 ?2.2403833 ?-0.77741757]
? [ 0.33532261 ?0.27309929 ?1.07279005 ?0.79952468]]?[[ 0.18503166 ?0.90777579 -1.52837098 -1.23783753]
? [ 0.9327577 ? 1.61876194 ?0.52191996 ?0.53451075]
? [-1.05485337 ?1.01472352 ?0.19376936 ?0.00278223]]] (2, 3, 4)
3.np.random.randint返回一定范圍的一維或者多維整數
numpy.random.randint(low, high=None, size=None, dtype=’l’)
返回隨機整數,范圍區間為[low,high),包含low,不包含high
size為數組維度,元組形式,如(2,3)#2行3列
high沒有填寫時,默認生成隨機數的范圍是[0,low)
dtype指定數據類型,默認int
a = np.random.randint(low=6,high=10,size=(2,3,4),dtype='int')
print(a,a.shape)
[[[8 8 7 8]
? [8 8 6 9]
? [9 6 7 7]]?[[7 7 9 8]
? [9 6 6 7]
? [8 9 7 7]]] (2, 3, 4)
4.np.random.choice從給定的一維數組中隨機選擇數生成隨機數
numpy.random.choice(a, size=None, replace=True, p=None)
a為一維數組類似數據或整數;size為數組維度;p為數組中的數據出現的概率
a為整數時,對應的一維數組為np.arange(a)
a = np.random.choice(a = [3,5,6],size=(2,3,4),replace=True,p=[0.1,0.5,0.4])
print(a,a.shape)
[[[5 6 3 5]
? [6 5 5 5]
? [6 5 6 6]]?[[5 5 5 3]
? [6 5 6 6]
? [5 6 5 6]]] (2, 3, 4)
5.np.random.normal(loc=0.0, scale=1.0, size=None),生成符合指定分布的正態分布。
a = np.random.normal(loc=4,scale=6,size=(2,3,4))
print(a)
[[[13.19667529 12.81615262 ?4.92968455 ?6.26897512]
? [-1.32671449 -7.88477881 ?1.9125271 ? 4.93809381]
? [11.38174408 11.21427909 ?1.6760391 ? 2.1861835 ]]?[[-2.29131779 -4.52010762 -6.23762114 15.70465237]
? [ 0.94208691 ?1.37155419 -3.51677216 ?8.66494213]
? [-5.68338709 ?2.72355832 -1.37279937 ?6.32141499]]]
6.np.random.random(size=None),生成符合0到1的均勻分布數組。
a = np.random.random((2,3,4))
print(a)
[[[0.19658236 0.36872517 0.82099323 0.09710128]
? [0.83794491 0.09609841 0.97645947 0.4686512 ]
? [0.97676109 0.60484552 0.73926358 0.03918779]]?[[0.28280696 0.12019656 0.2961402 ?0.11872772]
? [0.31798318 0.41426299 0.0641475 ?0.69247212]
? [0.56660145 0.26538949 0.52324805 0.09394051]]]
7. np.random.ranf(size=None),生成符合0到1的均勻分布數組。
a = np.random.ranf((10))
a
array([0.82894003, 0.00469548, 0.67781654, 0.27000797, 0.73519402,
0.96218855, 0.24875314, 0.57615733, 0.59204193, 0.57225191])
8.np.random.uniform(low=0.0, high=1.0, size=None),生成符合指定均勻分布的數組
g=np.random.uniform(-1,1,10)#指定均勻分布
print(g)
[ 0.07315842 ?0.79334259 ?0.98067789 -0.56620603 ?0.32615641 -0.47335525
?-0.958698 ? ?0.51675731 -0.3599657 ?-0.23307221]
9.np.random.shuffle(x),隨機打亂數組順序
a = np.arange(10)
np.random.shuffle(a)
print(a)
[6 3 4 9 0 8 1 5 2 7]
10. 產生其他分布的函數
- binomial() ,二項分布
- chisquare(),卡方分布
- poisson(),泊松分布
- uiform(),均勻分布
- normal(),正態分布
原文鏈接:https://blog.csdn.net/qq_43790749/article/details/120069276
相關推薦
- 2022-04-23 uni-app之條件注釋實現跨端兼容
- 2023-04-08 React中的useEffect?useLayoutEffect到底怎么用_React
- 2022-08-16 C#獲取Description特性的擴展類詳解_C#教程
- 2022-05-08 Entity?Framework根據實體的EntityState狀態實現增刪改查_實用技巧
- 2022-05-29 C#實現文字轉語音功能_C#教程
- 2022-06-18 C#使用LOCK實現線程同步_C#教程
- 2022-09-08 Go語言中循環Loop的用法介紹_Golang
- 2022-09-09 Nginx配置解決NetCore的跨域問題_nginx
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支