NumpyToday's Session
NumpyToday's Session
1 What is NumPy?
NumPy (Numerical Python) is a powerful Python library for numerical comput-
ing. It provides support for large, multi-dimensional arrays and matrices, along
with a collection of mathematical functions to operate on these arrays efficiently.
· Why NumPy? NumPy is faster than Python lists for numerical operations
due to its optimized C-based implementations and contiguous memory al-
location.
Output:
Type of pyl: <class ’list’>
Type of pyl_array: <class ’numpy.ndarray’>
Array:
[[1 2 3 4 5 6]
[1 2 3 4 5 6]]
Output:
1
Introduction to NumPy
Zeros array:
[[0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0.]]
Ones array:
[[1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1.]]
Full array with 10:
[[10 10 10 10 10]
[10 10 10 10 10]]
Output:
np.arange(1, 22, 3):
[ 1 4 7 10 13 16 19]
np.linspace(1, 22, 10):
[ 1. 3.33333333 5.66666667 8. 10.33333333 12.66666667
15. 17.33333333 19.66666667 22. ]
Output:
Dimensions: 2
Size: 12
Shape: (2, 6)
Data type: int64
· Note: The shape attribute returns a tuple, which is immutable, so direct
assignment like s[0] = 4 will raise an error.
2
Introduction to NumPy
1 x = np.zeros(20)
2 x = x.reshape(2, 10)
3 print(”Reshaped to (2, 10):\n”, x)
4 x = x.reshape(4, 5)
5 print(”Reshaped to (4, 5):\n”, x)
6 x = x.T
7 print(”Transposed:\n”, x)
8 x = x.flatten()
9 print(”Flattened:\n”, x)
Output:
Reshaped to (2, 10):
[[0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]]
Reshaped to (4, 5):
[[0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0.]]
Transposed:
[[0. 0. 0. 0.]
[0. 0. 0. 0.]
[0. 0. 0. 0.]
[0. 0. 0. 0.]
[0. 0. 0. 0.]]
Flattened:
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
3
Introduction to NumPy
[72 22 33 23 24]]
After scalar multiplication by 2:
[[ 46 22 98 6 72]
[166 40 32 28 10]
[ 46 30 190 142 168]
[ 10 2 174 114 190]
[144 44 66 46 48]]
After scalar addition by 2:
[[ 48 24 100 8 74]
[168 42 34 30 12]
[ 48 32 192 144 170]
[ 12 4 176 116 192]
[146 46 68 48 50]]
Output:
Matrix multiplication (arr.T @ arr1):
[[ 130 140]
[ 380 410]
[ 630 680]
[ 880 950]
[1130 1220]]
Matrix multiplication (np.dot):
[[ 130 140]
[ 380 410]
[ 630 680]
[ 880 950]
[1130 1220]]
4
Introduction to NumPy
Output:
Sliced array (index 5 to end-1):
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
Output:
Array: [0 1 2 3 4 5 6 7 8 9]
Elements where arr % 2 == 1:
[1 3 5 7 9]
Output:
Filtering with np.where (0 for odd, 1 for even):
[1 0 1 0 1 0 1 0 1 0]
2.7 Aggregations
NumPy provides functions like sum, min, max, mean, std, and var for statistical
operations.
1 arr = np.random.randint(0, 100, 10)
2 print(”Array:”, arr)
3 print(”Sum:”, arr.sum())
4 print(”Min:”, arr.min())
5 print(”Max:”, arr.max())
6 print(”Mean:”, arr.mean())
7 print(”Standard Deviation:”, arr.std())
8 print(”Variance:”, arr.var())
9 print(”Sorted array:”, arr[arr.argsort()])
5
Introduction to NumPy
Array: [ 1 24 30 4 85 14 39 23 66 76]
Sum: 362
Min: 1
Max: 85
Mean: 36.2
Standard Deviation: 28.314529936775716
Variance: 801.76
Sorted array: [ 1 4 14 23 24 30 39 66 76 85]
Output:
File saved successfully
File loaded successfully
File content: [0. 1. 2. 3. 4. 5. 6. 7. 8. 9.]
· Note: File handling operations are not supported in some environments
(e.g., Pyodide in browsers). Ensure your environment supports file I/O.
Output:
Array with NaN: [ 0. 1. 2. 3. nan 5. 6. 7. 8. 9.]
Mean ignoring NaN: 4.555555555555555
6
Introduction to NumPy
6 arr1 = np.arange(1000000)
7 arr2 = np.arange(1000000)
8 %time result = arr1 * arr2
NumPy arrays:
CPU times: user 0 ns, sys: 4.24 ms, total: 4.24 ms
Wall time: 4.32 ms
· Note: NumPy’s performance advantage comes from its ability to perform
operations on entire arrays at once, avoiding Python’s loop overhead.
4 Conclusion
This guide covered the basics of NumPy, including array creation, attributes, op-
erations, indexing, aggregations, and file handling. NumPy is an essential tool
for numerical computing in Python, offering efficient and versatile array oper-
ations. Practice these examples to build proficiency in using NumPy for data
science and scientific computing tasks.