{ "nbformat": 4, "nbformat_minor": 0, "metadata": { "colab": { "name": "Numpy 1.ipynb", "version": "0.3.2", "provenance": [], "collapsed_sections": [], "include_colab_link": true }, "kernelspec": { "name": "python3", "display_name": "Python 3" } }, "cells": [ { "cell_type": "markdown", "metadata": { "id": "view-in-github", "colab_type": "text" }, "source": [ "\"Open" ] }, { "cell_type": "markdown", "metadata": { "id": "Ta9eb8MHW-EJ", "colab_type": "text" }, "source": [ "# Python Numpy tutorial" ] }, { "cell_type": "markdown", "metadata": { "id": "TKvYfcCqW3fa", "colab_type": "text" }, "source": [ "## This notebook helps to understand numpy from scratch, this exercise was taken from python data science handbook.\n" ] }, { "cell_type": "markdown", "metadata": { "id": "7mfPM-H8Y8ob", "colab_type": "text" }, "source": [ "![alt text](https://stackabuse.com/content/images/2018/09/numpy-tutorial-simple-example-based-guide.png)" ] }, { "cell_type": "markdown", "metadata": { "id": "fTabKGXIXxTQ", "colab_type": "text" }, "source": [ "## 1) Create a length-10 integer array filled with zeros\n" ] }, { "cell_type": "code", "metadata": { "id": "0MzRAnM-W2iK", "colab_type": "code", "outputId": "74ab7651-7f3b-414e-c77f-dec96e90cc0b", "colab": { "base_uri": "https://localhost:8080/", "height": 35 } }, "source": [ "import numpy as np\n", "a = np.zeros(10, dtype = int)\n", "a" ], "execution_count": 12, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0])" ] }, "metadata": { "tags": [] }, "execution_count": 12 } ] }, { "cell_type": "markdown", "metadata": { "id": "8Ury1CAPXzSk", "colab_type": "text" }, "source": [ "\n", "\n", "---\n", "\n" ] }, { "cell_type": "markdown", "metadata": { "id": "dYjRdHopX19p", "colab_type": "text" }, "source": [ "## 2) Create a 3x5 floating-point array filled with 1s\n" ] }, { "cell_type": "code", "metadata": { "id": "VOpOMiVWXbOy", "colab_type": "code", "outputId": "eb95fe66-4865-40f1-e2a9-35c6ca86d8dc", "colab": { "base_uri": "https://localhost:8080/", "height": 69 } }, "source": [ "a = np.ones((3,5), dtype = float)\n", "a" ], "execution_count": 22, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "array([[1., 1., 1., 1., 1.],\n", " [1., 1., 1., 1., 1.],\n", " [1., 1., 1., 1., 1.]])" ] }, "metadata": { "tags": [] }, "execution_count": 22 } ] }, { "cell_type": "markdown", "metadata": { "id": "IyFgzU4PX3-W", "colab_type": "text" }, "source": [ "\n", "\n", "---\n", "\n" ] }, { "cell_type": "markdown", "metadata": { "id": "ZAaaM1WMX5-e", "colab_type": "text" }, "source": [ "## 3) Create a 3x5 array filled with 3.14\n" ] }, { "cell_type": "code", "metadata": { "id": "5ibzOFejXdBw", "colab_type": "code", "outputId": "b1fc4739-e833-4c67-8333-3f7b726786f0", "colab": { "base_uri": "https://localhost:8080/", "height": 69 } }, "source": [ "a = np.full((3,5), 3.14)\n", "a" ], "execution_count": 14, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "array([[3.14, 3.14, 3.14, 3.14, 3.14],\n", " [3.14, 3.14, 3.14, 3.14, 3.14],\n", " [3.14, 3.14, 3.14, 3.14, 3.14]])" ] }, "metadata": { "tags": [] }, "execution_count": 14 } ] }, { "cell_type": "markdown", "metadata": { "id": "XU6sXESHX7Yq", "colab_type": "text" }, "source": [ "\n", "\n", "---\n", "\n" ] }, { "cell_type": "markdown", "metadata": { "id": "DBjESrc-X-Jd", "colab_type": "text" }, "source": [ "## 4) Create an array filled with a linear sequence. Starting at 0, ending at 20, stepping by 2 (this is similar to the built-in range() function)." ] }, { "cell_type": "code", "metadata": { "id": "SmnG7bsJXXNd", "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", "height": 35 }, "outputId": "3a764070-9ab5-4eff-d3c4-470b6b17419c" }, "source": [ "np.arange(0, 20, 2)" ], "execution_count": 15, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "array([ 0, 2, 4, 6, 8, 10, 12, 14, 16, 18])" ] }, "metadata": { "tags": [] }, "execution_count": 15 } ] }, { "cell_type": "markdown", "metadata": { "id": "pD6cSanKYDBc", "colab_type": "text" }, "source": [ "\n", "\n", "---\n", "\n" ] }, { "cell_type": "markdown", "metadata": { "id": "dv690QypYFvA", "colab_type": "text" }, "source": [ "## 5) Create an array of five values evenly spaced between 0 and 1" ] }, { "cell_type": "code", "metadata": { "id": "jQ4TWY29XeMG", "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", "height": 35 }, "outputId": "4d7d9357-7554-4c2a-de94-a8ddd82f3906" }, "source": [ "np.linspace(0, 1, 5)" ], "execution_count": 16, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "array([0. , 0.25, 0.5 , 0.75, 1. ])" ] }, "metadata": { "tags": [] }, "execution_count": 16 } ] }, { "cell_type": "markdown", "metadata": { "id": "neH26JU1YHfn", "colab_type": "text" }, "source": [ "\n", "\n", "---\n", "\n" ] }, { "cell_type": "markdown", "metadata": { "id": "J_lovxi_YNlH", "colab_type": "text" }, "source": [ "## 6) Create a 3x3 array of uniformly distributed random values between 0 and 1" ] }, { "cell_type": "markdown", "metadata": { "id": "a9nBsRDnYQfx", "colab_type": "text" }, "source": [ "**HINT when you just use np.random(3, 3), then you get this error: 'module' object is not callable**" ] }, { "cell_type": "code", "metadata": { "id": "c3h2AQNnXfbj", "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", "height": 69 }, "outputId": "3267695e-3b10-4fe8-e60d-a4b6bdb503ff" }, "source": [ "a = np.random.random((3, 3))\n", "a" ], "execution_count": 17, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "array([[0.92882601, 0.15277742, 0.61847288],\n", " [0.26835166, 0.1343164 , 0.13999919],\n", " [0.43270145, 0.89307018, 0.52218327]])" ] }, "metadata": { "tags": [] }, "execution_count": 17 } ] }, { "cell_type": "markdown", "metadata": { "id": "-WlPKcHgYSSu", "colab_type": "text" }, "source": [ "\n", "\n", "---\n", "\n" ] }, { "cell_type": "markdown", "metadata": { "id": "WYCxtYkJYVd4", "colab_type": "text" }, "source": [ "## 7) Create a 3x3 array of normally distributed random values with mean 0 and standard deviation 1" ] }, { "cell_type": "code", "metadata": { "id": "597oUpxWXjN_", "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", "height": 69 }, "outputId": "39b82f9f-3c37-4625-ad71-3c4027d41b68" }, "source": [ "a = np.random.normal(0, 1, (3, 3))\n", "a" ], "execution_count": 18, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "array([[ 0.54278146, -0.29350707, -0.76737292],\n", " [-0.00821164, -0.49738633, 0.13785728],\n", " [ 1.27666228, -1.43017619, -1.2518886 ]])" ] }, "metadata": { "tags": [] }, "execution_count": 18 } ] }, { "cell_type": "markdown", "metadata": { "id": "BuNlmsZrYW0G", "colab_type": "text" }, "source": [ "\n", "\n", "---\n", "\n" ] }, { "cell_type": "markdown", "metadata": { "id": "41u4LXXmYZIf", "colab_type": "text" }, "source": [ "## 8) Create a 3x3 array of random integers in the interval [0, 10)\n" ] }, { "cell_type": "code", "metadata": { "id": "T9A9H2q-Xm8W", "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", "height": 69 }, "outputId": "ba885c13-5a6f-487a-a41e-64b75311c8a2" }, "source": [ "a = np.random.randint(0, 10, (3, 3))\n", "a" ], "execution_count": 19, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "array([[8, 7, 3],\n", " [7, 7, 0],\n", " [1, 7, 8]])" ] }, "metadata": { "tags": [] }, "execution_count": 19 } ] }, { "cell_type": "markdown", "metadata": { "id": "jifmAAzTYb0F", "colab_type": "text" }, "source": [ "\n", "\n", "---\n", "\n" ] }, { "cell_type": "markdown", "metadata": { "id": "zCM5oKA_YdRG", "colab_type": "text" }, "source": [ "## 9) Create a 3x3 identity matrix\n" ] }, { "cell_type": "code", "metadata": { "id": "LdN51hmlXo0g", "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", "height": 69 }, "outputId": "7c59f2a2-b635-49f8-eee4-1764296f1da7" }, "source": [ "a = np.eye(3)\n", "a" ], "execution_count": 20, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "array([[1., 0., 0.],\n", " [0., 1., 0.],\n", " [0., 0., 1.]])" ] }, "metadata": { "tags": [] }, "execution_count": 20 } ] }, { "cell_type": "markdown", "metadata": { "id": "oi9JH0mmYe5j", "colab_type": "text" }, "source": [ "\n", "\n", "---\n", "\n" ] }, { "cell_type": "markdown", "metadata": { "id": "5aX0jBwDYijO", "colab_type": "text" }, "source": [ "## 10) Create an uninitialized array of three integers. The values will be whatever happens to already exist at that memory location\n" ] }, { "cell_type": "code", "metadata": { "id": "-UwNYT1tXrG1", "colab_type": "code", "colab": { "base_uri": "https://localhost:8080/", "height": 35 }, "outputId": "ad7bbd04-67ae-4036-b1f4-8af588731413" }, "source": [ "np.empty(3)" ], "execution_count": 21, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "array([1., 1., 1.])" ] }, "metadata": { "tags": [] }, "execution_count": 21 } ] }, { "cell_type": "markdown", "metadata": { "id": "DCsFgrFUYkFD", "colab_type": "text" }, "source": [ "\n", "\n", "---\n", "\n" ] } ] }