X Tutup
{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import numpy as np" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "arr = np.random.rand(5,5)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### sort an array along a specified axis" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[0.32367677 0.160538 0.07577069 0.02051216 0.23410999]\n", " [0.54927129 0.20910696 0.2196075 0.22756129 0.67417506]\n", " [0.63553217 0.56279363 0.62776371 0.32657277 0.79901529]\n", " [0.7183594 0.69288395 0.70838123 0.52581954 0.96520237]\n", " [0.81701677 0.79908087 0.98800108 0.54773388 0.97694313]]\n" ] } ], "source": [ "# sort along the row and return a copy\n", "print(np.sort(arr, axis=0)) " ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[0.32367677 0.160538 0.07577069 0.02051216 0.23410999]\n", " [0.54927129 0.20910696 0.2196075 0.22756129 0.67417506]\n", " [0.63553217 0.56279363 0.62776371 0.32657277 0.79901529]\n", " [0.7183594 0.69288395 0.70838123 0.52581954 0.96520237]\n", " [0.81701677 0.79908087 0.98800108 0.54773388 0.97694313]]\n" ] } ], "source": [ "# sort along the row in place\n", "arr.sort(axis=0)\n", "print(arr)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[0.02051216 0.07577069 0.160538 0.23410999 0.32367677]\n", " [0.20910696 0.2196075 0.22756129 0.54927129 0.67417506]\n", " [0.32657277 0.56279363 0.62776371 0.63553217 0.79901529]\n", " [0.52581954 0.69288395 0.70838123 0.7183594 0.96520237]\n", " [0.54773388 0.79908087 0.81701677 0.97694313 0.98800108]]\n" ] } ], "source": [ "# sort along the column and return a copy\n", "print(np.sort(arr, axis=1)) " ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[0.02051216 0.07577069 0.160538 0.23410999 0.32367677]\n", " [0.20910696 0.2196075 0.22756129 0.54927129 0.67417506]\n", " [0.32657277 0.56279363 0.62776371 0.63553217 0.79901529]\n", " [0.52581954 0.69288395 0.70838123 0.7183594 0.96520237]\n", " [0.54773388 0.79908087 0.81701677 0.97694313 0.98800108]]\n" ] } ], "source": [ "# sort along the column in place\n", "arr.sort(axis=1) \n", "print(arr)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### compute the indices that would sort an array along a specified axis" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [], "source": [ "arr = np.random.rand(5,5)" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[1 1 2 2 0]\n", " [2 2 1 3 2]\n", " [0 4 0 1 4]\n", " [4 3 3 0 3]\n", " [3 0 4 4 1]]\n" ] } ], "source": [ "# along the row\n", "print(np.argsort(arr, axis=0))" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[4 3 2 0 1]\n", " [0 3 1 2 4]\n", " [3 2 4 0 1]\n", " [3 4 0 1 2]\n", " [4 0 1 2 3]]\n" ] } ], "source": [ "# along the column\n", "print(np.argsort(arr, axis=1))" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[13 12 5 18 4 8 6 7 14 3 10 24 2 11 0 19 20 21 9 15 16 17 22 1\n", " 23]\n" ] } ], "source": [ "# if axis=None, return the indices of a flattened array\n", "print(np.argsort(arr, axis=None))" ] } ], "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.6.6" } }, "nbformat": 4, "nbformat_minor": 2 }
X Tutup