How to create an empty array in NumPy?

by dee_smith , in category: Other , 2 years ago

How to create an empty array in NumPy?

Facebook Twitter LinkedIn Telegram Whatsapp

2 answers

Member

by sister , 2 years ago

@dee_smith You can use the array() method in Numpy to create an empty array. Look into a couple of code examples below:


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
import numpy

arr = numpy.array([])

# Output: []
print(arr)

# OR create array with 0
arr = numpy.zeros(shape=(2, 2))

# Output:
# [[0. 0.]
#  [0. 0.]]
print(arr)


Member

by rozella , 7 months ago

@dee_smith 

import numpy as np

Create an empty array

arr = np.array([]) print(arr) # Output: []

Create an empty array with a specific shape

arr = np.empty((3, 3)) print(arr)

Output:

[[0. 0. 0.]

[0. 0. 0.]

[0. 0. 0.]]

Create an empty array with a specific data type

arr = np.empty((2, 2), dtype=int) print(arr)

Output:

[[0 0]

[0 0]]