From 09d62f0eeb52f56ebe4205058c386cc5f3960898 Mon Sep 17 00:00:00 2001 From: Diwakar Gupta <39624018+Diwakar-Gupta@users.noreply.github.com> Date: Sat, 30 Apr 2022 13:28:41 +0530 Subject: [PATCH] numpy Assignment solution --- 22-04-16-Numpy/Assignment_Solution.ipynb | 1299 ++++++++++++++++++++++ 1 file changed, 1299 insertions(+) create mode 100644 22-04-16-Numpy/Assignment_Solution.ipynb diff --git a/22-04-16-Numpy/Assignment_Solution.ipynb b/22-04-16-Numpy/Assignment_Solution.ipynb new file mode 100644 index 0000000..64c1267 --- /dev/null +++ b/22-04-16-Numpy/Assignment_Solution.ipynb @@ -0,0 +1,1299 @@ +{ + "nbformat": 4, + "nbformat_minor": 0, + "metadata": { + "colab": { + "name": "numpy_excercise0_SOLUTIONS.ipynb", + "provenance": [], + "collapsed_sections": [], + "authorship_tag": "ABX9TyMpBR/US0lY6F/j21AFRQQ1", + "include_colab_link": true + }, + "kernelspec": { + "name": "python3", + "display_name": "Python 3" + }, + "language_info": { + "name": "python" + } + }, + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "id": "view-in-github", + "colab_type": "text" + }, + "source": [ + "\"Open" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "gX8SgQjYg9Pv" + }, + "source": [ + "**Import numpy as np**" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "jv3Xo5nXg3tA" + }, + "source": [ + "import numpy as np" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "oTfF2hlPhCmF" + }, + "source": [ + "**Create an array of 10 zeros**" + ] + }, + { + "cell_type": "code", + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "wIwsJrYchFsj", + "outputId": "a8f9b701-3441-4d34-b51c-e1a2ef9626ac" + }, + "source": [ + "np.zeros(10, dtype='int32')" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0], dtype=int32)" + ] + }, + "metadata": {}, + "execution_count": 3 + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "vcIl9pJ8hGAM" + }, + "source": [ + "**Create an array of 10 ones**" + ] + }, + { + "cell_type": "code", + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "FNBOoy7YhJDz", + "outputId": "b0b91615-e122-41a1-f38a-8c9e9c90859a" + }, + "source": [ + "np.ones(10)" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "array([1., 1., 1., 1., 1., 1., 1., 1., 1., 1.])" + ] + }, + "metadata": {}, + "execution_count": 4 + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "iGTVWS-xhJR_" + }, + "source": [ + "**Create an array of 10 fives**" + ] + }, + { + "cell_type": "code", + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "bioPQimlhMaF", + "outputId": "3b70e3ad-e4d1-4251-83c9-e12678314a37" + }, + "source": [ + "print(np.ones(10) * 5)\n", + "\n", + "print(np.full(10, 5))" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "[5. 5. 5. 5. 5. 5. 5. 5. 5. 5.]\n", + "[5 5 5 5 5 5 5 5 5 5]\n" + ] + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "DhZUq38whgCu" + }, + "source": [ + "**Create an array of the integers from 10 to 50**\n", + "\n", + "New " + ] + }, + { + "cell_type": "code", + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "j_u7EixahfS-", + "outputId": "2350b8f7-e478-4f47-aeb1-ec6f2a646c53" + }, + "source": [ + "np.arange(10, 51)" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "array([10, 13, 16, 19, 22, 25, 28, 31, 34, 37, 40, 43, 46, 49])" + ] + }, + "metadata": {}, + "execution_count": 13 + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "yL3qc8vmhtMX" + }, + "source": [ + "**Create an array of all the even integers from 10 to 50**" + ] + }, + { + "cell_type": "code", + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "0pckFbQGhw4Z", + "outputId": "ff048182-fa7a-4536-a652-ffe9f8001d65" + }, + "source": [ + "print(np.arange(10, 51, 2))\n", + "\n", + "\n", + "# another method\n", + "a = np.arange(10, 51)\n", + "\n", + "a[ a % 2 == 0]" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "[10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50]\n" + ] + }, + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "array([10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42,\n", + " 44, 46, 48, 50])" + ] + }, + "metadata": {}, + "execution_count": 16 + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "SdtofJi_h5WM" + }, + "source": [ + "**Create a 3x4 matrix with numbers ranging from 0 to 12**" + ] + }, + { + "cell_type": "code", + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "V_mDcyyPh0kM", + "outputId": "1d65253d-b650-4b57-a60a-c34d9f9a75f3" + }, + "source": [ + "np.arange(12).reshape(3,4)" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "array([[ 0, 1, 2, 3],\n", + " [ 4, 5, 6, 7],\n", + " [ 8, 9, 10, 11]])" + ] + }, + "metadata": {}, + "execution_count": 20 + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "TF63DVYdiHUp" + }, + "source": [ + "**Create a 3x3 identity matrix**" + ] + }, + { + "cell_type": "code", + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "5Z8Kl49niDj_", + "outputId": "c2d48cb1-64e9-4054-a981-51c7a4e468ff" + }, + "source": [ + "np.identity(3, dtype='int32')" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "array([[1, 0, 0],\n", + " [0, 1, 0],\n", + " [0, 0, 1]], dtype=int32)" + ] + }, + "metadata": {}, + "execution_count": 22 + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "7-TKphGniSni" + }, + "source": [ + "**Use Numpy to generate a random number between 0 and 1**" + ] + }, + { + "cell_type": "code", + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "qqXA7JMPiRod", + "outputId": "5d712553-d9d7-496b-9b7b-e5e550119868" + }, + "source": [ + "# new method\n", + "print(np.random.random())\n", + "\n", + "#\n", + "np.random.rand(1)[0]" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "0.3850256101588396" + ] + }, + "metadata": {}, + "execution_count": 26 + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "z3xKZYsSjDDE" + }, + "source": [ + "**Use Numpy to generate an array of 25 random numbers sampled from a standard normal distribution**" + ] + }, + { + "cell_type": "code", + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "okMA4he2ib4V", + "outputId": "60d90ee2-2f59-4a0b-f0aa-a6f80ad978c7" + }, + "source": [ + "np.random.standard_normal(25).std()\n", + "\n", + "\n", + "\n", + "np.random.randn(25).std()" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "0.721640404743356" + ] + }, + "metadata": {}, + "execution_count": 30 + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "LNnEXURBjcLs" + }, + "source": [ + "**Create an (10x10)array having numbers from 0.01 to 1 increasing by 0.01**" + ] + }, + { + "cell_type": "code", + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "Fskh08Q1jmXp", + "outputId": "04624887-4bc5-4d42-cac9-5d34d6efbf87" + }, + "source": [ + "np.linspace(0.01, 1, 100).reshape(10, 10)" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "array([[0.01, 0.02, 0.03, 0.04, 0.05, 0.06, 0.07, 0.08, 0.09, 0.1 ],\n", + " [0.11, 0.12, 0.13, 0.14, 0.15, 0.16, 0.17, 0.18, 0.19, 0.2 ],\n", + " [0.21, 0.22, 0.23, 0.24, 0.25, 0.26, 0.27, 0.28, 0.29, 0.3 ],\n", + " [0.31, 0.32, 0.33, 0.34, 0.35, 0.36, 0.37, 0.38, 0.39, 0.4 ],\n", + " [0.41, 0.42, 0.43, 0.44, 0.45, 0.46, 0.47, 0.48, 0.49, 0.5 ],\n", + " [0.51, 0.52, 0.53, 0.54, 0.55, 0.56, 0.57, 0.58, 0.59, 0.6 ],\n", + " [0.61, 0.62, 0.63, 0.64, 0.65, 0.66, 0.67, 0.68, 0.69, 0.7 ],\n", + " [0.71, 0.72, 0.73, 0.74, 0.75, 0.76, 0.77, 0.78, 0.79, 0.8 ],\n", + " [0.81, 0.82, 0.83, 0.84, 0.85, 0.86, 0.87, 0.88, 0.89, 0.9 ],\n", + " [0.91, 0.92, 0.93, 0.94, 0.95, 0.96, 0.97, 0.98, 0.99, 1. ]])" + ] + }, + "metadata": {}, + "execution_count": 32 + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "aJk9hkikliYL" + }, + "source": [ + "#Numpy Indexing and Slicing\n" + ] + }, + { + "cell_type": "code", + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "oBLqBk-0lnKC", + "outputId": "7cea9050-eba9-4870-a581-a694e763fecc" + }, + "source": [ + "ar = (np.arange(25)+1).reshape(5,5)\n", + "ar" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "array([[ 1, 2, 3, 4, 5],\n", + " [ 6, 7, 8, 9, 10],\n", + " [11, 12, 13, 14, 15],\n", + " [16, 17, 18, 19, 20],\n", + " [21, 22, 23, 24, 25]])" + ] + }, + "metadata": {}, + "execution_count": 35 + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "JjWRLf4omWwt" + }, + "source": [ + "**extract array**\n", + "\n", + "[\n", + " [12,13,14],\n", + "\n", + " [17,18,19],\n", + "\n", + " [22,23,24]\n", + "]" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "G_F5Qn-EmQnX", + "colab": { + "base_uri": "https://localhost:8080/" + }, + "outputId": "430cf30b-b3cb-458b-848b-f74a485d6b3d" + }, + "source": [ + "ar[2:, 1:4]" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "array([[12, 13, 14],\n", + " [17, 18, 19],\n", + " [22, 23, 24]])" + ] + }, + "metadata": {}, + "execution_count": 37 + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "wIb1kQsSmupr" + }, + "source": [ + "**extract array**\n", + "\n", + "[\n", + " [14, 13, 12],\n", + "\n", + " [19, 18, 17],\n", + "\n", + " [24, 23, 22]\n", + "]" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "0GbIQPLkm1vT", + "colab": { + "base_uri": "https://localhost:8080/" + }, + "outputId": "704b22d1-9bee-4f6f-cdf6-1a78cf340811" + }, + "source": [ + "ar[2:, 3:0:-1]" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "array([[14, 13, 12],\n", + " [19, 18, 17],\n", + " [24, 23, 22]])" + ] + }, + "metadata": {}, + "execution_count": 38 + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "Q3fF94y1m4qC" + }, + "source": [ + "**extract array**\n", + "\n", + "[\n", + " [24, 23, 22],\n", + "\n", + " [19, 18, 17],\n", + "\n", + " [14, 13, 12], \n", + "]" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "_ZqZT-l8m82Z", + "colab": { + "base_uri": "https://localhost:8080/" + }, + "outputId": "621c9907-943d-4e0e-c7ab-698b33bab715" + }, + "source": [ + "ar[4:1:-1, 3:0:-1]" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "array([[24, 23, 22],\n", + " [19, 18, 17],\n", + " [14, 13, 12]])" + ] + }, + "metadata": {}, + "execution_count": 40 + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "YHmvl3VrnEzY" + }, + "source": [ + "#Split Concatinate" + ] + }, + { + "cell_type": "code", + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "bNbdzPb8nH5c", + "outputId": "20fb71e7-7078-4028-a53c-18437eb51e07" + }, + "source": [ + "ar = np.arange(2,10)\n", + "ar" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "array([2, 3, 4, 5, 6, 7, 8, 9])" + ] + }, + "metadata": {}, + "execution_count": 43 + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "Q7JpN_ElnV0E" + }, + "source": [ + "**Split array to generate 3 arrays with content**\n", + "\n", + "[2,3,4]\n", + "\n", + "[5, 6]\n", + "\n", + "[]\n", + "\n", + "[7,8,9]" + ] + }, + { + "cell_type": "code", + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "DD9ppBCpnR94", + "outputId": "44892da8-6f85-4f3f-abf8-a599e08c3594" + }, + "source": [ + "np.split(ar, [3,5,5])" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "[array([2, 3, 4]), array([5, 6]), array([], dtype=int64), array([7, 8, 9])]" + ] + }, + "metadata": {}, + "execution_count": 44 + } + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "waNqYReToJvC" + }, + "source": [ + "a = np.array([\n", + " [1,2,3],\n", + " [4,5,6]\n", + "])\n", + "b = np.array([\n", + " [7,8,9]\n", + "])" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "D9KgMNsOn__h" + }, + "source": [ + "**Concatinate array to create new array with content**\n", + "\n", + "[\n", + " [1,2,3],\n", + "\n", + " [4,5,6],\n", + " \n", + " [7,8,9]\n", + "]" + ] + }, + { + "cell_type": "code", + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "uRc1NdAr_SRW", + "outputId": "8d14d00b-cc5c-4fe8-b7f1-3ebf9dbe5f70" + }, + "source": [ + "np.vstack([a,b, b])" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "array([[1, 2, 3],\n", + " [4, 5, 6],\n", + " [7, 8, 9],\n", + " [7, 8, 9]])" + ] + }, + "metadata": {}, + "execution_count": 48 + } + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "3HZZlRPtobB1" + }, + "source": [ + "a = np.array([\n", + " [1,2,3],\n", + " [4,5,6]\n", + "])\n", + "b = np.array([\n", + " [7,8,9, 10],\n", + " [11, 12, 13, 14]\n", + "])" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "V9LH9_agowZr" + }, + "source": [ + "**Concatinate array to create new array with content**\n", + "\n", + "[\n", + "\n", + " [1,2,3, 7,8,9, 10],\n", + "\n", + " [4,5,6, 11, 12, 13, 14]\n", + " \n", + "]" + ] + }, + { + "cell_type": "code", + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "AwPpcU7i_qrJ", + "outputId": "fbf5d1ee-8a7f-445a-ca3f-2c6cfdccc17a" + }, + "source": [ + "np.hstack([a,b])" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "array([[ 1, 2, 3, 7, 8, 9, 10],\n", + " [ 4, 5, 6, 11, 12, 13, 14]])" + ] + }, + "metadata": {}, + "execution_count": 50 + } + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "EivQu5Eaou4A" + }, + "source": [ + "" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "-uCyX-bvo-23" + }, + "source": [ + "#Broadcasting and Indexing" + ] + }, + { + "cell_type": "code", + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "dmdf9qNgpDGt", + "outputId": "be04cefc-77ad-4ff1-b04e-9f7798785c1b" + }, + "source": [ + "ar = np.round(np.random.rand(10)*10)\n", + "ar" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "array([ 3., 0., 6., 5., 7., 5., 9., 3., 10., 1.])" + ] + }, + "metadata": {}, + "execution_count": 55 + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "UWHDTOfMpWhs" + }, + "source": [ + "**create new array containing all numbers which are smaller than 10 in ar**" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "6hwVV5lWpU8s", + "colab": { + "base_uri": "https://localhost:8080/" + }, + "outputId": "6aae9dd8-e0fd-4e9e-ad7f-e1ed38e0bdfc" + }, + "source": [ + "ar[ ar < 10 ]" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "array([3., 0., 6., 5., 7., 5., 9., 3., 1.])" + ] + }, + "metadata": {}, + "execution_count": 56 + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "GlwR-PQdpmGD" + }, + "source": [ + "**create new array containing all numbers which are in range [3, 11] in ar**" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "aVBrLn2Hpr1n", + "colab": { + "base_uri": "https://localhost:8080/" + }, + "outputId": "30aa7603-de5f-4b37-caf4-ef268caaece3" + }, + "source": [ + "ar[ (ar >= 3) & (ar <= 11) ] # & | ^ ~" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "array([ 3., 6., 5., 7., 5., 9., 3., 10.])" + ] + }, + "metadata": {}, + "execution_count": 58 + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "aGY4VLiPptFG" + }, + "source": [ + "**count how many numbers are minimum in ar**\n", + "\n", + "example for arr = [2, 4, 6, 2, 9, 2]\n", + "\n", + "min is 2\n", + "\n", + "there are 3 occurences of 2" + ] + }, + { + "cell_type": "code", + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "gIdSqYkmAgNw", + "outputId": "284e19aa-8ab2-4b3f-de6c-49fc138225ff" + }, + "source": [ + "arr = np.round(np.random.rand(10)*10)\n", + "arr" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "array([ 2., 7., 10., 6., 8., 2., 7., 7., 6., 9.])" + ] + }, + "metadata": {}, + "execution_count": 59 + } + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "EgT3w1GUqVRM", + "colab": { + "base_uri": "https://localhost:8080/" + }, + "outputId": "46665627-0352-4f63-f640-246984018763" + }, + "source": [ + "arr[ arr == arr.min() ].size" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "2" + ] + }, + "metadata": {}, + "execution_count": 62 + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "dpBgZ_PjqXoR" + }, + "source": [ + "**Create an array of length 20 with first 15 numbers equal to 2 and last 5 numbers = -5**" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "i4xc-k3WqoPi", + "colab": { + "base_uri": "https://localhost:8080/" + }, + "outputId": "5e741e97-c4d5-46f5-e5a7-86acfaf2d0ba" + }, + "source": [ + "a = np.ones(15)*2\n", + "b = np.ones(5)*-5\n", + "\n", + "np.concatenate([a,b])" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "array([ 2., 2., 2., 2., 2., 2., 2., 2., 2., 2., 2., 2., 2.,\n", + " 2., 2., -5., -5., -5., -5., -5.])" + ] + }, + "metadata": {}, + "execution_count": 63 + } + ] + }, + { + "cell_type": "code", + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "oFaWcgjysF-j", + "outputId": "ba853386-bc72-4264-9f83-e7db42170bd2" + }, + "source": [ + "mat = np.round(np.random.rand(50)*100).reshape(5,10)\n", + "mat" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "array([[ 98., 78., 8., 25., 10., 51., 62., 63., 87., 53.],\n", + " [ 23., 47., 99., 22., 18., 17., 34., 17., 7., 52.],\n", + " [ 47., 94., 60., 86., 15., 90., 46., 100., 37., 7.],\n", + " [ 68., 55., 3., 94., 41., 60., 40., 42., 54., 65.],\n", + " [ 1., 0., 47., 26., 36., 1., 54., 35., 47., 40.]])" + ] + }, + "metadata": {}, + "execution_count": 64 + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "4o5xRHS7sAh_" + }, + "source": [ + "**Sum min max of all elements of matrix**" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "jJZKNPsYsFb-", + "colab": { + "base_uri": "https://localhost:8080/" + }, + "outputId": "8d21b0f9-8c09-4aa8-940f-bca5d9954424" + }, + "source": [ + "print(mat.min())\n", + "print(mat.max())\n", + "print(mat.sum())\n" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "0.0\n", + "100.0\n", + "2262.0\n" + ] + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "BLvLnGGNsYdv" + }, + "source": [ + "**Standard deviation of mat**" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "3surFJbasbWX", + "colab": { + "base_uri": "https://localhost:8080/" + }, + "outputId": "bde8f259-288a-4038-9c45-fb2e3c93db9d" + }, + "source": [ + "mat.std()" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "28.664654192925475" + ] + }, + "metadata": {}, + "execution_count": 67 + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "0fJVpTNBsbqi" + }, + "source": [ + "**Sum of all columns of mat**" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "p2xPyXinsfVS", + "colab": { + "base_uri": "https://localhost:8080/" + }, + "outputId": "7cfe4416-caca-4ac1-89a5-f2c4fb176a0f" + }, + "source": [ + "mat.sum(axis=0)" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "array([237., 274., 217., 253., 120., 219., 236., 257., 232., 217.])" + ] + }, + "metadata": {}, + "execution_count": 73 + } + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "odiKjyVdBcqi" + }, + "source": [ + "ar = np.array([\n", + " [1,2,3,4],\n", + " [10,10,10,10]\n", + "])" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "vUnfwlUMBgo5", + "outputId": "e7a8ef87-e6f9-49d5-98f5-36a6e01481d9" + }, + "source": [ + "ar.sum(axis=1)" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "array([10, 40])" + ] + }, + "metadata": {}, + "execution_count": 79 + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "u_xIopR4B-7V" + }, + "source": [ + "**create array of dimension same as arr**" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "pkfSo-CBCCrF" + }, + "source": [ + "arr = np.arange(100).reshape(4,5,5)" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "id": "JyO5TFBcCMxa" + }, + "source": [ + "dim = arr.shape" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "id": "AJM5JHKPCQ6V", + "colab": { + "base_uri": "https://localhost:8080/" + }, + "outputId": "69a0d245-5e7b-4ed2-cbc8-a66a131193cc" + }, + "source": [ + "arr2 = np.arange(arr.size).reshape(arr.shape)\n", + "arr2" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "array([[[ 0, 1, 2, 3, 4],\n", + " [ 5, 6, 7, 8, 9],\n", + " [10, 11, 12, 13, 14],\n", + " [15, 16, 17, 18, 19],\n", + " [20, 21, 22, 23, 24]],\n", + "\n", + " [[25, 26, 27, 28, 29],\n", + " [30, 31, 32, 33, 34],\n", + " [35, 36, 37, 38, 39],\n", + " [40, 41, 42, 43, 44],\n", + " [45, 46, 47, 48, 49]],\n", + "\n", + " [[50, 51, 52, 53, 54],\n", + " [55, 56, 57, 58, 59],\n", + " [60, 61, 62, 63, 64],\n", + " [65, 66, 67, 68, 69],\n", + " [70, 71, 72, 73, 74]],\n", + "\n", + " [[75, 76, 77, 78, 79],\n", + " [80, 81, 82, 83, 84],\n", + " [85, 86, 87, 88, 89],\n", + " [90, 91, 92, 93, 94],\n", + " [95, 96, 97, 98, 99]]])" + ] + }, + "metadata": {}, + "execution_count": 85 + } + ] + } + ] +} \ No newline at end of file