4

How to create an empty NumPy Array in Python?

 1 year ago
source link: https://thispointer.com/how-to-create-an-empty-numpy-array-in-python/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

In this article we will learn how to create an empty NumPy array in Python.

Table Of Contents

What is an empty Numpy array?

The size of an empty array is zero, and it does not contain any values.

Example: An empty array

[]

There are multiple ways to create an empty NumPy array in Python. Lets discuss all the methods one by one with proper approach and a working code example.

Advertisements

Create empty NumPy Array using empty() method

The numpy module have an empty() method, and it is used to create empty arrays. This method take the shape and type as arguments and returns a new array of given shape and type, without initializing entries.

Syntax of empty() method

numpy.empty(shape, dtype)
numpy.empty(shape, dtype)
  • Parameters:
    • shape = Shape of the empty array.
    • dtype = Datatype of the array elements.
  • Returns:
    • Returns a new array of given shape and type, without initializing entries.

Approach:

  1. Import numpy library
  2. Pass the shape of array as 0 to the empty() method.
  3. The empty() method will Returns a new array of given shape.
  4. print the Array

Source Code

import numpy as np
# Creating a empty array
arr = np.empty(0)
# printing the empty array
print(arr)
print(arr.size)
import numpy as np

# Creating a empty array 
arr = np.empty(0)

# printing the empty array
print(arr)
print(arr.size)

Output:

[]
0

Create empty NumPy Array using arange() method

The numpy module has the arange() method,and it is used to create an evenly spaced array within the given range(includnig start and excluding stop). This method take start, stop, step values as arguments and returns evenly spaced values within a given interval. Here the step specifies gap between each element if array.

Now to create an empty array, pass 0 as the argument to the arange() method. This will return a empty array.

Syntax of arange() method

numpy.arange(start, stop, step)
numpy.arange(start, stop, step)
  • Parameters:
    • start = Start of interval. The interval includes this value. The default start value is 0.
    • stop = End of interval. The interval does not include this value.
    • step = Spacing between values, default step size is 1.
  • Returns:
    • Returns evenly spaced values within a given interval.

Approach:

  1. Import numpy library.
  2. pass 0 as argument to the arange() method.
  3. The arange() method will Returns an array of size zero.
  4. print the Array.

Source Code

import numpy as np
# Creating a empty array
arr = np.arange(0)
# printing the empty array
print("array =", arr)
print("Size of array =", arr.size)
import numpy as np

# Creating a empty array 
arr = np.arange(0)

# printing the empty array
print("array =", arr)
print("Size of array =", arr.size)

Output:

array = []
Size of array = 0
array = []
Size of array = 0

Create empty NumPy Array using random() method

The random class of numpy module has a random() method, and it is used to create a random array of given size. It takes size as an argument and returns array with random floats in the half-open interval [0.0, 1.0). Now to create an empty array, pass 0 as argument to the random() method, this will return a empty array.

Syntax of random() method

numpy.random.random(size)
numpy.random.random(size)
  • Parameters:
    • start = Size of the array
  • Returns:
    • It returns an array with random floats between 0.0 and 1.0

Approach:

  1. Import numpy library.
  2. pass 0 as argument to the random() method.
  3. The random() method will Returns a array of size zero.
  4. Print the Array.

Source Code

import numpy as np
# Creating a empty array
arr = np.random.random(0)
# printing the empty array
print("array =", arr)
print("Size of array =", arr.size)
import numpy as np

# Creating a empty array 
arr = np.random.random(0)

# printing the empty array
print("array =", arr)
print("Size of array =", arr.size)

Output:

array = []
Size of array = 0
array = []
Size of array = 0

Create empty NumPy Array using np.zeros() method

The numpy module has the zeros() method, and it is used to create array with elements as zeros. This method takes shape and type as arguments and returns array filled with zeros. Now to create an empty array, pass 0 as argument to the zeros() method, this will return a empty array.

Syntax of zeros() method

numpy.zeros(shape, dtype)
numpy.zeros(shape, dtype)
  • Parameters:
    • shape = Shape of the empty array.
    • dtype = Datatype of the array elements.
  • Returns:
    • It Returns a new array of zeros with given shape and type.

Approach:

  1. Import numpy library.
  2. pass 0 as argument to the zeros() method.
  3. The zeros() method will Returns a array of size zero.
  4. Print the Array.

Source Code

import numpy as np
# Creating a empty array
arr = np.zeros(0)
# Printing the empty array
print("array =",arr)
print("Size of array =",arr.size)
import numpy as np

# Creating a empty array 
arr = np.zeros(0)

# Printing the empty array
print("array =",arr)
print("Size of array =",arr.size)

Output:

array = []
Size of array = 0
array = []
Size of array = 0

Create empty NumPy Array using np.ones() method

The numpy module has the ones() method, and it is used to create array with elements as ones. This method takes shape and type as arguments and returns array filled with ones. Now to create an empty array, pass 0 as argument to the ones() method, this will return a empty array.

Syntax of ones() method

numpy.ones(shape, dtype)
numpy.ones(shape, dtype)
  • Parameters:
    • shape = Shape of the empty array.
    • dtype = Datatype of the array elements.
  • Returns:
    • Returns a new array of ones with given shape and type.

Approach:

  1. Import numpy library
  2. pass 0 as argument to the ones() method.
  3. The ones() method will Returns a array of size zero.
  4. print the Array

Source Code

import numpy as np
# Creating a empty array
arr = np.ones(0)
# printing the empty array
print("array =",arr)
print("Size of array =",arr.size)
import numpy as np

# Creating a empty array 
arr = np.ones(0)

# printing the empty array
print("array =",arr)
print("Size of array =",arr.size)

Output:

array = []
Size of array = 0
array = []
Size of array = 0

Create empty NumPy Array using np.full() method

The numpy module has the full() method, and it is used to create array with given shape and element. This method takes shape and fill value as arguments and returns array filled with fill value. Now to create an empty array, pass 0 as shape and 0 as fill value to the full() method, this will return a empty array.

Syntax of full() method

numpy.full(shape, flll)
numpy.full(shape, flll)
  • Parameters:
    • shape = Shape of the empty array.
    • fill = Value to be filled in array.
  • Returns:
    • Return a new array of given shape and type, filled with fill_value.

Approach:

  1. Import numpy library
  2. pass 0,0 as argument to the full() method.
  3. The full() method will Returns a array of size zero.
  4. print the Array

Source Code

import numpy as np
# Creating a empty array
arr = np.full(0,0)
# printing the empty array
print("array =",arr)
print("Size of array =",arr.size)
import numpy as np

# Creating a empty array 
arr = np.full(0,0)

# printing the empty array
print("array =",arr)
print("Size of array =",arr.size)

Output:

array = []
Size of array = 0
array = []
Size of array = 0

Summary

Great! you made it. We have discussed all possible methods to Create an empty numpy array using Python. Happy learning.

Pandas Tutorials -Learn Data Analysis with Python

 

 

Are you looking to make a career in Data Science with Python?

Data Science is the future, and the future is here now. Data Scientists are now the most sought-after professionals today. To become a good Data Scientist or to make a career switch in Data Science one must possess the right skill set. We have curated a list of Best Professional Certificate in Data Science with Python. These courses will teach you the programming tools for Data Science like Pandas, NumPy, Matplotlib, Seaborn and how to use these libraries to implement Machine learning models.

Checkout the Detailed Review of Best Professional Certificate in Data Science with Python.

Remember, Data Science requires a lot of patience, persistence, and practice. So, start learning today.

Join a LinkedIn Community of Python Developers

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK