0% found this document useful (0 votes)
7 views4 pages

Numpynotes Grok

The document provides comprehensive notes on a NumPy course, highlighting its importance as a fundamental library for scientific computing and data science. Key features include fast multi-dimensional array operations, mathematical functions, and applications in various fields like machine learning and image processing. It also covers installation, array initialization, indexing, and practical examples to enhance understanding of NumPy's capabilities.

Uploaded by

Njan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views4 pages

Numpynotes Grok

The document provides comprehensive notes on a NumPy course, highlighting its importance as a fundamental library for scientific computing and data science. Key features include fast multi-dimensional array operations, mathematical functions, and applications in various fields like machine learning and image processing. It also covers installation, array initialization, indexing, and practical examples to enhance understanding of NumPy's capabilities.

Uploaded by

Njan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

numpynotes.

md 2025-05-16

Notes on NumPy Course (Based on YouTube Video Transcript)

Overview:

NumPy: Fundamental Python library for scientific computing, serving as the backbone for data science
libraries like Pandas.
Importance: Provides fast, efficient multi-dimensional array operations, critical for data science, machine
learning, and mathematical computations.

1. Why NumPy?

Speed: NumPy is significantly faster than Python lists due to:


Fixed Types: Uses compact data types (e.g., int32, int16) requiring fewer bytes than Python’s
built-in int (which includes object value, type, reference count, and size).
Example: NumPy int32 uses 4 bytes vs. Python int (~24 bytes).
Contiguous Memory: Stores data in adjacent memory blocks, unlike lists, which scatter data
with pointers.
Benefits:
Enables SIMD (Single Instruction, Multiple Data) vector processing for parallel
computations.
Better cache utilization, reducing memory lookup times.
No Type Checking: NumPy arrays have uniform types, eliminating per-element type checks
required in lists.
Flexibility: Supports multi-dimensional arrays (1D, 2D, 3D, etc.) and advanced mathematical operations.

2. Key Features & Applications

Multi-Dimensional Arrays: Store data in 1D (vectors), 2D (matrices), 3D, or higher-dimensional arrays.


Mathematical Operations:
Element-wise operations (e.g., addition, multiplication).
Linear algebra (matrix multiplication, determinants, eigenvalues).
Statistical functions (min, max, mean, sum).
Applications:
Replaces MATLAB for mathematical computations.
Backend for Pandas, image processing (e.g., PNG storage), and game boards (e.g., Connect 4).
Foundation for machine learning (tensors are similar to NumPy arrays).
Integration with SciPy for advanced mathematical functions.

3. Getting Started

Installation: pip install numpy or pip3 install numpy.


Import: import numpy as np.
Environment: Code demonstrated in Jupyter Notebook (available on GitHub).

4. Array Initialization

1/4
numpynotes.md 2025-05-16

Basic Arrays:
1D: np.array([1, 2, 3]) → [1, 2, 3].
2D: np.array([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]]) → 2x3 matrix.
3D+: Nest lists for higher dimensions.
Special Arrays:
Zeros: np.zeros((2, 3)) → 2x3 matrix of zeros.
Ones: np.ones((4, 2, 2), dtype='int32') → 4x2x2 array of ones.
Full: np.full((2, 2), 99, dtype='float32') → 2x2 matrix of 99s.
Full Like: np.full_like(a, 4) → Array with same shape as a, filled with 4s.
Random:
Decimals: np.random.rand(4, 2) → 4x2 array of random floats (0 to 1).
Integers: np.random.randint(4, 7, size=(3, 3)) → 3x3 array of integers between 4
and 6.
Identity: np.identity(3) → 3x3 identity matrix.
Repeat: np.repeat([[1, 2, 3]], 3, axis=0) → Repeats array along specified axis.

5. Array Attributes

Dimensions: a.ndim → Number of dimensions (e.g., 1 for 1D, 2 for 2D).


Shape: a.shape → Tuple of dimensions (e.g., (3,) for 1D, (2, 3) for 2x3).
Data Type: a.dtype → Type (e.g., int32, float64). Can specify: np.array([1, 2, 3],
dtype='int16').
Memory Usage:
Item Size: a.itemsize → Bytes per element (e.g., 4 for int32).
Total Size: a.size * a.itemsize or a.nbytes → Total bytes.

6. Accessing & Modifying Arrays

Indexing:
Element: a[1, 5] → Element at row 1, column 5 (0-based indexing).
Negative Indexing: a[-2, -2] → Second-to-last row, second-to-last column.
Row: a[0, :] → All columns in row 0.
Column: a[:, 2] → All rows in column 2.
Slicing: a[0, 1:6:2] → Row 0, elements 1 to 5, step by 2 (e.g., [2, 4, 6]).
3D: b[0, 1, 1] → Work outside-in for higher dimensions.
Modifying:
Element: a[1, 5] = 20 → Change element to 20.
Column: a[:, 2] = [1, 2] → Replace column with new values (same shape).
Subarray: b[1, :, :] = [[9, 9], [8, 8]] → Replace 2D slice (must match dimensions).

7. Copying Arrays

Issue: Direct assignment (b = a) creates a reference, not a copy. Modifying b changes a.


Solution: Use b = a.copy() to create an independent copy.

8. Mathematical Operations
2/4
numpynotes.md 2025-05-16

Element-Wise:
Arithmetic: a + 2, a * 2, a / 2, a ** 2 → Apply to each element.
Array Operations: a + b → Element-wise addition of arrays (same shape).
Functions: np.sin(a), np.cos(a) → Apply to all elements.
Linear Algebra:
Matrix Multiplication: np.matmul(a, b) → Multiplies matrices (e.g., 2x3 × 3x2 → 2x2).
Determinant: np.linalg.det(c) → Computes determinant (e.g., 1 for identity matrix).
Others: Eigenvalues, matrix inverse (see documentation).
Statistics:
Min/Max: np.min(stats), np.max(stats, axis=1) → Min/max overall or by axis.
Sum: np.sum(stats, axis=0) → Sum along columns.

9. Reorganizing Arrays

Reshape: before.reshape((8, 1)) → Change shape (must have same number of elements).
Stacking:
Vertical: np.vstack([v1, v2]) → Stack arrays vertically (same column count).
Horizontal: np.hstack([h1, h2]) → Stack arrays horizontally (same row count).

10. Loading Data

From File: np.genfromtxt('data.txt', delimiter=',') → Load data from text file (e.g., CSV).
Type Casting: file_data.astype('int32') → Convert array to specified type (creates copy if types
differ).

11. Advanced Indexing & Boolean Masking

Boolean Masking:
Condition: file_data > 50 → Array of True/False for elements > 50.
Indexing: file_data[file_data > 50] → Extract elements where condition is True.
Multiple Conditions: file_data[(file_data > 50) & (file_data < 100)].
Negation: file_data[~((file_data > 50) & (file_data < 100))].
List Indexing: a[[0, 1, 8]] → Select elements at indices 0, 1, 8.
Any/All:
np.any(file_data > 50, axis=0) → Check if any value in each column > 50.
np.all(file_data > 50, axis=1) → Check if all values in each row > 50.

12. Practical Example

Challenge: Create a 5x5 matrix with 1s, a 3x3 zero submatrix in the center, and a 9 in the middle.

output = np.ones((5, 5))


z = np.zeros((3, 3))
z[1, 1] = 9
output[1:4, 1:4] = z # or output[1:-1, 1:-1] = z

3/4
numpynotes.md 2025-05-16

Output:

[[1 1 1 1 1]
[1 0 0 0 1]
[1 0 9 0 1]
[1 0 0 0 1]
[1 1 1 1 1]]

13. Indexing Quiz

1. Index [[2, 3], [4, 5]] (rows 1-2, columns 0-1):


a[1:3, 0:2]
2. Index [[2, 3], [4, 5], [6, 7], [8, 9]] (rows 0-3, columns 1-2):
a[[0, 1, 2, 3], [1, 1, 1, 1]] or a[0:4, 1:2]
3. Index [[4, 5, 6], [8, 9, 10], [12, 13, 14]] (rows 0, 2, 3, columns 3+):
a[[0, 2, 3], 3:] or a[[0, 2, 3], 3:5]

14. Resources

GitHub: Code and data files (e.g., data.txt) available in video description.
Documentation: Links to NumPy array creation, math, linear algebra, and advanced indexing routines.
SciPy: For additional mathematical functions if NumPy is insufficient.

Tips:

Use Google for syntax errors (e.g., np.random.rand vs. tuple input).
Experiment with indexing and reshaping to build intuition.
Be cautious with copying to avoid unintended modifications.
Leverage NumPy’s speed and flexibility for large datasets and complex computations.

Conclusion: This course provides a comprehensive introduction to NumPy, covering array creation,
manipulation, mathematical operations, and advanced indexing. It’s a critical tool for data science and
machine learning, offering performance and versatility over Python lists. Practice with provided code and
explore documentation for deeper understanding.

4/4

You might also like