How to initialize a NumPy Array in Python?

In this article, we will learn how to initialize a NumPy Array in Python

Table of Contents

What is initialization?

Initialization is nothing but to set some value to a variable. There are multiple ways to initialize a NumPy array. Lets discuss all the methods one by one with proper approach and a working code example

Initialize a NumPy Array by directly passing values to the array

The numpy module has an array() method, and it takes a sequence or list as an input and returns a ndarray.

Syntax of array() function

Advertisements
numpy.array(object)
  • Parameters:
    • object : array_like.
  • Returns:
    • A ndarray.

Approach:

  1. Import numpy library.
  2. Pass the values to be present in the array to the array().
  3. It retuns a NumPyArray, initialized with the given values.

Source Code

import numpy as np

# initializing an array
arr = np.array([1, 2, 3, 4, 5])

print(arr)

Output:

[1 2 3 4 5]

Initialize a NumPy Array by using asarray()

The numpy module has an asarray() method, and it takes a sequence or an array_like object as an input and returns a ndarray, with the object values initialized.

Syntax of asarray() function

numpy.asarray(object)
  • Parameters:
    • object : array_like.
  • Returns:
    • A ndarray.

Approach:

  1. Import numpy library.
  2. Pass the values to be present in the array to the asarray().
  3. It retuns a array with the given values initialized.

Source Code

import numpy as np

# initializing an array
arr = np.asarray((1, 2, 3, 4, 5))

print(arr)

Output:

[1 2 3 4 5]

Initialize a NumPy Array by using zeros()

The numpy module has a zeros() method, and it takes a shape as an input and returns an array of given shape, but filled with zeros. In other words, zeros() can be used to initialize an array with zeros.

Syntax of zeros() function

numpy.zeros(shape)
  • Parameters:
    • shape : int or tuple of ints
      .
  • Returns:
    • A ndarray.

Approach:

  1. Import numpy library.
  2. Pass the shape to the np.zeros() method.
  3. It returns an array of zeros with given shape.

Source Code

import numpy as np

# initializing an array
arr = np.zeros(5)

print(arr)

Output:

[0. 0. 0. 0. 0.]

Initialize a NumPy Array by using ones()

The numpy module has ones() method, and it takes a shape as an input and returns an array of ones with given shape. In other words, ones() method can be used to initialize an array with ones.

Syntax of ones() function

numpy.ones(shape)
  • Parameters:
    • shape : int or tuple of ints
      .
  • Returns:
    • A ndarray.

Approach:

  1. Import numpy library.
  2. Pass the shape to the ones() method.
  3. It returns an array of ones with given shape.

Source Code

import numpy as np

# initializing an array
arr = np.ones(5)

print(arr)

Output:

[1. 1. 1. 1. 1.]

Initialize a NumPy Array by using full()

The numpy module has full() method, and it takes a shape and a fill_value as input and returns an NumPy Array of given shape filled with the fill_value. In other words, the full() method can be used to initialize an entire array with the same value.

Syntax of full() function

numpy.full(shape, fill_value)
  • Parameters:
    • shape : int or tuple of ints
    • fill_value : value to be filled in the array
  • Returns:
    • A ndarray of given shape.

Approach:

  1. Import numpy library.
  2. Pass the shape and fill value to the full() method.
  3. It returns an array of fill_value with the given shape.

Source Code

import numpy as np

# initializing an array
arr = np.full(shape = 5, fill_value = 4)

print(arr)

Output:

[4 4 4 4 4]

Initialize a NumPy Array by using arange()

The numpy module has arange() method, and it takes start_value, stop_value and step as input and returns evenly spaced values within a given interval. By default the start_value is 0 and step_value is 1.

Syntax of arange() function

numpy.arange(start, stop, step)
  • Parameters:
    • start : integer or real, optional. Start of interval. The interval includes this value. The default start value is 0.
    • stop : integer or real. End of interval. The interval does not include this value.
    • step : integer or real, optional. Spacing between values
  • Returns:
    • Return array of evenly spaced values within a given interval.

Approach:

  1. Import numpy library.
  2. Pass the start and stop to the arange() method.
  3. It returns an array of evenly spaced values within a given interval.

Source Code

import numpy as np

# initializing an array
arr = np.arange(5)

print(arr)

Output:

[0 1 2 3 4]

Initialize a NumPy Array by using random().

The numpy module has a random() method, and it takes a shape and return an array of given shape, initialized with random values.

Syntax of random() function

numpy.random(shape)
  • Parameters:
    • shape : int or tuple of ints
      .
  • Returns:
    • ndarray : array of random values with given shape.

Approach:

  1. Import numpy library.
  2. Pass the shape to the random() method.
  3. It returns an array of random values with given shape.

Source Code

import numpy as np

# initializing an array
arr = np.random.random(5)

print(arr)

Output:

[0.15737063 0.09794934 0.29919287 0.41237273 0.94688651]

Summary

Great! you made it, we have discussed all possible methods to initialize a NumPy array. 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

Leave a Comment

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top