Skip to content

Instantly share code, notes, and snippets.

@Kanav-Arora
Created July 13, 2023 17:04
Show Gist options
  • Save Kanav-Arora/36e5bac2ad19c03a3329c597217eb337 to your computer and use it in GitHub Desktop.
Save Kanav-Arora/36e5bac2ad19c03a3329c597217eb337 to your computer and use it in GitHub Desktop.
Codebank for Numpy
Display the source blob
Display the rendered blob
Raw
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"orig_nbformat": 4,
"language_info": {
"name": "python",
"version": "3.9.7",
"mimetype": "text/x-python",
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"pygments_lexer": "ipython3",
"nbconvert_exporter": "python",
"file_extension": ".py"
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3.9.7 64-bit"
},
"interpreter": {
"hash": "aee8b7b246df8f9039afb4144a1f6fd8d2ca17a180786b69acc140d282b71a49"
},
"colab": {
"provenance": [],
"collapsed_sections": [
"ph-K8a3s7DuI",
"vmCq-2l87DuT",
"z94tQt1-7DuW",
"UH7iamfP7DuX",
"do1AmNDb8Msf",
"9qohmk6D830E",
"0EuGNF3i-KbX",
"ubR9tJJbCA0h"
]
}
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "Ecn0wtxn7Dt7"
},
"source": [
"# Numpy"
]
},
{
"cell_type": "code",
"metadata": {
"id": "YhQcUVHi7Dt_"
},
"source": [
"import numpy as np"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "TdpmHdzg7DuA",
"outputId": "5363427d-9394-47a8-fa8a-74a8fcae75c2"
},
"source": [
"myarr = np.array([[1,2,3,4],[5,6,7,8]], np.int8) # shape should be homogeneous\n",
"myarr"
],
"execution_count": null,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"array([[1, 2, 3, 4],\n",
" [5, 6, 7, 8]], dtype=int8)"
]
},
"metadata": {},
"execution_count": 3
}
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "PabarnIv7DuB",
"outputId": "56489e2c-dbff-4934-bc19-ec8c18229da1"
},
"source": [
"myarr.ndim"
],
"execution_count": null,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"2"
]
},
"metadata": {},
"execution_count": 4
}
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "LyOfG9Iu7DuB",
"outputId": "51adb499-e087-474a-e191-ca7abbf04c19"
},
"source": [
"myarr.shape"
],
"execution_count": null,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"(2, 4)"
]
},
"metadata": {},
"execution_count": 5
}
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "pWCKoPbF7DuC",
"outputId": "c4e328bb-dd4c-4c97-e5f5-6a20c21ba3e0"
},
"source": [
"print(myarr.dtype)"
],
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"int8\n"
]
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "g64_nT6V7DuC"
},
"source": [
"### Array Functions"
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "vES8WFvX7DuD",
"outputId": "ba0d3445-825a-4c9d-e4b3-367ec686e630"
},
"source": [
"# From python datatypes\n",
"myarr = np.array([[1,2,3,4],[5,6,7,8]], np.int8)\n",
"myarrd = np.array({1,2,3})\n",
"myarrd"
],
"execution_count": null,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"array({1, 2, 3}, dtype=object)"
]
},
"metadata": {},
"execution_count": 7
}
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "yu_F2lYa7DuE",
"outputId": "bd8cddca-3ac5-414f-ebda-e06d2f4ea04e"
},
"source": [
"myarr.itemsize # size in bytes of one element"
],
"execution_count": null,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"1"
]
},
"metadata": {},
"execution_count": 8
}
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "beUBILPf7DuE",
"outputId": "16ee0b18-9991-40a6-bf57-01d17567bdbf"
},
"source": [
"myarr.nbytes # total size of array in bytes"
],
"execution_count": null,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"8"
]
},
"metadata": {},
"execution_count": 9
}
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "vsBrte1i7DuF",
"outputId": "8bc41f60-6f24-4d72-c0df-0d401ba49ba1"
},
"source": [
"myarr.size # total number of elements in array"
],
"execution_count": null,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"8"
]
},
"metadata": {},
"execution_count": 10
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "D7eG8Q5f7DuF"
},
"source": [
"## Row sorting"
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "c_03Q0Dk7DuG",
"outputId": "328f4251-b3ee-4764-8a27-f16b93124067"
},
"source": [
"myarr = np.array([[1,2,3,4],[5,6,7,8]])\n",
"myarr[0]"
],
"execution_count": null,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"array([1, 2, 3, 4])"
]
},
"metadata": {},
"execution_count": 11
}
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "2JbdZ4oV7DuG",
"outputId": "a6a854f5-b018-4b85-ae01-79941584133f"
},
"source": [
"myarr[0:]"
],
"execution_count": null,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"array([[1, 2, 3, 4],\n",
" [5, 6, 7, 8]])"
]
},
"metadata": {},
"execution_count": 12
}
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "nbHrIEEs7DuH",
"outputId": "5517ec52-9728-4586-a4b7-45f91b177c6f"
},
"source": [
"myarr[1:]"
],
"execution_count": null,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"array([[5, 6, 7, 8]])"
]
},
"metadata": {},
"execution_count": 13
}
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "tqCfCMIH7DuH",
"outputId": "ab741160-56c1-4b62-cc86-a555e48b2b9a"
},
"source": [
"myarr[0:,0::2]"
],
"execution_count": null,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"array([[1, 3],\n",
" [5, 7]])"
]
},
"metadata": {},
"execution_count": 14
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "ph-K8a3s7DuI"
},
"source": [
"## Array Structure"
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "Hh2yMpB87DuI",
"outputId": "664a6088-ac29-45bf-c040-645b24f39c5c"
},
"source": [
"myarr[:,1] = 10 #update value in all rows\n",
"myarr"
],
"execution_count": null,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"array([[ 1, 10, 3, 4],\n",
" [ 5, 10, 7, 8]])"
]
},
"metadata": {},
"execution_count": 15
}
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "kXNk-RyN7DuJ",
"outputId": "eb8b8d92-5f74-431b-b611-fa15affa2ca4"
},
"source": [
"myarr[:,1] = [15,20] #update value in all rows\n",
"myarr"
],
"execution_count": null,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"array([[ 1, 15, 3, 4],\n",
" [ 5, 20, 7, 8]])"
]
},
"metadata": {},
"execution_count": 16
}
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "XKLij0Zt7DuJ",
"outputId": "7e0e9b9b-686b-4ea0-e5ba-cf4df745b8cc"
},
"source": [
"myarr[0:,] = 30\n",
"myarr"
],
"execution_count": null,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"array([[30, 30, 30, 30],\n",
" [30, 30, 30, 30]])"
]
},
"metadata": {},
"execution_count": 17
}
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "Ka4cf-0c7DuK",
"outputId": "9d290d19-4f54-4570-dadd-f7015469b9d9"
},
"source": [
"zeroes = np.zeros((2,5), np.int32) # default datatype will be float\n",
"zeroes"
],
"execution_count": null,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"array([[0, 0, 0, 0, 0],\n",
" [0, 0, 0, 0, 0]], dtype=int32)"
]
},
"metadata": {},
"execution_count": 18
}
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "55_AOF8A7DuK",
"outputId": "0ce98eeb-4d49-4192-944d-672c8768f275"
},
"source": [
"ones = np.ones((3,3), dtype = np.int8)\n",
"ones"
],
"execution_count": null,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"array([[1, 1, 1],\n",
" [1, 1, 1],\n",
" [1, 1, 1]], dtype=int8)"
]
},
"metadata": {},
"execution_count": 19
}
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "lZtYYw8S7DuL",
"outputId": "e492e3e8-682b-484d-b5e9-75f94c5abecf"
},
"source": [
"fullarr = np.full((3,3),30)\n",
"fullarr"
],
"execution_count": null,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"array([[30, 30, 30],\n",
" [30, 30, 30],\n",
" [30, 30, 30]])"
]
},
"metadata": {},
"execution_count": 20
}
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "BiYkTOAv7DuL",
"outputId": "2461bbdc-8769-4502-e975-3d18c9fafe13"
},
"source": [
"a = np.array([1,2,3,4,5])\n",
"np.full_like(a,30)"
],
"execution_count": null,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"array([30, 30, 30, 30, 30])"
]
},
"metadata": {},
"execution_count": 21
}
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "pWgRlglt7DuL",
"outputId": "02547e53-222a-418e-a0ec-83dbbb5aede5"
},
"source": [
"zeroes[0][0] = 1\n",
"zeroes"
],
"execution_count": null,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"array([[1, 0, 0, 0, 0],\n",
" [0, 0, 0, 0, 0]], dtype=int32)"
]
},
"metadata": {},
"execution_count": 22
}
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "tMFKa1LZ7DuM",
"outputId": "64221368-f42e-44bb-e43c-f1778b4a7442"
},
"source": [
"rng = np.arange(1,16,10) #arange(start,stop,steps)\n",
"rng"
],
"execution_count": null,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"array([ 1, 11])"
]
},
"metadata": {},
"execution_count": 23
}
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "M2xYh__S7DuM",
"outputId": "f0f0ff3a-7695-4da5-f71b-f833f8fba438"
},
"source": [
"lspace = np.linspace(1,5,4) #gives 4 equally spaces numbers b/w 1 and 5\n",
"lspace"
],
"execution_count": null,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"array([1. , 2.33333333, 3.66666667, 5. ])"
]
},
"metadata": {},
"execution_count": 24
}
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "2YxvPAMJ7DuN",
"outputId": "8904fd36-2736-480a-95fd-4da9e5f34eff"
},
"source": [
"emp = np.empty((4,6)) # creates an array with provided dimensions with random values\n",
"emp"
],
"execution_count": null,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"array([[4.66431484e-310, 6.79038653e-313, 2.37663529e-312,\n",
" 2.05833592e-312, 2.41907520e-312, 2.56761491e-312],\n",
" [1.93101617e-312, 9.33678148e-313, 9.33678148e-313,\n",
" 9.33678148e-313, 9.33678148e-313, 1.97345609e-312],\n",
" [2.12199579e-313, 2.37663529e-312, 2.16443571e-312,\n",
" 2.29175545e-312, 2.01589600e-312, 2.22809558e-312],\n",
" [2.14321575e-312, 2.05833592e-312, 1.08221785e-312,\n",
" 8.70018275e-313, 6.79769037e-310, 0.00000000e+000]])"
]
},
"metadata": {},
"execution_count": 25
}
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "OxnTL_Ce7DuO",
"outputId": "af80fe55-846b-4d3a-e1dc-841e24d875a4"
},
"source": [
"iden = np.identity(5) # gives NxN identity matrix\n",
"iden"
],
"execution_count": null,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"array([[1., 0., 0., 0., 0.],\n",
" [0., 1., 0., 0., 0.],\n",
" [0., 0., 1., 0., 0.],\n",
" [0., 0., 0., 1., 0.],\n",
" [0., 0., 0., 0., 1.]])"
]
},
"metadata": {},
"execution_count": 26
}
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "bflneAso7DuP",
"outputId": "1bd1692a-06b7-4b28-cd7b-5a129a8a39df"
},
"source": [
"arr = np.arange(99)\n",
"arr"
],
"execution_count": null,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,\n",
" 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33,\n",
" 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50,\n",
" 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67,\n",
" 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84,\n",
" 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98])"
]
},
"metadata": {},
"execution_count": 27
}
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "bUtJ7Ntl7DuP",
"outputId": "db8056f9-dd0b-4c03-8ed3-87b1e4d1dd78"
},
"source": [
"arr.reshape(3,33) # this will not change arr"
],
"execution_count": null,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"array([[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,\n",
" 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,\n",
" 32],\n",
" [33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48,\n",
" 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64,\n",
" 65],\n",
" [66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81,\n",
" 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97,\n",
" 98]])"
]
},
"metadata": {},
"execution_count": 28
}
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "SJAGy4QQ7DuQ",
"outputId": "bae0d62e-754a-4514-f6f2-604c61703215"
},
"source": [
"arr = arr.reshape(3,33) # this will change arr variable\n",
"arr"
],
"execution_count": null,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"array([[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,\n",
" 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,\n",
" 32],\n",
" [33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48,\n",
" 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64,\n",
" 65],\n",
" [66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81,\n",
" 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97,\n",
" 98]])"
]
},
"metadata": {},
"execution_count": 29
}
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "qv5v2CM-7DuS",
"outputId": "c236796f-4af3-4f25-ae3f-eaa1f9adcc6f"
},
"source": [
"arr = arr.ravel()\n",
"arr"
],
"execution_count": null,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,\n",
" 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33,\n",
" 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50,\n",
" 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67,\n",
" 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84,\n",
" 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98])"
]
},
"metadata": {},
"execution_count": 30
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "vmCq-2l87DuT"
},
"source": [
"## Axis\n",
"\n",
"Axis 0 denotes direction along rows and columnwise operations.\n",
"\n",
"Axis 1 denotes direction along column and rowwise operations."
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "HGVWLZ5x7DuU",
"outputId": "63f26e94-72f9-4877-8c24-a0f48ba7821d"
},
"source": [
"list1 = [[1,1,1],[2,2,2],[3,3,3]]\n",
"arr = np.array(list1)\n",
"arr"
],
"execution_count": null,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"array([[1, 1, 1],\n",
" [2, 2, 2],\n",
" [3, 3, 3]])"
]
},
"metadata": {},
"execution_count": 31
}
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "pzzbBl1i7DuV",
"outputId": "e346bab1-45d5-4247-b3ab-8b068aad0054"
},
"source": [
"arr.sum(axis=1)"
],
"execution_count": null,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"array([3, 6, 9])"
]
},
"metadata": {},
"execution_count": 32
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "z94tQt1-7DuW"
},
"source": [
"## Transpose an array"
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "_9YH9ckk7DuW",
"outputId": "be94a5e9-f29a-4d5f-ac5b-b45652ed0105"
},
"source": [
"arr.T"
],
"execution_count": null,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"array([[1, 2, 3],\n",
" [1, 2, 3],\n",
" [1, 2, 3]])"
]
},
"metadata": {},
"execution_count": 33
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "UH7iamfP7DuX"
},
"source": [
"## Iterator"
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "BEZ-Lck37DuX",
"outputId": "607af1aa-a7e4-46b8-c38c-73c43e59bf0f"
},
"source": [
"arr.flat"
],
"execution_count": null,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"<numpy.flatiter at 0x55dccc8b8d00>"
]
},
"metadata": {},
"execution_count": 34
}
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "CcKSe9gD7DuY",
"outputId": "9276d744-c450-4d3e-e83a-133dede25e7f"
},
"source": [
"for item in arr.flat:\n",
" print(item)"
],
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"1\n",
"1\n",
"1\n",
"2\n",
"2\n",
"2\n",
"3\n",
"3\n",
"3\n"
]
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "do1AmNDb8Msf"
},
"source": [
"## Copy"
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "7vwzK0lU8dq0",
"outputId": "b1062f31-98a5-456b-9462-814128fe062a"
},
"source": [
"a = np.array([1,2,3,4,5])\n",
"b = a.copy()\n",
"b[0] = 100\n",
"print(a)\n",
"print(b)"
],
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"[1 2 3 4 5]\n",
"[100 2 3 4 5]\n"
]
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "9qohmk6D830E"
},
"source": [
"## Arithmetic Operations"
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "J5CKixzi89Vh",
"outputId": "3e094fdc-4112-4c93-91fe-705a0d51c9af"
},
"source": [
"print(a+2)\n",
"print(a-2)\n",
"print(a*2)\n",
"print(a**2)\n",
"print(a/2)\n",
"print(a%2)"
],
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"[3 4 5 6 7]\n",
"[-1 0 1 2 3]\n",
"[ 2 4 6 8 10]\n",
"[ 1 4 9 16 25]\n",
"[0.5 1. 1.5 2. 2.5]\n",
"[1 0 1 0 1]\n"
]
}
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "Btk8UYDa91Ej",
"outputId": "73395e87-d6a9-4819-d467-fcd50728814f"
},
"source": [
"# Addition of two arrays\n",
"a = np.array([1,2,3,4,5,6])\n",
"b = np.array([11,12,13,14,15,16])\n",
"c = a+b\n",
"print(c)"
],
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"[12 14 16 18 20 22]\n"
]
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "0EuGNF3i-KbX"
},
"source": [
"## Linear Algebra"
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "TWrGK28E-Ohv",
"outputId": "bd3e2621-3cf5-40e4-ea09-31cf73d08925"
},
"source": [
"# matrix multiplication\n",
"mat1 = np.array([[1, 6, 5],[3 ,4, 8],[2, 12, 3]])\n",
"print(mat1)\n",
"mat2 = np.array([[3, 4, 6],[5, 6, 7],[6,56, 7]])\n",
"print(mat2)\n",
"np.matmul(mat1,mat2)"
],
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"[[ 1 6 5]\n",
" [ 3 4 8]\n",
" [ 2 12 3]]\n",
"[[ 3 4 6]\n",
" [ 5 6 7]\n",
" [ 6 56 7]]\n"
]
},
{
"output_type": "execute_result",
"data": {
"text/plain": [
"array([[ 63, 320, 83],\n",
" [ 77, 484, 102],\n",
" [ 84, 248, 117]])"
]
},
"metadata": {},
"execution_count": 43
}
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "K2YmvKbU-l8M",
"outputId": "6788446a-1a4c-4a8c-90a3-a4a6aea7695e"
},
"source": [
"# determinant\n",
"mat1 = np.array([[1, 6, 5],[3 ,4, 8],[2, 12, 3]])\n",
"print(mat1)\n",
"np.linalg.det(mat1)"
],
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"[[ 1 6 5]\n",
" [ 3 4 8]\n",
" [ 2 12 3]]\n"
]
},
{
"output_type": "execute_result",
"data": {
"text/plain": [
"98.00000000000004"
]
},
"metadata": {},
"execution_count": 46
}
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "ktvL0Ssk_Mbd",
"outputId": "08d27d64-bc84-4a12-df94-5b9eeafbc5c3"
},
"source": [
"# Eigen\n",
"mat1 = np.array([[5, -10, -5],[2 ,14, 2],[-4, -8, 6]])\n",
"np.linalg.eig(mat1)"
],
"execution_count": null,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"(array([ 5., 10., 10.]),\n",
" array([[-7.45355992e-01, -9.10262523e-16, -6.65010470e-01],\n",
" [ 2.98142397e-01, 4.47213595e-01, 5.72374320e-01],\n",
" [-5.96284794e-01, -8.94427191e-01, -4.79738170e-01]]))"
]
},
"metadata": {},
"execution_count": 47
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "8rkXoy6h_cTN"
},
"source": [
"# Minimum element\n",
"mat1 = np.array([[1, 6, 5],[3 ,4, 8],[2, 12, 3]])\n",
"np.min(mat1)"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "PWQvvd1B_iA9",
"outputId": "d85d47d1-4f5a-4d3d-8207-ba1c75ac2bba"
},
"source": [
"# Rowwise min\n",
"mat1 = np.array([[1, 6, 5],[3 ,4, 8],[2, 12, 3]])\n",
"print(mat1)\n",
"np.min(mat1, axis = 1)"
],
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"[[ 1 6 5]\n",
" [ 3 4 8]\n",
" [ 2 12 3]]\n"
]
},
{
"output_type": "execute_result",
"data": {
"text/plain": [
"array([1, 4, 3])"
]
},
"metadata": {},
"execution_count": 49
}
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "cDGHm7BN_wEh",
"outputId": "54246212-f941-4c17-c183-80691e982c4d"
},
"source": [
"# Column min\n",
"mat1 = np.array([[1, 6, 5],[3 ,4, 8],[2, 12, 3]])\n",
"print(mat1)\n",
"np.min(mat1, axis = 0)"
],
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"[[ 1 6 5]\n",
" [ 3 4 8]\n",
" [ 2 12 3]]\n"
]
},
{
"output_type": "execute_result",
"data": {
"text/plain": [
"array([1, 3, 2])"
]
},
"metadata": {},
"execution_count": 51
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "F-frUKWtAxP9"
},
"source": [
"Similary we can use sum, max, mean, median, std - standard deviation, var - variance and in all of these we can define axis as 0 or 1"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "ubR9tJJbCA0h"
},
"source": [
"## Numpy Intersection and Set Difference"
]
},
{
"cell_type": "code",
"metadata": {
"id": "0IrjOjVpB-B0"
},
"source": [
"# Intersection\n",
"import numpy as np\n",
"a = np.array([1,2,3,4,5,6])\n",
"b = np.array([2,7,8,3,5,4])\n",
"np.intersect1d(a,b)"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "McvMFc86CJTx"
},
"source": [
"# Difference - a-b\n",
"import numpy as np\n",
"a = np.array([1,2,3,4,5,6])\n",
"b = np.array([2,7,8,3,5,4])\n",
"np.setdiff1d(a,b)"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "tsErpciiCNYr"
},
"source": [
"# Difference - b-a\n",
"import numpy as np\n",
"a = np.array([1,2,3,4,5,6])\n",
"b = np.array([2,7,8,3,5,4])\n",
"np.setdiff1d(b,a)"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "FmwpXw3BSPDt"
},
"source": [
"## Random Function"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "rrxMQHT3UeS0"
},
"source": [
"### Rand"
]
},
{
"cell_type": "code",
"metadata": {
"id": "OOfZ3IxtST0H"
},
"source": [
"from numpy import random"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "BtPzB0VVSjfJ",
"outputId": "c6439c4a-7218-452c-8101-ab07609d11c8"
},
"source": [
"x = random.randint(100)\n",
"x"
],
"execution_count": null,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"8"
]
},
"metadata": {},
"execution_count": 3
}
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "NBB0Lx7vSu3H",
"outputId": "af4af9f8-0513-4b24-dc31-7ecf7adb03af"
},
"source": [
"x = random.rand() # returns value between 0 and 1\n",
"x"
],
"execution_count": null,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"0.5498833247163208"
]
},
"metadata": {},
"execution_count": 4
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "QL9_kgvNS9IV",
"colab": {
"base_uri": "https://localhost:8080/"
},
"outputId": "60f40c30-43c0-4eaf-ee25-72fcd4d345d8"
},
"source": [
"x = random.randint(100, size=(3, 5))\n",
"print(x)"
],
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"[[85 41 23 46 25]\n",
" [26 13 80 42 70]\n",
" [94 67 29 56 60]]\n"
]
}
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "dQa9trpaUSMf",
"outputId": "fa50a1b2-6980-412d-d2a2-86743546af09"
},
"source": [
"x = random.rand(3, 5)\n",
"\n",
"print(x)"
],
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"[[0.68320469 0.39278924 0.57673955 0.06167313 0.63146798]\n",
" [0.35185564 0.87080826 0.03102952 0.77311397 0.74816741]\n",
" [0.84921683 0.64273779 0.62312206 0.20398498 0.6723331 ]]\n"
]
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "-K7GESIyUhEN"
},
"source": [
"### Choice"
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "dz1OMwd6Ul_v",
"outputId": "8bfee3b3-7956-45d8-9ffa-4db2b9e80899"
},
"source": [
"x = random.choice([1,2,3,4,5], size = (10)) # choose a random value from an array - works only for numbers\n",
"x"
],
"execution_count": null,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"array([2, 2, 5, 4, 5, 1, 1, 3, 4, 4])"
]
},
"metadata": {},
"execution_count": 13
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "so-U4-cbXUCV"
},
"source": [
"x = random.choice(['H','T'], size= (1000))\n",
"x"
],
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "ZCdrjG8LWCLc"
},
"source": [
"### Seed"
]
},
{
"cell_type": "code",
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "NZaOiPv4WD1x",
"outputId": "412cb9ec-b16c-4e70-b58d-093fd1401a17"
},
"source": [
"random.seed(10)\n",
"x = random.random()\n",
"x"
],
"execution_count": null,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"0.771320643266746"
]
},
"metadata": {},
"execution_count": 20
}
]
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment