NumPy: Numerical Computing Library
Introduction
NumPy is a powerful Python library for numerical computing, enabling efficient and fast array
operations. It is extensively used for data science, machine learning, scientific computing, and
more. This cheat sheet covers the key concepts, with practical examples of input and output,
and demonstrates various methods for different tasks, from basic array operations to
advanced machine learning applications.
Installing NumPy
You can install NumPy using either pip or conda:
• Pip:
python
pip install numpy
• Conda
python
conda install numpy
Arrays in NumPy
Array Creation:
• 1D array: It’s a simple one-dimensional array with three elements, as follows:
python
import numpy as np
arr = [Link]([1, 2, 3])
print("1D Array:", arr)
NUMPY: NUMERICAL COMPUTING LIBRARY 01
• Input:
python
[1, 2, 3]
• Output:
python
[1 2 3]
• 2D array: It’s a two-dimensional array with 2 rows and 2 columns, as follows:
python
arr_2d = [Link]([[1, 2], [3, 4]])
print("2D Array:", arr_2d)
• Input:
python
[[1, 2], [3, 4]]
• Output:
python
[[1 2]
[3 4]]
• 3D array: It’s a three-dimensional array with nested arrays, as follows:
python
arr_3d = [Link]([[[1], [2]], [[3], [4]]])
print("3D Array:", arr_3d)
NUMPY: NUMERICAL COMPUTING LIBRARY 02
• Input:
python
[[[1], [2]], [[3], [4]]]
• Output:
python
[[[1]
[2]]
[[3]
[4]]]
NumPy Basics
Basic Indexing
• Accessing elements in a 1D array: Access the first element of a 1D array.
python
arr = [Link]([1, 2, 3])
print("First Element:", arr[0])
• Input:
python
[1, 2, 3]
• Output:
python
NUMPY: NUMERICAL COMPUTING LIBRARY 03
• Accessing elements in a 2D array: Access the element in the first row and second
column.
python
arr_2d = [Link]([[1, 2], [3, 4]])
print("Element in (0, 1):", arr_2d[0, 1])
• Input:
python
[[1, 2], [3, 4]]
• Output:
python
Array Attributes
Shape, size, and data type: Get the shape, size, and data type of a 2D array.
python
print("Shape:", arr_2d.shape)
print("Size:", arr_2d.size)
print("Data Type:", arr_2d.dtype)
• Input:
python
[[1, 2], [3, 4]]
NUMPY: NUMERICAL COMPUTING LIBRARY 04
• Output:
python
Shape: (2, 2)
Size: 4
Data Type: int64
Advanced NumPy Array Operations
Array Indexing and Selection
• Subsetting with conditions: Select only positive values from the array.
python
arr = [Link]([1, -2, 3, -4])
positive_values = arr[arr > 0]
print("Positive Values:", positive_values)
• Input:
python
[1, -2, 3, -4]
• Output:
python
[1 3]
• Fancy indexing: Use an index array to select specific elements.
python
arr = [Link]([1, 2, 3, 4, 5])
selected_values = arr[[0, 2, 4]]
print("Selected Values:", selected_values)
NUMPY: NUMERICAL COMPUTING LIBRARY 05
• Input:
python
[1, 2, 3, 4, 5]
• Output:
python
[1 3 5]
Array Manipulation
• Transpose of an array: The transpose flips the array over its diagonal.
python
arr_2d = [Link]([[1, 2], [3, 4]])
transposed = arr_2d.T
print("Transposed Array:", transposed)
• Input:
python
[[1, 2], [3, 4]]
• Output:
python
[[1 3]
[2 4]]
• Reshape array: Change the shape of an array.
python
arr = [Link]([1, 2, 3, 4, 5, 6])
reshaped = [Link](2, 3)
print("Reshaped Array:", reshaped)
NUMPY: NUMERICAL COMPUTING LIBRARY 06
• Input:
python
[1, 2, 3, 4, 5, 6]
• Output:
python
[[1 2 3]
[4 5 6]]
Broadcasting in NumPy
What Is Broadcasting?
Broadcasting allows NumPy to perform operations on arrays of different shapes by
automatically expanding the smaller array. Here’s a visual aid to help explain the concept:
Diagram:
x (3 x 3) y (3) Result (3 x 3)
0 0 0 20 21 22 20 21 22
1 2 3 + 20 21 22 = 21 23 25
4 5 6 20 21 22 24 26 28
python
import numpy as np
# Example 1: Broadcasting a 1D array to a 2D array
a = [Link]([1, 2, 3])
b = [Link]([[1], [2]])
Code continues on the next page...
NUMPY: NUMERICAL COMPUTING LIBRARY 07
python
# Broadcasting a to match the shape of b
a_broadcasted = [Link](a, (2, 1)) # Repeat 'a' twice along axis 0 (rows)
# Calculate result
result = a_broadcasted + b
print("Array a (1D): ", a)
print("Array b (2D): ", b)
print("After Broadcasting:")
print("Array a:", a_broadcasted)
print("Array b:", b)
print("Result: ", result)
#Example 2: Broadcasting a 1D array with a scalar
scalar = 5
result = a + scalar
print("\nScalar value:", scalar)
print("Array a:", a)
print("Result of broadcasting a scalar and an array: ", result)
• Output:
python
Result: [6 7 8]
Broadcasting Example
• Adding Arrays of Different Shapes:
python
a = [Link]([1, 2, 3])
b = [Link]([[1], [2]])
result = a + b
print("Broadcasted Result:", result)
• Input:
python
a = [1, 2, 3]
b = [[1], [2]]
NUMPY: NUMERICAL COMPUTING LIBRARY 08
• Output:
python
[[2 3 4]
[3 4 5]]
NumPy for Statistics
Normal Distribution
• Generate random numbers from a normal distribution.
python
mean, std_dev, size = 0, 1, 1000
data = [Link](mean, std_dev, size)
print("Normal Distribution Sample:", data[:5])
• Output
python
Normal Distribution Sample: [ 0.21823085 -0.21952287 -1.13819249 -0.43916598 0.02013997]
Standard Deviation
• Compute the standard deviation.
python
print("Standard Deviation:", [Link](data)) # Example: ~1.0
• Output
python
Output: Standard Deviation: 1.0203029981828742
Code continues on the next page...
NUMPY: NUMERICAL COMPUTING LIBRARY 09
Histogram Visualization
• Use [Link] to get bin counts.
python
counts, bins = [Link](data, bins=10)
print("Histogram Counts:", counts)
print("Histogram Bins:", bins)
• Output
python
Histogram Counts: [ 3 14 55 146 243 257 176 78 18 10]
Histogram Bins: [-3.52374524 -2.84559269 -2.16744015 -1.4892876 -0.81113505 -0.13298251 0.54517004 1.22332258
1.90147513 2.57962768 3.25778022]
Mean and Axis-Specific Mean
• Compute the overall mean.
python
print("Mean:", [Link](data)) # Example: ~0.0
Mean: -0.040803288331032334
• Compute mean along a specific axis.
python
arr = [Link]([[1, 2, 3], [4, 5, 6]])
print("Mean (axis=0):", [Link](arr, axis=0)) # [2.5, 3.5, 4.5]
Mean (axis=0): [2.5 3.5 4.5]
NUMPY: NUMERICAL COMPUTING LIBRARY 10
Percentiles and Quartiles
• Compute percentiles.
python
print("50th Percentile:", [Link](data, 50)) # Median
50th Percentile: -0.020241045615570108
• Compute quartiles.
python
print("25th and 75th Percentiles:", [Link](data, [25, 75])) # [Q1, Q3]
25th and 75th Percentiles: [-0.71818199 0.61807325]
NumPy for Data Scientists
NumPy: I/O and Data Handling
I/O: Input and Output
Save to the disk:
• Save a NumPy array to a .npy file.
python
import numpy as np
arr = [Link]([1, 2, 3, 4, 5])
[Link]('array_file.npy', arr)
print("Array saved to 'array_file.npy'")
Load from the disk
• Load a NumPy array to a .npy file.
python
loaded_arr = [Link]('array_file.npy')
print("Loaded Array:", loaded_arr)
NUMPY: NUMERICAL COMPUTING LIBRARY 11
Save/Load text:
• Save a NumPy array to a text file.
python
[Link]('array_file.txt', arr, fmt='%d')
print("Array saved to 'array_file.txt'")
• Load the array back from the text file.
python
loaded_text_arr = [Link]('array_file.txt', dtype=int)
print("Loaded Text Array:", loaded_text_arr)
Summarize the Data
Sum with axis:
• Compute the sum across specific axes.
python
arr_2d = [Link]([[1, 2, 3], [4, 5, 6]])
print("Sum (Axis=0):", [Link](arr_2d, axis=0)) # [5, 7, 9]
print("Sum (Axis=1):", [Link](arr_2d, axis=1)) # [6, 15]
Handling the Missing Data
Replace NaN:
• Replace NaN values with a specified number.
python
arr_with_nan = [Link]([1, [Link], 2, [Link], 3])
cleaned_arr = np.nan_to_num(arr_with_nan, nan=0.0)
print("Original Array with NaN:", arr_with_nan)
print("Cleaned Array:", cleaned_arr)
NUMPY: NUMERICAL COMPUTING LIBRARY 12
Group the Data
Group mean:
• Compute group means using slicing or indexing.
python
data = [Link]([10, 20, 30, 40, 50])
groups = [Link]([0, 0, 1, 1, 1]) # Group labels
group_means = [data[groups == g].mean() for g in [Link](groups)]
print("Group Means:", group_means)
Sliding Windows
Create sliding windows:
• Generate sliding windows using slicing.
python
arr = [Link]([1, 2, 3, 4, 5, 6])
window_size = 3
sliding_windows = [Link]([arr[i:i + window_size] for i in range(len(arr) - window_size + 1)])
print("Sliding Windows:\n", sliding_windows)
• Output
python
[[1 2 3]
[2 3 4]
[3 4 5]
[4 5 6]]
Histograms
A histogram is a graphical representation of the distribution of data.
• 1D histogram: Plot a histogram for a 1D array of random data.
python
import [Link] as plt
data = [Link](1000)
Code continues on the next page...
NUMPY: NUMERICAL COMPUTING LIBRARY 13
python
[Link](data, bins=30, alpha=0.7)
[Link]('1D Histogram')
[Link]()
• Input:
python
[Link](1000)
• Output: A histogram plot representing the distribution of values in the data.
1D Histogram
80
60
40
20
0
-3 -2 -1 0 1 2 3
• 2D histogram: Plot a histogram for a 1D array of random data.
python
x = [Link](1000)
y = [Link](1000)
plt.hist2d(x, y, bins=30, cmap='Blues')
[Link]('2D Histogram')
[Link]()
[Link]()
NUMPY: NUMERICAL COMPUTING LIBRARY 14
• Input:
python
[Link](1000)
[Link](1000)
• Output: A 2D histogram plot showing the relationship between x and y.
2D Histogram
12
3
10
2
8
1
0 6
-1 4
-2 2
-3 0
-2 -1 0 1 2
NUMPY: NUMERICAL COMPUTING LIBRARY 15
NumPy for Machine Learning Researchers
Linear Algebra
• Matrix multiplication:
python
A = [Link]([[1, 2], [3, 4]])
B = [Link]([[5, 6], [7, 8]])
result = [Link](A, B)
print("Matrix Multiplication Result:", result)
• Input:
python
A = [[1, 2], [3, 4]]
B = [[5, 6], [7, 8]]
• Output:
python
Matrix Multiplication Result: [[19 22]
[43 50]]
• Solving linear equations: Solve a system of linear equations using [Link]().
python
A = [Link]([[3, 1], [1, 2]])
b = [Link]([9, 8])
x = [Link](A, b)
print("Solution of the System:", x)
• Input:
python
A = [[3, 1], [1, 2]]
b = [9, 8]
NUMPY: NUMERICAL COMPUTING LIBRARY 16
• Output:
python
Solution of the System: [2. 3.]
Common Mistakes
• Shape mismatch in operations:
◦ Ensure array dimensions align for operations like addition or multiplication.
◦ Example:
python
arr1 = [Link]([1, 2, 3])
arr2 = [Link]([[1], [2], [3]])
result = arr1 + arr2 # Works because of broadcasting
• Unintentional data type casting:
• Pay attention to NumPy’s default types (e.g., integers in division can truncate).
python
arr = [Link]([1, 2, 3])
print(arr / 2) # Automatically converts to float
• Using built-in Python lists instead of NumPy arrays:
◦ Mixing Python lists with NumPy arrays can cause unexpected errors.
python
arr = [Link]([1, 2, 3])
print(arr + [4, 5, 6]) # Works because of broadcasting
• Confusing axis definitions:
◦ Remember, axis=0 refers to rows (vertical operations), while axis=1 refers to columns
(horizontal operations).
NUMPY: NUMERICAL COMPUTING LIBRARY 17
Troubleshooting Tips
• Debugging array shapes:
◦ Use [Link] to inspect dimensions.
◦ Use [Link]() to fix shape issues.
• Check for missing values:
◦ Use [Link](arr).any() to check for NaN values.
• Inspect data types:
◦ Use [Link] to confirm the array type.
• Visualize intermediate results:
◦ Print arrays or use matplotlib to plot results for verification.
Key Takeaways
• NumPy is a versatile library for numerical computations, offering features like:
◦ Efficient array operations
◦ Broadcasting and array manipulation
◦ Comprehensive mathematical, statistical, and random data capabilities
◦ Integration with visualization libraries like Matplotlib
Next Steps
• Explore advanced topics:
◦ Linear algebra
◦ Fourier transforms
◦ Statistical modeling
• Dive into related libraries:
◦ pandas: For data manipulation and analysis
◦ Matplotlib: For advanced data visualization
◦ scikit-learn: For machine learning applications
• Practice with real-world projects:
◦ Work on datasets and apply NumPy concepts to solidify learning.
NUMPY: NUMERICAL COMPUTING LIBRARY 18