NumPy for Data Science – Part 4
- Naveen
- 0
Broadcasting NumPy Arrays
The term broadcasting describes how NumPy treats arrays with different shapes during arithmetic operations. Subject to certain constraints, the smaller array is “broadcast” across the larger array in order that they have compatible shapes.
NumPy operations are usually done on pairs of arrays on an element-by-element basis. Within the simplest case, the 2 arrays must have exactly the same shape, as within the following example:
Indexing and Slicing
Array indexing is that the same as accessing an array element.
You can access an array element by referring to its index number.
The indexes in NumPy arrays start with 0, meaning that the primary element has index 0, and therefore the second has index 1 etc.
Slicing in python means taking elements from one given index to a different given index.
We pass slice rather than of index like this: [start:end].
We can also define the step, like this: [start:end:step].
If we do not pass start its considered 0
If we do not pass end its considered length of array in that dimension
If we do not pass step its considered 1
Iterating NumPy Arrays
Iterating means going through elements one by one.
As we deal with multi-dimensional arrays in numpy, we can do this using basic for loop of python.
If we iterate on a 1-D array it’ll go through each element one by one.
If we iterate on 2-D array than we have to use two for loops to get all the elements one by one.
If we iterate on 3-D array than we have to use three for loops to get all the elements one by one.