0% found this document useful (0 votes)
5 views3 pages

MultiDimensional Array Guide

The document provides an overview of multi-dimensional arrays in Python, specifically using NumPy. It covers creation, accessing elements, slicing, shape manipulation, iteration, arithmetic operations, broadcasting, and useful functions. Additionally, it highlights the performance advantages of NumPy arrays over Python lists and their application in Pandas DataFrames.

Uploaded by

mrinmoyeeasansol
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views3 pages

MultiDimensional Array Guide

The document provides an overview of multi-dimensional arrays in Python, specifically using NumPy. It covers creation, accessing elements, slicing, shape manipulation, iteration, arithmetic operations, broadcasting, and useful functions. Additionally, it highlights the performance advantages of NumPy arrays over Python lists and their application in Pandas DataFrames.

Uploaded by

mrinmoyeeasansol
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Understanding Multi-Dimensional Arrays in Python

1. What is a Multi-Dimensional Array?

A multi-dimensional array is an array with more than one axis.

Examples:

- 1D: [1, 2, 3]

- 2D: [[1, 2], [3, 4]]

- 3D: [[[1], [2]], [[3], [4]]]

2. Creating Multi-Dimensional Arrays with NumPy

import numpy as np

# 1D

np.array([1, 2, 3])

# 2D

np.array([[1, 2], [3, 4]])

# 3D

np.array([[[1], [2]], [[3], [4]]])

3. Accessing Elements

Use indices: array[row][col] or array[row, col]

Example:

arr[0][1] or arr[0, 1] returns the second element of the first row.

4. Slicing

You can use slicing just like Python lists.

Example:
Understanding Multi-Dimensional Arrays in Python

array[:, 1] gets all rows, second column.

5. Shape, Reshape, and Flatten

- .shape shows dimensions

- .reshape(new_shape) changes shape

- .flatten() converts to 1D

6. Iteration

Nested loops allow element-by-element access:

for row in arr:

for val in row:

print(val)

7. Operations

Supports arithmetic:

- Addition: arr1 + arr2

- Multiplication: arr1 * arr2

- Dot product: np.dot(arr1, arr2)

8. Broadcasting

Allows operation between arrays of different shapes:

arr + scalar or arr + arr_with_fewer_dims

9. Useful NumPy Functions

- np.zeros((2,2))

- np.ones((3,3))

- np.eye(3)

- np.arange(6).reshape(2,3)

- np.random.rand(2,2)

- np.sum(), np.mean()
Understanding Multi-Dimensional Arrays in Python

10. Memory and Performance

NumPy arrays are faster and more memory-efficient than Python lists due to contiguous memory storage.

11. Use in Pandas

Pandas DataFrames can be thought of as labeled 2D arrays.

Example:

import pandas as pd

df = pd.DataFrame([[1,2],[3,4]], columns=['A','B'])

Summary Table

Topic | Summary

-------------|--------------------------------------

Dimensions | 1D, 2D, 3D arrays

Indexing | arr[row][col] or arr[row, col]

Slicing | Use ':' for ranges

Shape | .shape, .reshape(), .flatten()

Operations | +, *, np.dot()

Broadcasting | Auto-expands arrays

You might also like