{ "metadata": { "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.9.2-final" }, "orig_nbformat": 2, "kernelspec": { "name": "python392jvsc74a57bd0a27d3f2bf68df5402465348834a2195030d3fc5bfc8e594e2a17c8c7e2447c85", "display_name": "Python 3.9.2 64-bit" } }, "nbformat": 4, "nbformat_minor": 2, "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import numpy as np" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "\n\nsize of x: 28\nsize of y: 160\n" ] } ], "source": [ "x = 42\n", "y = 2**1000\n", "print(x.__repr__)\n", "print(y.__repr__)\n", "print(f\"size of x: {sys.getsizeof(x)}\")\n", "print(f\"size of y: {sys.getsizeof(y)}\")\n" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "[ 1 4 7 10 13]\n['1' '4' 'two']\n" ] } ], "source": [ "# let's create a numpy vector (1 dimensional array)\n", "list_1 = [x for x in range(1, 16, 3)]\n", "vector = np.array(list_1)\n", "print(vector)\n", "\n", "list_2 = [1, 4, \"two\"]\n", "vector = np.array(list_2)\n", "print(vector)\n" ] }, { "source": [ "What happened there? Recall that NumPy only supports arrays whose elements are all the same type (unlike Python's lists). So, when it got our second list it attmepted to resolve the problem by trying to cast the elements to one of the types in the list. The only one that would work, was casting them all to strings. There was no way to cast \"two\" to an integer.\n", "\n", "We can tell NumPy that we want all our elements to be of a specific type. So, let's try \"list_2\" again specifying that we want all elements of the NumPy array to be integers.\n" ], "cell_type": "markdown", "metadata": {} }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "output_type": "error", "ename": "ValueError", "evalue": "invalid literal for int() with base 10: 'two'", "traceback": [ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[1;31mValueError\u001b[0m Traceback (most recent call last)", "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mnp\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0marray\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mlist_2\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mdtype\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;34m'int32'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[1;31mValueError\u001b[0m: invalid literal for int() with base 10: 'two'" ] } ], "source": [ "np.array(list_2, dtype='int32')\n" ] }, { "source": [ "As expected an error is raised. Which is probably what we'd rather have happen then all the elements being turned into strings. Let's try a string, that could be cast into an integer." ], "cell_type": "markdown", "metadata": {} }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "[ 1 7 42]\n" ] } ], "source": [ "list_3 = [1, 7, '42']\n", "vector = np.array(list_3, dtype='int32')\n", "print(vector)\n" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "array = \n[[ 42 126 210 294 378]\n [ 22 66 110 154 198]]\n\narray_2 = \n[[ 22 66 110 154 198]\n [ 42 126 210 294 378]\n [101 102 103 104 105]]\n" ] } ], "source": [ "# let's quickly look at initializing mutlidimensional arrays\n", "list_4 = [x for x in range(42, 424, 84)]\n", "list_5 = [x for x in range(22, 223, 44)]\n", "array = np.array([list_4, list_5])\n", "print(f\"array = \\n{array}\")\n", "list_6 = [x for x in range(101, 106)]\n", "array_2 = np.array([list_5, list_4, list_6])\n", "print(f\"\\narray_2 = \\n{array_2}\")\n" ] }, { "source": [ "NumPy provides numerous methods to generate arrays. Some of these will likely come in handy down the road." ], "cell_type": "markdown", "metadata": {} }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "[0 0 0 0 0 0 0 0 0 0]\n" ] } ], "source": [ "# Create a 10 element integer vector filled with zeros\n", "vector_0 = np.zeros(10, dtype='uint16')\n", "# the underscore character refers to the value of the last expression the interpreter generated\n", "print(vector_0)\n" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "[[1. 1. 1.]\n [1. 1. 1.]\n [1. 1. 1.]]\n" ] } ], "source": [ "# Create a 3x3 array full of ones (as floats)\n", "array_1s = np.ones((3, 3), dtype=float)\n", "print(array_1s)" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "[[1.4142135 1.4142135 1.4142135 1.4142135]\n [1.4142135 1.4142135 1.4142135 1.4142135]\n [1.4142135 1.4142135 1.4142135 1.4142135]\n [1.4142135 1.4142135 1.4142135 1.4142135]]\n" ] } ], "source": [ "# Create a 4x4 array filled with the sqrt of 2, specifically of type float32\n", "array_sqrt = np.full((4, 4), 2**0.5, dtype='float32')\n", "print(array_sqrt)\n" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "[[0.54329109 0.72895073 0.01688145]\n [0.3303388 0.36872182 0.04830367]\n [0.10453019 0.09743752 0.24540331]]\n\n [[-0.27811698 -0.23079156 0.88909145]\n [ 0.12753144 0.8172136 0.20920219]\n [-0.82934137 0.20437988 0.17573818]]\n\n [[3 1 1]\n [6 4 4]\n [6 5 6]]\n" ] } ], "source": [ "# set seed so that repeatedly running the cell gives the same result each time\n", "np.random.seed(333)\n", "\n", "# how about a 3x3 array of uniformly distributed random values between 0 and 1\n", "a_rnd = np.random.random((3, 3))\n", "print(a_rnd)\n", "\n", "# or a random set of normally distributed values with a mean of 0.25 and standard deviation of 1\n", "a_norm = np.random.normal(0.25, 1, (3, 3))\n", "print(\"\\n\", a_norm)\n", "#print(sum([ 1.3940603, 1.74046459, -1.61346926]) / 3)\n", "\n", "# or 3x3 array of random integers in the interval (1, 6) inclusive\n", "a_die = np.random.randint(1, 7, (3, 3))\n", "print(\"\\n\", a_die)\n" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "[[1 0 0]\n [0 1 0]\n [0 0 1]]\n" ] } ], "source": [ "# and the last example, create a 3x3 integer identity matrix\n", "m_id = np.eye(3, dtype=int)\n", "print(m_id)" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "[ 1 5 9 13 17 21 25]\n\n [1. 1.5 2. 2.5 3. ]\n\n [[0.27811698 0.23079156 0.88909145]\n [0.12753144 0.8172136 0.20920219]\n [0.82934137 0.20437988 0.17573818]]\n" ] } ], "source": [ "# decided to add a couple of methods not covered in the blog post (bonus!)\n", "\n", "# fill array with a sequence of numbers, sort of like Python's range() function\n", "a_rng = np.arange(1, 26, 4)\n", "print(a_rng)\n", "\n", "# or how about an array of evenly spaced values, 5 between 1 and 3 inclusive\n", "a_ln = np.linspace(1, 3, 5)\n", "print(\"\\n\", a_ln)\n", "\n", "# or an empty array, this one is a bit strange, but allows for very quick array initialization\n", "a_mt = np.empty((3, 3))\n", "print(\"\\n\", a_mt)" ] }, { "source": [ "I said an empty array, but doesn't look empty? Well memory isn't always empty. So whatever was previously in the memory the array was given become the values in the array (if any). So, make sure you eventually assign proper values to each element, or your code will have bugs you may have trouble sorting out." ], "cell_type": "markdown", "metadata": {} } ] }