Python Forum
Create an empty NumPy array - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Create an empty NumPy array (/thread-18016.html)



Create an empty NumPy array - karansingh - May-03-2019

How can I create an empty NumPy array in different ways.


RE: Create an empty NumPy array - scidam - May-03-2019

You can create empty numpy array by passing arbitrary iterable to array constructor numpy.array, e.g.
import numpy as np
np.array(list())
np.array(tuple())
np.array(dict())
np.fromfunction(lambda x: x, shape=(0,)) 
Why do you asking about multiple ways of doing that?


RE: Create an empty NumPy array - DeaD_EyE - May-03-2019

shape = (3, 256, 256, 2)
zeros = np.zeros(shape, dtype=np.complex128)
ones = np.ones(shape, dtype=np.complex128)

zeros_like = np.zeros_like(zeros)
ones_like = np.ones_like(ones)

zeros_like_f64 = np.zeros_like(zeros, dtype=np.float64)
ones_like_f64 = np.ones_like(ones, dtype=np.float64)

# window funcitons
hanning = np.hanning(256)
hamming = np.hamming(256)
blackman = np.blackman(256)

# random samples
rnd = np.random.random(shape)

# random complex
real_part = np.random.random(shape) # float64
imag_part = np.random.random(shape) # float64
signal = real_part + 1j * image_part # multiplication with float and imaginary number casts to complex128
print(np.eye(3))
Output:
[[1. 0. 0.] [0. 1. 0.] [0. 0. 1.]]



RE: Create an empty NumPy array - dukoolsharma - May-04-2019

There are two methods to create NumPy array-

First method-
>>> import numpy
>>> numpy.array([])
array([], dtype=float64)
Second method-
>>> numpy.empty(shape=(0,0))
array([], shape=(0, 0), dtype=float64)