Saturday, October 24, 2026

Python NumPy: From Beginner to Advanced - Part 2

In Part 1, we covered the basics of NumPy, including mean(), min(), max(), array slicing, indexing, shape, and more.

If you haven't completed Part 1 yet, I recommend practicing those concepts first before moving on to Part 2.

numpy part2

Python Numpy Part 2

Filtering

"Filter" word is a very familiar to understand that we get the value that we need for data analysis or other tasks.

In other words, filter() is a python built-in function that helps us select elements from an iterable based on condition.

import numpy as np

number = np.array([12, 52, 41, 61, 34, 54])
print(number[number > 30])

In this example, we filter the data to include only numbers greater than 30. Similarly, we can apply other mathematical functions to obtain the desired output.

Filter + Average

import numpy as np

number = np.array([32, 41, 23, 56, 37, 85, 91])
new_num = number[number > 50]

print(new_num)
print(new_num.mean())

Aggregation

It is use to find the result of given array. We already learn about these functions in the part -1.
import numpy as np

numbers = np.array([10, 20, 30, 40, 50])

print("Sum:", np.sum(numbers))
print("Mean:", np.mean(numbers))
print("Maximum:", np.max(numbers))
print("Minimum:", np.min(numbers))
print("Standard Deviation:", np.std(numbers))
Note: 
  • np.min(numbers): It take Array as argument in Numpy function 
  • numbers.min(): It directly apply on Array.
Both provides the same output so keep remember both senerios.

The last output "Standard Deviation", It indicates how spread out the values ​​are from their average. A low standard deviation means the values ​​are close to the average, while a higher value indicates that they are more spread out.

Broadcasting

In broadcasting, we can apply mathematical operations to be performed on arrays of different shapes without manually changing their structure. Also provided shapes must follow broadcasting rules..
import numpy as np

a = np.array([10, 20, 30]) b = np.array([1, 2, 3]) print(a + b)

Random Numbers

It is used in simulations, testing, and machine learning.


No comments:

Post a Comment

Featured Post

Python OOPs Advanced: Class Variables and Instance Variables

We have learned how to create classes and objects . Now, we will explore class variables and instance variables in Python, including how the...