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

NumPy Basics: Arrays & Operations Guide

This document provides an overview of NumPy, a fundamental library for numerical computing in Python, highlighting its efficient N-dimensional array object and fast operations. It covers key topics such as creating ndarrays, array attributes, elementwise operations, indexing, slicing, and various array operations. Additionally, it discusses aggregations, statistics, fancy indexing, and broadcasting, showcasing the library's capabilities for numerical analysis.

Uploaded by

swapnabk991997
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)
8 views3 pages

NumPy Basics: Arrays & Operations Guide

This document provides an overview of NumPy, a fundamental library for numerical computing in Python, highlighting its efficient N-dimensional array object and fast operations. It covers key topics such as creating ndarrays, array attributes, elementwise operations, indexing, slicing, and various array operations. Additionally, it discusses aggregations, statistics, fancy indexing, and broadcasting, showcasing the library's capabilities for numerical analysis.

Uploaded by

swapnabk991997
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

NumPy Notes - Intellipaat YouTube Class

1. Introduction to NumPy
What is NumPy?
- Fundamental library for numerical computing in Python.
- Provides powerful N-dimensional array object (ndarray), with fast operations.

Key Benefits:
- Efficient elementwise operations (vectorization).
- Memory efficient and faster than Python lists.

2. Creating ndarrays
From Python lists:
>>> import numpy as np
>>> a = [Link]([1, 2, 3]) # 1D array
>>> b = [Link]([[1, 2], [3, 4]]) # 2D array

Using built-in creation functions:


>>> [Link]((2,3)) # array of zeros
>>> [Link]((3,)) # array of ones
>>> [Link](0, 10, 2) # [0, 2, 4, 6, 8]
>>> [Link](0, 1, 5) # [0., 0.25, 0.5, 0.75, 1.]

3. Array Attributes
>>> [Link] # Dimensions
>>> [Link] # Data type (e.g., int64)
>>> [Link] # Number of axes (dimensions)
>>> [Link] # Total number of elements

4. Elementwise Operations
Arithmetic:
>>> x = [Link]([1, 2, 3])
>>> y = x * 2 # [2, 4, 6]
>>> z = x + y # [3, 6, 9]

Universal functions (ufuncs):


>>> [Link](x)
>>> [Link](y)
>>> [Link]([Link]([4,9,16]))

5. Indexing & Slicing


1D indexing:
>>> a = [Link](10)
>>> a[3] #3
>>> a[3:7] # elements 3,4,5,6

2D arrays:
>>> M = [Link]([[1,2,3],[4,5,6],[7,8,9]])
>>> M[1,2] #6
>>> M[:,1] # [2,5,8]

Boolean indexing:
>>> mask = a % 2 == 0
>>> a[mask] # [0,2,4,6,8]

6. Array Operations
Reshaping:
>>> a = [Link](12)
>>> a2 = [Link]((3,4))

Transpose:
>>> M.T

Concatenation:
>>> [Link]([a2, a2])
>>> [Link]([a2, a2])
7. Aggregations & Statistics
>>> [Link](axis=0) # column sums
>>> [Link](axis=1) # row means
Also: min, max, std, argmax, cumsum

8. Fancy Indexing & Broadcasting


Fancy indexing:
>>> idx = [0,2]
>>> a2[idx, idx] # selects (0,0) and (2,2)

Broadcasting:
>>> M + [Link]([1,0,1])

Common questions

Powered by AI

Common universal functions or ufuncs provided by NumPy include operations like np.sin, np.exp, and np.sqrt, which perform elementwise mathematical operations on arrays. For example, np.sin computes the sine of each element in an input array, np.exp calculates the exponential, and np.sqrt computes the square root. These functions allow for fast computation over array elements without writing explicit loops .

In 1D arrays, elements are accessed like regular lists: a[3] retrieves the fourth element, and slices a[3:7] access a subarray. In 2D arrays, indexing uses two indices separated by a comma: M[1, 2] accesses an element in the second row and third column. Boolean indexing allows filtering based on conditions: a[a % 2 == 0] retrieves even numbers. Fancy indexing uses lists of indices: idx = [0,2]; a2[idx, idx] selects elements at (0,0) and (2,2).

NumPy's ndarray provides several advantages over Python lists: it supports efficient elementwise operations due to vectorization, is generally more memory efficient, and allows for faster computation of array operations. Additionally, ndarrays support powerful, fast N-dimensional array objects that can handle complex mathematical operations with better performance than traditional Python lists .

NumPy arrays enable aggregation operations such as sum, mean, min, max, and others, performed over specified axes or the entire array. For example, a2.sum(axis=0) computes the sum of elements column-wise, and a2.mean(axis=1) calculates the mean of elements row-wise. These operations are optimized for performance and allow for concise computations over large datasets .

To create a 2D array of zeros in NumPy, you can use the np.zeros function: np.zeros((2, 3)), which creates a 2D array with two rows and three columns filled with zeros. To create an array with equally spaced values, np.linspace is used: np.linspace(0, 1, 5), which generates five equally spaced values between 0 and 1 .

Fancy indexing in NumPy allows for indexing arrays using integer arrays or lists, enabling access to multiple array elements non-sequentially, unlike standard sequential and slice-based access. For example, given a2 = np.array([[1,2,3],[4,5,6],[7,8,9]]), fancy indexing with idx = [0, 2] and a2[idx, idx] selects diagonal elements (0,0) and (2,2), which are elements 1 and 9. This flexible indexing mode facilitates complex data manipulation .

Broadcasting in NumPy allows operations between arrays of different shapes by automatically expanding the smaller array along its dimensions. An example of broadcasting is adding a 1D array to each row of a 2D array: for instance, if M is a 3x3 matrix M = np.array([[1,2,3],[4,5,6],[7,8,9]]), and a 1D array np.array([1,0,1]) is added to it, broadcasting will expand the 1D array so it can be added to each row of the matrix M .

NumPy achieves memory efficiency and faster elementwise operations through vectorization, which allows operations to be performed on entire arrays without explicit looping. This is achieved by optimizing low-level implementations for arrays, utilizing CPU SIMD instructions and minimizing the overhead associated with standard Python operations; all of which contribute to improved performance over Python lists that often require loops or comprehension for similar operations .

Vectorization in NumPy significantly boosts computational performance by enabling batch operations on data arrays without the need for explicit loops. It leverages efficient low-level C and Fortran subroutines, utilizing CPU architecture capabilities, like SIMD, to accelerate processing, particularly advantageous for large datasets. This approach reduces the overhead of Python loops, optimizing both speed and memory usage .

Reshaping in NumPy can be accomplished using the reshape method, which, for instance, transforms a 1D array into a 2D array: a = np.arange(12), a.reshape((3, 4)) changes it into a 3x4 array. Transposing is achieved using the .T attribute which flips a matrix along its diagonal; for example, M.T, where M = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]), results in the transposed matrix swapping rows and columns .

You might also like