{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Creating Arrays\n", "\n", "- [Download the lecture notes](https://philchodrow.github.io/PIC16A/content/np_plt/numpy_2.ipynb). \n", "\n", "As described in the previous lecture, `numpy` arrays allow us to write highly readable and performant code when working with batches of numbers. In the next couple lectures, we'll go into a bit more detail about how to create, modify, and retrieve information from arrays. \n", "\n", "## Creating Arrays\n", "\n", "As we already discussed, the simplest way to create small, custom arrays in `numpy` is by transforming a list into an array. " ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([1, 2, 3, 4])" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import numpy as np\n", "\n", "L = [1, 2, 3, 4]\n", "a = np.array(L)\n", "a" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`numpy` also offers functions for building a number of common arrays. " ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([0., 0., 0.])" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "n = 3\n", "np.zeros(n)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([1., 1., 1.])" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.ones(n)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([0.82969143, 0.84623333, 0.95229689])" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# random numbers between 0 and 1\n", "np.random.rand(n)" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([0, 1, 2])" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# an array version of range()\n", "np.arange(n)" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([0. , 0.5, 1. ])" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# 3 evenly-spaced points between 0 and 1, inclusive\n", "np.linspace(0, 1, 3)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Multidimensional Arrays\n", "\n", "`numpy` arrays have **dimensions** (or **axes**). So far, we've only worked with one-dimensional arrays. A good way to check the dimensions of an array is with the `shape` attribute: " ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(3,)" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = np.ones(n)\n", "a.shape" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This says that `a` has a single dimension, and has three entries along that dimension. Here's a 2d array: " ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(3, 3)" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A = np.ones((n, n))\n", "A.shape" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now, `A` has two dimensions, and has three entries along each dimension. For 2-dimensional arrays, `numpy` will helpfully print out the array in format that makes the dimensions clear. " ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[1., 1., 1.],\n", " [1., 1., 1.],\n", " [1., 1., 1.]])" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can create arrays of arbitrary numbers of dimensions, although it might be confusing to keep track of them after a while..." ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[[1., 1., 1.],\n", " [1., 1., 1.],\n", " [1., 1., 1.]],\n", "\n", " [[1., 1., 1.],\n", " [1., 1., 1.],\n", " [1., 1., 1.]],\n", "\n", " [[1., 1., 1.],\n", " [1., 1., 1.],\n", " [1., 1., 1.]]])" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A = np.ones((n, n, n))\n", "A" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(3, 3, 3)" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A.shape" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The `reshape` method allows you to alter the dimensions of an array. For example, let's initialize a 1d array and transform it into a 2d array. " ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14])" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = np.arange(15)\n", "a" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[ 0, 1, 2, 3, 4],\n", " [ 5, 6, 7, 8, 9],\n", " [10, 11, 12, 13, 14]])" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "A = a.reshape(3, 5) # number of \"rows\" and \"columns\"\n", "A" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can also undo this operation: " ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14])" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = A.reshape(15)\n", "a" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "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.8.5" } }, "nbformat": 4, "nbformat_minor": 4 }