DEV Community

Sarvesh Kesharwani
Sarvesh Kesharwani

Posted on

np.expand_dims(x, axis=0)?

np.expand_dims(x, axis=0) is a NumPy function used to add a new dimension to a given NumPy array x at the specified axis (in this case, axis 0).

For example, if x is a NumPy array of shape (3, 4) which represents a 3x4 matrix, calling np.expand_dims(x, axis=0) will add a new dimension to x at axis 0, resulting in a new NumPy array of shape (1, 3, 4) which represents a 1x3x4 matrix.

This can be useful when working with neural networks, as many deep learning frameworks expect inputs to be in the form of batches. Adding a new dimension to a single input array using np.expand_dims can allow it to be passed through a neural network that expects inputs in batch form, such as resnet50 in TensorFlow.

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay