<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://mscneuro.neuro.uni-bremen.de/index.php?action=history&amp;feed=atom&amp;title=Dimensions_and_shape</id>
	<title>Dimensions and shape - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://mscneuro.neuro.uni-bremen.de/index.php?action=history&amp;feed=atom&amp;title=Dimensions_and_shape"/>
	<link rel="alternate" type="text/html" href="https://mscneuro.neuro.uni-bremen.de/index.php?title=Dimensions_and_shape&amp;action=history"/>
	<updated>2026-04-20T02:53:16Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.43.5</generator>
	<entry>
		<id>https://mscneuro.neuro.uni-bremen.de/index.php?title=Dimensions_and_shape&amp;diff=347&amp;oldid=prev</id>
		<title>Davrot at 16:33, 17 October 2025</title>
		<link rel="alternate" type="text/html" href="https://mscneuro.neuro.uni-bremen.de/index.php?title=Dimensions_and_shape&amp;diff=347&amp;oldid=prev"/>
		<updated>2025-10-17T16:33:24Z</updated>

		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;table style=&quot;background-color: #fff; color: #202122;&quot; data-mw=&quot;interface&quot;&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;tr class=&quot;diff-title&quot; lang=&quot;en&quot;&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;← Older revision&lt;/td&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;Revision as of 16:33, 17 October 2025&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot; id=&quot;mw-diff-left-l1&quot;&gt;Line 1:&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 1:&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;−&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;del style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;== The goal ==&lt;/del&gt;&lt;/div&gt;&lt;/td&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-side-added&quot;&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;Matrices have dimensions. But how to add and remove extra dimensions (i.e. dimensions with length 1)?&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;Matrices have dimensions. But how to add and remove extra dimensions (i.e. dimensions with length 1)?&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br&gt;&lt;/td&gt;&lt;/tr&gt;

&lt;!-- diff cache key wiki:diff:1.41:old-173:rev-347:php=table --&gt;
&lt;/table&gt;</summary>
		<author><name>Davrot</name></author>
	</entry>
	<entry>
		<id>https://mscneuro.neuro.uni-bremen.de/index.php?title=Dimensions_and_shape&amp;diff=173&amp;oldid=prev</id>
		<title>Davrot: Created page with &quot;== The goal == Matrices have dimensions. But how to add and remove extra dimensions (i.e. dimensions with length 1)?  Questions to [mailto:davrot@uni-bremen.de David Rotermund]  == [https://numpy.org/doc/stable/reference/generated/numpy.ndarray.shape.html numpy.ndarray.shape] == &lt;syntaxhighlight lang=&quot;python&quot;&gt;ndarray.shape&lt;/syntaxhighlight&gt;&lt;blockquote&gt;Tuple of array dimensions.  The shape property is usually used to get the current shape of an array, but may also be used...&quot;</title>
		<link rel="alternate" type="text/html" href="https://mscneuro.neuro.uni-bremen.de/index.php?title=Dimensions_and_shape&amp;diff=173&amp;oldid=prev"/>
		<updated>2025-10-16T16:06:14Z</updated>

		<summary type="html">&lt;p&gt;Created page with &amp;quot;== The goal == Matrices have dimensions. But how to add and remove extra dimensions (i.e. dimensions with length 1)?  Questions to [mailto:davrot@uni-bremen.de David Rotermund]  == [https://numpy.org/doc/stable/reference/generated/numpy.ndarray.shape.html numpy.ndarray.shape] == &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;ndarray.shape&amp;lt;/syntaxhighlight&amp;gt;&amp;lt;blockquote&amp;gt;Tuple of array dimensions.  The shape property is usually used to get the current shape of an array, but may also be used...&amp;quot;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;== The goal ==&lt;br /&gt;
Matrices have dimensions. But how to add and remove extra dimensions (i.e. dimensions with length 1)?&lt;br /&gt;
&lt;br /&gt;
Questions to [mailto:davrot@uni-bremen.de David Rotermund]&lt;br /&gt;
&lt;br /&gt;
== [https://numpy.org/doc/stable/reference/generated/numpy.ndarray.shape.html numpy.ndarray.shape] ==&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;ndarray.shape&amp;lt;/syntaxhighlight&amp;gt;&amp;lt;blockquote&amp;gt;Tuple of array dimensions.&lt;br /&gt;
&lt;br /&gt;
The shape property is usually used to get the current shape of an array, but may also be used to reshape the array in-place by assigning a tuple of array dimensions to it. As with numpy.reshape, one of the new shape dimensions can be -1, in which case its value is inferred from the size of the array and the remaining dimensions. Reshaping an array in-place will fail if a copy is required.&amp;lt;/blockquote&amp;gt;&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;import numpy as np&lt;br /&gt;
&lt;br /&gt;
data = np.zeros((2, 4, 2, 7, 2))&lt;br /&gt;
print(data.shape)  # -&amp;gt; (2, 4, 2, 7, 2)&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Vanishing dimensions ==&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;import numpy as np&lt;br /&gt;
&lt;br /&gt;
data = np.zeros((5, 3, 2))&lt;br /&gt;
&lt;br /&gt;
# All the same dimensionwise&lt;br /&gt;
print(data.shape)  # -&amp;gt; (5, 3, 2)&lt;br /&gt;
print(data[:].shape)  # -&amp;gt;  (5, 3, 2)&lt;br /&gt;
print(data[:, :, :].shape)  # -&amp;gt;  (5, 3, 2)&lt;br /&gt;
print(data[...].shape)  # -&amp;gt;  (5, 3, 2)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
print(data[0, :, :].shape)  # -&amp;gt;  (3, 2)&lt;br /&gt;
print(data[:, 0, :].shape)  # -&amp;gt;  (5, 2)&lt;br /&gt;
print(data[:, :, 0].shape)  # -&amp;gt;  (5, 3)&lt;br /&gt;
&lt;br /&gt;
print(data[:, 0, 0].shape)  # -&amp;gt;  (5,)&lt;br /&gt;
print(data[0, :, 0].shape)  # -&amp;gt;  (3,)&lt;br /&gt;
print(data[0, 0, :].shape)  # -&amp;gt;  (2,)&lt;br /&gt;
&lt;br /&gt;
print(data[0, 0, 0].shape)  # -&amp;gt;  ()&lt;br /&gt;
print(type(data[0, 0, 0]))  # -&amp;gt;  &amp;lt;class &amp;#039;numpy.float64&amp;#039;&amp;gt;&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== keepdims ===&lt;br /&gt;
There are functions like&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;ndarray.sum(axis=None, dtype=None, out=None, keepdims=False, initial=0, where=True)&lt;br /&gt;
ndarray.mean(axis=None, dtype=None, out=None, keepdims=False, *, where=True)&lt;br /&gt;
ndarray.prod(axis=None, dtype=None, out=None, keepdims=False, initial=1, where=True)&lt;br /&gt;
ndarray.max(axis=None, out=None, keepdims=False, initial=&amp;lt;no value&amp;gt;, where=True)&lt;br /&gt;
ndarray.min(axis=None, out=None, keepdims=False, initial=&amp;lt;no value&amp;gt;, where=True)&lt;br /&gt;
ndarray.argmax(axis=None, out=None, *, keepdims=False)&lt;br /&gt;
ndarray.argmin(axis=None, out=None, *, keepdims=False)&amp;lt;/syntaxhighlight&amp;gt;that normally make one dimension vanish. However, often this type of functions have an argument &amp;#039;&amp;#039;&amp;#039;keepdims&amp;#039;&amp;#039;&amp;#039; that keeps this dimension alive.&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;import numpy as np&lt;br /&gt;
&lt;br /&gt;
data = np.zeros((5, 3, 2))&lt;br /&gt;
&lt;br /&gt;
# All the same dimensionwise&lt;br /&gt;
print(data.shape)  # -&amp;gt; (5, 3, 2)&lt;br /&gt;
print(data.sum().shape)  # -&amp;gt; ()&lt;br /&gt;
print(data.sum(axis=0).shape)  # -&amp;gt; (3, 2)&lt;br /&gt;
print(data.sum(axis=1).shape)  # -&amp;gt; (5, 2)&lt;br /&gt;
print(data.sum(axis=2).shape)  # -&amp;gt; (5, 3)&lt;br /&gt;
&lt;br /&gt;
# You can use keepdims:&lt;br /&gt;
&lt;br /&gt;
print(data.sum(axis=0, keepdims=True).shape)  # -&amp;gt; (1, 3, 2)&lt;br /&gt;
print(data.sum(axis=1, keepdims=True).shape)  # -&amp;gt; (5, 1, 2)&lt;br /&gt;
print(data.sum(axis=2, keepdims=True).shape)  # -&amp;gt; (5, 3, 1)&amp;lt;/syntaxhighlight&amp;gt;As as reminder, shape is only availabe for np.ndarray and torch.Tensor matrices:&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;z = int(7)&lt;br /&gt;
print(np.array(z).shape)  # -&amp;gt; ()&lt;br /&gt;
print(type(np.array(z)))  # -&amp;gt; &amp;lt;class &amp;#039;numpy.ndarray&amp;#039;&amp;gt;&lt;br /&gt;
print(type(z))  # -&amp;gt; &amp;lt;class &amp;#039;int&amp;#039;&amp;gt;&lt;br /&gt;
print(z.shape)  # -&amp;gt; AttributeError: &amp;#039;int&amp;#039; object has no attribute &amp;#039;shape&amp;#039;&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Stop vanishing dimensions ==&lt;br /&gt;
One way to do stop vanishing dimensions is to use slices of thickness 1. If you want the nth element, then use &amp;#039;&amp;#039;&amp;#039;n:n+1&amp;#039;&amp;#039;&amp;#039;:&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;import numpy as np&lt;br /&gt;
&lt;br /&gt;
data = np.zeros((5, 3, 2))&lt;br /&gt;
&lt;br /&gt;
# All the same dimensionwise&lt;br /&gt;
print(data.shape)  # -&amp;gt; (5, 3, 2)&lt;br /&gt;
&lt;br /&gt;
print(data[0:1, :, :].shape)  # -&amp;gt;  (1, 3, 2)&lt;br /&gt;
print(data[:, 0:1, :].shape)  # -&amp;gt;  (5, 1, 2)&lt;br /&gt;
print(data[:, :, 0:1].shape)  # -&amp;gt;  (5, 3, 1)&lt;br /&gt;
&lt;br /&gt;
print(data[:, 0:1, 0:1].shape)  # -&amp;gt;  (5, 1, 1)&lt;br /&gt;
print(data[0:1, :, 0:1].shape)  # -&amp;gt;  (1, 3, 1)&lt;br /&gt;
print(data[0:1, 0:1, :].shape)  # -&amp;gt;  (1, 1, 2)&lt;br /&gt;
&lt;br /&gt;
print(data[0:1, 0:1, 0:1].shape)  # -&amp;gt;  (1, 1, 1)&lt;br /&gt;
print(type(data[0:1, 0:1, 0:1]))  # -&amp;gt;  &amp;lt;class &amp;#039;numpy.ndarray&amp;#039;&amp;gt;&amp;lt;/syntaxhighlight&amp;gt;&amp;#039;&amp;#039;&amp;#039;Please understand this creates a view which is connected to original data.&amp;#039;&amp;#039;&amp;#039; If necessary make a &amp;#039;&amp;#039;&amp;#039;copy()&amp;#039;&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
== Adding dimensions with [https://numpy.org/doc/stable/reference/constants.html#numpy.newaxis np.newaxis] ==&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;numpy.newaxis&amp;lt;/syntaxhighlight&amp;gt;&amp;lt;blockquote&amp;gt;A convenient alias for None, useful for indexing arrays.&amp;lt;/blockquote&amp;gt;&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;import numpy as np&lt;br /&gt;
&lt;br /&gt;
data = np.zeros((2))&lt;br /&gt;
print(data.shape)  # -&amp;gt; (2,)&lt;br /&gt;
print(data[:, np.newaxis].shape)  # -&amp;gt; (2, 1)&lt;br /&gt;
print(data[np.newaxis, :].shape)  # -&amp;gt; (1, 2)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
print(data[:, np.newaxis, np.newaxis].shape)  # -&amp;gt; (2, 1, 1)&lt;br /&gt;
print(data[np.newaxis, :, np.newaxis].shape)  # -&amp;gt; (1, 2, 1)&lt;br /&gt;
print(data[:, np.newaxis, np.newaxis].shape)  # -&amp;gt; (2, 1, 1)&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Adding dimensions with [https://numpy.org/doc/stable/reference/generated/numpy.expand_dims.html numpy.expand_dims] ==&lt;br /&gt;
In the case you don’t like newaxis for adding a new dimension…&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;numpy.expand_dims(a, axis)&amp;lt;/syntaxhighlight&amp;gt;&amp;lt;blockquote&amp;gt;Expand the shape of an array.&lt;br /&gt;
&lt;br /&gt;
Insert a new axis that will appear at the axis position in the expanded array shape.&amp;lt;/blockquote&amp;gt;&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;import numpy as np&lt;br /&gt;
&lt;br /&gt;
data = np.zeros((2))&lt;br /&gt;
print(data.shape)  # -&amp;gt; (2,)&lt;br /&gt;
print(np.expand_dims(data, axis=0).shape)  # -&amp;gt; (1, 2)&lt;br /&gt;
print(np.expand_dims(data, axis=1).shape)  # -&amp;gt; (2, 1)&lt;br /&gt;
print(np.expand_dims(data, axis=(1, 2, 3)).shape)  # -&amp;gt; (2, 1, 1, 1)&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Removing dimensions with [https://numpy.org/doc/stable/reference/generated/numpy.squeeze.html numpy.squeeze] ==&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;numpy.squeeze(a, axis=None)&amp;lt;/syntaxhighlight&amp;gt;&amp;lt;blockquote&amp;gt;Remove axes of length one from a.&amp;lt;/blockquote&amp;gt;&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;import numpy as np&lt;br /&gt;
&lt;br /&gt;
data = np.zeros((1, 2, 1, 1))&lt;br /&gt;
print(data.shape)  # -&amp;gt; (1, 2, 1, 1)&lt;br /&gt;
print(np.squeeze(data).shape)  # -&amp;gt; (2,)&lt;br /&gt;
print(np.squeeze(data, axis=0).shape)  # -&amp;gt; (2, 1, 1)&lt;br /&gt;
print(np.squeeze(data, axis=2).shape)  # -&amp;gt; (1, 2, 1)&lt;br /&gt;
print(np.squeeze(data, axis=3).shape)  # -&amp;gt; (1, 2, 1)&lt;br /&gt;
&lt;br /&gt;
print(np.squeeze(data, axis=(0, -1)).shape)  # -&amp;gt; (2, 1)&lt;br /&gt;
&lt;br /&gt;
print(&lt;br /&gt;
    np.squeeze(data, axis=1).shape&lt;br /&gt;
)  # -&amp;gt; ValueError: cannot select an axis to squeeze out which has size not equal to one&amp;lt;/syntaxhighlight&amp;gt;&lt;/div&gt;</summary>
		<author><name>Davrot</name></author>
	</entry>
</feed>