<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>「broadcast」タグの記事一覧Python Tech</title>
	<atom:link href="https://tech.nkhn37.net/tag/broadcast/feed/" rel="self" type="application/rss+xml" />
	<link>https://tech.nkhn37.net</link>
	<description>Python学習サイト</description>
	<lastBuildDate>Sun, 14 Dec 2025 04:52:58 +0000</lastBuildDate>
	<language>ja</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=7.0</generator>

<image>
	<url>https://tech.nkhn37.net/wp-content/uploads/2021/01/cropped-lion-normal-clear-1-32x32.png</url>
	<title>「broadcast」タグの記事一覧Python Tech</title>
	<link>https://tech.nkhn37.net</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>【NumPy】ブロードキャスト（broadcast）の基本</title>
		<link>https://tech.nkhn37.net/numpy-broadcasting-basic/</link>
					<comments>https://tech.nkhn37.net/numpy-broadcasting-basic/#respond</comments>
		
		<dc:creator><![CDATA[naoki-hn]]></dc:creator>
		<pubDate>Sun, 30 Jan 2022 00:00:00 +0000</pubDate>
				<category><![CDATA[NumPy]]></category>
		<category><![CDATA[broadcast]]></category>
		<category><![CDATA[ufunc]]></category>
		<category><![CDATA[ブロードキャスト]]></category>
		<category><![CDATA[ユニバーサル関数]]></category>
		<guid isPermaLink="false">https://tech.nkhn37.net/?p=2141</guid>

					<description><![CDATA[NumPy の特徴的な機能であるブロードキャスト（broadcast）の基本的な考え方を紹介します。 ブロードキャスト（broadcast）の基本 NumPy の特徴的な機能としてブロードキャスト（broadcast）と [&#8230;]]]></description>
										<content:encoded><![CDATA[
<p class="wp-block-paragraph">NumPy の特徴的な機能である<span class="jinr-d--text-color d--marker1 d--bold">ブロードキャスト（broadcast）</span>の基本的な考え方を紹介します。</p>



<h2 class="wp-block-heading jinr-heading d--bold" id="ブロードキャスト-broadcast-の基本">ブロードキャスト（broadcast）の基本</h2>



<p class="wp-block-paragraph">NumPy の特徴的な機能として<span class="jinr-d--text-color d--marker1 d--bold">ブロードキャスト（broadcast）</span>という機能があります。ブロードキャストは、NumPy が高速にベクトル演算するユニバーサル関数（<code>ufunc</code>）の土台となる機能です。</p>



<p class="wp-block-paragraph">ユニバーサル関数は、例えば、<code>np.add</code>　や　<code>np.multiply</code> というような関数のことです。NumPy 配列（<code>ndarray</code>）の <code>+</code> や <code>*</code> 演算子は、内部でそれらの <code>ufunc</code> を呼び出す糖衣構文となっています。</p>



<p class="wp-block-paragraph">この記事では、<span class="jinr-d--text-color d--marker1 d--bold">ブロードキャスト（broadcast）の基本</span>について説明します。</p>



<section class="wp-block-jinr-blocks-iconbox b--jinr-block b--jinr-iconbox"><div class="d--simple-iconbox6 ">
			<i class="jif jin-ifont-v2books" aria-hidden="true"></i>
			<div class="a--jinr-iconbox">
<p class="wp-block-paragraph">NumPy のユニバーサル関数（<code>ufunc</code>）については「<a href="https://tech.nkhn37.net/numpy-ufuncs-ndarray-calculation/" target="_blank" rel="noreferrer noopener">ユニバーサル関数（ufuncs）を用いた配列（ndarray）の計算</a>」を参考にしてください。</p>
</div>
		</div></section>



<h3 class="wp-block-heading jinr-heading d--bold" id="ブロードキャスト-broadcast-の基本的な考え方">ブロードキャスト（broadcast）の基本的な考え方</h3>



<p class="wp-block-paragraph">ブロードキャストの基本的な考え方について、簡単な例を使って見ていきましょう。</p>



<h4 class="wp-block-heading jinr-heading d--bold" id="ユニバーサル関数のベクトル計算">ユニバーサル関数のベクトル計算</h4>



<p class="wp-block-paragraph">ユニバーサル関数（<code>ufunc</code>）のベクトル計算のイメージを説明します。以下は同じサイズの NumPy の配列（<code>ndarray</code>）を足し算するプログラムです。</p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">import numpy as np

x1 = np.arange(1, 6)
print(f'x1 = {x1}')
x2 = np.arange(6, 11)
print(f'x2 = {x2}')

print(f'x1 + x2 = {x1 + x2}')</pre>



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">【実行結果】
x1 = [1 2 3 4 5]
x2 = [ 6  7  8  9 10]
x1 + x2 = [ 7  9 11 13 15]</pre>



<p class="wp-block-paragraph">ユニバーサル関数では、ベクトル演算をするため、各要素ごとに演算が行われます。イメージ図にしてみると以下のようになります。今回は足し算（<code>+</code>）の例ですが、他の演算でも同様です。</p>


<div class="wp-block-image">
<figure class="aligncenter size-medium is-resized"><img fetchpriority="high" decoding="async" width="300" height="200" src="https://tech.nkhn37.net/wp-content/uploads/2022/01/image-300x200.png" alt="ユニバーサル関数（ufunc） ベクトル計算" class="wp-image-2387" style="width:350px" srcset="https://tech.nkhn37.net/wp-content/uploads/2022/01/image-300x200.png 300w, https://tech.nkhn37.net/wp-content/uploads/2022/01/image.png 558w" sizes="(max-width: 300px) 100vw, 300px" /></figure>
</div>


<h4 class="wp-block-heading jinr-heading d--bold" id="ブロードキャスト例-1次元">ブロードキャスト例（1 次元）</h4>



<p class="wp-block-paragraph">上記で説明した例は、配列の形状が一致している場合でした。配列の形状（<code>shape</code>）が異なる場合について見てみましょう。以下は配列と数値の足しているプログラムです。このような場合に、ブロードキャストの機能が動作します。</p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">import numpy as np

x1 = np.arange(1, 6)
print(f'x1 = {x1}')

print(f'x1 + 5 = {x1 + 5}')</pre>



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">【実行結果】
x1 = [1 2 3 4 5]
x1 + 5 = [ 6  7  8  9 10]</pre>



<p class="wp-block-paragraph">結果を見ると、<code>x1</code> の各要素にそれぞれ 5 が足し算されていることが分かります。この時、NumPy は 5 という数字を拡張して、<code>x1</code> と同じ形状の配列にしてからベクトル計算をします。イメージにすると以下のようになります。</p>



<p class="wp-block-paragraph">このように NumPy が内部的に形状を拡張して対応することを「<span class="jinr-d--text-color d--marker1 d--bold">ブロードキャスト</span>」と言います。</p>


<div class="wp-block-image">
<figure class="aligncenter size-medium"><img decoding="async" width="300" height="203" src="https://tech.nkhn37.net/wp-content/uploads/2022/01/image-3-300x203.png" alt="ブロードキャスト（broadcast）1次元" class="wp-image-2392" srcset="https://tech.nkhn37.net/wp-content/uploads/2022/01/image-3-300x203.png 300w, https://tech.nkhn37.net/wp-content/uploads/2022/01/image-3.png 558w" sizes="(max-width: 300px) 100vw, 300px" /></figure>
</div>


<h4 class="wp-block-heading jinr-heading d--bold" id="ブロードキャスト例-2次元-行方向">ブロードキャスト例（2 次元：行方向）</h4>



<p class="wp-block-paragraph">2 次元配列の場合も見てみましょう。以下の例は、<code>np.eye</code> で 5 × 5 の単位行列を作成しています。足し算するのは、1 次元の 1 ~ 5 までの数字が入った配列です。</p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">import numpy as np

x1 = np.eye(5)
print(f'x1 = \n{x1}')
x2 = np.arange(1, 6)
print(f'x2 = {x2}')

print(f'x1 + x2 = \n{x1 + x2}')</pre>



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">【実行結果】
x1 = 
[[1. 0. 0. 0. 0.]
 [0. 1. 0. 0. 0.]
 [0. 0. 1. 0. 0.]
 [0. 0. 0. 1. 0.]
 [0. 0. 0. 0. 1.]]
x2 = [1 2 3 4 5]
x1 + x2 = 
[[2. 2. 3. 4. 5.]
 [1. 3. 3. 4. 5.]
 [1. 2. 4. 4. 5.]
 [1. 2. 3. 5. 5.]
 [1. 2. 3. 4. 6.]]</pre>



<p class="wp-block-paragraph">例では、行方向のサイズが異なっているため、<code>x2</code> の配列を行方向に拡張し、<code>x1</code> と同じ形状にして計算がされます。イメージは、以下のようになります。</p>


<div class="wp-block-image">
<figure class="aligncenter size-large"><img decoding="async" width="1024" height="328" src="https://tech.nkhn37.net/wp-content/uploads/2022/01/image-2-1024x328.png" alt="ブロードキャスト（broadcast）2次元 行方向" class="wp-image-2390" srcset="https://tech.nkhn37.net/wp-content/uploads/2022/01/image-2-1024x328.png 1024w, https://tech.nkhn37.net/wp-content/uploads/2022/01/image-2-300x96.png 300w, https://tech.nkhn37.net/wp-content/uploads/2022/01/image-2-768x246.png 768w, https://tech.nkhn37.net/wp-content/uploads/2022/01/image-2.png 1344w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>
</div>


<h4 class="wp-block-heading jinr-heading d--bold" id="ブロードキャスト例-2次元-列方向">ブロードキャスト例（2 次元：列方向）</h4>



<p class="wp-block-paragraph">列方向に拡張される例も見てみましょう。</p>



<pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">import numpy as np

x1 = np.eye(5)
print(f'x1 = \n{x1}')
x2 = np.arange(1, 6)[:, np.newaxis]
print(f'x2 = {x2}')

print(f'x1 + x2 = \n{x1 + x2}')</pre>



<pre class="EnlighterJSRAW" data-enlighter-language="raw" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="false" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">【実行結果】
x1 = 
[[1. 0. 0. 0. 0.]
 [0. 1. 0. 0. 0.]
 [0. 0. 1. 0. 0.]
 [0. 0. 0. 1. 0.]
 [0. 0. 0. 0. 1.]]
x2 = [[1]
 [2]
 [3]
 [4]
 [5]]
x1 + x2 = 
[[2. 1. 1. 1. 1.]
 [2. 3. 2. 2. 2.]
 [3. 3. 4. 3. 3.]
 [4. 4. 4. 5. 4.]
 [5. 5. 5. 5. 6.]]</pre>



<p class="wp-block-paragraph">例では、列方向のサイズが異なっているため、<code>x2</code> の配列を列方向に拡張して、<code>x1</code> と同じ形状にして計算がされます。イメージは、以下のようになります。</p>


<div class="wp-block-image">
<figure class="aligncenter size-large"><img decoding="async" width="1024" height="351" src="https://tech.nkhn37.net/wp-content/uploads/2022/01/image-4-1024x351.png" alt="ブロードキャスト（broadcast）2次元 列方向" class="wp-image-2393" srcset="https://tech.nkhn37.net/wp-content/uploads/2022/01/image-4-1024x351.png 1024w, https://tech.nkhn37.net/wp-content/uploads/2022/01/image-4-300x103.png 300w, https://tech.nkhn37.net/wp-content/uploads/2022/01/image-4-768x263.png 768w, https://tech.nkhn37.net/wp-content/uploads/2022/01/image-4.png 1344w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>
</div>


<h2 class="wp-block-heading jinr-heading d--bold" id="ブロードキャスト-broadcast-のルール">ブロードキャスト（broadcast）のルール</h2>



<p class="wp-block-paragraph">これまで見てきた例は非常にシンプルな例となっていますが、NumPy の高速な演算を支えるブロードキャストという仕組みの概要です。より詳細にはブロードキャストの具体的なルールがありますが、この記事はブロードキャストのイメージを持ってもらうことに留めようかと思います。</p>



<p class="wp-block-paragraph">より詳細なブロードキャストのルールに興味がある方は、公式ドキュメントの<a href="https://numpy.org/doc/stable/user/basics.broadcasting.html" target="_blank" rel="noreferrer noopener">こちら</a>を参考にしてください。</p>



<h2 class="wp-block-heading jinr-heading d--bold">まとめ</h2>



<p class="wp-block-paragraph">NumPy の特徴的な機能である<span class="jinr-d--text-color d--marker1 d--bold">ブロードキャスト（broadcast）</span>の基本的な考え方を紹介しました。</p>



<p class="wp-block-paragraph">ブロードキャストは、NumPy の <code>np.add</code> や <code>np.multiply</code> といったユニバーサル関数（<code>ufunc</code>）が、内部的に形状を拡張して演算に対応することを言います。</p>



<p class="wp-block-paragraph">ブロードキャストはNumPyの高速演算を支える重要な考え方の 1 つです。NumPy のユニバーサル関数（<code>ufunc</code>）ではブロードキャストという機能で内部的に形状を拡張して対応していることを意識してみてください。</p>


<section class="b--jinr-block b--jinr-blogcard d--blogcard-hover-up d--blogcard-style1 d--blogcard-mysite t--round "><div class="a--blogcard-label ef">あわせて読みたい</div><a class="o--blogcard-link t--round" href="https://tech.nkhn37.net/python-tech-summary-page/"><div class="c--blogcard-image"><img decoding="async" class="a--blogcard-img-src" width="128" height="72" src="https://tech.nkhn37.net/wp-content/uploads/2024/08/Python-Tech-Pythonプログラミングガイド_new1-640x360.jpg" alt="【Python Tech】プログラミングガイド" /></div><div class="a--blogcard-title d--bold">【Python Tech】プログラミングガイド</div></a></section>]]></content:encoded>
					
					<wfw:commentRss>https://tech.nkhn37.net/numpy-broadcasting-basic/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>

<!--
Performance optimized by W3 Total Cache. Learn more: https://www.boldgrid.com/w3-total-cache/?utm_source=w3tc&utm_medium=footer_comment&utm_campaign=free_plugin

Disk: Enhanced  を使用したページ キャッシュ

Served from: tech.nkhn37.net @ 2026-06-21 22:58:22 by W3 Total Cache
-->