Posts

Showing posts with the label Aggregation

NumPy Array Operations: Slicing, Indexing, and Reshaping

Essential NumPy Array Operations NumPy arrays are the backbone of scientific computing in Python. These operations are fundamental: 1. Element-wise Operations These apply functions or operations to each element of an array individually, or between corresponding elements of two arrays. They are much faster than Python loops. Scalar Operations: Python import numpy as np arr = np.array([ 1 , 2 , 3 ]) print( "Original:" , arr) print( "Add 5:" , arr + 5 ) # Output: [6 7 8] print( "Multiply by 2:" , arr * 2 ) # Output: [2 4 6] print( "Square:" , arr ** 2 ) # Output: [1 4 9] Array-Array Operations: Python arr1 = np.array([ 1 , 2 , 3 ]) arr2 = np.array([ 4 , 5 , 6 ]) print( "Arr1 + Arr2:" , arr1 + arr2) # Output: [5 7 9] print( "Arr1 * Arr2:" , arr1 * arr2) # Output: [ 4 10 18] 2. Aggregation Functions Aggregations perform a calculation on an array (or a part of it) to return a single value. Function Description sum() Su...