{ "cells": [ { "cell_type": "markdown", "id": "942f3aba", "metadata": {}, "source": [ "# Arithmetic\n", "\n", "Arrays allow you to perform stack operations on data without having to use `for` loops. This is called *vectorisation* in NumPy. For all arithmetic operations between arrays of the same size, the operation is applied element by element:" ] }, { "cell_type": "code", "execution_count": 1, "id": "cc82112f", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[-0.04778788, -0.03971093, -1.71110236],\n", " [ 1.18746556, 0.5643496 , 0.2859378 ],\n", " [-1.28686573, 0.40332209, -1.35913635],\n", " [-0.20140587, 1.60498449, 0.23844523],\n", " [-0.74730775, 0.11601887, -0.34109057],\n", " [-0.08702842, -1.1238352 , 1.21659397],\n", " [ 0.54382846, 0.12373199, -0.02012923]])" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import numpy as np\n", "\n", "\n", "data = np.random.randn(7, 3)\n", "data" ] }, { "cell_type": "code", "execution_count": 2, "id": "1d3fdcf6", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[-20.92580985, -25.18198603, -0.58441857],\n", " [ 0.84212969, 1.77195127, 3.49726411],\n", " [ -0.77708185, 2.47940793, -0.73576135],\n", " [ -4.96509854, 0.62305898, 4.19383522],\n", " [ -1.3381368 , 8.61928749, -2.93177261],\n", " [-11.49049937, -0.88981018, 0.82196692],\n", " [ 1.83881514, 8.0819842 , -49.67899596]])" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "1 / data" ] }, { "cell_type": "code", "execution_count": 3, "id": "3b409fac", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[2.28368106e-03, 1.57695769e-03, 2.92787128e+00],\n", " [1.41007445e+00, 3.18490476e-01, 8.17604243e-02],\n", " [1.65602341e+00, 1.62668712e-01, 1.84725163e+00],\n", " [4.05643252e-02, 2.57597521e+00, 5.68561274e-02],\n", " [5.58468871e-01, 1.34603783e-02, 1.16342779e-01],\n", " [7.57394581e-03, 1.26300555e+00, 1.48010090e+00],\n", " [2.95749391e-01, 1.53096057e-02, 4.05185952e-04]])" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data**2" ] }, { "cell_type": "markdown", "id": "8bb8388b", "metadata": {}, "source": [ "Comparison of two arrays:" ] }, { "cell_type": "code", "execution_count": 4, "id": "248171de", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[ True, False, False],\n", " [ True, True, False],\n", " [False, True, False],\n", " [ True, True, True],\n", " [False, False, True],\n", " [ True, False, True],\n", " [ True, True, False]])" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "data2 = np.random.randn(7, 3)\n", "data > data2" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3.13 Kernel", "language": "python", "name": "python313" }, "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.13.0" }, "widgets": { "application/vnd.jupyter.widget-state+json": { "state": {}, "version_major": 2, "version_minor": 0 } } }, "nbformat": 4, "nbformat_minor": 5 }