Adding dimensions to numpy.arrays: newaxis v.s. reshape v.s. expand_dims

  • 1.1 From 0-D (scalar) to n-D
  • 1.1.1 From 0-D to 1-D
  • 1.1.2 From 0-D to 2-D
  • 1.2 From 1-D to n-D (n>1)
  • 1.2.1 From 1-D to 2-D
  • 1.3 From 2-D to n-D (n>2)
  • 1.3.1 2-D to 3-D
  • 1.3.1.1 Case 1 : Add newaxis at the beginning
  • 1.3.1.2 Case 2: Squeeze newaxis in the middle
  • 1.3.1.3 Case 3: Add newaxis to the end
from numpy import array, newaxis, expand_dims
def show_array(y):print('array: \n', y)print('array.ndim:', y.ndim)print('array.shape:', y.shape)

1.1 From 0-D (scalar) to n-D

x = array(5)show_array(x)array: 
5
array.ndim: 0
array.shape: ()

1.1.1 From 0-D to 1-D

y = array(x)[newaxis]show_array(y)array: 
[5]
array.ndim: 1
array.shape: (1,)
y = expand_dims(x, axis=0)show_array(y)array: 
[5]
array.ndim: 1
array.shape: (1,)
y = expand_dims(x, axis=123456)show_array(y)array: 
[5]
array.ndim: 1
array.shape: (1,)
/opt/conda/lib/python3.6/site-packages/ipykernel_launcher.py:1: DeprecationWarning: Both axis > a.ndim and axis < -a.ndim - 1 are deprecated and will raise an AxisError in the future.
"""Entry point for launching an IPython kernel.
y = x.reshape(-1,)show_array(y)array: 
[5]
array.ndim: 1
array.shape: (1,)

1.1.2 From 0-D to 2-D

y = array(x)[newaxis, newaxis]show_array(y)array: 
[[5]]
array.ndim: 2
array.shape: (1, 1)
y = expand_dims(x, axis=0)z = expand_dims(y, axis=0)show_array(z)array:
[[5]]
array.ndim: 2
array.shape: (1, 1)
y = x.reshape(-1, 1)show_array(y)array:
[[5]]
array.ndim: 2
array.shape: (1, 1)

1.2 From 1-D to n-D (n>1)

x = array([5, 6, 7])show_array(x)array: 
[5 6 7]
array.ndim: 1
array.shape: (3,)

1.2.1 From 1-D to 2-D

y = array(x)[newaxis, :]show_array(y)array: 
[[5 6 7]]
array.ndim: 2
array.shape: (1, 3)
y = array(x)[newaxis] # This is short hand of y = array(x)[newaxis, :]show_array(y)array:
[[5 6 7]]
array.ndim: 2
array.shape: (1, 3)
y = expand_dims(x, axis=0)show_array(y)array:
[[5 6 7]]
array.ndim: 2
array.shape: (1, 3)
y = x.reshape(1, -1)show_array(y)array:
[[5 6 7]]
array.ndim: 2
array.shape: (1, 3)
y = array(x)[:, newaxis]show_array(y)array: 
[[5]
[6]
[7]]
array.ndim: 2
array.shape: (3, 1)
y = expand_dims(x, axis=1)show_array(y)array:
[[5]
[6]
[7]]
array.ndim: 2
array.shape: (3, 1)
y = expand_dims(x, axis=-1)show_array(y)array: 
[[5]
[6]
[7]]
array.ndim: 2
array.shape: (3, 1)
y = expand_dims(x, axis=123456)show_array(y)array: 
[[5]
[6]
[7]]
array.ndim: 2
array.shape: (3, 1)
/opt/conda/lib/python3.6/site-packages/ipykernel_launcher.py:1: DeprecationWarning: Both axis > a.ndim and axis < -a.ndim - 1 are deprecated and will raise an AxisError in the future.
"""Entry point for launching an IPython kernel.
y = x.reshape(-1, 1)show_array(y)array:
[[5]
[6]
[7]]
array.ndim: 2
array.shape: (3, 1)

1.3 From 2-D to n-D (n>2)

x = array([[1, 2, 3], [4, 5, 6]])show_array(x)array: 
[[1 2 3]
[4 5 6]]
array.ndim: 2
array.shape: (2, 3)

1.3.1 2-D to 3-D

1.3.1.1 Case 1 : Add newaxis at the beginning

y = array(x)[newaxis, :, :]show_array(y)array: 
[[[1 2 3]
[4 5 6]]]
array.ndim: 3
array.shape: (1, 2, 3)
y = array(x)[newaxis, :]show_array(y)array: 
[[[1 2 3]
[4 5 6]]]
array.ndim: 3
array.shape: (1, 2, 3)
y = array(x)[newaxis]show_array(y)array: 
[[[1 2 3]
[4 5 6]]]
array.ndim: 3
array.shape: (1, 2, 3)
y = expand_dims(x, axis=0)show_array(y)array:
[[[1 2 3]
[4 5 6]]]
array.ndim: 3
array.shape: (1, 2, 3)
y = x.reshape(-1, 2, 3)show_array(y)array:
[[[1 2 3]
[4 5 6]]]
array.ndim: 3
array.shape: (1, 2, 3)
y = x.reshape(-1, *x.shape)show_array(y)array:
[[[1 2 3]
[4 5 6]]]
array.ndim: 3
array.shape: (1, 2, 3)

1.3.1.2 Case 2: Squeeze newaxis in the middle

y = array(x)[:, newaxis, :]show_array(y)array: 
[[[1 2 3]]
[[4 5 6]]]
array.ndim: 3
array.shape: (2, 1, 3)
y = array(x)[:, newaxis]show_array(y)array:
[[[1 2 3]]
[[4 5 6]]]
array.ndim: 3
array.shape: (2, 1, 3)
y = expand_dims(x, axis=1)show_array(y)array:
[[[1 2 3]]
[[4 5 6]]]
array.ndim: 3
array.shape: (2, 1, 3)
y = x.reshape(2, 1, 3)show_array(y)array:
[[[1 2 3]]
[[4 5 6]]]
array.ndim: 3
array.shape: (2, 1, 3)
y = x.reshape(x.shape[0], -1, x.shape[1])show_array(y)array:
[[[1 2 3]]
[[4 5 6]]]
array.ndim: 3
array.shape: (2, 1, 3)

1.3.1.3 Case 3: Add newaxis to the end

y = array(x)[:, :, newaxis]show_array(y)array: 
[[[1]
[2]
[3]]
[[4]
[5]
[6]]]
array.ndim: 3
array.shape: (2, 3, 1)
y = expand_dims(x, axis=2)show_array(y)array:
[[[1]
[2]
[3]]
[[4]
[5]
[6]]]
array.ndim: 3
array.shape: (2, 3, 1)
y = expand_dims(x, axis=-1)show_array(y)array:
[[[1]
[2]
[3]]
[[4]
[5]
[6]]]
array.ndim: 3
array.shape: (2, 3, 1)
y = expand_dims(x, axis=123456)show_array(y)array: 
[[[1]
[2]
[3]]
[[4]
[5]
[6]]]
array.ndim: 3
array.shape: (2, 3, 1)
/opt/conda/lib/python3.6/site-packages/ipykernel_launcher.py:1: DeprecationWarning: Both axis > a.ndim and axis < -a.ndim - 1 are deprecated and will raise an AxisError in the future.
"""Entry point for launching an IPython kernel.
y = x.reshape(*x.shape, -1)show_array(y)array:
[[[1]
[2]
[3]]
[[4]
[5]
[6]]]
array.ndim: 3
array.shape: (2, 3, 1)

--

--

--

Software Engineering SMTS at Salesforce Commerce Cloud Einstein

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Android Data Binding: Say no with findViewById, RxBinding!

Platform as a Service (PaaS) — going beyond SaaS

The Ultimate Guide to Web Scraping with JavaScript and Node.Js

OpenSource is Awesome, Get started with ml-jar open source MLOPS Project

Merge Two DataFrames in Python

4 frequent objections to side projects and my answer

#100DaysOfCode Day 17: POST Request, Tweet from Home (Part 2 of Developing a Twitter Client)

Do Developers Need Computer Science Degrees?

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Yang Zhang

Yang Zhang

Software Engineering SMTS at Salesforce Commerce Cloud Einstein

More from Medium

Moving average filter (C vs Python)

Python Time Calculator

Identifier In Python