0% found this document useful (0 votes)
6 views8 pages

NumpyToday's Session

This document is a comprehensive guide to NumPy, a powerful library for numerical computing in Python, covering topics such as array creation, attributes, operations, indexing, and file handling. It highlights the performance advantages of NumPy over Python lists and provides numerous examples and code snippets for practical understanding. The guide concludes by emphasizing the importance of NumPy for data science and scientific computing.

Uploaded by

i240608
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)
6 views8 pages

NumpyToday's Session

This document is a comprehensive guide to NumPy, a powerful library for numerical computing in Python, covering topics such as array creation, attributes, operations, indexing, and file handling. It highlights the performance advantages of NumPy over Python lists and provides numerous examples and code snippets for practical understanding. The guide concludes by emphasizing the importance of NumPy for data science and scientific computing.

Uploaded by

i240608
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/ 8

Introduction to NumPy: A

Comprehensive Guide A detailed tutorial


based on a Jupyter Notebook Generated on June 27, 2025
Introduction to NumPy

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.

2 Key Concepts and Operations


2.1 Installing and Importing NumPy
NumPy is typically installed using pip (pip install numpy) and must be im-
ported before use. The alias np is commonly used for convenience.
1 import numpy as np

2.2 Creating NumPy Arrays


NumPy arrays can be created from Python lists or using built-in functions like
np.array, np.zeros, np.ones, np.full, np.arange, and np.linspace.

2.2.1 Example: Converting a List to a NumPy Array


1 import numpy as np
2 pyl = [1, 2, 3, 4, 5, 6]
3 pyl_array = np.array([pyl, pyl])
4 print(”Type of pyl:”, type(pyl))
5 print(”Type of pyl_array:”, type(pyl_array))
6 print(”Array:\n”, pyl_array)

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]]

2.2.2 Example: Creating Arrays with Zeros, Ones, and Full


1 x = np.zeros(20).reshape(4, 5)
2 print(”Zeros array:\n”, x)
3 q = np.ones((2, 5)) * 1
4 print(”Ones array:\n”, q)
5 w = np.full((2, 5), 10)
6 print(”Full array with 10:\n”, w)

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]]

2.2.3 Example: Using arange and linspace


1 print(”np.arange(1, 22, 3):\n”, np.arange(1, 22, 3))
2 print(”np.linspace(1, 22, 10):\n”, np.linspace(1, 22, 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. ]

2.3 Array Attributes


NumPy arrays have attributes like ndim, size, shape, and dtype to provide in-
formation about the array.
1 pyl = np.array([[1, 2, 3, 4, 5, 6], [1, 2, 3, 4, 5, 6]])
2 print(”Dimensions:”, pyl.ndim)
3 print(”Size:”, pyl.size)
4 print(”Shape:”, pyl.shape)
5 print(”Data type:”, pyl.dtype)

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.4 Reshaping and Transposing Arrays


Arrays can be reshaped using reshape and transposed using .T or flatten to
convert to 1D.

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.]

2.5 Array Operations


NumPy supports element-wise operations, matrix multiplication, and scalar op-
erations.

2.5.1 Example: Scalar Operations


1 arr = np.random.randint(0, 100, (5, 5))
2 print(”Original array:\n”, arr)
3 arr = arr * 2
4 print(”After scalar multiplication by 2:\n”, arr)
5 arr = arr + 2
6 print(”After scalar addition by 2:\n”, arr)

Output (example values):


Original array:
[[23 11 49 3 36]
[83 20 16 14 5]
[23 15 95 71 84]
[ 5 1 87 57 95]

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]]

2.5.2 Example: Element-wise Multiplication


1 arr1 = arr
2 print(”Element-wise multiplication:\n”, arr1 * arr)

Output (example values):


Element-wise multiplication:
[[ 2304 576 10000 64 5476]
[28224 1764 1156 900 144]
[ 2304 1024 36864 20736 28900]
[ 144 16 30976 13456 36864]
[21316 2116 4624 2304 2500]]

2.5.3 Example: Matrix Multiplication


1 arr = np.arange(10).reshape(2, 5)
2 arr1 = np.arange(10, 20).reshape(5, 2)
3 print(”Matrix multiplication (arr.T @ arr1):\n”, arr @ arr1)
4 print(”Matrix multiplication (np.dot):\n”, np.dot(arr, arr1))

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

2.6 Indexing and Slicing


NumPy arrays support indexing, slicing, and boolean indexing for accessing and
modifying elements.

2.6.1 Example: Slicing


1 x = np.zeros(20).flatten()
2 print(”Sliced array (index 5 to end-1):\n”, x[5:-1])

Output:
Sliced array (index 5 to end-1):
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]

2.6.2 Example: Boolean Indexing


1 arr = np.arange(10)
2 print(”Array:”, arr)
3 print(”Elements where arr % 2 == 1:\n”, arr[arr % 2 == 1])

Output:
Array: [0 1 2 3 4 5 6 7 8 9]
Elements where arr % 2 == 1:
[1 3 5 7 9]

2.6.3 Example: Filtering with np.where


1 print(”Filtering with np.where (0 for odd, 1 for even):\n”,
np.where(arr % 2 == 0, 1, 0))

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()])

Output (example values):

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]

2.8 File Handling


NumPy can save and load arrays to/from files using np.savetxt and np.loadtxt.
1 arr = np.arange(10)
2 np.savetxt(’numpy.txt’, arr)
3 print(”File saved successfully”)
4 arr1 = np.loadtxt(’numpy.txt’)
5 print(”File loaded successfully”)
6 print(”File content:”, arr1)

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.

2.9 Handling Missing Values


NumPy provides functions like np.nanmean to handle missing values (NaN).
1 arr1 = np.array([0, 1, 2, 3, np.nan, 5, 6, 7, 8, 9])
2 print(”Array with NaN:”, arr1)
3 print(”Mean ignoring NaN:”, np.nanmean(arr1))

Output:
Array with NaN: [ 0. 1. 2. 3. nan 5. 6. 7. 8. 9.]
Mean ignoring NaN: 4.555555555555555

3 Performance Comparison: Python Lists vs. NumPy Arrays


NumPy arrays are significantly faster than Python lists for numerical operations
due to vectorization.
1 import numpy as np
2 list1 = list(range(1000000))
3 list2 = list(range(1000000))
4 %time result = [x * y for x, y in zip(list1, list2)]
5

6
Introduction to NumPy

6 arr1 = np.arange(1000000)
7 arr2 = np.arange(1000000)
8 %time result = arr1 * arr2

Output (example timings):


Python lists:
CPU times: user 90.2 ms, sys: 23.1 ms, total: 113 ms
Wall time: 119 ms

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.

You might also like