Question #635

In Python, how to create an empty NumPy array?

Answer

There are several ways to create an empty NumPy array in Python. In the following we'll assume NumPy is imported according to the following:

import numpy as np


Create an empty Numpy array:

>>> a = np.array([])
array([], dtype=float64)


Create a NumPy array filled with zero of a given size:

>>> a = np.zeros([4,3])
array([[0., 0., 0.],
       [0., 0., 0.],
       [0., 0., 0.],
       [0., 0., 0.]])


Create an uninitialized NumPy array with a given size:

>>> a = np.empty([2,2])
array([[5.e-324, 5.e-324],
       [5.e-324, 0.e+000]])

3 events in history
Question by Alphonsio 01/11/2021 at 06:52:06 PM
In Python, how to create an empty NumPy array?
Answer by Alphonsio 01/11/2021 at 03:30:27 PM

There are several ways to create an empty NumPy array in Python. In the following we'll assume NumPy is imported according to the following:

import numpy as np


Create an empty Numpy array:

>>> a = np.array([])
array([], dtype=float64)


Create a NumPy array filled with zero of a given size:

>>> a = np.zeros([4,3])
array([[0., 0., 0.],
       [0., 0., 0.],
       [0., 0., 0.],
       [0., 0., 0.]])


Create an uninitialized NumPy array with a given size:

>>> a = np.empty([2,2])
array([[5.e-324, 5.e-324],
       [5.e-324, 0.e+000]])

Answer by Alphonsio 01/11/2021 at 03:29:58 PM

There are several ways to create an empty NumPy array in Python. In the following we'll assume numpy is imported according to the following:

import numpy as np


Create an empty Numpy array:

>>> a = np.array([])
array([], dtype=float64)


Create a NumPy array filled with zero of a given size:

>>> a = np.zeros([4,3])
array([[0., 0., 0.],
       [0., 0., 0.],
       [0., 0., 0.],
       [0., 0., 0.]])


Create an uninitialized NumPy array with a given size:

>>> a = np.empty([2,2])
array([[5.e-324, 5.e-324],
       [5.e-324, 0.e+000]])

# ID Query URL Count

Icons proudly provided by Friconix.