NumPy for Data Science – Part 4

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.

NumPy for Data Science – Part 1

NumPy for Data Science – Part 2

NumPy for Data Science – Part 3

Popular Posts

Author

  • Naveen Pandey Data Scientist Machine Learning Engineer

    Naveen Pandey has more than 2 years of experience in data science and machine learning. He is an experienced Machine Learning Engineer with a strong background in data analysis, natural language processing, and machine learning. Holding a Bachelor of Science in Information Technology from Sikkim Manipal University, he excels in leveraging cutting-edge technologies such as Large Language Models (LLMs), TensorFlow, PyTorch, and Hugging Face to develop innovative solutions.

    View all posts
Spread the knowledge
 
  

Leave a Reply

Your email address will not be published. Required fields are marked *