0
0
NumPydata~5 mins

ndarray as the core data structure in NumPy

Choose your learning style9 modes available
Introduction

The ndarray is the main way to store and work with numbers in numpy. It helps you handle many numbers at once, like a list but faster and smarter.

When you want to store a table of numbers, like a spreadsheet.
When you need to do math on many numbers quickly, like adding or multiplying all at once.
When you want to work with images or sound data that are stored as grids of numbers.
When you want to organize data in multiple dimensions, like rows and columns or more.
When you want to use numpy functions that expect data in a fast, organized way.
Syntax
NumPy
import numpy as np

# Create a 1D ndarray
array_1d = np.array([1, 2, 3, 4])

# Create a 2D ndarray
array_2d = np.array([[1, 2], [3, 4]])

An ndarray can have any number of dimensions (1D, 2D, 3D, etc.).

You create an ndarray by passing a list or list of lists to np.array().

Examples
This shows an empty array with no elements and shape (0,).
NumPy
import numpy as np

# Empty array (0 elements)
empty_array = np.array([])
print(empty_array)
print(empty_array.shape)
An array with a single number has shape (1,).
NumPy
import numpy as np

# Array with one element
one_element_array = np.array([42])
print(one_element_array)
print(one_element_array.shape)
This is a 2D array with 1 row and 3 columns, shape (1, 3).
NumPy
import numpy as np

# 2D array with one row
one_row_array = np.array([[10, 20, 30]])
print(one_row_array)
print(one_row_array.shape)
This is a 2D array with 3 rows and 1 column, shape (3, 1).
NumPy
import numpy as np

# 2D array with one column
one_column_array = np.array([[5], [15], [25]])
print(one_column_array)
print(one_column_array.shape)
Sample Program

This program creates a 2D ndarray, prints it, then shows how to add and multiply all elements easily.

NumPy
import numpy as np

# Create a 2D ndarray representing a small table of numbers
numbers_table = np.array([[1, 2, 3], [4, 5, 6]])

print("Original ndarray:")
print(numbers_table)
print("Shape:", numbers_table.shape)

# Add 10 to every element
numbers_plus_ten = numbers_table + 10
print("\nAfter adding 10 to each element:")
print(numbers_plus_ten)

# Multiply every element by 2
numbers_times_two = numbers_table * 2
print("\nAfter multiplying each element by 2:")
print(numbers_times_two)
OutputSuccess
Important Notes

Time complexity: Accessing or modifying elements is very fast, usually O(1) per element.

Space complexity: Uses memory proportional to the number of elements stored.

Common mistake: Confusing Python lists with ndarrays. ndarrays support fast math operations on all elements at once, unlike lists.

Use ndarrays when you need fast, efficient math on large sets of numbers. Use lists if you need more flexible data types or structures.

Summary

ndarray is numpy's main way to store numbers in one or more dimensions.

It lets you do math on many numbers quickly and easily.

You create an ndarray by passing lists or nested lists to np.array().