Created
May 12, 2020 09:49
-
-
Save HarshCasper/bb46189963a10e2462465a622d5ba31c to your computer and use it in GitHub Desktop.
Numpy Indexing and Slicing.ipynb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"nbformat": 4, | |
"nbformat_minor": 0, | |
"metadata": { | |
"colab": { | |
"name": "Numpy Indexing and Slicing.ipynb", | |
"provenance": [], | |
"authorship_tag": "ABX9TyOA50+IyBTaV2fNk5ZtNBBa", | |
"include_colab_link": true | |
}, | |
"kernelspec": { | |
"name": "python3", | |
"display_name": "Python 3" | |
} | |
}, | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "view-in-github", | |
"colab_type": "text" | |
}, | |
"source": [ | |
"<a href=\"https://colab.research.google.com/gist/HarshCasper/bb46189963a10e2462465a622d5ba31c/numpy-indexing-and-slicing.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "VqIG3FOxVopu", | |
"colab_type": "text" | |
}, | |
"source": [ | |
"# Numpy Indexing and Slicing" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "EpmfrVqKVq8d", | |
"colab_type": "code", | |
"outputId": "5b3fb9ac-f97d-4fcc-b0d1-c2beec7ef7d9", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 34 | |
} | |
}, | |
"source": [ | |
"import numpy as np\n", | |
"list1=[1,2,3,4,5]\n", | |
"arr=np.array(list1)\n", | |
"print(arr)" | |
], | |
"execution_count": 0, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": [ | |
"[1 2 3 4 5]\n" | |
], | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "aitVzzPxVso4", | |
"colab_type": "code", | |
"outputId": "71218b13-c492-4f41-d0e6-586d1c044832", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 50 | |
} | |
}, | |
"source": [ | |
"# Accessing the Array Elements\n", | |
"\n", | |
"print(\"The First Element of the Array is %r\" %(arr[0]))\n", | |
"print(\"The Fifth Element of the Array is %r\" %(arr[4]))" | |
], | |
"execution_count": 0, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": [ | |
"The First Element of the Array is 1\n", | |
"The Fifth Element of the Array is 5\n" | |
], | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "bvq0c3olXpsP", | |
"colab_type": "code", | |
"outputId": "04bd6854-479d-460a-df88-037f1bf08d7e", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 50 | |
} | |
}, | |
"source": [ | |
"arr=np.array([[1,2,4],[5,6,7]])\n", | |
"arr" | |
], | |
"execution_count": 0, | |
"outputs": [ | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"text/plain": [ | |
"array([[1, 2, 4],\n", | |
" [5, 6, 7]])" | |
] | |
}, | |
"metadata": { | |
"tags": [] | |
}, | |
"execution_count": 5 | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "E35dpeLhYAId", | |
"colab_type": "code", | |
"outputId": "565e29ca-4f3c-4d56-99af-d3ea52230ffd", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 34 | |
} | |
}, | |
"source": [ | |
"# Get a Specific Row\n", | |
"print(arr[0,:])" | |
], | |
"execution_count": 0, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": [ | |
"[1 2 4]\n" | |
], | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "XE-uEaNjYefN", | |
"colab_type": "code", | |
"outputId": "2f246cb3-9009-44a9-b7e2-8cfaed503032", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 34 | |
} | |
}, | |
"source": [ | |
"# Get a Specific Column\n", | |
"print(arr[:,2])" | |
], | |
"execution_count": 0, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": [ | |
"[4 7]\n" | |
], | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "XeK13EoOYrrs", | |
"colab_type": "code", | |
"outputId": "c40450d9-a6a0-404c-908f-d70dc7782483", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 34 | |
} | |
}, | |
"source": [ | |
"# Getting a Particular Element\n", | |
"print(\"The Element at the Second Row and Third Column is %r\" %(arr[1,2]))" | |
], | |
"execution_count": 0, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": [ | |
"The Element at the Second Row and Third Column is 7\n" | |
], | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "qSFVb1p3Zl7O", | |
"colab_type": "code", | |
"outputId": "92841212-9c5e-4a3a-d4bc-8ca1d10a019d", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 84 | |
} | |
}, | |
"source": [ | |
"# Declaring a Three-Dimensional Matrix and Accessing-Indexing Elements\n", | |
"\n", | |
"arr=np.array([[1,2,3],[4,5,6],[7,8,9]])\n", | |
"print(arr)\n", | |
"print(\"The Element present at the index where two diagonals intersect is %r\" %(arr[1,1]))" | |
], | |
"execution_count": 0, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": [ | |
"[[1 2 3]\n", | |
" [4 5 6]\n", | |
" [7 8 9]]\n", | |
"The Element present at the index where two diagonals intersect is 5\n" | |
], | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "RtjAKZDfaknV", | |
"colab_type": "code", | |
"outputId": "d04951db-2699-46cd-88ce-2f5e917a867f", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 50 | |
} | |
}, | |
"source": [ | |
"print(arr[0:2,1:3])" | |
], | |
"execution_count": 0, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": [ | |
"[[2 3]\n", | |
" [5 6]]\n" | |
], | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "De8FuE8Ra_bI", | |
"colab_type": "code", | |
"outputId": "5d09d005-6651-4377-8724-3f57a1ddded7", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 34 | |
} | |
}, | |
"source": [ | |
"print(arr[0:2,2])" | |
], | |
"execution_count": 0, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": [ | |
"[3 6]\n" | |
], | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "XKE7ngT3cEg9", | |
"colab_type": "code", | |
"outputId": "c470081d-4495-4390-f0d9-6183ee3016d6", | |
"colab": { | |
"base_uri": "https://localhost:8080/", | |
"height": 50 | |
} | |
}, | |
"source": [ | |
"print(arr[1:3,0:])" | |
], | |
"execution_count": 0, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": [ | |
"[[4 5 6]\n", | |
" [7 8 9]]\n" | |
], | |
"name": "stdout" | |
} | |
] | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment