From 9c4ee843a96975f3a3f6cc1b206e059b75edc902 Mon Sep 17 00:00:00 2001 From: Weiheng Su <52441506+BuWeiSU@users.noreply.github.com> Date: Mon, 22 May 2023 14:13:55 +0800 Subject: [PATCH] added and fixed demean_axis in appa --- appa.ipynb | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/appa.ipynb b/appa.ipynb index a4e26b8ad..3f31cdccb 100644 --- a/appa.ipynb +++ b/appa.ipynb @@ -387,6 +387,22 @@ "demeaned.mean(2)" ] }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "def demean_axis(arr, axis=0):\n", + " means = arr.mean(axis)\n", + "\n", + " # This generalizes things like [:, :, np.newaxis] to N dimensions\n", + " indexer = [slice(None)] * arr.ndim\n", + " indexer[axis] = np.newaxis\n", + " #Tuple is added in the next line to avoid non-tuple sequence for multidimensional indexing error\n", + " return arr - means[tuple(indexer)]" + ] + }, { "cell_type": "code", "execution_count": 36, @@ -939,9 +955,9 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.6" + "version": "3.10.7" } }, "nbformat": 4, "nbformat_minor": 4 -} \ No newline at end of file +}