0% found this document useful (0 votes)
4 views25 pages

NumPy Revision DS

NumPy is a powerful library for numerical computing in Python, offering significant speed and memory efficiency compared to Python lists due to its C-optimized backend and support for vectorized operations. It allows for the creation of arrays, manipulation of data types, and efficient memory usage, making it ideal for handling large datasets. Key features include built-in mathematical functions, reduced memory overhead, and the ability to perform operations without explicit loops.

Uploaded by

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

NumPy Revision DS

NumPy is a powerful library for numerical computing in Python, offering significant speed and memory efficiency compared to Python lists due to its C-optimized backend and support for vectorized operations. It allows for the creation of arrays, manipulation of data types, and efficient memory usage, making it ideal for handling large datasets. Key features include built-in mathematical functions, reduced memory overhead, and the ability to perform operations without explicit loops.

Uploaded by

yuvraj
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
516/25, &:08 PM 01_why_use_numpy Why use NumPy? Python lists are flexible but slow for numerical computing because they ® Store elements as pointers instead of a continuous block of memory. * Lack vectorized operations, relying on loops instead * Have significant overhead due to dynamic typing, NumPy's Features - ® Faster than Python lists (C-optimized backend} Uses less memory (efficient storage) * Supports vectorized operations (no explicit loops needed) Has built-in mathematical functions NumPy vs Python Lists Example 1 - Adding Two Lists vs NumPy Arrays import numpy as np import time # Python List size = 1_900_000 List1 = list(range(size)) List2 = list(range(size)) start = [Link]() result = [x + y for x, y in zip(list1, list2)] end = [Link]() print(*List addition tine -" end = start) # NumPy array arri = np-array(list1) start = [Link]() result = arrl + arr2 end = time. time() print(*NumPy array addition time -", end - start) List addition time - @.976823091506958 NumPy array addition time - .20178937911987305, __Kahani ka Sheershak -__ NumPy is significantly faster because it performs operations in C, avoiding Python loops localhost 888abrree/Using_ Numpy/01_why_use_numpyipynb? 4 516/25, &:08 PM 01_why_use_numpy __Important baat -__List homogeneous ho ya na ho NumPy Array hona chahiye just like any other array. Creating NumPy Arrays import numpy as np # Creating @ 1D NumPy array arri = [Link]([1, 2, 3, 4 5]) print(arrt) # Creating @ 20 NumPy array are2 = [Link](([1, 2, 3], [4, 5, 6]]) print(arr2) # checking type and shape print("Type -", type(arri)) print("Shape -", [Link]) [12345 [f1 23 [45 6]] Type - Shape - (2, 3) Kavi Yahan Kya Kehna Chahta hai ~__ * NumPy stores data in a contiguous memory block, making access faster than lists * Shape shows the dimensions of an array. Using zip 1, 2, 3] 2, 4, 6] List(zip(11, 12)) # Type casted to List 1G, 2), (2, 4), G3, 6)] Memory Efficiency - NumPy vs Lists import sys List_data = 1ist(range(1000)) nunpy_data = np. array(list_data) localhost 888abrree/Using_ Numpy/01_why_use_numpyipynb? 24 516/25, &:08 PM 01_why_use_numpy print("Python List size -", [Link](1ist_data) * len(1ist_data), “bytes") print("NumPy array size -", numpy_data.nbytes, "bytes") Python List size - 8056080 bytes NunPy array size - 4900 bytes __Learning -__ NumPy arrays use significantly less memory compared to Python lists, Vectorization - No More Loops NumPy avoids loops by applying operations to entire arrays at once using SIMD (Single Instruction, Multiple Data) and other low-level optimizations. SIMD is a CPU-leve optimization provided by modern processors. Example - Squaring Elements import numpy as np # Python List (Loop Based) lista = [1, 2, 3, 4, 5] List_squares = [x ** 2 for x in list1] print(1ist_squares) # nunpy (Vectorizing) ari = [Link]([1, 2, 3, 4, 5]) nunpy_squares = arra ** 2; print (nunpy_squares) 1, 4, 9, 36, 25) [1 4 91625) __Learning -__NumPy is cleaner and faster Practice Questions - © Create a NumPy array with values from 10 to 100 and print its shape. import numpy as np arr = [Link](12, 101) print("Array -", arr) print("Shape -", [Link]) localhost 888abrree/Using_ Numpy/01_why_use_numpyipynb? aa 516/25, &:08 PM 01_why_use_numpy Array - [18 11 12 13 14 15 16 17 18 19 26 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 as 46 47 48 49 SQ 51 52 53 S54 55 56 57 SB 59 60 61 62 63 54 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 98 91 92 93 94 95 96 97 98 99 100) Shape - (91,) * Compare the time taken to multiply two Python lists vs two NumPy arrays. import numpy as np import time # Using Lists size = 1_900_000 List = list(range(size)) List2 = list(range(size)) start = [Link]() result = [x * y for x, y in zip(list2, 1ist2)] end = time. time() print("List multiplication time -", end - start) # Using Numby arr = np-arange(size) arr2 = [Link](size) start = [Link]() result = arr * arr2 end = [Link]() print(“NunPy Multiplication time -", end - start) List multiplication time - 0.40917348861694336 NunPy Multiplication time - .06451416015625 * Find the memory size of a NumPy array with 1 million elements I import numpy as np import sys size = 1_000_000 arr = [Link](size) print(*NunPy array size ~ [Link], “bytes") NumPy array size - 4000000 bytes localhost 888abrree/Using_ Numpy/01_why_use_numpyipynb? 4 1516725, 807 PM 02_creating_numpy_arrays Creating NumPy Arrays From Python Lists import numpy as np arrl arr2 p-array([1, 2, 3, 4 S]) p-array({(1, 2, 3], [4, 5, 6]]) print(*Array 1 -", arr) print("Areay 2 -\n", arr2) Array 1 - [12345 Array 2 - {{1 2 3] [45 6]] __Note -__In NumPy arrays - unlike lists, all elements must have the same data type. ‘#4 From Scratch import numpy as np # 3x3 array of zeros arrl = [Link]((3, 3)) # 2x4 array of ones arr2 = [Link]((2, 4)) # 2x2 array filled with 7 arr3 = [Link]((2, 2), 7) # axa tdentity matrix arra = np-eye(4) # [1, 3, 5, 7, 9] (Like range) arrs = np-arange(t, 10, 2) # [@. 0.25 0.5 0.75 1.] (evenly spaced) are = [Link](®, 4, 5) print("Array 1 -\n", arrt, “\n") print("Array 2 -\n", arr2, "\n") print(*Array 3 -\n", arr3, “\n") print("Array 4 -\n", arr4, "\n") print("Array 5 -\n", arr5, “\n") print("Array 6 -\n", arr6, “\n") locahoste8esiabiree/Using Numpy!02_creating_numpy_arraysipynb? 18 1516725, 807 PM 02_creating_numpy_arrays Array 1 - [[@. @ @] [8. 2. 2 [e. 2. @.]] Array 2 - (1. 2.4.2. [haa a.]] Array 3 - (17 7: 071) Array 4 - [[1. 8. @. @ [a. 1. 0. 8. [a. 2. 1. 0. [a. 8. 8. 1. I Array 5 - (13579) Array 6 - [e. 9,250.5 0.751. ] __Kahani Ka Saar -__ NumPy offers powerful shortcuts to create arrays without loops Checking Array Properties import numpy as np arr = [Link]({[1, 2, 3], [4, 5, 6]]) print("Shape -", [Link]) # (2, 3) » 2 rows, 3 columns print("Size -", [Link]) # 6 » total elements print("Dimensions -", [Link]) # 2 + 2D array print(“Data type -", [Link]) shape - (2, 3) Size - 6 Dimensions - 2 data type - int32 __Note -__ NumPy arrays are strongly typed, meaning all elements share the same date type Changing Data Types locahoste8esiabiree/Using Numpy!02_creating_numpy_arraysipynb? 1516725, 807 PM localhost B888abRree/Using_ NumpyI02.. 02_creating_numpy_arrays import numpy as np arr = [Link]([1, 2, 3], dtype = np.float32) # Explicit type print([Link]) print(arr) print("\n") arr_int = [Link](np.int32) # Converting float to int print(arr_int.dtype) print(arr_int) Float32, (1. 2. 3. int32 (1.23 __ Efficient memory usage by choosing the right data type.__ Reshaping and Flattening Arrays import numpy as np arn = [Link]({[1, 2. 3], [4s 5, 6]]) print("Shape of arr -", [Link]) reshaped = [Link]((3, 2)) print("\nReshaped array -\n", reshaped) flattened = [Link]() print("\nFlattened array -", flattened) Shape of arr - (2, 3) Reshaped array - {{1 2 [34] [s 6] Flattened array - [12345 6) ating_numpy_arrays.ipynb? 1516725, 807 PM 03_indexing_and_sicng Indexing And Slicing Indexing (Same as Python Lists) import numpy as np arr = [Link]({1, 2, 3, 4]) print("Elenent at @th index -", arr[@]) print("Elenent at -1th index -", arr[-1]) # ALlows Negative Indexing Element at @th index - 1 Element at -1th index - 4 Slicing (Extracting Parts of an Array) import numpy as np arr = [Link]([1, 2, 3, 4, 5, 6]) print(arr[1:4]) # [2 3 4] (slice from index 1 to 3) print(arr[:3]) # [1 2 3] (first 3 elements) print(arr[::2]) # [1 3 5] (every 2nd eLement) Slicing returns a view, not a copy! Changes affect the original array. This might seem counterintuitive since Python lists create copies when sliced. But in NumPy, slicing returns a view of the original array. Both the sliced array and the original array share the same data in memory, so changes in the slice affect the original array. __Why does this happen?__ * __Memory Efficiency -__ Avoids unnecessary copies, making operations faster anc saving memory. + __Performance -__ Enables faster access and manipulation of large datasets without duplicating data import numpy as np arr = [Link]([1, 2, 3, 4, 5, 6, 71) sliced = arr[1:4] sliced[@] = 23 print("sliced -", print("Aarray now iced) arr) locahost8esiabiree/Using Numpy!03_Indexing_and slicing ioynb? 18 1516725, 807 PM 03_indexing_and_sicng Sliced - [23 3 4] Array now [123 3 4 5 6 7 __Note -__ Use copy( if you need an independent copy. Fancy Indexing and Boolean Masking Fancy Indexing (Select Multiple Elements) import numpy as np arr = [Link]([1, 2, 3, 4 5, 6 71) index = [@, 2, 4] # Indices to select print(arr[index]) (135 Boolean Masking (Filter Data) import numpy as np arr = [Link]([1, 2, 3, 4, 5, 6]) mask = arr > 3 # Condition - values greater than 3 print(are[mask]) [456 __This is a powerful way to fiter large datasets efficiently!_ Practice Questions - * Create a 3x3 array filled with random numbers and print its shape import numpy as np arr = [Link]({{1, 2, 3], [4, 5, 6], [7, 8, 9]]) print([Link]) @ 3) © Convert an array of floats [1.1, 2.2, 3.3] into integers. import numpy as np are = [Link]([1.1, 2.2, 3.3]) arr_int = [Link](np.int32) print (arr_int) locahost8esiabiree/Using Numpy!03_Indexing_and slicing ioynb? 1516725, 807 PM 03_indexing_and_sicng (1.23, © Use fancy indexing to extract even numbers from [1, 2, 3,4, 5, 6] I import numpy as np arr = [Link]([1, 2, 3, 4, 5, 6]) even_indices = [i for i, x in enunerate(arr) if x % 2 evens = arr[even_indices] print("Even nunbers -", evens) Even numbers - [2 4 6 © Reshape a 1D array of size 9 into a 3x3 matrix I import numpy as np arr = [Link](9) print(arr) print("Shape of the array is -", [Link]) print("\n") reshaped = [Link]((3, 3)) print(reshaped) print("Reshaped array shape -", reshaped. shape) (@12345678] Shape of the array is - (9,) [le 12 B45 [6 7 8]] Reshaped array shape - (3, 3) * Use boolean masking to filter numbers greater than 50 in an array. I import numpy as np arr = [Link]([1@, 62, 28, 72, 90, 100, 38, 50]) mask = arn > 50 print("Masked array -", arr{mask]) Masked array - [ 68 70 98 100 locahost B88sabiree/Using_ NumpyI03. fxing_and_sticingioynb? 1516725, 807 PM (04_muttdimensionalindexing_and_axis Multidimensional Indexing and Axis NumPy allows you to efficiently work with multidimensional arrays, where indexing and axis ‘manipulation play a crucial role, Understanding how indexing works across multiple dimensions is essential for data science and machine learning tasks Understanding Axes in NumPy Each dimension in a NumPy array is called an axis, Axes are numbered starting from 0. For example ~ © _1Darray +__1 axis (axis 0) © __2D array +__2 axes (axis © 3D array ~__3 axes (axis ‘ows, axis 1 = columns) = depth, axis = rows, axis 2 = columns) Example - Axes in 2D Array import numpy as np arr = [Link]([[1, 2, 3], [4 5, 6] (7, 8 911) print("Array -\n", arr) print("\n") # Axis @ (rows) > Operations move down the columns # Axis 1 (columns) + Operations move across the rows print("Colunn sum [C1, C2, C3] -", [Link](arr, axis = @)) ‘# Sum along rows (down each coLumn) print("Row sum [R1, R2, R3] -", [Link](arr, axis = 1)) # Sum along columns (across each row) Array - ([1 2 3] [4 5 6: [789i] Column sum (C1, ¢2, C3] - [12 15 18] Row sum [R1, R2, R3] - [ 6 15 24] Indexing in Multidimensional Arrays You can access elements using row and column indices. You can also use slicing to extract garts of an array - localhost B888abitree/Using. Numpy/04_mulidimensional_indexing_and_axipynb? 4 1516725, 807 PM (04_muttdimensionalindexing_and_axis import numpy as np are = [Link]([[1, 2, 3], [4, 5, 6], [7, 8 91]) print(arr[1, 2]) # Row index 1, Column index 2» Output - 6 print(arr[@:2, 1:3]) # Extracts first 2 rows and last 2 columns # 0:2 => @, 1 wali row retrieve karna # 1:3 => 1, 2 wala column retrieve karna Indexing in 3D Arrays For 3D arrays, the first index refers to the “depth” (sheets of data! Amport numpy as np arr3D = [Link]({{[1, 2, 3], [4, 5, 6]], [{7, 8 9], [18 11, 12]]]) # Output of arr3D. shape is » (depth, rows, columns) print("3D Array is -\n", arr3D) print("\nShape of 30 Array is -", [Link]) 3D Array is - (L223) [45 6]) (L7 8 9] 1@ 11 12]] Shape of 3D Array is - (2, 2, 3) Accessing Elements in 3D # First sheet, second row, third column print("First sheet, second row, third column -", arr3b[@, 1, 2]) # Get the first row from both sheets print("\nFirst row from both sheets -\n", arr30[:, @, :]) First sheet, second row, third column - 6 iest row from both sheets - {1 2 3] (78 9]] Selecting Data along Axes localhost B888abitree/Using. Numpy/04_mulidimensional_indexing_and_axipynb? 24 1516725, 807 PM (04_muttdimensionalindexing_and_axis import numpy as np arr = [Link]([[1, 2, 3], [4, 5, 6], [7, 8 911) arr3D = [Link]({{[1, 2, 3], [4, 5, 6]], [{7, 8 9], [28 12, 12]]]) # Get all rows of the first column first_col = arr[:, @] print("All rows of the first column -\n", first_col) # Get the first row from each "sheet in a 3D orray first_row = arr30[:, 2, :] print("\nFirst row from each sheet in a 30 array -\n", first_row) All rows of the first column - 1147) First row from each sheet in a 3D array ~ (1 2 3] (78 9]] Changing Data Along an Axis import numpy as np arr = [Link]([[1, 2, 3], [4, 5, 6], [7, 8 91]) print("Original Array -\n", arr) # Replace all elements in column 1 with @ arr[:, 1] = 0 print("\nUpdated Array -\r » arr) Original Array - [[1 23] [45 6) (7 89}] Updated Array - {[1 6 3) [4 @ 6: (7 @9}] localhost B888abitree/Using. Numpy/04_mulidimensional_indexing_and_axipynb? 1516725, 807 PM (04_muttdimensionalindexing_and_axis Summary - © Axis rows (vertical movement), Axis 1 = columns (horizontal movement} # Indexing works as arrirow, column] for 2D arrays and art{depth, row, column] for 3 arrays ® Slicing allows extracting subarrays * Operations along axes help efficiently manipulate data without loops localhost B888abitree/Using. Numpy/04_mulidimensional_indexing_and_axipynb? 4 516725, 8:08 PM 05, datatypes_in_numpy DataTypes in NumPy NumPy arrays are homogeneous, meaning that they can only store elements of the same type. This is different from Python lists, which can hold mixed data types. NumPy supports various data types (also called dtypes), and understanding them is crucial for optimizing memory usage and performance. Common Data Types in NumPy - © int32, int64 - Integer types with different bit sizes ® float32, float64 - Floating-point types with different precision * bool - Boolean data type © complex64, complex128 - Complex number types. * object - For storing objects (e.g, Python objects, strings). You can check the dtype of a NumPy array using the .dtype attribute import numpy as np arr = [Link]((1, 2, 3, 4, 5]) print([Link]) # Usually gives inté4 for mac and int32 for windows int32 Changing Data Types You can cast (convert) the data type of an array using the astype() method. This is usefu when you need to change the type for a specific operation or when you want to reduce memory usage Example - Changing Data Types import numpy as np arr = [Link]([1.5, 2.7, 3.9]) print([Link]) arr_int = [Link](np.int32) # Converting float to int print(arr_int) print(arr_int.dtype) floated (123 int32 localhost e8esiabiree/Using_ Numpy!05_datalypes_in_numpy.ipynb? 4 516725, 8:08 PM 05, datatypes_in_numpy Example - Downcasting to Save Memory import numpy as np arr_large = [Link]([1000800, 200000, 300000], dtype = np.int6a) arr_small = arr_large.astype(np-int32) # Downcasting to a smaller dtype print(arr_small) # Output - [100000 2008000 3000000] print(arr_small.dtype) # Output - int32 [1988222 2000000 3000000) int32 Why Data Types Matter in NumPy? The choice of data type affects - * __Memory Usage -__ Smaller data types use less memory, + __ Performance -__ Operations on smaller data types are faster due to less data being processed, * __Precision -__ Choosing the appropriate data type ensures that you don't lose precision (ex - using float32 instead of float64 if you don't need that extra precision) Example - import numpy as np arr_int64 = [Link]([1, 2, 3], dtype arr_int32 = [Link]([1, 2, 3], dtype = np. print(*Menory occupied by int64 -", arr_int64.nbytes) # Output - 24 bytes (3 elements * 8 bytes each) print(*Menory occupied by int32 -", arr_int32.nbytes) # Output - 12 bytes (3 elements * 4 bytes each) Memory occupied by inte4 - 24 Memory occupied by int32 - 12 String DataType in NumPy Although NumPy arrays typically store numerical data, you can also store strings by using the dtype='str or dtype="U' (Unicode string) format, However, working with strings ir NumPy is less efficent than using lists or Python's built-in string types Example - String Array localhost e8esiabiree/Using_ Numpy!05_datalypes_in_numpy.ipynb? 24 516725, 8:08 PM 05, datatypes_in_numpy import numpy as np arr = [Link](["yuvraj', ‘rajvir', ‘sachdeva'], dtype = "U1e") # Unicode String array print(arr) ['yuvraj’ ‘rajvir’ ‘sachdeva" ] Complex Numbers NumPy also supports complex numbers, which consist of a real and imaginary part. You can store complex numbers using complex64 or complex128 data types. Example - Amport numpy as np arr = [Link]([1 + 2J, 2 + 3, 3 + 43], dtype = “complexsa”) print(arr) [1.42.5 2.43.5 3.44.5] Object Data Type If you need to store mixed or complex data types (e.g, Python objects), you can use dtype="object’. However, this type sacrifices performance, so it should only be used when absolutely necessary. Example - import numpy as np are = [Link]([{'a': 1}, [1, 2, 3], "Yuvraj'], dtype = object) print(arr) [('ats 1p List([1, 2, 3]) "Yuvraj" Choosing the Right Data Type Choosing the correct data type is essential for - * __Optimizing memory -__ Using the smallest data type that fits your data * __Improving performance -_ Smaller types generally lead to faster operations localhost e8esiabiree/Using_ Numpy!05_datalypes_in_numpy.ipynb? aa 516725, 8:08 PM 05, datatypes_in_numpy ® __Ensuring precision -__ Avoid truncating or losing important decimal places or values Summary - * NumPy arrays are homogeneous, meaning all elements must be of the same type. * Use astype() to change data types and optimize memory and performance. © The choice of data type affects memory usage, performance, and precision ® Be mindful of complex numbers and object data types, which can increase memory usage and reduce performance. localhost e8esiabiree/Using_ Numpy!05_datalypes_in_numpy.ipynb? 4 516725, 8:08 PM 06, broadcasting Broadcasting In NumPy Vectorization and Broadcasting in NumPy are key to boosting performance in numerical operations by avoiding slow loops and memory inefficiency, Why Loops are Slow? In Python, loops are typically slow because - * __Python’s interpreter -__ Every iteration of the loop requires Python to interpret the loop logic, which is inherently slower than lower-level, compiled code * __High overhead -__ Each loop iteration in Python involves additional overhead for function calls, memory access, and index management While Python loops are convenient, they don't take advantage of the optimized memory and computation that libraries like NumPy provide Example - Looping Over Arrays in Python import nunpy as np arr = [Link]([1, 2, 3, 4, 5]) result = [] # Using a Loop to square each element (slow) for num in arr: [Link](nun ** 2) print(result) 2, 4, 9, 16, 25) __This works, but i's not efficient. Each loop iteration is slow, especially with large datasets._ Vectorization - Fixing the Loop Problem Vectorization allows you to perform operations on entire arrays at once, instead of iterating over elements one by one. This is made possible by NumPy's optimized C-based backenc that executes operations in compiled code, which is much faster than Python loops. Vectorized operations are also more readable and compact, making your code easier to maintain. localhost B888abitree/Using_ Numpy!06_proadcastingipynb? 4 516725, 8:08 PM 06 broadcasting Example - Vectorized Operation import numpy as np arr = [Link]([1, 2, 3, 4 5]) result = arr ** 2 print("Vectorized Array -", result) Vectorized Array - [1 4 9 16 25] Here, the operation is applied to all elements of the array simultaneously, and i's much faster than looping over the array. Why is it faster? * __Low-level implementation -__ NumPy's vectorized operations are implemented in C {compiled language), which is much faster than Python loops * __Batch Processing -__ NumPy processes multiple elements in parallel using SIMD {Single Instruction, Multiple Data), allowing multiple operations to be done simultaneously. Broadcasting - Scaling Arrays Without Extra Memory Broadcasting is a powerful feature of NumPy that allows you to perform operations on arrays of different shapes without creating copies. It “stretches” smaller arrays across larger arrays in a memory-efficient way, avoiding the overhead of creating multiple copies of data Example - Broadcasting with Scalar Broadcasting is often used when you want to perform an operation on an array and a scalar value (e.g, add a number to all elements of an array) import nunpy as np are = [Link]([1, 2, 3, 4 5]) result = arr + 10 print(result) # The scalar 1@ is broadcasted across the entire # array, and no extra memory is used. [11 12 13 14 15] Broadcasting with Arrays of Different Shapes localhost B888abitree/Using_ Numpy!06_proadcastingipynb? 24 516725, 8:08 PM 06, broadcasting Broadcasting becomes more powerful when you apply operations on arrays of different shapes. NumPy automatically adjusts the shapes of arrays to make them compatible for element-wise operations, without actually copying the data Example - Broadcasting with Two Arrays import numpy as np arr = [Link]([1, 2, 3]) arr2 = [Link]([4, 5, 6]) result = arrl + arr2 # NumPy autonaticaLly aligns the two arrays and # performs element-wise addition, treating them as if they have the sane shape. print (result) 1579 Example - Broadcasting a 2D Array and a 1D Array import numpy as np arrl = [Link](({1, 2, 3], [4, 5, 6]]) arr2 = [Link]({1, 2, 3]) result = arr + arr2 # Broadcasting arr2 across arr, adding # (1, 3] to each row. print("Broadcasted array -\n", result) Broadcasted array - ([2 4 6] [57 9]] How Broadcasting Works? ® __Dimensions must be compatible -__ The size of the trailing dimensions of the arrays must be either the same or one of them must be 1 * _ Stretching arrays -__ Ifthe shapes are compatible, NumPy stretches the smaller array to match the larger one, element-wise, without copying data Hands-on - Applying Broadcasting to Real-World Scenarios Broadcasting can be used in data science tasks, such as normalizing datasets (__Scaling date in machine learning_), without sacrificing memory or performance Example - Normalizing Data Using Broadcasting localhost B888abitree/Using_ Numpy!06_proadcastingipynb? aa 516725, 8:08 PM 06, broadcasting Imagine you have a dataset where each row represents a sample and each column ‘epresents a feature. You can normalize the data by subtracting the mean of each columr and dividing by the standard deviation import numpy as np # Simulating a dataset (5 samples, 3 features) data = [Link]([[1@, 20, 36], [a5, 25, 35], [20, 3@, 40], [25, 35, 45], [3e, 4@, 58]]) # Calculating mean and standard deviation for each feature (column) mean = [Link](axis = 0) std = [Link](axis = @) # Normalizing the data using broadcasting normalized data = (data - nean)/std print(normalized data) # Broadcasting allows you to subtract the mean # and divide by the standard deviation for each feature without needing # Loops or creating copies of the data. [[-1.41421356 -1.41421356 -1.41421356] 70718678 -0.70710678 -2.70710678) . e. e. 1 70710678 0.70710678 @.70710678) 41421356 1.41421356 1.41421356]] localhost B888abitree/Using_ Numpy!06_proadcastingipynb? 4 7 _bull_p_rrathematical functions Built in Mathematical Functions in NumPy Some common NumPy methods that are frequently used for statistical and mathematical operations - * __npmean() =__ Compute the __mean (average)__ of an array * __npstd0 -—__ Compute the __standard deviation__of an array ® __npvar() -__Compute the _variance__ of an array. + __np.min) —_ Compute the __minimum value__ of an array. * __np.max() —__ Compute the _maximum value__of an array. * __npsumi) -__ Compute the __sum of all elements__ in an array. * __npprodd -__ Compute the __product of all elements__in an array. * __npmedian)) —_ Compute the _median__ of an array. + __nppercentile() —__ Compute the __percentile__of an array. + __npargmind -__ Return the __index of the minimum value__ in an array. * __npargmax() -__ Return the __index of the maximum value__ in an array. + __np.corrcoeff) -__ Compute the __correlation coefficient matrix of two arrays_. *® __np.unique() -__ Find the __unique elements__ of an array. * __np.iff) —_ Compute the __n-th differences__ of an array. + __np.cumsum( -__ Compute the _cumulative sum__ of an array * __nplinspace() -__ Create an array with __evenly spaced numbers over a specified interval__ * __nplog( -__ Compute the __natural logarithm__ of an array * __npexp)-__ Compute the __exponential__of an array. import numpy as np arr = [Link]([23, 21, 5, 19, 16]) brr = [Link]([1, 2, 3, 4, 5]) # Mean print("Mean -", [Link](arr)) # Standard Deviation print("\nStandard Deviation ", [Link](are)) # Variance print(*\nVariance -" [Link](arr)) # Minimum Value print(*\nMinimum Value - ", [Link](arr)) # Maximum Value print(*\nMaximun Value -", [Link](arr)) # Sum all eLements localhost B888abiree/Using_ Numpy/07_bult_i_mathematical functions ipynb? 18 516725, &:09 PM 7 _bull_p_rrathematical functions print("\nSum OF all elements -", [Link](arr)) # Product of all elements print("\nProduct of all elements -", [Link](arr)) # Median print("\nMedian -", [Link](arr)) # For the Seth percentile (median) print("\nS@th percentile -", np-percentile(arr, 50) # Index of minimum print("\nMinimun Index -", [Link](arr)) # Index of maximum print("\nMaximun Index -", [Link](arr)) # Coefficient of correlation matrix print("\nCorrelation Coefficient matrix -\n", [Link](arr, brr)) # Unique Elements print("\nUnique Elements. - ", [Link](arr)) # nth differences of an array print("\nnth Difference -", [Link](arr)) # arr[iv1] - arr[i] for n times, n if not entered is taken by default = 1 # CumuLative sum print("\nCunulative Sum - > np-cumsum(arr)) # Creating an array with evenly spaced numbers over a specified interval print("\névenly spaced array -", [Link](@, 10, 5)) #5 numbers from 0 to 10 # Natural. Logarithm print("\nNatural log -", np-log(arr)) # Exponential, print(*\nExponential -", np-exp(arr)) localhost B888abiree/Using_ Numpy/07_bult_i_mathematical functions ipynb? 516725, &:09 PM 7 _bull_p_rrathematical functions Mean - 16.8 Standard Deviation - 6.337191807101944 Variance - 40.160000000000004 Minimum Value - 5 Maximum Value - 23 Sum Of all elements - 84 Product of all elements - 734160 Median - 19.¢ Seth percentile - 19.0 Minimum Index - 2 Maximum Index - @ Correlation Coefficient matrix - (la. -0.35705747] [-0.35705747 1. v Unique Elements - [ 5 16 19 21 23 nth Difference - [ -2 -16 14-3 Cumulative Sum - [23 44 49 68 84] Evenly spaced array - [ 0. 2.5 5. Natural log - [3.13549422 3.04452244 1. 7.5 10. 60943791 2.94443898 2.77258872] Exponential - [9.74480345e+09 1.31881573e+89 1.48413159e+02 1.78482301¢+08 8,886110520+06] localhost B888abiree/Using_ Numpy/07_bult_i_mathematical functions ipynb?

You might also like