From 5dcc6da9ed081324c40e49767f7b2b4182ec7338 Mon Sep 17 00:00:00 2001 From: Yassine Regayeg Date: Mon, 16 Dec 2024 14:25:09 +0100 Subject: [PATCH 01/10] Update numpy_questions.py --- numpy_questions.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/numpy_questions.py b/numpy_questions.py index 07a10c1..81d35d6 100644 --- a/numpy_questions.py +++ b/numpy_questions.py @@ -22,7 +22,7 @@ def max_index(X): """Return the index of the maximum in a numpy array. Parameters - ---------- + ------- X : ndarray of shape (n_samples, n_features) The input array. From 17365a3da758c8f5313c13f2e10f370c8b1d54b9 Mon Sep 17 00:00:00 2001 From: Yassine Regayeg Date: Mon, 16 Dec 2024 15:39:45 +0000 Subject: [PATCH 02/10] change --- numpy_questions.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/numpy_questions.py b/numpy_questions.py index 81d35d6..27bf368 100644 --- a/numpy_questions.py +++ b/numpy_questions.py @@ -27,7 +27,7 @@ def max_index(X): The input array. Returns - ------- + (i, j) : tuple(int) The row and columnd index of the maximum. From 5f4bfed2bff5e58b2c2b7129768c96530eb8efed Mon Sep 17 00:00:00 2001 From: Yassine Regayeg Date: Mon, 16 Dec 2024 16:16:05 +0000 Subject: [PATCH 03/10] update --- numpy_questions.py | 46 ++++------------------ test.ipynb | 95 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 102 insertions(+), 39 deletions(-) create mode 100644 test.ipynb diff --git a/numpy_questions.py b/numpy_questions.py index 27bf368..fb4a5e5 100644 --- a/numpy_questions.py +++ b/numpy_questions.py @@ -19,49 +19,17 @@ def max_index(X): - """Return the index of the maximum in a numpy array. - - Parameters - ------- - X : ndarray of shape (n_samples, n_features) - The input array. - - Returns - (i, j) : tuple(int) - The row and columnd index of the maximum. - - Raises - ------ - ValueError - If the input is not a numpy array or - if the shape is not 2D. - """ - i = 0 - j = 0 - - # TODO + flat_index = np.argmax(X) # Flattened index of the maximum + i, j = divmod(flat_index, X.shape[1]) return i, j def wallis_product(n_terms): - """Implement the Wallis product to compute an approximation of pi. - - See: - https://en.wikipedia.org/wiki/Wallis_product - - Parameters - ---------- - n_terms : int - Number of steps in the Wallis product. Note that `n_terms=0` will - consider the product to be `1`. + produit = 1 + for i in range(1, n_terms+1): + produit *= 4*i**2/(4*i**2 - 1) + return(produit) - Returns - ------- - pi : float - The approximation of order `n_terms` of pi using the Wallis product. - """ - # XXX : The n_terms is an int that corresponds to the number of - # terms in the product. For example 10000. - return 0. +print("The approxiamtion of pi is", 2*wallis_product(1000000)) \ No newline at end of file diff --git a/test.ipynb b/test.ipynb new file mode 100644 index 0000000..904903b --- /dev/null +++ b/test.ipynb @@ -0,0 +1,95 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "3.141592653589793\n" + ] + } + ], + "source": [ + "import numpy as np " + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [], + "source": [ + "import pandas as pd" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [], + "source": [ + "def max_index(X):\n", + " \n", + " flat_index = np.argmax(X) # Flattened index of the maximum\n", + " i, j = divmod(flat_index, X.shape[1])\n", + "\n", + " return (i,j)" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": {}, + "outputs": [], + "source": [ + "def wallis_product(n_terms):\n", + " produit = 1\n", + " for i in range(1, n_terms+1):\n", + " produit *= 4*i**2/(4*i**2 - 1)\n", + " return(produit)" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The approxiamtion of pi is 3.141591868192149\n" + ] + } + ], + "source": [ + "print(\"The approxiamtion of pi is\", 2*wallis_product(1000000))" + ] + } + ], + "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.12.1" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} From fa8013bc047a143dec11eca7a98e85f8d7ac1707 Mon Sep 17 00:00:00 2001 From: Yassine Regayeg Date: Mon, 16 Dec 2024 17:11:45 +0000 Subject: [PATCH 04/10] update --- numpy_questions.py | 24 +++++++++++++++++------- test_numpy_questions.py | 3 +++ 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/numpy_questions.py b/numpy_questions.py index fb4a5e5..0410c2d 100644 --- a/numpy_questions.py +++ b/numpy_questions.py @@ -19,17 +19,27 @@ def max_index(X): + """Retourne les indices (i, j) de la valeur maximale dans une matrice 2D.""" + if not isinstance(X, np.ndarray): + raise ValueError("X doit être un tableau numpy.") + if X.ndim != 2: + raise ValueError("X doit être une matrice 2D.") - flat_index = np.argmax(X) # Flattened index of the maximum + # Trouver l'indice aplati du maximum. + flat_index = np.argmax(X) + # Convertir en indices 2D. i, j = divmod(flat_index, X.shape[1]) - return i, j + def wallis_product(n_terms): produit = 1 - for i in range(1, n_terms+1): - produit *= 4*i**2/(4*i**2 - 1) - return(produit) - -print("The approxiamtion of pi is", 2*wallis_product(1000000)) \ No newline at end of file + if n_terms==0: + return(2) + else: + for i in range(1, n_terms+1): + produit *= 4*i**2/(4*i**2 - 1) + return(2*produit) + +print("The approxiamtion of pi is", wallis_product(1000000)) \ No newline at end of file diff --git a/test_numpy_questions.py b/test_numpy_questions.py index 8be9fab..b99af65 100644 --- a/test_numpy_questions.py +++ b/test_numpy_questions.py @@ -37,3 +37,6 @@ def test_wallis_product(): pi_approx = wallis_product(100000) assert abs(pi_approx - m.pi) < 1e-4 + +test_max_index() +test_wallis_product() \ No newline at end of file From 5558f7aaa6f3b34792d5fd34685a0485f288fcde Mon Sep 17 00:00:00 2001 From: Yassine Regayeg Date: Mon, 16 Dec 2024 18:16:53 +0100 Subject: [PATCH 05/10] Delete test.ipynb --- test.ipynb | 95 ------------------------------------------------------ 1 file changed, 95 deletions(-) delete mode 100644 test.ipynb diff --git a/test.ipynb b/test.ipynb deleted file mode 100644 index 904903b..0000000 --- a/test.ipynb +++ /dev/null @@ -1,95 +0,0 @@ -{ - "cells": [ - { - "cell_type": "code", - "execution_count": 1, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "3.141592653589793\n" - ] - } - ], - "source": [ - "import numpy as np " - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "metadata": {}, - "outputs": [], - "source": [ - "import pandas as pd" - ] - }, - { - "cell_type": "code", - "execution_count": 4, - "metadata": {}, - "outputs": [], - "source": [ - "def max_index(X):\n", - " \n", - " flat_index = np.argmax(X) # Flattened index of the maximum\n", - " i, j = divmod(flat_index, X.shape[1])\n", - "\n", - " return (i,j)" - ] - }, - { - "cell_type": "code", - "execution_count": 14, - "metadata": {}, - "outputs": [], - "source": [ - "def wallis_product(n_terms):\n", - " produit = 1\n", - " for i in range(1, n_terms+1):\n", - " produit *= 4*i**2/(4*i**2 - 1)\n", - " return(produit)" - ] - }, - { - "cell_type": "code", - "execution_count": 15, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "The approxiamtion of pi is 3.141591868192149\n" - ] - } - ], - "source": [ - "print(\"The approxiamtion of pi is\", 2*wallis_product(1000000))" - ] - } - ], - "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.12.1" - } - }, - "nbformat": 4, - "nbformat_minor": 2 -} From 4764b9afc8bb225a2c5d63e4fb874ff60699424e Mon Sep 17 00:00:00 2001 From: Yassine Regayeg Date: Tue, 17 Dec 2024 19:58:10 +0100 Subject: [PATCH 06/10] Update test_numpy_questions.py --- test_numpy_questions.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/test_numpy_questions.py b/test_numpy_questions.py index b99af65..8be9fab 100644 --- a/test_numpy_questions.py +++ b/test_numpy_questions.py @@ -37,6 +37,3 @@ def test_wallis_product(): pi_approx = wallis_product(100000) assert abs(pi_approx - m.pi) < 1e-4 - -test_max_index() -test_wallis_product() \ No newline at end of file From 32c7bd7525e7b462268339b7e18970d7e7ebbbbf Mon Sep 17 00:00:00 2001 From: Yassine Regayeg Date: Tue, 17 Dec 2024 20:01:51 +0100 Subject: [PATCH 07/10] Update numpy_questions.py --- numpy_questions.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/numpy_questions.py b/numpy_questions.py index 0410c2d..a761f8f 100644 --- a/numpy_questions.py +++ b/numpy_questions.py @@ -24,7 +24,6 @@ def max_index(X): raise ValueError("X doit être un tableau numpy.") if X.ndim != 2: raise ValueError("X doit être une matrice 2D.") - # Trouver l'indice aplati du maximum. flat_index = np.argmax(X) # Convertir en indices 2D. @@ -41,5 +40,3 @@ def wallis_product(n_terms): for i in range(1, n_terms+1): produit *= 4*i**2/(4*i**2 - 1) return(2*produit) - -print("The approxiamtion of pi is", wallis_product(1000000)) \ No newline at end of file From 06adab33d4f20e32d15d08ad9fbc6751ce781a58 Mon Sep 17 00:00:00 2001 From: Yassine Regayeg Date: Tue, 17 Dec 2024 20:10:29 +0100 Subject: [PATCH 08/10] Update numpy_questions.py --- numpy_questions.py | 54 +++++++++++++++++++++++++++++++++++----------- 1 file changed, 41 insertions(+), 13 deletions(-) diff --git a/numpy_questions.py b/numpy_questions.py index a761f8f..0721586 100644 --- a/numpy_questions.py +++ b/numpy_questions.py @@ -18,25 +18,53 @@ import numpy as np + def max_index(X): - """Retourne les indices (i, j) de la valeur maximale dans une matrice 2D.""" + """ + Return the indices (i, j) of the maximum value in a 2D matrix. + + Parameters: + X (np.ndarray): A 2D numpy array. + + Returns: + tuple: Indices (i, j) of the maximum value in the array. + + Raises: + ValueError: If X is not a numpy array or not a 2D array. + """ if not isinstance(X, np.ndarray): - raise ValueError("X doit être un tableau numpy.") + raise ValueError("X must be a numpy array.") if X.ndim != 2: - raise ValueError("X doit être une matrice 2D.") - # Trouver l'indice aplati du maximum. + raise ValueError("X must be a 2D array.") + + # Find the flattened index of the maximum value flat_index = np.argmax(X) - # Convertir en indices 2D. + # Convert to 2D indices i, j = divmod(flat_index, X.shape[1]) return i, j - def wallis_product(n_terms): - produit = 1 - if n_terms==0: - return(2) - else: - for i in range(1, n_terms+1): - produit *= 4*i**2/(4*i**2 - 1) - return(2*produit) + """ + Approximate the value of pi using the Wallis product. + + Parameters: + n_terms (int): The number of terms in the Wallis product. + + Returns: + float: Approximation of pi. + + Raises: + ValueError: If n_terms is negative. + """ + if not isinstance(n_terms, int) or n_terms < 0: + raise ValueError("n_terms must be a non-negative integer.") + + product = 1.0 + if n_terms == 0: + return 2.0 + + for i in range(1, n_terms + 1): + product *= (4 * i**2) / (4 * i**2 - 1) + + return 2 * product From b8cca7e02c64fdb2b3f094cc5694b96a34873f13 Mon Sep 17 00:00:00 2001 From: Yassine Regayeg Date: Tue, 17 Dec 2024 20:12:55 +0100 Subject: [PATCH 09/10] Update numpy_questions.py --- numpy_questions.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/numpy_questions.py b/numpy_questions.py index 0721586..2c62738 100644 --- a/numpy_questions.py +++ b/numpy_questions.py @@ -17,8 +17,6 @@ """ import numpy as np - - def max_index(X): """ Return the indices (i, j) of the maximum value in a 2D matrix. @@ -43,7 +41,6 @@ def max_index(X): i, j = divmod(flat_index, X.shape[1]) return i, j - def wallis_product(n_terms): """ Approximate the value of pi using the Wallis product. From ba2f9c3e3b33b2e718edd0c308a1dffca2981214 Mon Sep 17 00:00:00 2001 From: Yassine Regayeg Date: Tue, 17 Dec 2024 20:14:35 +0100 Subject: [PATCH 10/10] Update numpy_questions.py --- numpy_questions.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/numpy_questions.py b/numpy_questions.py index 2c62738..5214d20 100644 --- a/numpy_questions.py +++ b/numpy_questions.py @@ -1,4 +1,5 @@ -"""Assignment - using numpy and making a PR. +""" +Assignment - using numpy and making a PR. The goals of this assignment are: * Use numpy in practice with two easy exercises. @@ -17,6 +18,7 @@ """ import numpy as np + def max_index(X): """ Return the indices (i, j) of the maximum value in a 2D matrix. @@ -41,6 +43,7 @@ def max_index(X): i, j = divmod(flat_index, X.shape[1]) return i, j + def wallis_product(n_terms): """ Approximate the value of pi using the Wallis product.