{ "cells": [ { "cell_type": "markdown", "id": "06ac89df", "metadata": {}, "source": [ "# Sort\n", "\n", "As in Python’s `list`, NumPy arrays can be sorted in-place using the [numpy.sort](https://numpy.org/doc/stable/reference/generated/numpy.sort.html) method. You can sort any one-dimensional section of values in a multidimensional array in place along an axis by passing the axis number to sort:" ] }, { "cell_type": "code", "execution_count": 1, "id": "a4099946", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[-0.61482053, 0.17896449, -1.29336591],\n", " [ 0.08364197, 0.67494142, -0.59437682],\n", " [-1.72673297, 0.96615644, 1.60456092],\n", " [ 0.98478964, -0.70769764, 0.60908458],\n", " [ 2.572301 , 1.20356725, 0.10333016],\n", " [-0.57424083, 0.87287599, 0.47258124],\n", " [ 1.2490576 , 0.99737005, -0.73766589]])" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import numpy as np\n", "\n", "\n", "data = np.random.randn(7, 3)\n", "\n", "data" ] }, { "cell_type": "code", "execution_count": 2, "id": "b2d7e426", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[-1.72673297, -0.70769764, -1.29336591],\n", " [-0.61482053, 0.17896449, -0.73766589],\n", " [-0.57424083, 0.67494142, -0.59437682],\n", " [ 0.08364197, 0.87287599, 0.10333016],\n", " [ 0.98478964, 0.96615644, 0.47258124],\n", " [ 1.2490576 , 0.99737005, 0.60908458],\n", " [ 2.572301 , 1.20356725, 1.60456092]])" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data.sort(0)\n", "\n", "data" ] }, { "cell_type": "markdown", "id": "bccf7aeb", "metadata": {}, "source": [ "`np.sort`, on the other hand, returns a sorted copy of an array instead of changing the array in place:" ] }, { "cell_type": "code", "execution_count": 3, "id": "4d6e17da", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[-1.72673297, -1.29336591, -0.70769764],\n", " [-0.73766589, -0.61482053, 0.17896449],\n", " [-0.59437682, -0.57424083, 0.67494142],\n", " [ 0.08364197, 0.10333016, 0.87287599],\n", " [ 0.47258124, 0.96615644, 0.98478964],\n", " [ 0.60908458, 0.99737005, 1.2490576 ],\n", " [ 1.20356725, 1.60456092, 2.572301 ]])" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.sort(data, axis=1)" ] }, { "cell_type": "code", "execution_count": 4, "id": "97af8303", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[-1.72673297, -0.70769764, -1.29336591],\n", " [-0.61482053, 0.17896449, -0.73766589],\n", " [-0.57424083, 0.67494142, -0.59437682],\n", " [ 0.08364197, 0.87287599, 0.10333016],\n", " [ 0.98478964, 0.96615644, 0.47258124],\n", " [ 1.2490576 , 0.99737005, 0.60908458],\n", " [ 2.572301 , 1.20356725, 1.60456092]])" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3.13 Kernel", "language": "python", "name": "python313" }, "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.13.0" }, "widgets": { "application/vnd.jupyter.widget-state+json": { "state": {}, "version_major": 2, "version_minor": 0 } } }, "nbformat": 4, "nbformat_minor": 5 }