{"id":132,"date":"2026-03-02T14:42:01","date_gmt":"2026-03-02T09:42:01","guid":{"rendered":"https:\/\/gigz.pk\/python\/?post_type=lesson&#038;p=132"},"modified":"2026-03-17T07:49:23","modified_gmt":"2026-03-17T02:49:23","slug":"broadcasting","status":"publish","type":"lesson","link":"https:\/\/gigz.pk\/python\/lesson\/broadcasting\/","title":{"rendered":"Broadcasting"},"content":{"rendered":"\n<p>Broadcasting is a powerful feature in NumPy that allows arrays of different shapes to perform mathematical operations together.<\/p>\n\n\n\n<p>It automatically expands the smaller array to match the shape of the larger array without copying data. This makes calculations faster and memory-efficient.<\/p>\n\n\n\n<p>First, import NumPy:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">import numpy as np<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Simple Example of Broadcasting<\/h2>\n\n\n\n<p>Create an array:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">arr = np.array([1, 2, 3, 4])<\/pre>\n\n\n\n<p>Add a scalar value:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">arr + 5<\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">[6 7 8 9]<\/pre>\n\n\n\n<p>Here, the number 5 is automatically broadcasted to all elements.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Broadcasting with Two Arrays<\/h2>\n\n\n\n<p>Example:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">a = np.array([1, 2, 3])<br>b = np.array([10, 20, 30])a + b<\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">[11 22 33]<\/pre>\n\n\n\n<p>Element-wise addition happens because both arrays have the same shape.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Broadcasting with Different Shapes<\/h2>\n\n\n\n<p>Example:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">a = np.array([[1, 2, 3],<br>              [4, 5, 6]])b = np.array([10, 20, 30])a + b<\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">[[11 22 33]<br> [14 25 36]]<\/pre>\n\n\n\n<p>Here, <code>b<\/code> is broadcasted across each row of <code>a<\/code>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Broadcasting Rules<\/h2>\n\n\n\n<p>NumPy follows these rules:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>If arrays have different dimensions, the smaller one is expanded.<\/li>\n\n\n\n<li>Dimensions are compatible if:\n<ul class=\"wp-block-list\">\n<li>They are equal, or<\/li>\n\n\n\n<li>One of them is 1.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>If dimensions are not compatible, an error occurs.<\/li>\n<\/ol>\n\n\n\n<p>Example of incompatible shapes:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">a = np.array([[1, 2, 3],<br>              [4, 5, 6]])b = np.array([10, 20])a + b<\/pre>\n\n\n\n<p>This will produce a shape mismatch error.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Broadcasting with Column Vector<\/h2>\n\n\n\n<p>Example:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">a = np.array([[1, 2, 3],<br>              [4, 5, 6]])b = np.array([[10],<br>              [20]])a + b<\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">[[11 12 13]<br> [24 25 26]]<\/pre>\n\n\n\n<p>Here, <code>b<\/code> is broadcasted across columns.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Why Broadcasting is Important in Data Analytics<\/h2>\n\n\n\n<p>Broadcasting allows you to:<\/p>\n\n\n\n<p>Normalize data<br>Scale features<br>Perform fast matrix operations<br>Apply transformations to entire datasets<br>Avoid writing loops<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Key Takeaway<\/h2>\n\n\n\n<p>Broadcasting automatically expands smaller arrays to match larger ones during operations. It makes NumPy powerful, efficient, and ideal for large-scale data analysis.<\/p>\n\n\n<div class=\"yoast-breadcrumbs\"><span><span><a href=\"https:\/\/gigz.pk\/python\/\">Home<\/a><\/span> \u00bb <span class=\"breadcrumb_last\" aria-current=\"page\">PYTHON FOR DATA ANALYTICS (PYDA) > NumPy > Broadcasting<\/span><\/span><\/div>\n\n\n<div class=\"schema-faq wp-block-yoast-faq-block\"><div class=\"schema-faq-section\" id=\"faq-question-1773715948237\"><strong class=\"schema-faq-question\"><\/strong> <p class=\"schema-faq-answer\"><\/p> <\/div> <\/div>\n","protected":false},"menu_order":69,"template":"","class_list":["post-132","lesson","type-lesson","status-publish","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.5 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Broadcasting - One Language. Endless Possibilities<\/title>\n<meta name=\"description\" content=\"Learn NumPy broadcasting to perform fast array operations, handle different shapes, and scale data efficiently in Python.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/gigz.pk\/python\/lesson\/broadcasting\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Broadcasting - One Language. Endless Possibilities\" \/>\n<meta property=\"og:description\" content=\"Learn NumPy broadcasting to perform fast array operations, handle different shapes, and scale data efficiently in Python.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/gigz.pk\/python\/lesson\/broadcasting\/\" \/>\n<meta property=\"og:site_name\" content=\"One Language. Endless Possibilities\" \/>\n<meta property=\"article:modified_time\" content=\"2026-03-17T02:49:23+00:00\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":[\"WebPage\",\"FAQPage\"],\"@id\":\"https:\\\/\\\/gigz.pk\\\/python\\\/lesson\\\/broadcasting\\\/\",\"url\":\"https:\\\/\\\/gigz.pk\\\/python\\\/lesson\\\/broadcasting\\\/\",\"name\":\"Broadcasting - One Language. Endless Possibilities\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/gigz.pk\\\/python\\\/#website\"},\"datePublished\":\"2026-03-02T09:42:01+00:00\",\"dateModified\":\"2026-03-17T02:49:23+00:00\",\"description\":\"Learn NumPy broadcasting to perform fast array operations, handle different shapes, and scale data efficiently in Python.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/gigz.pk\\\/python\\\/lesson\\\/broadcasting\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/gigz.pk\\\/python\\\/lesson\\\/broadcasting\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/gigz.pk\\\/python\\\/lesson\\\/broadcasting\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/gigz.pk\\\/python\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"PYTHON FOR DATA ANALYTICS (PYDA) > NumPy > Broadcasting\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/gigz.pk\\\/python\\\/#website\",\"url\":\"https:\\\/\\\/gigz.pk\\\/python\\\/\",\"name\":\"One Language. Endless Possibilities\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/gigz.pk\\\/python\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Broadcasting - One Language. Endless Possibilities","description":"Learn NumPy broadcasting to perform fast array operations, handle different shapes, and scale data efficiently in Python.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/gigz.pk\/python\/lesson\/broadcasting\/","og_locale":"en_US","og_type":"article","og_title":"Broadcasting - One Language. Endless Possibilities","og_description":"Learn NumPy broadcasting to perform fast array operations, handle different shapes, and scale data efficiently in Python.","og_url":"https:\/\/gigz.pk\/python\/lesson\/broadcasting\/","og_site_name":"One Language. Endless Possibilities","article_modified_time":"2026-03-17T02:49:23+00:00","twitter_card":"summary_large_image","twitter_misc":{"Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":["WebPage","FAQPage"],"@id":"https:\/\/gigz.pk\/python\/lesson\/broadcasting\/","url":"https:\/\/gigz.pk\/python\/lesson\/broadcasting\/","name":"Broadcasting - One Language. Endless Possibilities","isPartOf":{"@id":"https:\/\/gigz.pk\/python\/#website"},"datePublished":"2026-03-02T09:42:01+00:00","dateModified":"2026-03-17T02:49:23+00:00","description":"Learn NumPy broadcasting to perform fast array operations, handle different shapes, and scale data efficiently in Python.","breadcrumb":{"@id":"https:\/\/gigz.pk\/python\/lesson\/broadcasting\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/gigz.pk\/python\/lesson\/broadcasting\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/gigz.pk\/python\/lesson\/broadcasting\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/gigz.pk\/python\/"},{"@type":"ListItem","position":2,"name":"PYTHON FOR DATA ANALYTICS (PYDA) > NumPy > Broadcasting"}]},{"@type":"WebSite","@id":"https:\/\/gigz.pk\/python\/#website","url":"https:\/\/gigz.pk\/python\/","name":"One Language. Endless Possibilities","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/gigz.pk\/python\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"}]}},"_links":{"self":[{"href":"https:\/\/gigz.pk\/python\/wp-json\/wp\/v2\/lesson\/132","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gigz.pk\/python\/wp-json\/wp\/v2\/lesson"}],"about":[{"href":"https:\/\/gigz.pk\/python\/wp-json\/wp\/v2\/types\/lesson"}],"wp:attachment":[{"href":"https:\/\/gigz.pk\/python\/wp-json\/wp\/v2\/media?parent=132"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}