From 692b0a55f376c99bca6a449835c7a9dac71ac7be Mon Sep 17 00:00:00 2001 From: Suneel Patel <48252015+suneelpatel@users.noreply.github.com> Date: Sat, 31 Aug 2019 21:58:46 +0530 Subject: [PATCH] Add files via upload --- Cheatsheets1/Numpy_Cheatsheet.ipynb | 1903 +++++++++++++++++++++++++++ 1 file changed, 1903 insertions(+) create mode 100644 Cheatsheets1/Numpy_Cheatsheet.ipynb diff --git a/Cheatsheets1/Numpy_Cheatsheet.ipynb b/Cheatsheets1/Numpy_Cheatsheet.ipynb new file mode 100644 index 0000000..87cbe76 --- /dev/null +++ b/Cheatsheets1/Numpy_Cheatsheet.ipynb @@ -0,0 +1,1903 @@ +{ + "nbformat": 4, + "nbformat_minor": 0, + "metadata": { + "colab": { + "name": "Numpy_Cheatsheet.ipynb", + "version": "0.3.2", + "provenance": [] + }, + "kernelspec": { + "name": "python3", + "display_name": "Python 3" + } + }, + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "id": "GSao7LovOgpm", + "colab_type": "text" + }, + "source": [ + "The NumPy library is the core library for scientific computing in\n", + "Python. It provides a high-performance multidimensional array\n", + "object, and tools for working with these arrays.\n", + "\n", + "---\n", + "\n", + "import numpy as np\n", + "\n", + "Use the following import convention:" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "HdacAqT_OfLJ", + "colab_type": "code", + "colab": {} + }, + "source": [ + "import numpy as np" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "id": "9STEWhl1OtR1", + "colab_type": "code", + "outputId": "386624c8-0942-44dc-bfe6-50fc174cd7bd", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 153 + } + }, + "source": [ + "# Creating Array\n", + "a = np.array([1,2,3]) #one dimensional array\n", + "b = np.array([(1.5,2,3), (4,5,6)], dtype = float)\n", + "c = np.array([[(1.5,2,3), (4,5,6)], [(3,2,1), (4,5,6)]],\n", + "dtype = float)\n", + "print(a)\n", + "print(b)\n", + "print(c)" + ], + "execution_count": 89, + "outputs": [ + { + "output_type": "stream", + "text": [ + "[1 2 3]\n", + "[[1.5 2. 3. ]\n", + " [4. 5. 6. ]]\n", + "[[[1.5 2. 3. ]\n", + " [4. 5. 6. ]]\n", + "\n", + " [[3. 2. 1. ]\n", + " [4. 5. 6. ]]]\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "tlBZjVxdUaTq", + "colab_type": "text" + }, + "source": [ + "## Initial Placeholders" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "ZZN-53biUXFr", + "colab_type": "code", + "outputId": "3b0c798b-a2e9-433f-98a0-480ea4dd093d", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 68 + } + }, + "source": [ + "# Create an array of zeros\n", + "np.zeros((3,4))" + ], + "execution_count": 90, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "array([[0., 0., 0., 0.],\n", + " [0., 0., 0., 0.],\n", + " [0., 0., 0., 0.]])" + ] + }, + "metadata": { + "tags": [] + }, + "execution_count": 90 + } + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "fysO8kheUj3k", + "colab_type": "code", + "outputId": "47e95be7-0b97-400f-fe1a-841a9c6b9f30", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 68 + } + }, + "source": [ + "# Create an array of ones\n", + "np.ones((3,4), dtype = np.int16)" + ], + "execution_count": 91, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "array([[1, 1, 1, 1],\n", + " [1, 1, 1, 1],\n", + " [1, 1, 1, 1]], dtype=int16)" + ] + }, + "metadata": { + "tags": [] + }, + "execution_count": 91 + } + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "pb_nbJpNUorE", + "colab_type": "code", + "outputId": "3a0872b2-d2e3-4ffa-8a35-3c1a1aa87b2e", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + } + }, + "source": [ + "#Create an array of evenly spaced values (step value)\n", + "d = np.arange(10,25,5) \n", + "print(d)" + ], + "execution_count": 92, + "outputs": [ + { + "output_type": "stream", + "text": [ + "[10 15 20]\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "jfMt7ydzVCjq", + "colab_type": "code", + "outputId": "9218c6d8-e1d0-49b8-b06f-99d5ad35e45e", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + } + }, + "source": [ + "# Create an array of evenly spaced values (number of samples)\n", + "np.linspace(0,2,9)" + ], + "execution_count": 93, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "array([0. , 0.25, 0.5 , 0.75, 1. , 1.25, 1.5 , 1.75, 2. ])" + ] + }, + "metadata": { + "tags": [] + }, + "execution_count": 93 + } + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "9XR-jZaCVnpd", + "colab_type": "code", + "outputId": "42ff0997-e2b2-4e9e-de25-693b8ce67275", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 51 + } + }, + "source": [ + "# Create a constant array\n", + "e = np.full((2,2),7)\n", + "print(e)" + ], + "execution_count": 94, + "outputs": [ + { + "output_type": "stream", + "text": [ + "[[7 7]\n", + " [7 7]]\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "r3cPqm-GVujq", + "colab_type": "code", + "outputId": "f8a0e0a5-df91-41e0-84c2-cab4a9716908", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 51 + } + }, + "source": [ + "# Create a 2X2 identity matrix\n", + "f = np.eye(2)\n", + "print(f)" + ], + "execution_count": 95, + "outputs": [ + { + "output_type": "stream", + "text": [ + "[[1. 0.]\n", + " [0. 1.]]\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "P6siTuL2V58G", + "colab_type": "code", + "outputId": "84fb78dc-dcdb-4a90-cf06-8e3ab4a0a209", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 51 + } + }, + "source": [ + "# Create an array with random values\n", + "np.random.random((2,2))" + ], + "execution_count": 96, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "array([[0.16349153, 0.45692408],\n", + " [0.5095391 , 0.47522291]])" + ] + }, + "metadata": { + "tags": [] + }, + "execution_count": 96 + } + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "iTqUrqsQWAKA", + "colab_type": "code", + "outputId": "af65697d-31d8-470f-c24f-5421c8d2393f", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 68 + } + }, + "source": [ + "# Create an empty array\n", + "np.empty((3,2))" + ], + "execution_count": 97, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "array([[1.5, 2. ],\n", + " [3. , 4. ],\n", + " [5. , 6. ]])" + ] + }, + "metadata": { + "tags": [] + }, + "execution_count": 97 + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "12yj21WRWHiy", + "colab_type": "text" + }, + "source": [ + "# I/O" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "_Ih9evxOWMmo", + "colab_type": "text" + }, + "source": [ + "## Saving & Loading On Disk" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "JTZLKJSgWGxx", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + }, + "outputId": "805ca9ae-103e-4d0f-8ed1-210dfa182564" + }, + "source": [ + "np.save('my_array', a)\n", + "np.savez('array.npz', a, b)\n", + "np.load('my_array.npy')" + ], + "execution_count": 98, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "array([1, 2, 3])" + ] + }, + "metadata": { + "tags": [] + }, + "execution_count": 98 + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "Tkfr5MFvYgcn", + "colab_type": "text" + }, + "source": [ + "## Inspecting Your Array" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "xS07k10LYQmQ", + "colab_type": "code", + "outputId": "5eed1203-1549-4f13-c725-d10ac4838fc2", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 0 + } + }, + "source": [ + "# Array dimensions\n", + "a.shape" + ], + "execution_count": 99, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "(3,)" + ] + }, + "metadata": { + "tags": [] + }, + "execution_count": 99 + } + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "Ho9G8ik3YxN2", + "colab_type": "code", + "outputId": "c6d63ae7-7ba5-4deb-8078-d72d2ea56670", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 0 + } + }, + "source": [ + "# Length of array\n", + "# a.size\n", + "print(len(a))" + ], + "execution_count": 100, + "outputs": [ + { + "output_type": "stream", + "text": [ + "3\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "jxXe8Jo7ZD1V", + "colab_type": "code", + "outputId": "2dce6d17-bed5-4930-defa-ecad6c1535e7", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 0 + } + }, + "source": [ + "# Number of array dimensions\n", + "b.ndim" + ], + "execution_count": 101, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "2" + ] + }, + "metadata": { + "tags": [] + }, + "execution_count": 101 + } + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "1LqzMBkIZOtz", + "colab_type": "code", + "outputId": "0b61ec46-72b3-4dc7-edf9-4167bbe1aaa8", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 0 + } + }, + "source": [ + "# Number of array elements\n", + "e.size" + ], + "execution_count": 102, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "4" + ] + }, + "metadata": { + "tags": [] + }, + "execution_count": 102 + } + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "MF7pZR_4ZVRU", + "colab_type": "code", + "outputId": "18db6fd5-9c27-486f-e5c9-4f13ab4514d7", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 0 + } + }, + "source": [ + "# Data type of array elements\n", + "b.dtype" + ], + "execution_count": 103, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "dtype('float64')" + ] + }, + "metadata": { + "tags": [] + }, + "execution_count": 103 + } + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "nNbxc-ulZfNj", + "colab_type": "code", + "outputId": "5b0df354-1a6d-4f44-e215-231ceffb7f97", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 0 + } + }, + "source": [ + "# Name of data type\n", + "b.dtype.name" + ], + "execution_count": 104, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "'float64'" + ] + }, + "metadata": { + "tags": [] + }, + "execution_count": 104 + } + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "Pjc3bMbLZfLW", + "colab_type": "code", + "outputId": "257cf84b-8e47-4969-e1eb-f316bd29201e", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 0 + } + }, + "source": [ + "# Convert an array to a different type\n", + "b.astype(int)" + ], + "execution_count": 105, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "array([[1, 2, 3],\n", + " [4, 5, 6]])" + ] + }, + "metadata": { + "tags": [] + }, + "execution_count": 105 + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "rSef8NB1aArP", + "colab_type": "text" + }, + "source": [ + "# Array Mathematics" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "vYxGrmVvaFE8", + "colab_type": "text" + }, + "source": [ + "## Arithmetic Operations" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "TmonErjDaBm1", + "colab_type": "code", + "outputId": "467ef534-3f44-441b-9f95-b3d3eee3aab8", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 51 + } + }, + "source": [ + "# Subtraction\n", + "g = a - b\n", + "print(g)" + ], + "execution_count": 106, + "outputs": [ + { + "output_type": "stream", + "text": [ + "[[-0.5 0. 0. ]\n", + " [-3. -3. -3. ]]\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "YDmUV_zuaOYT", + "colab_type": "code", + "outputId": "40cea5e1-a3d7-469a-bbe6-0c84392adbff", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 51 + } + }, + "source": [ + "# Substrcation\n", + "np.subtract(a,b)" + ], + "execution_count": 107, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "array([[-0.5, 0. , 0. ],\n", + " [-3. , -3. , -3. ]])" + ] + }, + "metadata": { + "tags": [] + }, + "execution_count": 107 + } + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "e-6xKb9maZLJ", + "colab_type": "code", + "outputId": "67291840-8456-41cf-c65b-7a799ef85769", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 51 + } + }, + "source": [ + "# Addition\n", + "h = a + b\n", + "print(h)" + ], + "execution_count": 108, + "outputs": [ + { + "output_type": "stream", + "text": [ + "[[2.5 4. 6. ]\n", + " [5. 7. 9. ]]\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "ykIO5yMtaeYd", + "colab_type": "code", + "outputId": "d7d9c782-4d0a-4a12-be5d-9b12d2ce3752", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 51 + } + }, + "source": [ + "# Addition\n", + "np.add(a,b)" + ], + "execution_count": 109, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "array([[2.5, 4. , 6. ],\n", + " [5. , 7. , 9. ]])" + ] + }, + "metadata": { + "tags": [] + }, + "execution_count": 109 + } + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "zHanUrd4apQ8", + "colab_type": "code", + "outputId": "f55e1459-2ed3-4cce-f98e-2bdf02cd013e", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 51 + } + }, + "source": [ + "# Division\n", + "i = a/b\n", + "print(i)" + ], + "execution_count": 110, + "outputs": [ + { + "output_type": "stream", + "text": [ + "[[0.66666667 1. 1. ]\n", + " [0.25 0.4 0.5 ]]\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "O8l0qZxNat3B", + "colab_type": "code", + "outputId": "d98e6a40-45aa-413a-c140-03c0a8f6ae1c", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 51 + } + }, + "source": [ + "# Divisoin\n", + "np.divide(a,b)" + ], + "execution_count": 111, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "array([[0.66666667, 1. , 1. ],\n", + " [0.25 , 0.4 , 0.5 ]])" + ] + }, + "metadata": { + "tags": [] + }, + "execution_count": 111 + } + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "cpqAPDJRa1F6", + "colab_type": "code", + "outputId": "1edd61c4-e81b-4984-cf96-9ad812b3977b", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 51 + } + }, + "source": [ + "# Multiplication\n", + "k = a*b\n", + "print(k)" + ], + "execution_count": 112, + "outputs": [ + { + "output_type": "stream", + "text": [ + "[[ 1.5 4. 9. ]\n", + " [ 4. 10. 18. ]]\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "1Thf3QuObITP", + "colab_type": "code", + "outputId": "fe08b3b0-32f0-4ce2-b7fc-1a73168bb4a4", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 51 + } + }, + "source": [ + "# Multiplication\n", + "np.multiply(a,b)" + ], + "execution_count": 113, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "array([[ 1.5, 4. , 9. ],\n", + " [ 4. , 10. , 18. ]])" + ] + }, + "metadata": { + "tags": [] + }, + "execution_count": 113 + } + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "P9ynGmfRbrbw", + "colab_type": "code", + "outputId": "320a36d2-0e68-4222-e7af-a9c8575f64dd", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 51 + } + }, + "source": [ + "# Exponentiation\n", + "np.exp(b)" + ], + "execution_count": 114, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "array([[ 4.48168907, 7.3890561 , 20.08553692],\n", + " [ 54.59815003, 148.4131591 , 403.42879349]])" + ] + }, + "metadata": { + "tags": [] + }, + "execution_count": 114 + } + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "Wf7__Jt3buug", + "colab_type": "code", + "outputId": "eb7a4f68-6fb0-4f43-9a4c-5324669fc0c3", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 51 + } + }, + "source": [ + "# Square root\n", + "np.sqrt(b)" + ], + "execution_count": 115, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "array([[1.22474487, 1.41421356, 1.73205081],\n", + " [2. , 2.23606798, 2.44948974]])" + ] + }, + "metadata": { + "tags": [] + }, + "execution_count": 115 + } + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "9GpCUJPgbzj3", + "colab_type": "code", + "outputId": "8a8f6569-b953-47e0-feb0-64ca9baaf6ef", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + } + }, + "source": [ + "# Print sines of an array\n", + "np.sin(a)" + ], + "execution_count": 116, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "array([0.84147098, 0.90929743, 0.14112001])" + ] + }, + "metadata": { + "tags": [] + }, + "execution_count": 116 + } + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "a7VFvAyHb4K9", + "colab_type": "code", + "outputId": "e041dc04-ee58-418c-8a84-6e2abec4a2bc", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + } + }, + "source": [ + "# Element-wise cosine\n", + "np.cos(a)" + ], + "execution_count": 117, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "array([ 0.54030231, -0.41614684, -0.9899925 ])" + ] + }, + "metadata": { + "tags": [] + }, + "execution_count": 117 + } + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "nIeescw8cDhZ", + "colab_type": "code", + "outputId": "0bf55834-6d38-46af-bfe8-645169369467", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + } + }, + "source": [ + "# Element-wise natural logarithm\n", + "np.log(a)" + ], + "execution_count": 118, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "array([0. , 0.69314718, 1.09861229])" + ] + }, + "metadata": { + "tags": [] + }, + "execution_count": 118 + } + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "EI3Wxehyb5Yf", + "colab_type": "code", + "outputId": "a0e70552-279b-44a5-d2cf-587b76a6a54b", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 51 + } + }, + "source": [ + "# Dot product\n", + "e.dot(f)" + ], + "execution_count": 119, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "array([[7., 7.],\n", + " [7., 7.]])" + ] + }, + "metadata": { + "tags": [] + }, + "execution_count": 119 + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "lzTLsrtTWlR_", + "colab_type": "text" + }, + "source": [ + "## Comparison" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "xZSl8preW6mP", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 51 + }, + "outputId": "f1ab2bed-736e-48b3-bfe1-5dc57952992d" + }, + "source": [ + "# Element-wise comparison\n", + "a == b" + ], + "execution_count": 120, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "array([[False, True, True],\n", + " [False, False, False]])" + ] + }, + "metadata": { + "tags": [] + }, + "execution_count": 120 + } + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "kJM9HUGnXZ3M", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + }, + "outputId": "88d2af72-6539-4116-b2a1-357be59c4400" + }, + "source": [ + "# Element-wise comparison\n", + "a < 2" + ], + "execution_count": 121, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "array([ True, False, False])" + ] + }, + "metadata": { + "tags": [] + }, + "execution_count": 121 + } + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "LFJAm8cuXgiq", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + }, + "outputId": "ab03c30c-7289-4254-d737-92c1f5c13161" + }, + "source": [ + "# Array-wise comparison\n", + "np.array_equal(a, b)" + ], + "execution_count": 122, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "False" + ] + }, + "metadata": { + "tags": [] + }, + "execution_count": 122 + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "zECulZVYXtox", + "colab_type": "text" + }, + "source": [ + "## Aggregate Functions" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "Em8uRLUkXpNz", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + }, + "outputId": "21f460eb-9523-4a28-b502-891c03dd9def" + }, + "source": [ + "# Array-wise sum\n", + "a.sum()" + ], + "execution_count": 123, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "6" + ] + }, + "metadata": { + "tags": [] + }, + "execution_count": 123 + } + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "PxJZ0WDwYVRr", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + }, + "outputId": "85d446a4-e7eb-4b2e-ac72-f57db164384e" + }, + "source": [ + "# Array-wise minimum value\n", + "a.min()" + ], + "execution_count": 124, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "1" + ] + }, + "metadata": { + "tags": [] + }, + "execution_count": 124 + } + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "rviZv-stYbcs", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + }, + "outputId": "48228dcd-6a57-4a07-fad2-8ea4dfe06171" + }, + "source": [ + "# Maximum value of an array row\n", + "b.max(axis=0)" + ], + "execution_count": 125, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "array([4., 5., 6.])" + ] + }, + "metadata": { + "tags": [] + }, + "execution_count": 125 + } + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "qbqB39KXYgyN", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 51 + }, + "outputId": "fc3a5ddb-1fe4-49ba-ed26-5b64ee0bb003" + }, + "source": [ + "# Cumulative sum of the elements\n", + "b.cumsum(axis=1)" + ], + "execution_count": 126, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "array([[ 1.5, 3.5, 6.5],\n", + " [ 4. , 9. , 15. ]])" + ] + }, + "metadata": { + "tags": [] + }, + "execution_count": 126 + } + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "QAw7bpoKYm05", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + }, + "outputId": "2c5c0f88-0421-49ac-c982-9f4d914d465a" + }, + "source": [ + "# Mean\n", + "a.mean()" + ], + "execution_count": 127, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "2.0" + ] + }, + "metadata": { + "tags": [] + }, + "execution_count": 127 + } + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "E_yhi-h7YtCC", + "colab_type": "code", + "colab": {} + }, + "source": [ + "# Median\n", + "a.median()" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "id": "FdOBEt6DZvYt", + "colab_type": "code", + "colab": {} + }, + "source": [ + "#Correlation coefficient\n", + "a.corrcoef()" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "id": "UxUXoFsDaSuO", + "colab_type": "code", + "colab": {} + }, + "source": [ + "#Standard deviation\n", + "np.std(b)" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "lgKZ41_0aliQ", + "colab_type": "text" + }, + "source": [ + "## Copying Arrays" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "sm4GFW6rafx7", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + }, + "outputId": "7d04708d-21d6-413b-bf8e-e2c9e24c3132" + }, + "source": [ + "# Create a view of the array with the same data\n", + "h = a.view()\n", + "print(h)" + ], + "execution_count": 129, + "outputs": [ + { + "output_type": "stream", + "text": [ + "[1 2 3]\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "_GpUS22uav2s", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + }, + "outputId": "ae5654cd-1d89-4042-e54c-10a10922b289" + }, + "source": [ + "# Create a copy of the array\n", + "np.copy(a)" + ], + "execution_count": 130, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "array([1, 2, 3])" + ] + }, + "metadata": { + "tags": [] + }, + "execution_count": 130 + } + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "n3hSEsEQa2Uk", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + }, + "outputId": "60ecad99-22d7-4c41-9d20-1b0a67ffe6bb" + }, + "source": [ + "# Create a deep copy of the array\n", + "h = a.copy()\n", + "print(h)" + ], + "execution_count": 131, + "outputs": [ + { + "output_type": "stream", + "text": [ + "[1 2 3]\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "8BxkaAEcb7mP", + "colab_type": "text" + }, + "source": [ + "## Sorting Arrays" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "bxnd8G1hb88g", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + }, + "outputId": "bd79fdd3-1c5b-4aca-c330-9f1fef6a27e2" + }, + "source": [ + "# Sort an array\n", + "print(a.sort())" + ], + "execution_count": 132, + "outputs": [ + { + "output_type": "stream", + "text": [ + "None\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "ZAsOEeXAcCiw", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + }, + "outputId": "34b6f462-b60f-4a8c-aeaf-82fd1d4e6ef1" + }, + "source": [ + "#Sort the elements of an array's axis\n", + "print(c.sort(axis=0))" + ], + "execution_count": 133, + "outputs": [ + { + "output_type": "stream", + "text": [ + "None\n" + ], + "name": "stdout" + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "EtrPuNQAczT-", + "colab_type": "text" + }, + "source": [ + "## Subsetting, Slicing, Indexing" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "qq4IMDcxdIeD", + "colab_type": "text" + }, + "source": [ + "### Subsetting" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "CheFYDqVc0rp", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + }, + "outputId": "fd6f6c02-d926-4f1d-f833-cc343790fc88" + }, + "source": [ + "#Select the element at the 2nd index\n", + "a[2]" + ], + "execution_count": 134, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "3" + ] + }, + "metadata": { + "tags": [] + }, + "execution_count": 134 + } + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "YKd91Jevc_vK", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + }, + "outputId": "417b48dd-43e8-423a-8ffd-f1b7dfbeddbe" + }, + "source": [ + "# Select the element at row 1 column 2 (equivalent to b[1][2])\n", + "b[1,2]" + ], + "execution_count": 136, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "6.0" + ] + }, + "metadata": { + "tags": [] + }, + "execution_count": 136 + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "r0WRV-n4dMw_", + "colab_type": "text" + }, + "source": [ + "### Slicing" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "CU5gQ850dFbH", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + }, + "outputId": "06f0bc2e-2c5b-46ef-e771-a28ef0b1c491" + }, + "source": [ + "# Select items at index 0 and 1\n", + "a[0:2]" + ], + "execution_count": 137, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "array([1, 2])" + ] + }, + "metadata": { + "tags": [] + }, + "execution_count": 137 + } + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "S1sbExp1dTIA", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + }, + "outputId": "0cb6da77-f88e-4caa-8e01-0e8ff522e48d" + }, + "source": [ + "# Select items at rows 0 and 1 in column 1\n", + "b[0:2,1]" + ], + "execution_count": 138, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "array([2., 5.])" + ] + }, + "metadata": { + "tags": [] + }, + "execution_count": 138 + } + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "_kJWm4ODduvw", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + }, + "outputId": "022f3b69-9a0e-4519-a7f7-a828fe3fef95" + }, + "source": [ + "# Select all items at row 0 (equivalent to b[0:1, :])\n", + "b[:1]" + ], + "execution_count": 140, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "array([[1.5, 2. , 3. ]])" + ] + }, + "metadata": { + "tags": [] + }, + "execution_count": 140 + } + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "26WtK85od7tt", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + }, + "outputId": "7ca2882b-9808-48ff-8c1e-d4ac8a1306df" + }, + "source": [ + "# Reversed array a\n", + "a[ : :-1]" + ], + "execution_count": 141, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "array([3, 2, 1])" + ] + }, + "metadata": { + "tags": [] + }, + "execution_count": 141 + } + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "kFMHE-n3ffNk", + "colab_type": "text" + }, + "source": [ + "## Array Manipulation" + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "qeeP6LDYfgR8", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 51 + }, + "outputId": "d697ca35-ceaa-4a59-8cc1-3c2881727a0c" + }, + "source": [ + "#Transposing Array\n", + "i = np.transpose(b)\n", + "i.T" + ], + "execution_count": 143, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "array([[1.5, 2. , 3. ],\n", + " [4. , 5. , 6. ]])" + ] + }, + "metadata": { + "tags": [] + }, + "execution_count": 143 + } + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "mhSCx7sAftkO", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + }, + "outputId": "c1c48d28-67fb-4f12-8f77-fc5e402887ac" + }, + "source": [ + "# Changing Array Shape\n", + "b.ravel()" + ], + "execution_count": 145, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "array([1.5, 2. , 3. , 4. , 5. , 6. ])" + ] + }, + "metadata": { + "tags": [] + }, + "execution_count": 145 + } + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "Z5TiOitefyJq", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 68 + }, + "outputId": "282145f0-6175-47f5-93f1-d7b4f3ed7b20" + }, + "source": [ + "g.reshape(3,-2)" + ], + "execution_count": 148, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "array([[-0.5, 0. ],\n", + " [ 0. , -3. ],\n", + " [-3. , -3. ]])" + ] + }, + "metadata": { + "tags": [] + }, + "execution_count": 148 + } + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "yZ9ANvtKf3On", + "colab_type": "code", + "colab": {} + }, + "source": [ + "## Adding/Removing Elements\n", + "h.resize((2,6))\n" + ], + "execution_count": 0, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "id": "ObTo41NmgBa-", + "colab_type": "code", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 34 + }, + "outputId": "52f8c726-4c8f-462e-f893-bd270a0a25a2" + }, + "source": [ + "# Combining Arrays\n", + "np.concatenate((a,d),axis=0)" + ], + "execution_count": 150, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "array([ 1, 2, 3, 10, 15, 20])" + ] + }, + "metadata": { + "tags": [] + }, + "execution_count": 150 + } + ] + } + ] +} \ No newline at end of file