Set random seeds where applicable
This commit is contained in:
parent
3125c82096
commit
f0d92ed229
3 changed files with 82 additions and 48 deletions
|
@ -418,21 +418,39 @@
|
|||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"To access a function inside the [random <img height=\"12\" style=\"display: inline-block\" src=\"../static/link/to_py.png\">](https://docs.python.org/3/library/random.html) module, for example, the [random() <img height=\"12\" style=\"display: inline-block\" src=\"../static/link/to_py.png\">](https://docs.python.org/3/library/random.html#random.random) function, we use the `.` operator, formally called the attribute access operator. The [random() <img height=\"12\" style=\"display: inline-block\" src=\"../static/link/to_py.png\">](https://docs.python.org/3/library/random.html#random.random) function simply returns a random decimal number between `0` and `1`."
|
||||
"To access a function inside the [random <img height=\"12\" style=\"display: inline-block\" src=\"../static/link/to_py.png\">](https://docs.python.org/3/library/random.html) module, for example, the [seed() <img height=\"12\" style=\"display: inline-block\" src=\"../static/link/to_py.png\">](https://docs.python.org/3/library/random.html#random.seed) function, we use the `.` operator, formally called the attribute access operator. \n",
|
||||
"\n",
|
||||
"We use [random.seed() <img height=\"12\" style=\"display: inline-block\" src=\"../static/link/to_py.png\">](https://docs.python.org/3/library/random.html#random.seed) to make the random numbers *replicable* on separate runs of this notebook."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 16,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"random.seed(42)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"The [random() <img height=\"12\" style=\"display: inline-block\" src=\"../static/link/to_py.png\">](https://docs.python.org/3/library/random.html#random.random) function simply returns a random decimal number between `0` and `1`."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 17,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"0.7021021034327006"
|
||||
"0.6394267984578837"
|
||||
]
|
||||
},
|
||||
"execution_count": 16,
|
||||
"execution_count": 17,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
|
@ -450,16 +468,16 @@
|
|||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 17,
|
||||
"execution_count": 18,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"False"
|
||||
"True"
|
||||
]
|
||||
},
|
||||
"execution_count": 17,
|
||||
"execution_count": 18,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
|
@ -477,7 +495,7 @@
|
|||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 18,
|
||||
"execution_count": 19,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
|
@ -486,7 +504,7 @@
|
|||
"3"
|
||||
]
|
||||
},
|
||||
"execution_count": 18,
|
||||
"execution_count": 19,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
|
|
|
@ -1274,13 +1274,29 @@
|
|||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Let us quickly generate some random data points and draw a scatter plot with [matplotlib <img height=\"12\" style=\"display: inline-block\" src=\"../static/link/to_plt.png\">](https://matplotlib.org/)'s [plt.scatter() <img height=\"12\" style=\"display: inline-block\" src=\"../static/link/to_plt.png\">](https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.scatter.html#matplotlib.pyplot.scatter) function."
|
||||
"First, let's set the [np.random.seed() <img height=\"12\" style=\"display: inline-block\" src=\"../static/link/to_np.png\">](https://docs.python.org/3/library/random.html#random.seed) to make the random numbers *replicable* on separate runs of this notebook."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 44,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"np.random.seed(42)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Then, let us quickly generate some random data points and draw a scatter plot with [matplotlib <img height=\"12\" style=\"display: inline-block\" src=\"../static/link/to_plt.png\">](https://matplotlib.org/)'s [plt.scatter() <img height=\"12\" style=\"display: inline-block\" src=\"../static/link/to_plt.png\">](https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.scatter.html#matplotlib.pyplot.scatter) function."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 45,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
|
@ -1288,7 +1304,7 @@
|
|||
"<matplotlib.collections.PathCollection at 0x7f008b235d00>"
|
||||
]
|
||||
},
|
||||
"execution_count": 44,
|
||||
"execution_count": 45,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
},
|
||||
|
|
|
@ -761,9 +761,9 @@
|
|||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"array([1, 0, 2, 2, 1, 0, 1, 1, 1, 0, 0, 2, 2, 0, 0, 2, 0, 1, 0, 0, 2, 2,\n",
|
||||
" 0, 2, 1, 0, 2, 2, 2, 1, 0, 1, 1, 2, 0, 1, 2, 1, 2, 1, 2, 1, 0, 1,\n",
|
||||
" 0])"
|
||||
"array([2, 1, 2, 1, 2, 2, 1, 1, 0, 2, 0, 0, 2, 2, 0, 2, 1, 0, 0, 0, 1, 0,\n",
|
||||
" 1, 2, 2, 1, 1, 1, 1, 0, 2, 2, 1, 0, 2, 0, 0, 0, 0, 1, 1, 0, 2, 2,\n",
|
||||
" 1])"
|
||||
]
|
||||
},
|
||||
"execution_count": 19,
|
||||
|
@ -772,7 +772,7 @@
|
|||
}
|
||||
],
|
||||
"source": [
|
||||
"X_train, X_test, y_train, y_test = train_test_split(X, y, train_size=0.7, test_size=0.3, stratify=y)\n",
|
||||
"X_train, X_test, y_train, y_test = train_test_split(X, y, train_size=0.7, test_size=0.3, random_state=42, stratify=y)\n",
|
||||
"\n",
|
||||
"y_test"
|
||||
]
|
||||
|
@ -869,9 +869,9 @@
|
|||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"array([1, 0, 2, 2, 1, 0, 1, 1, 1, 0, 0, 2, 1, 0, 0, 2, 0, 2, 0, 0, 2, 2,\n",
|
||||
" 0, 2, 1, 0, 2, 1, 2, 1, 0, 1, 1, 2, 0, 1, 2, 1, 2, 1, 2, 1, 0, 1,\n",
|
||||
" 0])"
|
||||
"array([2, 1, 2, 1, 2, 2, 1, 1, 0, 2, 0, 0, 2, 2, 0, 2, 1, 0, 0, 0, 1, 0,\n",
|
||||
" 1, 2, 2, 1, 1, 1, 1, 0, 2, 2, 1, 0, 2, 0, 0, 0, 0, 1, 1, 0, 1, 2,\n",
|
||||
" 1])"
|
||||
]
|
||||
},
|
||||
"execution_count": 23,
|
||||
|
@ -898,9 +898,9 @@
|
|||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"array([1, 0, 2, 2, 1, 0, 1, 1, 1, 0, 0, 2, 2, 0, 0, 2, 0, 1, 0, 0, 2, 2,\n",
|
||||
" 0, 2, 1, 0, 2, 2, 2, 1, 0, 1, 1, 2, 0, 1, 2, 1, 2, 1, 2, 1, 0, 1,\n",
|
||||
" 0])"
|
||||
"array([2, 1, 2, 1, 2, 2, 1, 1, 0, 2, 0, 0, 2, 2, 0, 2, 1, 0, 0, 0, 1, 0,\n",
|
||||
" 1, 2, 2, 1, 1, 1, 1, 0, 2, 2, 1, 0, 2, 0, 0, 0, 0, 1, 1, 0, 2, 2,\n",
|
||||
" 1])"
|
||||
]
|
||||
},
|
||||
"execution_count": 24,
|
||||
|
@ -927,7 +927,7 @@
|
|||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"(array([12, 17, 27]),)"
|
||||
"(array([42]),)"
|
||||
]
|
||||
},
|
||||
"execution_count": 25,
|
||||
|
@ -954,7 +954,7 @@
|
|||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"np.float64(0.9333333333333333)"
|
||||
"np.float64(0.9777777777777777)"
|
||||
]
|
||||
},
|
||||
"execution_count": 26,
|
||||
|
@ -981,7 +981,7 @@
|
|||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"np.float64(0.9523809523809523)"
|
||||
"np.float64(0.9714285714285714)"
|
||||
]
|
||||
},
|
||||
"execution_count": 27,
|
||||
|
@ -1066,35 +1066,35 @@
|
|||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"1 0.9555555555555556\n",
|
||||
"2 0.9333333333333333\n",
|
||||
"3 0.9333333333333333\n",
|
||||
"4 0.9333333333333333\n",
|
||||
"5 0.9333333333333333\n",
|
||||
"1 0.9333333333333333\n",
|
||||
"2 0.9111111111111111\n",
|
||||
"3 0.9555555555555556\n",
|
||||
"4 0.9555555555555556\n",
|
||||
"5 0.9777777777777777\n",
|
||||
"6 0.9333333333333333\n",
|
||||
"7 0.9111111111111111\n",
|
||||
"8 0.9111111111111111\n",
|
||||
"9 0.9111111111111111\n",
|
||||
"10 0.9333333333333333\n",
|
||||
"11 0.9555555555555556\n",
|
||||
"12 0.9555555555555556\n",
|
||||
"7 0.9555555555555556\n",
|
||||
"8 0.9333333333333333\n",
|
||||
"9 0.9555555555555556\n",
|
||||
"10 0.9555555555555556\n",
|
||||
"11 0.9333333333333333\n",
|
||||
"12 0.9333333333333333\n",
|
||||
"13 0.9333333333333333\n",
|
||||
"14 0.9111111111111111\n",
|
||||
"15 0.9333333333333333\n",
|
||||
"16 0.9111111111111111\n",
|
||||
"17 0.9333333333333333\n",
|
||||
"18 0.9111111111111111\n",
|
||||
"19 0.9333333333333333\n",
|
||||
"14 0.9333333333333333\n",
|
||||
"15 0.9555555555555556\n",
|
||||
"16 0.9555555555555556\n",
|
||||
"17 0.9555555555555556\n",
|
||||
"18 0.9555555555555556\n",
|
||||
"19 0.9555555555555556\n",
|
||||
"20 0.9333333333333333\n",
|
||||
"21 0.9333333333333333\n",
|
||||
"21 0.9555555555555556\n",
|
||||
"22 0.9333333333333333\n",
|
||||
"23 0.9111111111111111\n",
|
||||
"24 0.9555555555555556\n",
|
||||
"25 0.9111111111111111\n",
|
||||
"26 0.9333333333333333\n",
|
||||
"27 0.9111111111111111\n",
|
||||
"28 0.9333333333333333\n",
|
||||
"29 0.9555555555555556\n",
|
||||
"23 0.9555555555555556\n",
|
||||
"24 0.9333333333333333\n",
|
||||
"25 0.9333333333333333\n",
|
||||
"26 0.9555555555555556\n",
|
||||
"27 0.9333333333333333\n",
|
||||
"28 0.9111111111111111\n",
|
||||
"29 0.9111111111111111\n",
|
||||
"30 0.9111111111111111\n"
|
||||
]
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue