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.
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
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.
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
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