diff --git a/AshishChoudhary_Assignment_1.ipynb b/AshishChoudhary_Assignment_1.ipynb new file mode 100644 index 0000000..5ebe254 --- /dev/null +++ b/AshishChoudhary_Assignment_1.ipynb @@ -0,0 +1,429 @@ +{ + "nbformat": 4, + "nbformat_minor": 0, + "metadata": { + "colab": { + "name": "AshishChoudhary_Assignment_1.ipynb", + "provenance": [] + }, + "kernelspec": { + "name": "python3", + "display_name": "Python 3" + } + }, + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "id": "zep0qYvUDPXQ", + "colab_type": "text" + }, + "source": [ + "Assignment\n", + "Import numpy as np" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "jz8PKLNBDUdR", + "colab_type": "code", + "colab": {} + }, + "source": [ + "import numpy as np" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "lnMI6OcmDZe-", + "colab_type": "text" + }, + "source": [ + "Make a python list => [1,2,3,4,5]\n", + "\n", + "Convert it into numpy array and print it" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "URrIl5SxDaGx", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + }, + "outputId": "94f5a1b0-e234-47f6-b90e-6a299c76121a" + }, + "source": [ + "lists=[1,2,3,4,5]\n", + "lists2=np.array(lists)\n", + "print(lists2)" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "text": [ + "[1 2 3 4 5]\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "fi9CjOgIDfvT", + "colab_type": "text" + }, + "source": [ + "Make a python matrix (3 x 3) => [[1,2,3],[4,5,6],[7,8,9]]\n", + "\n", + "Convert it into numpy array and print it" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "kltptA9HDk9J", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 68 + }, + "outputId": "77a8de85-edde-4848-af3d-bc4a3c437d1c" + }, + "source": [ + "mat=[[1,2,3],[4,5,6],[7,8,9]]\n", + "mat2=np.array(mat)\n", + "print(mat2)" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "text": [ + "[[1 2 3]\n", + " [4 5 6]\n", + " [7 8 9]]\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "RHlQrr4dDn7z", + "colab_type": "text" + }, + "source": [ + "Make a matrix (3 x 3) using built-in methods (like arange(), reshape() etc.):\n", + "\n", + "[ [1,3,5],\n", + "\n", + "[7,9,11],\n", + "\n", + "[13,15,17] ]" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "5yZ8R0uXDrv5", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 68 + }, + "outputId": "6fa5b280-23e4-48d3-bb6f-d92bbc7c3c69" + }, + "source": [ + "arr=np.arange(1,18,2).reshape(3,3)\n", + "print(arr)" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "text": [ + "[[ 1 3 5]\n", + " [ 7 9 11]\n", + " [13 15 17]]\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "uQ3TDYQlDu30", + "colab_type": "text" + }, + "source": [ + "Create a numpy array with 10 random numbers from 0 to 10 (there should be few numbers greater than 1)" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "E3p4DRIYDx3k", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 51 + }, + "outputId": "d30afc61-7f44-4a6a-ef50-306d7bf0f317" + }, + "source": [ + "arr2=np.array(np.random.rand(10)*10)\n", + "print(arr2)" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "text": [ + "[9.21322637 9.80938405 1.85697558 1.98333296 1.18344102 9.56819252\n", + " 5.2811133 5.20055839 6.52692004 9.12361983]\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "PIoIO9YyD9ti", + "colab_type": "text" + }, + "source": [ + "Create numpy array => [1,2,3,4,5] and convert it to 2D array with 5 rows" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "GtKSKqslD-j-", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 102 + }, + "outputId": "3e9a1ea8-66f0-4d06-e6ea-8c042bc01e0a" + }, + "source": [ + "arr3=np.array([1,2,3,4,5]).reshape(5,1)\n", + "print(arr3)" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "text": [ + "[[1]\n", + " [2]\n", + " [3]\n", + " [4]\n", + " [5]]\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "Ko6pqrxPEC5a", + "colab_type": "text" + }, + "source": [ + "Print the shape of the above created array" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "IoppNb2zEDfk", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + }, + "outputId": "d22f1fc0-1e9c-41c6-a219-17871ba81eb2" + }, + "source": [ + "arr3.shape" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "(5, 1)" + ] + }, + "metadata": { + "tags": [] + }, + "execution_count": 8 + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "YR4m4Od2EGbl", + "colab_type": "text" + }, + "source": [ + "Create a numpy array with 10 elements in it. Access and print its 3rd, 4th and 9th element." + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "XxGjRhocEJ7x", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + }, + "outputId": "a8fbabac-2c2c-4ebb-8019-1cf1534d0d34" + }, + "source": [ + "arr4=np.array(np.arange(1,11))\n", + "print(arr4[2],arr4[3],arr4[8])" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "text": [ + "3 4 9\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "RJNUblJfEr_J", + "colab_type": "text" + }, + "source": [ + "\n", + "Print alternate elements of that array" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "u52dcm6BEsq6", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + }, + "outputId": "12f7d0fc-295a-46de-ce06-81f82ef1942d" + }, + "source": [ + "print(arr4[::2])" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "text": [ + "[1 3 5 7 9]\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "58T1YtrgEtEJ", + "colab_type": "text" + }, + "source": [ + "Change last 3 elements into 100 using broadcasting and print" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "HbuvN4u1EvXM", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + }, + "outputId": "3a5e4dc6-737d-434f-8d84-6ee3f863c6b1" + }, + "source": [ + "arr4[-3:]=100\n", + "print(arr4)" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "text": [ + "[ 1 2 3 4 5 6 7 100 100 100]\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "880h_GWJEvhS", + "colab_type": "text" + }, + "source": [ + "Create a 5 x 5 matrix (fill it with any element you like), print it.\n", + "\n", + "Then print the middle (3 x 3) matrix." + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "rwhSH912ExfD", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 68 + }, + "outputId": "d093c1ca-9ef9-476d-d19a-f25f1efa2a6f" + }, + "source": [ + "arr6=np.array(np.arange(1,26).reshape(5,5))\n", + "print(arr6[1:4,1:4])" + ], + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "text": [ + "[[ 7 8 9]\n", + " [12 13 14]\n", + " [17 18 19]]\n" + ], + "name": "stdout" + } + ] + } + ] +} \ No newline at end of file