|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "# Context" |
| 8 | + ] |
| 9 | + }, |
| 10 | + { |
| 11 | + "cell_type": "markdown", |
| 12 | + "metadata": {}, |
| 13 | + "source": [ |
| 14 | + "In reality, we don't use the `NumPy` package directly much for data analysis. But Python libraries such as `Pandas`, `Scikit-learn` are all built upon `NumPy`, so it is a foundation library.\n", |
| 15 | + "\n", |
| 16 | + "It is *optional* for this course to learn `NumPy`. However, if you are curious about `NumPy`, this notebook includes some very basic concepts for `NumPy`, which will give you an idea of what `NumPy` is.\n", |
| 17 | + "\n", |
| 18 | + "For a detailed `NumPy` tutorial, you can read **Python NumPy Tutorial: Practical Basics for Data Science** (https://www.justintodata.com/python-numpy-tutorial-basics-for-data-science/)." |
| 19 | + ] |
| 20 | + }, |
| 21 | + { |
| 22 | + "cell_type": "markdown", |
| 23 | + "metadata": {}, |
| 24 | + "source": [ |
| 25 | + "# What is `NumPy`" |
| 26 | + ] |
| 27 | + }, |
| 28 | + { |
| 29 | + "cell_type": "markdown", |
| 30 | + "metadata": {}, |
| 31 | + "source": [ |
| 32 | + "`NumPy` is the fundamental library for array/scientific computing in Python. \n", |
| 33 | + "\n", |
| 34 | + "`NumPy` introduces powerful **n-dimensional arrays**, which are more memory efficient and faster than Python lists.\n", |
| 35 | + "\n", |
| 36 | + "`NumPy` makes Python easier for numerical operations, and it also provides useful linear algebra, Fourier transform, and random number capabilities." |
| 37 | + ] |
| 38 | + }, |
| 39 | + { |
| 40 | + "cell_type": "markdown", |
| 41 | + "metadata": {}, |
| 42 | + "source": [ |
| 43 | + "# Import `NumPy`" |
| 44 | + ] |
| 45 | + }, |
| 46 | + { |
| 47 | + "cell_type": "code", |
| 48 | + "execution_count": 1, |
| 49 | + "metadata": {}, |
| 50 | + "outputs": [], |
| 51 | + "source": [ |
| 52 | + "import numpy as np" |
| 53 | + ] |
| 54 | + }, |
| 55 | + { |
| 56 | + "cell_type": "markdown", |
| 57 | + "metadata": {}, |
| 58 | + "source": [ |
| 59 | + "# `NumPy` array creation and basic attributes" |
| 60 | + ] |
| 61 | + }, |
| 62 | + { |
| 63 | + "cell_type": "markdown", |
| 64 | + "metadata": {}, |
| 65 | + "source": [ |
| 66 | + "The main object within `NumPy` is the multi-dimensional array (`ndarray`). It’s a table of elements (usually numbers), all the same type, indexed by a tuple of non-negative integers." |
| 67 | + ] |
| 68 | + }, |
| 69 | + { |
| 70 | + "cell_type": "markdown", |
| 71 | + "metadata": {}, |
| 72 | + "source": [ |
| 73 | + "Below we create a multi-dimensional array from Python lists as an example." |
| 74 | + ] |
| 75 | + }, |
| 76 | + { |
| 77 | + "cell_type": "code", |
| 78 | + "execution_count": 2, |
| 79 | + "metadata": {}, |
| 80 | + "outputs": [ |
| 81 | + { |
| 82 | + "data": { |
| 83 | + "text/plain": [ |
| 84 | + "array([[1, 2, 3],\n", |
| 85 | + " [4, 5, 6],\n", |
| 86 | + " [7, 8, 9]])" |
| 87 | + ] |
| 88 | + }, |
| 89 | + "execution_count": 2, |
| 90 | + "metadata": {}, |
| 91 | + "output_type": "execute_result" |
| 92 | + } |
| 93 | + ], |
| 94 | + "source": [ |
| 95 | + "x = np.array([[1,2,3], [4,5,6], [7,8,9]])\n", |
| 96 | + "x" |
| 97 | + ] |
| 98 | + }, |
| 99 | + { |
| 100 | + "cell_type": "code", |
| 101 | + "execution_count": 3, |
| 102 | + "metadata": {}, |
| 103 | + "outputs": [ |
| 104 | + { |
| 105 | + "data": { |
| 106 | + "text/plain": [ |
| 107 | + "numpy.ndarray" |
| 108 | + ] |
| 109 | + }, |
| 110 | + "execution_count": 3, |
| 111 | + "metadata": {}, |
| 112 | + "output_type": "execute_result" |
| 113 | + } |
| 114 | + ], |
| 115 | + "source": [ |
| 116 | + "type(x)" |
| 117 | + ] |
| 118 | + }, |
| 119 | + { |
| 120 | + "cell_type": "markdown", |
| 121 | + "metadata": {}, |
| 122 | + "source": [ |
| 123 | + "Let’s also look at its important attributes:\n", |
| 124 | + "\n", |
| 125 | + "- `ndim`: the number of axes (dimensions).\n", |
| 126 | + "- `shape`: the dimensions of the array as a tuple. For a matrix with n rows and m columns, the shape will be (n,m).\n", |
| 127 | + "- `size`: the total number of elements of the array, which equals the product of the elements of shape.\n", |
| 128 | + "- `dtype`: the data type of the elements in the array. \n", |
| 129 | + "\n", |
| 130 | + "`NumPy` supports more numerical types than Python does. For instance, `NumPy` has its data types like `numpy.int32` and `numpy.float64`. For a complete list of data types in `NumPy`, take a look at the official data types document (https://numpy.org/devdocs/user/basics.types.html). \n", |
| 131 | + "We'll cover more on dtypes in the course." |
| 132 | + ] |
| 133 | + }, |
| 134 | + { |
| 135 | + "cell_type": "code", |
| 136 | + "execution_count": 4, |
| 137 | + "metadata": {}, |
| 138 | + "outputs": [ |
| 139 | + { |
| 140 | + "data": { |
| 141 | + "text/plain": [ |
| 142 | + "2" |
| 143 | + ] |
| 144 | + }, |
| 145 | + "execution_count": 4, |
| 146 | + "metadata": {}, |
| 147 | + "output_type": "execute_result" |
| 148 | + } |
| 149 | + ], |
| 150 | + "source": [ |
| 151 | + "x.ndim" |
| 152 | + ] |
| 153 | + }, |
| 154 | + { |
| 155 | + "cell_type": "code", |
| 156 | + "execution_count": 5, |
| 157 | + "metadata": {}, |
| 158 | + "outputs": [ |
| 159 | + { |
| 160 | + "data": { |
| 161 | + "text/plain": [ |
| 162 | + "(3, 3)" |
| 163 | + ] |
| 164 | + }, |
| 165 | + "execution_count": 5, |
| 166 | + "metadata": {}, |
| 167 | + "output_type": "execute_result" |
| 168 | + } |
| 169 | + ], |
| 170 | + "source": [ |
| 171 | + "x.shape" |
| 172 | + ] |
| 173 | + }, |
| 174 | + { |
| 175 | + "cell_type": "code", |
| 176 | + "execution_count": 6, |
| 177 | + "metadata": {}, |
| 178 | + "outputs": [ |
| 179 | + { |
| 180 | + "data": { |
| 181 | + "text/plain": [ |
| 182 | + "9" |
| 183 | + ] |
| 184 | + }, |
| 185 | + "execution_count": 6, |
| 186 | + "metadata": {}, |
| 187 | + "output_type": "execute_result" |
| 188 | + } |
| 189 | + ], |
| 190 | + "source": [ |
| 191 | + "x.size" |
| 192 | + ] |
| 193 | + }, |
| 194 | + { |
| 195 | + "cell_type": "code", |
| 196 | + "execution_count": 7, |
| 197 | + "metadata": {}, |
| 198 | + "outputs": [ |
| 199 | + { |
| 200 | + "data": { |
| 201 | + "text/plain": [ |
| 202 | + "dtype('int32')" |
| 203 | + ] |
| 204 | + }, |
| 205 | + "execution_count": 7, |
| 206 | + "metadata": {}, |
| 207 | + "output_type": "execute_result" |
| 208 | + } |
| 209 | + ], |
| 210 | + "source": [ |
| 211 | + "x.dtype" |
| 212 | + ] |
| 213 | + }, |
| 214 | + { |
| 215 | + "cell_type": "markdown", |
| 216 | + "metadata": {}, |
| 217 | + "source": [ |
| 218 | + "Many of the `NumPy` array operations are similar to `Pandas`, which we'll explore throughout the course. So we won't cover them here." |
| 219 | + ] |
| 220 | + }, |
| 221 | + { |
| 222 | + "cell_type": "code", |
| 223 | + "execution_count": null, |
| 224 | + "metadata": {}, |
| 225 | + "outputs": [], |
| 226 | + "source": [] |
| 227 | + } |
| 228 | + ], |
| 229 | + "metadata": { |
| 230 | + "kernelspec": { |
| 231 | + "display_name": "Python 3", |
| 232 | + "language": "python", |
| 233 | + "name": "python3" |
| 234 | + }, |
| 235 | + "language_info": { |
| 236 | + "codemirror_mode": { |
| 237 | + "name": "ipython", |
| 238 | + "version": 3 |
| 239 | + }, |
| 240 | + "file_extension": ".py", |
| 241 | + "mimetype": "text/x-python", |
| 242 | + "name": "python", |
| 243 | + "nbconvert_exporter": "python", |
| 244 | + "pygments_lexer": "ipython3", |
| 245 | + "version": "3.7.6" |
| 246 | + } |
| 247 | + }, |
| 248 | + "nbformat": 4, |
| 249 | + "nbformat_minor": 4 |
| 250 | +} |
0 commit comments