Creating NumPy Arrays in Python

Introduction

NumPy arrays are used to store multiple values in a single variable. In this chapter, we will learn how to create NumPy array objects and understand their dimensions. Creating NumPy Arrays in Python with multiple dimensions.

Create a NumPy ndarray Object

The array() function is used to create a NumPy array. The array object in NumPy is called ndarray.

Example:

import numpy as np


numbers = np.array([10, 20, 30, 40, 50])

print(numbers)

Output:

[10 20 30 40 50]

Dimensions in NumPy Arrays

A dimension represents the number of directions or levels in which array elements are arranged.

NumPy supports different types of array dimensions.

0-D Array

A 0-D array contains a single value. It is also called a scalar.

Example:

import numpy as np

arr = np.array(25)

print(arr)

Output:

25

1-D Array

A 1-D array contains a sequence of values in a single dimension.

Example:

arr = np.array([10, 20, 30, 40])

print(arr)

Output:

[10 20 30 40]

2-D Array

A 2-D array contains values arranged in rows and columns. It is commonly used to represent tables and matrices.

Example:

arr = np.array([[10, 20, 30],
                [40, 50, 60]])

print(arr)

Output:

[[10 20 30]
 [40 50 60]]

3-D Array

A 3-D array contains multiple 2-D arrays grouped together.

Example:

arr = np.array([
    [[1, 2], [3, 4]],
    [[5, 6], [7, 8]]
])

print(arr)

Output:

[[[1 2]
  [3 4]]

 [[5 6]
  [7 8]]]

Check Number of Dimensions

NumPy provides the ndim attribute to check how many dimensions an array has.

Example:

import numpy as np

arr = np.array([[10, 20], [30, 40]])

print(arr.ndim)

Output:

2

Here, ndim returns 2 because the array has two dimensions.

Higher Dimensional Arrays

NumPy also supports arrays with more than three dimensions. These are called higher-dimensional arrays.

You can create them by adding more levels of nested lists.

Example:

import numpy as np

arr = np.array([10, 20, 30], ndmin=5)

print(arr)
print(arr.ndim)

Output:

[[[[[10 20 30]]]]]
5

Here, ndmin=5 tells NumPy to create an array with at least five dimensions.

Student Task

Create NumPy arrays and complete these tasks:

  1. Create a 0-D array containing 100.
  2. Create a 1-D array containing five numbers.
  3. Create a 2-D array containing two rows and three columns.
  4. Create a 3-D array containing two 2-D arrays.
  5. Use ndim to check the dimensions of each array.

Final Student Challenge: Create Arrays with Different Dimensions

Write a Python program to create four NumPy arrays:

  • A 0-D array containing 50.
  • A 1-D array containing 10, 20, 30.
  • A 2-D array containing two rows and two columns.
  • A 3-D array containing two 2-D arrays.

Print each array and its number of dimensions using ndim.

Key Takeaways

  • NumPy array objects are called ndarray.
  • np.array() creates a NumPy array.
  • NumPy supports 0-D, 1-D, 2-D and 3-D arrays.
  • ndim returns the number of dimensions.
  • NumPy also supports higher-dimensional arrays using ndmin.

Frequently Asked Questions (FAQs)

1. What is an ndarray in NumPy?

ndarray is the main array object used by NumPy to store and work with data.

2. What is a 0-D array?

A 0-D array contains a single value and is also called a scalar.

3. What is a 1-D array?

A 1-D array stores values in a single dimension.

4. What is a 2-D array?

A 2-D array stores values in rows and columns.

5. What is a 3-D array?

A 3-D array contains multiple 2-D arrays.

6. How can we check the dimensions of a NumPy array?

Use the ndim attribute to find the number of dimensions.

7. What are higher-dimensional arrays?

Arrays with more than three dimensions are called higher-dimensional arrays.

Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.

Scroll to Top