NumPy Phase 1 Notes
1. Virtual Environments (Python)
A virtual environment is an isolated Python environment that allows you to install packages without affecting the
global Python installation.
Create a virtual environment: python -m venv num1
Activate (PowerShell): .\num1\Scripts\activate
Deactivate environment: deactivate
Purpose:
- Keeps project dependencies separate
- Avoids package conflicts
- Recommended for all Python projects
2. Importing Libraries
Before using NumPy or other modules, they must be imported.
Example:
import numpy as np, import time
np → shorthand alias for NumPy.
time → used to measure execution time of programs.
3. Measuring Execution Time
The time module helps measure how long a program takes to run. Example:
import time
start = [Link]()
# code block
a = [i*2 for i in range(100)]
end = [Link]()
print(end - start)
Explanation:
[Link]() returns the current time in seconds.
The difference gives total execution time.
4. Creating NumPy Arrays
NumPy arrays store numerical data [Link]:
import numpy as np
np_arr = [Link]([1,2,3])
print(np_arr)
NumPy arrays are faster and more memory‑efficient than Python lists.
5. NumPy arange()
arange() generates numbers within a [Link]:
import numpy as np
arr = [Link](10)
print(arr)
Output: [0 1 2 3 4 5 6 7 8 9]
6. zeros()
Creates an array filled with zeros.
import numpy as np
mtx = [Link]((3,4))
print(mtx)
Output is a 3x4 matrix filled with zeros.
7. full()
Creates an array filled with a specific value.
Example:
import numpy as np
arr = [Link]((2,3),7)
print(arr)
Output:
[[7 7 7]
[7 7 7]]
8. linspace()
Creates evenly spaced numbers between a start and end value.
import numpy as np
arr = [Link](0,10,5)
print(arr)
Output:
[0. 2.5 5. 7.5 10.]
9. [Link]()
Generates random numbers between 0 and 1.
import numpy as np
arr = [Link]((2,2))
print(arr)
10. Tensor Concept
A tensor is a generalization of arrays.
Scalar → 0D
Vector → 1D
Matrix → 2D
Tensor → 3D or higher dimensional array
->NumPy arrays represent tensors used in machine learning.
11. Array Properties
Important attributes of NumPy arrays:
Example:
import numpy as np
arr = [Link]([[1,2,3],[4,5,6]])
print([Link])
print([Link])
print([Link])
print([Link])
print([Link])
Explanation:
shape → rows and columns
ndim → number of dimensions
size → total elements
dtype → data type
itemsize → memory used by each element
12. reshape()
Changes the shape of an array without changing data.
import numpy as np
arr = [Link]([1,2,3,4,5,6])
new_arr = [Link](2,3)
print(new_arr)
Rule:
Total number of elements must remain the same.
13. flatten()
Converts multi‑dimensional arrays into a 1D array.
import numpy as np
arr = [Link]([[1,2,3],[4,5,6]])
flat = [Link]()
print(flat)
//flatten() returns a COPY of the array.
14. ravel()
Similar to flatten() but returns a view instead of a [Link]:
import numpy as np
arr = [Link]([[1,2,3],[4,5,6]])
r = [Link]()
print(r)
Changes may affect the original array.
15. transpose()
Swaps rows and columns.
import numpy as np
arr = [Link]([[1,2,3],[4,5,6]])
print(arr.T)
Original shape: (2,3)
Transpose shape: (3,2)