Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Create an empty NumPy array
#1
How can I create an empty NumPy array in different ways.
Reply
#2
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?
Reply
#3
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.]]
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#4
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)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Convert numpy array to image without loading it into RAM. DreamingInsanity 7 5,876 Feb-08-2024, 09:38 AM
Last Post: paul18fr
  IPython errors for numpy array min/max methods muelaner 1 555 Nov-04-2023, 09:22 PM
Last Post: snippsat
  Expand the range of a NumPy array? PythonNPC 0 746 Jan-31-2023, 02:41 AM
Last Post: PythonNPC
  Change a numpy array to a dataframe Led_Zeppelin 3 1,107 Jan-26-2023, 09:01 PM
Last Post: deanhystad
  from numpy array to csv - rounding SchroedingersLion 6 2,164 Nov-14-2022, 09:09 PM
Last Post: deanhystad
  numpy.array has no attribute head Led_Zeppelin 1 1,230 Jul-13-2022, 12:56 AM
Last Post: Led_Zeppelin
  Seeing al the data in a dataframe or numpy.array Led_Zeppelin 1 1,139 Jul-11-2022, 08:54 PM
Last Post: Larz60+
  go over and search in numpy array faster caro 7 1,742 Jun-20-2022, 04:54 PM
Last Post: deanhystad
  Create array of values from 2 variables paulo79 1 1,086 Apr-19-2022, 08:28 PM
Last Post: deanhystad
  Creating a numpy array from specific values of a spreadsheet column JulianZ 0 1,119 Apr-19-2022, 07:36 AM
Last Post: JulianZ

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020