Learn practical skills, build real-world projects, and advance your career

Basic Statistical Analysis with NumPy

What is NumPy

NumPy (short for Numerical Python) is an Open Source Python library used for working with numerical data in Python. It provides an efficient way to store and make operations on big datasets. According to the NumPy User Guide:

"It is a Python library that provides a multidimensional array object, various derived objects (such as masked arrays and matrices), and an assortment of routines for fast operations on arrays, including mathematical, logical, shape manipulation, sorting, selecting, I/O, discrete Fourier transforms, basic linear algebra, basic statistical operations, random simulation and much more."

NumPy arrays, similar but much more powerful than Python Lists, are at the core of many libraries in the Python Data Science ecosystem.

In this post we'll explore the following 5 NumPy functions used in statistical analysis:

  • np.amin
  • np.amax
  • np.ptp
  • np.percentile
  • np.quantile

These functions will help us analize the daily Wind Power Generation in an european country during a recent year and answer the following questions:

  • Which was the minimum daily Wind Power Generation in the period?
  • Which was the maximum daily Wind Power Generation in the period?
  • Below which value was the daily Wind Power Generation 10% of the time?
  • Below which value was the daily Wind Power Generation 50% of the time?
  • Over which value was the daily Wind Power Generation 20% of the time?

Let's beging by importing the python libraries we'll use:

import numpy as np
import matplotlib.pyplot as plt
# Our list of functions:
function1 = np.amin
function2 = np.amax
function3 = np.ptp
function4 = np.percentile
function5 = np.quantile

Our data will be loaded as a list manually:

input = [674080, 1095937, 1582607, 2729042, 2840247, 3265314, 1725902, 1786428, 1118446, 1135218, 1325941, 1408763, 750943, 2243894, 2316739, 2748082, 3433, 2491, 1814541, 1399721, 949638, 1708579, 1349892, 2165590, 1945, 635170, 746536, 699987, 683635, 1093312, 2188, 2954329, 2504, 1208531, 556, 515215, 758934, 1760, 2477503, 1264094, 1098558, 905078, 461, 1171, 645978, 663254, 555053, 1726, 2189329, 1494792, 1400472, 2052732, 1323719, 1259, 1644169, 1713990, 150128, 538, 1127472, 2584630, 1297308, 252519, 941836, 2196644, 3570985, 2655202, 847079, 552745, 1434393, 1106, 1195031, 1733314, 3542344, 3541780, 3814, 3139097, 2279040, 1920043, 1601, 2346, 3455941, 3594172, 1900702, 2191, 1750372, 2313166, 2713933, 1815878, 1727236, 2540055, 1940638, 1242545, 577763, 610916, 636737, 1410938, 2757906, 1771760, 892894, 1630294, 3206355, 3329936, 1315, 1635008, 2033511, 2387, 2207490, 2551044, 2157887, 3265637, 1907692, 3808693, 3539310, 1922211, 2398, 2175, 1185, 909946, 1530, 1026, 456303, 834946, 716272, 958145, 2294854, 3382530, 3559372, 3489693, 3743080, 3799883, 2428, 2746715, 2858, 1585129, 1261466, 936319, 1832285, 3453813, 4023937, 4305491, 3967767, 2475515, 1152997, 2788011, 3885309, 3569490, 3850814, 3378164, 3082, 3590302, 4108579, 3649090, 2730932, 3529779, 1903, 1334104, 2645477, 3576884, 3095970, 1212906, 1164702, 957188, 1359086, 2303137, 1668856, 2520292, 1391204, 3851086, 3136759, 3842140, 3350296, 1027372, 2567762, 911684, 1232, 1606236, 279532, 1001, 2806465, 2422821, 2044, 1798334, 1811241, 1897719, 1681, 1594009, 2728671, 1296011, 596863, 1242246, 1928197, 1889340, 644828, 2048262, 1685485, 688955, 664292, 679062, 419707, 360105, 712221, 2251, 1451053, 1359329, 696077, 386012, 931, 1728847, 2625081, 3035390, 2340562, 651869, 1359470, 876222, 190546, 380, 533904, 831560, 1808060, 1659516, 1429541, 596995, 711191, 996844, 974072, 579909, 280711, 291620, 1022, 2628870, 1220783, 721749, 863, 1218796, 1195457, 1298739, 1183643, 799039, 183955, 259918, 1312695, 1792080, 2103334, 1689081, 188, 541767, 1075262, 596545, 1044618, 1265863, 1072807, 466969, 705133, 965, 1813718, 2156, 1113418, 235084, 368364, 548982, 953660, 1141404, 1230034, 1152441, 611603, 121082, 343908, 609466, 575, 1037, 492363, 609519, 345517, 568752, 1010315, 759736, 686594, 1274, 1371430, 2240292, 1339170, 744080, 997510, 2472, 2644669, 2797913, 1301457, 567305, 693, 1463142, 812320, 523810, 282586, 464095, 234692, 220095, 203371, 137089, 287239, 634407, 648766, 377082, 389840, 1046968, 621617, 1383113, 832518, 2260888, 1747903, 630, 369111, 956945, 547688, 353781, 355390, 949209, 515, 325947, 248197, 547984, 830681, 1252744, 1251229, 903260, 366564, 255271, 549385, 491990, 347666, 263597, 815, 1475196, 1792490, 1938572, 955459, 1192306, 3846805, 1425328, 1367740, 884079, 627279, 523567, 366777, 240377, 1341845, 1271315, 1186319, 764882, 560009, 1543487, 1641342, 717604, 449210, 1020085, 1187249, 753381, 852857, 699721, 813, 1253579, 1395, 1255590, 1200550, 1244657, 1321652]