0% found this document useful (0 votes)
2 views6 pages

Numpy Notes 2

NumPy is a fundamental library in Python for numerical computations, enabling the creation and manipulation of arrays and matrices. It provides various functions for array creation, mathematical operations, statistical analysis, and reshaping. Key features include indexing, slicing, broadcasting, and unique value identification.
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)
2 views6 pages

Numpy Notes 2

NumPy is a fundamental library in Python for numerical computations, enabling the creation and manipulation of arrays and matrices. It provides various functions for array creation, mathematical operations, statistical analysis, and reshaping. Key features include indexing, slicing, broadcasting, and unique value identification.
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 (Numerical Python) is the fundamental Python library used for

numerical computations, arrays, matrices, statistics, broadcasting, and


scientific computation.

Importing NumPy

import numpy as np

NumPy Arrays

Creating Arrays

[Link]() – Create array from Python list

arr = [Link]([1, 2, 3])

Explanation: Creates a 1D array.

[Link]() – Create 2D array

arr = [Link]([[1, 2], [3, 4]])

Array Creation Functions

[Link]()

Creates array of zeros.

Syntax:

[Link](shape, dtype=float)

Example:

[Link]((2,3))

[Link]()

Creates array of ones.

[Link]((3,3), dtype=int)
[Link]()

Creates an array filled with a constant value.

[Link]((2,2), 7)

[Link]()

Creates a range of numbers.

[Link](1, 10, 2)

[Link]()

Evenly spaced numbers between a range.

[Link](0, 1, 5)

[Link]()

Creates identity matrix.

[Link](3)

[Link]()

Random numbers between 0 and 1.

[Link](2, 3)

[Link]()

Random integers in a range.

[Link](1, 10, size=(3,3))

Array Attributes

Attribute Description Example


.ndim Number of dimensions [Link]
.shape Dimensions (rows, columns) [Link]
.size Total elements [Link]
.dtype Data type [Link]
.itemsize Size of each element in bytes [Link]

Example:

arr = [Link]([[1,2,3],[4,5,6]])
print([Link]) # 2
print([Link]) # (2,3)
print([Link]) # 6
print([Link]) # int32

Indexing and Slicing

Basic Indexing

arr = [Link]([10,20,30,40])
arr[2]

Slicing

arr[1:4]
arr[:3]
arr[::2]

2D Indexing

arr = [Link]([[1,2,3], [4,5,6]])


arr[1][2] # 6
arr[1, 2] # 6

2D Slicing

arr[:, 1:] # All rows, columns from index 1

Mathematical Functions

Element-wise operations

a+b
a-b
a*b
a/b
a ** 2

Universal Functions (ufuncs)

Function Meaning Example


[Link]() Square root [Link](arr)
[Link]() Exponential [Link](arr)
[Link]() Natural log [Link](arr)
[Link]() Absolute value [Link](arr)
[Link]() Rounding [Link](arr)
[Link]() Flooring [Link](arr)
[Link]() Ceiling [Link](arr)

Statistical & Aggregate Functions

Function Description
[Link]() Minimum
[Link]() Maximum
[Link]() Sum
[Link]() Average
[Link]() Median
[Link]() Standard deviation
[Link]() Variance
[Link]() Index of max value
[Link]() Index of min value

Examples:

arr = [Link]([1,2,3,4,5])
[Link](arr)
[Link](arr)
[Link](arr)

Array Reshaping

reshape()

[Link](3,2)
Stacking Arrays

[Link]() – Horizontal stack

[Link]((a, b))

[Link]() – Vertical stack

[Link]((a, b))

[Link]()

[Link]((a, b), axis=0)

Splitting Arrays

[Link]()

[Link](arr, 2)

[Link]()

[Link](arr, 2)

Boolean Indexing

arr[arr > 50]


arr[(arr > 20) & (arr < 40)]

Broadcasting

Broadcasting allows arithmetic on arrays of different shapes.

Example:

arr = [Link]([1,2,3])
arr + 10

Sorting

[Link](arr)
[Link]()
Unique Values

[Link](arr)

You might also like