From f0d92ed229892ae9cdace8d77cc217e00a443a20 Mon Sep 17 00:00:00 2001 From: Alexander Hess Date: Mon, 15 Jul 2024 11:26:43 +0200 Subject: [PATCH] Set random seeds where applicable --- .../05_content_functions.ipynb | 34 +++++++-- 01_scientific_stack/00_content_numpy.ipynb | 20 ++++- 03_classification/00_content.ipynb | 76 +++++++++---------- 3 files changed, 82 insertions(+), 48 deletions(-) diff --git a/00_python_in_a_nutshell/05_content_functions.ipynb b/00_python_in_a_nutshell/05_content_functions.ipynb index 27c40fa..aed2f4c 100644 --- a/00_python_in_a_nutshell/05_content_functions.ipynb +++ b/00_python_in_a_nutshell/05_content_functions.ipynb @@ -418,21 +418,39 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "To access a function inside the [random ](https://docs.python.org/3/library/random.html) module, for example, the [random() ](https://docs.python.org/3/library/random.html#random.random) function, we use the `.` operator, formally called the attribute access operator. The [random() ](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 ](https://docs.python.org/3/library/random.html) module, for example, the [seed() ](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() ](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() ](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" } diff --git a/01_scientific_stack/00_content_numpy.ipynb b/01_scientific_stack/00_content_numpy.ipynb index b11a41f..ffd42c3 100644 --- a/01_scientific_stack/00_content_numpy.ipynb +++ b/01_scientific_stack/00_content_numpy.ipynb @@ -1274,13 +1274,29 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "Let us quickly generate some random data points and draw a scatter plot with [matplotlib ](https://matplotlib.org/)'s [plt.scatter() ](https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.scatter.html#matplotlib.pyplot.scatter) function." + "First, let's set the [np.random.seed() ](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 ](https://matplotlib.org/)'s [plt.scatter() ](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 @@ "" ] }, - "execution_count": 44, + "execution_count": 45, "metadata": {}, "output_type": "execute_result" }, diff --git a/03_classification/00_content.ipynb b/03_classification/00_content.ipynb index 731da15..8a9c7e5 100644 --- a/03_classification/00_content.ipynb +++ b/03_classification/00_content.ipynb @@ -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" ] }