{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "## numpy broadcasting is like \"sharing\" a smaller array for the bigger array\n", "- An array sharing a number\n", "- Regression paramters/weights sharing 1 intercept\n", "- Image data classification 2-D array of weights sharing 1-D array of intercept weights\n", "\n", "\n", "### Remember: \n", "\n", "\n", "1. shape (3,) is treated as (1,3)\n", "\n", "2. shape column sizes must match in order to broadcast\n", "\n", "3. any ufunc can be broadcasted, not just +-" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import numpy as np" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "## [Broadcasting 1 number](#Broadcasting1Number)\n", "\n", "## [Broadcasting 1D array](#Broadcasting1D)\n", "\n", "## [Broadcast 2 ways](#two-way)\n", "\n", "## [Examples](#examples)\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "*** \n", "\n", "## Broadcasting 1 number" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = np.arange(10)\n", "a" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([10, 11, 12, 13, 14, 15, 16, 17, 18, 19])" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a +10" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "a = np.arange(15).reshape(3,5)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[1000, 1001, 1002, 1003, 1004],\n", " [1005, 1006, 1007, 1008, 1009],\n", " [1010, 1011, 1012, 1013, 1014]])" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a + 1000" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "## Broadcasting 1D array" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([0, 1, 2])" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a = np.arange(3)\n", "a" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[1., 0., 0.],\n", " [0., 1., 0.],\n", " [0., 0., 1.]])" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b = np.eye(3)\n", "b" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[1., 1., 2.],\n", " [0., 2., 2.],\n", " [0., 1., 3.]])" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a + b" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [], "source": [ "b = np.ones((2,3), int)" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[1, 2, 3],\n", " [1, 2, 3]])" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a + b" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "