Skip to content

Create empty NumPy Array | Example code

  • by

There are two ways to create an empty NumPy Array. NumPy.zeros and NumPy.empty Both of these methods differ slightly.

numpy.zeros(shape, dtype=float, order='C')

numpy.empty(shape, dtype=float, order='C')
  • Shape – Shape of the new array, e.g., (2, 3) or 2.
  • dtype (optional) – The desired data-type for the array,e.g., numpy.int8. Default is numpy.float64.
  • order (optional) – Indicates whether multi-dimensional data should be stored in row-major (C-style) or column-major (Fortran-style) order in memory.

Example Create an empty NumPy Array

Simple example code. You have to import the NumPy module for both examples.

NumPy.empty

NumPy empty creates a Numpy matrix/array without initializing it with any values. It contains a junk value.

import numpy as np

emptyArr = np.empty((3, 2))
print(emptyArr)

Output:

Create empty NumPy Array

NumPy.zeros

NumPy zero creates a NumPy matrix/array and initializes all of its values to 0.

import numpy as np

zeroArr = np.zeros((2, 3))
print(zeroArr)

Output:

zero NumPy Array

Do comment if you have any questions on this Python NumPy topic.

Note: IDE: PyCharm 2021.3.3 (Community Edition)

Windows 10

Python 3.10.1

All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.

Leave a Reply

Your email address will not be published. Required fields are marked *