-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #29 from Jingjing092/main
numpy-random
- Loading branch information
Showing
1 changed file
with
310 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,310 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"# 随机数\n", | ||
" `numpy.random` 模块用于生成随机数(Random)。\n", | ||
" 电脑产生随机数是由一定的计算方法计算出的数值。只要计算方法一定,随机种子一定,那么产生的随机数就不会改变。" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 21, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"\n", | ||
"import numpy as np" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"## 随机数种子\n", | ||
" `seed(s)` 函数,随机数种子, `s` 是给定的种子值,用于保证每次随机生成的数组或数与上一次相同。" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"现在我们规定随机种子为 `10` ,使用 `.randint` 函数在 `[100,400)` 之前随机生成 `2` 个维度为 `3` ,维度下元素为 `4` 的随机整数数组,则,在规定了随机种子后,每次运行下方随机数生成代码生成的随机数相同。" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 22, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"data": { | ||
"text/plain": [ | ||
"array([[[365, 225, 115, 223],\n", | ||
" [256, 321, 108, 173],\n", | ||
" [356, 140, 116, 339]],\n", | ||
"\n", | ||
" [[154, 222, 162, 133],\n", | ||
" [300, 277, 279, 154],\n", | ||
" [177, 113, 343, 241]]])" | ||
] | ||
}, | ||
"execution_count": 22, | ||
"metadata": {}, | ||
"output_type": "execute_result" | ||
} | ||
], | ||
"source": [ | ||
"np.random.seed(10) \n", | ||
"np.random.randint(100,400,(2,3,4)) " | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"## 随机数生成\n" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"### 均匀分布\n", | ||
" `rand(d0,d1,..,dn)` 函数,根据 `d0-dn` 创建随机数数组,生成一个 `[0,1)` 之间的随机浮点数或 `N` 维浮点数组。\n" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"随机生成 `2` 个,维度为 `2` ,维度下元素为 `3` 的浮点数组,服从 `[0,1)` 均匀分布。" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 23, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"data": { | ||
"text/plain": [ | ||
"array([[[0.58390137, 0.18263144, 0.82608225],\n", | ||
" [0.10540183, 0.28357668, 0.06556327]],\n", | ||
"\n", | ||
" [[0.05644419, 0.76545582, 0.01178803],\n", | ||
" [0.61194334, 0.33188226, 0.55964837]]])" | ||
] | ||
}, | ||
"execution_count": 23, | ||
"metadata": {}, | ||
"output_type": "execute_result" | ||
} | ||
], | ||
"source": [ | ||
"np.random.rand(2,2,3)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"### 正态分布\n", | ||
" `randn(d0,d1,..,dn)` 函数,根据 `d0-dn` 创建随机数数组,生成一个的随机浮点数或 `N` 维浮点数组,服从标准正态分布。" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"随机生成 `2` 个,维度为 `2` ,维度下元素为 `3` 的浮点数组,服从标准正态分布。" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 24, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"data": { | ||
"text/plain": [ | ||
"array([[[-0.94245806, -1.74554298, -1.02094527],\n", | ||
" [-0.16206903, -0.6579709 , 0.42211306]],\n", | ||
"\n", | ||
" [[ 0.62402334, 0.5016401 , 0.84143473],\n", | ||
" [-1.64024998, 1.05800516, -0.09178444]]])" | ||
] | ||
}, | ||
"execution_count": 24, | ||
"metadata": {}, | ||
"output_type": "execute_result" | ||
} | ||
], | ||
"source": [ | ||
"np.random.randn(2,2,3)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"### 制定随机生成范围\n", | ||
" `randint(low[,low,shape]))` 函数,根据 `shape` 创建随机整数或整数数组,范围为 `[low,high)` 。" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"在 `[100,400)` 之前随机生成 `2` 个维度为 `3` ,维度下元素为 `4` 的随机整数数组。" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 25, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"data": { | ||
"text/plain": [ | ||
"array([[[234, 346, 185, 334],\n", | ||
" [250, 111, 212, 368],\n", | ||
" [320, 162, 285, 179]],\n", | ||
"\n", | ||
" [[332, 142, 337, 335],\n", | ||
" [145, 301, 265, 228],\n", | ||
" [123, 103, 385, 372]]])" | ||
] | ||
}, | ||
"execution_count": 25, | ||
"metadata": {}, | ||
"output_type": "execute_result" | ||
} | ||
], | ||
"source": [ | ||
"np.random.randint(100,400,(2,3,4)) " | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"## 排列\n", | ||
"将给定对象进行随机排列。" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
" `shuffle(x)` 函数,打乱对象 `x` (矩阵或者列表),其中,多维矩阵按照第一维打乱。" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"现在,我们在 `[100,400)` 之前随机生成 `1` 个维度为 `6` ,维度下元素为 `2` 的随机整数数组,并将该矩阵按照行打乱。" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 26, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"[[212 114]\n", | ||
" [151 179]\n", | ||
" [343 373]\n", | ||
" [278 153]\n", | ||
" [334 373]\n", | ||
" [333 141]]\n", | ||
"[[212 114]\n", | ||
" [151 179]\n", | ||
" [278 153]\n", | ||
" [333 141]\n", | ||
" [334 373]\n", | ||
" [343 373]]\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"x = np.random.randint(100,400,(6,2)) \n", | ||
"print(x)\n", | ||
"np.random.shuffle(x)\n", | ||
"print(x)\n" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"`permutation(x)` 函数,打乱并返回对象 `x` (整数或者矩阵),其中,多维矩阵按照第一维打乱。" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"现在,我们在 `[100,400)` 之前随机生成 `1` 个维度为 `6` ,维度下元素为 `2` 的随机整数数组,并将该矩阵按照行打乱。" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 27, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"[[244 228]\n", | ||
" [259 301]\n", | ||
" [344 354]\n", | ||
" [122 196]\n", | ||
" [167 195]\n", | ||
" [182 162]]\n", | ||
"[[167 195]\n", | ||
" [259 301]\n", | ||
" [244 228]\n", | ||
" [122 196]\n", | ||
" [344 354]\n", | ||
" [182 162]]\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"x = np.random.randint(100,400,(6,2)) \n", | ||
"print(x)\n", | ||
"y = np.random.permutation(x)\n", | ||
"print(y)\n" | ||
] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "Python 3", | ||
"language": "python", | ||
"name": "python3" | ||
}, | ||
"language_info": { | ||
"codemirror_mode": { | ||
"name": "ipython", | ||
"version": 3 | ||
}, | ||
"file_extension": ".py", | ||
"mimetype": "text/x-python", | ||
"name": "python", | ||
"nbconvert_exporter": "python", | ||
"pygments_lexer": "ipython3", | ||
"version": "3.10.11" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 2 | ||
} |