Lesson · 40 min · Free
NumPy Basics
NumPy Basics NumPy (Numerical Python) is the foundational library for scientific computing in Python. While regular Python lists are flexible, they are slow for large-scale numerical work. NumPy introduces the ndarray (n
NumPy Basics
NumPy (Numerical Python) is the foundational library for scientific computing in Python. While regular Python lists are flexible, they are slow for large-scale numerical work. NumPy introduces the ndarray (n-dimensional array), a data structure that stores elements of a single type in contiguous memory, enabling dramatically faster computations. For pharmacy and biotech students, this matters because real datasets—drug concentration measurements, genomic expression values, clinical trial results—often contain thousands to millions of numbers that must be processed efficiently. The core advantage of NumPy is vectorization : instead of writing loops to apply an operation to every element, you perform the operation on the entire array at once. For example, converting a list of drug concentrations from mg/L to μg/L requires a single multiplication rather than a for-loop. This makes code shorter, more readable, and significantly faster because the heavy lifting happens in optimized compiled code rather than interpreted Python. NumPy also provides powerful tools for creating arrays, generating descriptive statistics, and performing element-wise mathematical operations. These capabilities form the basis for higher-level libraries you will encounter later, such as pandas for data analysis and matplotlib for plotting. A solid understanding of NumPy is therefore essential before moving into data science applications in the pharmaceutical sciences. The first example below shows how to import NumPy, create arrays from Python lists, and perform vectorized arithmetic and statistics: import numpy as np # Create a 1D array of drug concentrations (mg/L) concentrations = np.array([2.5, 7.1, 12.8, 4.3, 9.6]) # Vectorized operations (no loops needed) conc_ug = concentrations * 1000 # convert mg/L to ug/L mean_conc = np.mean(concentrations) # average concentration std_conc = np.std(concentrations) # standard deviation print(conc_ug) # [ 2500. 7100. 12800. 4300. 9600.] print(mean_conc) # 7.26 print(std_conc) # ~3.68 The second example demonstrates two-dimensional arrays, which are ideal for representing tables such as patient records or dose-response data. It also shows array indexing, slicing, and computing statistics along an axis: import numpy as np # 2D array: rows = patients, columns = plasma concentrations at 3 time points data = np.array([ [1.2, 3.4, 2.8], [0.9, 2.7, 3.1], [1.5, 4.1, 3.6], [1.1, 3.0, 2.5] ]) # Access a single element: patient 2 (index 1), time point 3 (index 2) print(data[1, 2]) # 3.1 # Slice the first two patients and first two time points print(data[0:2, 0:2]) # [[1.2 3.4] # [0.9 2.7]] # Mean concentration per patient (across columns, axis=1) patient_means = np.mean(data, axis=1) print(patient_means) # [2.47 2.23 3.07 2.2 ] Key takeaways: NumPy's ndarray stores homogeneous data and enables fast, memory-efficient numerical computation. Vectorized operations apply calculations to entire arrays at once, replacing explicit Python loops. Arrays can be multi-dimensional : 1D arrays represent sequences (e.g., concentrations over time), while 2D arrays represent tables (e.g., patients × measurements). Use indexing and slicing (e.g., data[1, 2] , data[0:2, 0:2] ) to access subsets of data. Functions like np.mean() , np.std() , and the axis parameter let you compute summary statistics across rows or columns—essential for analyzing experimental data. Practice exercise: Create a NumPy array representing the hourly plasma concentrations (in ng/mL) of a drug for five patients over four hours, using values of your choice. Compute the overall mean concentration, the standard deviation across all measurements, and the maximum concentration for each patient using np.max() with the appropriate axis argument. Then convert the entire array to μg/mL by dividing by 1000 and print the result. Verify your answers by checking a few values manually.
Watch the full lesson — free
This topic is part of The Complete Python Bootcamp: Basics to Pharma & Data Science, a complete AI-narrated video course. Press play once and watch the entire lecture like a movie.
Start the course free →