Skip to content

Instantly share code, notes, and snippets.

@ananya868
Created July 27, 2021 08:17
Show Gist options
  • Save ananya868/f75f8cb750c599c523496c8ca50f6920 to your computer and use it in GitHub Desktop.
Save ananya868/f75f8cb750c599c523496c8ca50f6920 to your computer and use it in GitHub Desktop.
Numpy arg functions and sorting
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "26a98b25",
"metadata": {},
"outputs": [],
"source": [
"import numpy as np "
]
},
{
"cell_type": "code",
"execution_count": 12,
"id": "664be621",
"metadata": {},
"outputs": [],
"source": [
"a = np.arange(6)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "b22ad832",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([0, 1, 2, 3, 4, 5])"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "94d3bfc4",
"metadata": {},
"outputs": [],
"source": [
"b = np.array([1,2,5,8,5,6,45,56,1,2,6,5,56,5,5,89,7,5,6,5])"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "421f84b4",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"20"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"b.size"
]
},
{
"cell_type": "code",
"execution_count": 19,
"id": "5cbc5ae2",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[ 1, 2, 5, 8],\n",
" [ 5, 6, 45, 56],\n",
" [ 1, 2, 6, 5],\n",
" [56, 5, 5, 89],\n",
" [ 7, 5, 6, 5]])"
]
},
"execution_count": 19,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"b.reshape(5,4)"
]
},
{
"cell_type": "code",
"execution_count": 20,
"id": "3e3919cf",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([ 1, 2, 5, 8, 5, 6, 45, 56, 1, 2, 6, 5, 56, 5, 5, 89, 7,\n",
" 5, 6, 5])"
]
},
"execution_count": 20,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"b"
]
},
{
"cell_type": "code",
"execution_count": 21,
"id": "c74971c9",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([ 0, 8, 9, 1, 11, 17, 14, 13, 2, 19, 4, 10, 5, 18, 16, 3, 6,\n",
" 7, 12, 15], dtype=int64)"
]
},
"execution_count": 21,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"np.argsort(b)"
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "eacabc17",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"np.argmin(b)"
]
},
{
"cell_type": "markdown",
"id": "cea9c779",
"metadata": {},
"source": [
"np.armin gives the first element of argsort \n"
]
},
{
"cell_type": "code",
"execution_count": 14,
"id": "b4593d62",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"15"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"np.argmax(b)"
]
},
{
"cell_type": "markdown",
"id": "1893a52a",
"metadata": {},
"source": [
"np.argmax gives the last element of argsort "
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "afda548c",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"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.9.1"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment