NumPy is widely used in Data Science because it provides an extensive set of built-in mathematical and statistical functions. These functions make it incredibly simple to analyze datasets and calculate key metrics such as averages, totals, variances, and more.
Common Statistical Functions Overview
| Function | Description |
|---|---|
np.max() / np.amax() | Returns the maximum value in the array. |
np.min() / np.amin() | Returns the minimum value in the array. |
np.mean() | Calculates the arithmetic mean (average) of the array. |
np.median() | Finds the median value (middle element) of the data. |
np.average() | Computes the weighted average (defaults to mean if no weights given). |
np.sum() | Calculates the total sum of all elements in the array. |
np.prod() | Calculates the product of all elements in the array. |
np.std() | Calculates the standard deviation (spread of data). |
np.var() | Calculates the variance (squared standard deviation). |
np.argmax() | Returns the index of the maximum value. |
np.argmin() | Returns the index of the minimum value. |
np.percentile() | Computes the nth percentile of the data. |
Analyzing Data: A Practical Example
Let's use a real-world scenario. Imagine we have a list of marks scored by a student in different subjects. We can convert this list into a NumPy array and use statistical functions to analyze the student's performance.
1. Finding Maximum and Minimum Marks
Output:
2. Finding Mean, Median, and Average
Output:
3. Finding Total and Product
Output:
Advanced Statistical Operations
Beyond the basics, NumPy allows you to dig deeper into the distribution and spread of your data.
Standard Deviation & Variance
The standard deviation (np.std) tells you how spread out the numbers are from the mean. The variance (np.var) is simply the standard deviation squared.
Output:
Finding Indices of Max/Min Values
Sometimes you don't just want to know what the highest mark was, but where it is located in the array. argmax() and argmin() return the index positions.
Percentiles
Percentiles are used in statistics to give you a number that describes the value that a given percentage of the values are lower than.
Output: