<?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>「newaxis」タグの記事一覧Python Tech</title>
	<atom:link href="https://tech.nkhn37.net/tag/newaxis/feed/" rel="self" type="application/rss+xml" />
	<link>https://tech.nkhn37.net</link>
	<description>Python学習サイト</description>
	<lastBuildDate>Sat, 13 Dec 2025 03:33:38 +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>「newaxis」タグの記事一覧Python Tech</title>
	<link>https://tech.nkhn37.net</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>【NumPy】配列（ndarray）の形状を変更する方法 reshape</title>
		<link>https://tech.nkhn37.net/numpy-ndarray-reshape-newaxis/</link>
					<comments>https://tech.nkhn37.net/numpy-ndarray-reshape-newaxis/#respond</comments>
		
		<dc:creator><![CDATA[naoki-hn]]></dc:creator>
		<pubDate>Tue, 16 Nov 2021 00:00:00 +0000</pubDate>
				<category><![CDATA[NumPy]]></category>
		<category><![CDATA[ndarray]]></category>
		<category><![CDATA[newaxis]]></category>
		<category><![CDATA[reshape]]></category>
		<guid isPermaLink="false">https://tech.nkhn37.net/?p=2065</guid>

					<description><![CDATA[NumPy の配列（ndarray）の形状を変更する方法を解説します。 配列（ndarray）の形状を変更する方法 reshape NumPy の配列（ndarray）の形状を変更するには、reshape メソッドを使用 [&#8230;]]]></description>
										<content:encoded><![CDATA[
<p class="wp-block-paragraph"><span class="jinr-d--text-color d--marker1 d--bold">NumPy の配列（<code>ndarray</code>）の形状を変更する方法</span>を解説します。</p>



<h2 class="wp-block-heading jinr-heading d--bold" id="配列-ndarray-の形状を変更する方法-reshape">配列（<code>ndarray</code>）の形状を変更する方法 <code>reshape</code></h2>



<p class="wp-block-paragraph">NumPy の配列（<code>ndarray</code>）の形状を変更するには、<span class="jinr-d--text-color d--marker1 d--bold"><code>reshape</code></span> メソッドを使用します。</p>



<p class="wp-block-paragraph">この記事では、NumPy の配列（<code>ndarray</code>）の形状を変更する方法を紹介します。</p>



<h3 class="wp-block-heading jinr-heading d--bold" id="1次元配列から多次元配列を作成する">1 次元配列から多次元配列を作成する</h3>



<p class="wp-block-paragraph"><span class="jinr-d--text-color d--marker1 d--bold"><code>reshape</code></span> による形状の変更でよくある例は、1 次元配列を作成し、その後に 2 次元配列といった多次元配列に変換する場合です。</p>



<p class="wp-block-paragraph">1 次元の配列から 2 次元配列を <code>reshape</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

x = np.arange(1, 10).reshape((3, 3))
print(x)
print(f'size: {x.size}')
print(f'shape: {x.shape}')</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="">【実行結果】
[[1 2 3]
 [4 5 6]
 [7 8 9]]
size: 9
shape: (3, 3)</pre>



<p class="wp-block-paragraph">例では、<code>arange</code> により 9 個の要素を持つ 1 次元配列を作成し、<code>reshape</code> で形状を変更しています。<code>reshape</code> の引数には形状をタプルで指定します。例では <code>(3, 3)</code> といった形で指定することで 3 x 3 の配列に変更をしています。</p>



<p class="wp-block-paragraph">3 次元配列を作成する場合も同様です。</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

x = np.arange(1, 28).reshape((3, 3, 3))
print(x)
print(f'size: {x.size}')
print(f'shape: {x.shape}')</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="">【実行結果】
[[[ 1  2  3]
  [ 4  5  6]
  [ 7  8  9]]

 [[10 11 12]
  [13 14 15]
  [16 17 18]]

 [[19 20 21]
  [22 23 24]
  [25 26 27]]]
size: 27
shape: (3, 3, 3)</pre>



<p class="wp-block-paragraph">例では、3 x 3 x 3 の 3 次元配列になります。n 次元になっても同様ですが表示しても分かりにくいので 3 次元までの紹介にしておきます。</p>



<p class="wp-block-paragraph"><code>reshape</code> では、<span class="jinr-d--text-color d--marker1 d--bold">変換元の配列サイズと変換後の配列のサイズは一致している</span>必要があるので注意してください。必要があれば <code>ndarray</code> の属性である <code>size</code> を使って確認してください。</p>



<h3 class="wp-block-heading jinr-heading d--bold" id="行ベクトルを作成する">行ベクトルを作成する</h3>



<p class="wp-block-paragraph">配列の形状を変更したいもう 1 つのケースとして、行ベクトルを作成する場合があります。行ベクトルを作成する方法としては、<code>reshape</code> を使用する方法と、<span class="jinr-d--text-color d--marker1 d--bold"><code>np.newaxis</code></span> を使用する方法をそれぞれ紹介します。</p>



<h4 class="wp-block-heading jinr-heading d--bold" id="reshapeを使用する方法"><code>reshape</code> を使用する方法</h4>



<p class="wp-block-paragraph"><code>reshape</code> を用いて行ベクトルを作成するには、引数に <code>(1, size)</code> のタプルを指定します。ここでの <code>size</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

x = np.array([1, 2, 3, 4, 5])
print(f'x = {x}')
# 行ベクトルを作成する
x_row = x.reshape((1, x.size))
print(f'x_row = {x_row}')</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="">【実行結果】
x = [1 2 3 4 5]
x_row = [[1 2 3 4 5]]</pre>



<h4 class="wp-block-heading jinr-heading d--bold" id="np-newaxisを使用する方法"><code>np.newaxis</code> を使用する方法</h4>



<p class="wp-block-paragraph"><code>reshape</code> の他にも、<span class="jinr-d--text-color d--marker1 d--bold"><code>np.newaxis</code></span> を使用する方法があります。この場合は、<code>x[np.newaxis, :]</code> のように指定します。ここで <code>x</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

x = np.array([1, 2, 3, 4, 5])
print(f'x = {x}')
# 行ベクトルを作成する
x_row = x[np.newaxis, :]
print(f'x_row = {x_row}')</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="">【実行結果】
x = [1 2 3 4 5]
x_row = [[1 2 3 4 5]]</pre>



<h3 class="wp-block-heading jinr-heading d--bold" id="列ベクトルを作成する">列ベクトルを作成する</h3>



<p class="wp-block-paragraph">列ベクトルも同様に、<code>rehape</code> または <code>np.newaxis</code> を使用して作成できます。</p>



<h4 class="wp-block-heading jinr-heading d--bold" id="reshapeを使用する方法"><code>reshape</code> を使用する方法</h4>



<p class="wp-block-paragraph"><code>reshape</code> を用いて列ベクトルを作成するには、引数に <code>(size, 1)</code> を指定します。ここでの <code>size</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

x = np.array([1, 2, 3, 4, 5])
print(f'x = {x}')
# 列ベクトルを作成する
x_col = x.reshape((x.size, 1))
print(f'x_col = \n{x_col}')</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="">【実行結果】
x = [1 2 3 4 5]
x_col = 
[[1]
 [2]
 [3]
 [4]
 [5]]</pre>



<h4 class="wp-block-heading jinr-heading d--bold" id="np-newaxisを使用する方法"><code>np.newaxis</code> を使用する方法</h4>



<p class="wp-block-paragraph"><code>np.newaxis</code> を使用する場合には、<code>x[:, np.newaxis]</code> のように指定します。ここで <code>x</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

x = np.array([1, 2, 3, 4, 5])
print(f'x = {x}')
# 列ベクトルを作成する
x_col = x[:, np.newaxis]
print(f'x_col = \n{x_col}')</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="">【実行結果】
x = [1 2 3 4 5]
x_col = 
[[1]
 [2]
 [3]
 [4]
 [5]]</pre>



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



<p class="wp-block-paragraph"><span class="jinr-d--text-color d--marker1 d--bold">NumPy の配列（<code>ndarray</code>）の形状を変更する方法</span>を解説しました。</p>



<p class="wp-block-paragraph"><code>reshape</code> を使うことで配列（<code>ndarray</code>）の形状を簡単に変更することができます。この記事では、1 次元配列から 多次元配列に形状を変える方法や行ベクトルや列ベクトルを作成する方法を紹介しました。行ベクトル、列ベクトルの例では、<code>np.newaxis</code> での方法も紹介しています。</p>



<p class="wp-block-paragraph">配列の形状変更は、機械学習等の前の事前処理としてよく実行しますので、うまく使いこなせるようになってもらえたらと思います。</p>



<section class="wp-block-jinr-blocks-simplebox b--jinr-block-container"><div class="b--jinr-block b--jinr-box d--heading-box8  "><div class="a--simple-box-title d--bold">ソースコード</div><div class="c--simple-box-inner">
<p class="wp-block-paragraph">上記で紹介しているソースコードについては <a href="https://github.com/nkhn37/python-tech-sample-source/tree/main/python-data-analysis/numpy/ndarray-reshape" target="_blank" rel="noreferrer noopener">GitHub</a> にて公開しています。参考にしていただければと思います。</p>
</div></div></section>


<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-ndarray-reshape-newaxis/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 23:41:04 by W3 Total Cache
-->