0% found this document useful (0 votes)
23 views9 pages

Numpy Notes With Examples

This document is a tutorial on Numpy, covering installation, array creation, attributes, initialization methods, indexing, slicing, reshaping, stacking, and mathematical operations. It includes code examples demonstrating how to use various Numpy functionalities, such as creating 1D, 2D, and 3D arrays, performing operations on arrays, and manipulating array dimensions. The tutorial serves as a comprehensive guide for beginners to understand and utilize Numpy for numerical computations in Python.

Uploaded by

Manish
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)
23 views9 pages

Numpy Notes With Examples

This document is a tutorial on Numpy, covering installation, array creation, attributes, initialization methods, indexing, slicing, reshaping, stacking, and mathematical operations. It includes code examples demonstrating how to use various Numpy functionalities, such as creating 1D, 2D, and 3D arrays, performing operations on arrays, and manipulating array dimensions. The tutorial serves as a comprehensive guide for beginners to understand and utilize Numpy for numerical computations in Python.

Uploaded by

Manish
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

11/14/25, 3:30 PM numpy_tutorial_by_Rishabh_Mishra

Numpy Tutorial by Rishabh Mishra


In [ ]: # Numpy Full Course: [Link]

In [1]: # Install Numpy


!pip install numpy

Requirement already satisfied: numpy in c:\users\hp\appdata\local\programs\python


\python312\lib\site-packages (2.0.1)
[notice] A new release of pip is available: 24.0 -> 25.2
[notice] To update, run: [Link] -m pip install --upgrade pip

In [3]: # Import Numpy library


import numpy as np

In [3]: print(np.__version__) # Numpy version installed in the system

2.0.1

1. creating arrays
In [6]: list1 = [1,2,3]
print(list1)

[1, 2, 3]

In [7]: arr_list1 = [Link](list1) # creating array using list


print(arr_list1)

[1 2 3]

In [8]: print(type(arr_list1)) # check data type of variable

<class '[Link]'>

In [13]: print(arr_list1.ndim) # check dimension of array

In [9]: # creating 2-dim array using 2 lists


list1 = [1,2,3]
list2 = [4,5,6]
arr_list1_2 = [Link]([list1, list2])
print(arr_list1_2)

[[1 2 3]
[4 5 6]]

In [12]: print(arr_list1_2.ndim) # check dimension of array

In [10]: # creating 3-dim array using 3 lists


list1 = [1,2,3]
list2 = [4,5,6]
list3 = [7,8,9]
arr_list1_3 = [Link]([[list1, list2, list3]])
print(arr_list1_3)

[Link] 1/10
11/14/25, 3:30 PM numpy_tutorial_by_Rishabh_Mishra

[[[1 2 3]
[4 5 6]
[7 8 9]]]

In [11]: print(arr_list1_3.ndim) # check dimension of array

2. numpy array attributes


In [15]: arr1 = [Link]([[1,2,3], [4,5,6]])
print(arr1)

[[1 2 3]
[4 5 6]]

In [16]: # Attributes of Array (properties of array)


print("Shape", [Link]) # share= rows, columns and height of array
print("Size", [Link]) # number of elements in array
print("dType", [Link]) # data type of elements in array
print("n_dim", [Link]) # dimension of array

Shape (2, 3)
Size 6
dType int64
n_dim 2

In [17]: # HW: create two arrays - 1D and 3D, and find it's attributes

3. Array initialization methods


In [20]: # zeros array : all elements are 0

zero_arr = [Link]((2,3))
print(zero_arr)

[[0. 0. 0.]
[0. 0. 0.]]

In [23]: # ones array : all elements are 1

one_arr = [Link]((1,3))
print(one_arr)

[[1. 1. 1.]]

In [25]: # Full array : all elements are same based on given value

full_arr = [Link]((3,2), 9)
print(full_arr)

[[9 9]
[9 9]
[9 9]]

In [27]: # Identity Matrix : diagonal elements are ones and all other elements are zeros

id_arr = [Link](3)
print(id_arr)

[Link] 2/10
11/14/25, 3:30 PM numpy_tutorial_by_Rishabh_Mishra

[[1. 0. 0.]
[0. 1. 0.]
[0. 0. 1.]]

In [29]: # empty array


print([Link](2))

[1.20166646e-311 0.00000000e+000]

In [38]: # evenly spaced array : elements are evenly spaced based on step value
print([Link](2,10,2))

[2 4 6 8]

In [40]: # specific number of eqaully spaced values between a range : elements are equall
print([Link](1,10,4))

[ 1. 4. 7. 10.]

In [42]: # random values array - float


r_arr = [Link](3,2)
print(r_arr)

[[0.99189518 0.36638098]
[0.38564405 0.5403712 ]
[0.70119322 0.29187343]]

In [44]: # random values array - int


rint_arr = [Link](1,100,(3,3))
print(rint_arr)

[[60 13 62]
[ 3 36 14]
[96 78 10]]

4 Array Indexing and Slicing


In [7]: a = [Link]([1,3,5,7,9])
print(a)

[1 3 5 7 9]

In [8]: print(a[0]) # first element of array

In [9]: print(a[4]) # fifth element of array

In [10]: print(a[-1]) # last element of array

In [12]: a = [Link]([1,3,5,7,9])
print(a)
# Slicing :
# array[start:stop:step]
# stop - index, that is not included in output

[1 3 5 7 9]

In [14]: print(a[0:3]) # First 3 elements

[Link] 3/10
11/14/25, 3:30 PM numpy_tutorial_by_Rishabh_Mishra

[1 3 5]

In [25]: print(a[-3:]) # Last 3 elements

[5 7 9]

In [ ]: print(a[1:4]) # Middle 3 elements

In [ ]: print(a[0::3]) # Middle 3 elements with steps

In [ ]: print(a[0::2]) # All alternative elements with steps

In [34]: # Indexing on 2 Dimensional Array


arr2 = [Link]([[1,2,3], [4,5,6]])
print(arr2)

# 2D Array: [rows, columns] # 2D Array takes 2 values - rows a


# 3D Array: [layers/height, rows, columns] # 3D Array takes 3 values - height

[[1 2 3]
[4 5 6]]

In [69]: print(arr2[0]) # first row


print(arr2[1]) # second row

[1 2 3]
[4 5 6]

In [70]: # Slicing on 2D array


print(arr2[0][0]) # element of first row and first column
print(arr2[0][1]) # element of first row and second column
print(arr2[1][2]) # element of second row and third column

1
2
6

In [42]: print(arr2[1]) # specific element - 1D


print(arr2[1:]) # slicing part - 2D

[4 5 6]
[[4 5 6]]

In [43]: print(arr2)

# 2D Array: [rows, columns] | eg: arr2[0][1] # 2D Array takes 2 values - r

[[1 2 3]
[4 5 6]]

In [67]: print(arr2[:, 0]) # First column of an array


print(arr2[:, 1]) # Second column of an array
print(arr2[:, 2]) # Third column of an array

[1 4]

In [72]: arr3 = [Link]([[1,2,3],


[4,5,6],
[7,8,9]])
print(arr3)

# 3D Array: [layers/height, rows, columns] # 3D Array takes 3 values - he

[Link] 4/10
11/14/25, 3:30 PM numpy_tutorial_by_Rishabh_Mishra

[[1 2 3]
[4 5 6]
[7 8 9]]

In [74]: print(arr3[0])
print(arr3[1])
print(arr3[2])

[1 2 3]
[4 5 6]
[7 8 9]

In [76]: print(arr3[0][2])
print(arr3[2][2])

3
9

In [81]: print(arr3[:, 0]) # 1st col value


print(arr3[:, 1]) # 2nd col value
print(arr3[:, 2]) # 3rd col value

[1 4 7]
[3 6 9]

In [86]: print(arr3[::, 1:2]) #slicing on columns


print(arr3[2::, 1:2]) #slicing on rows and columns

[[2]
[5]
[8]]
[[8]]

5 Array Reshaping and Flattening


In [ ]: # Reshape Array: changing its dimensions (e.g., rows and columns) without alteri
# Flatten Array: transforming a multi-dimensional array into a single-dimensiona

In [95]: arr1 = [Link]([1,2,3,4,5,6]) # shape = (1,6) - 1 row and 6 columns


print(arr1)

[1 2 3 4 5 6]

In [91]: reshaped = [Link]((6,1)) # shape = (6,1) - 6 rows and 1 column


print(reshaped)

[[1]
[2]
[3]
[4]
[5]
[6]]

In [96]: reshaped2 = [Link]((2,3)) # re-shaped array from (1,6) to (2,3)


print(reshaped2)

[[1 2 3]
[4 5 6]]

In [97]: print([Link]()) # flattened array from higher (2D) dimension

[1 2 3 4 5 6]

[Link] 5/10
11/14/25, 3:30 PM numpy_tutorial_by_Rishabh_Mishra

6 Array Stacking and Splitting


In [ ]: # Array Stacking: combining multiple arrays along a new axis, resulting in an a
# Array Splitting:

In [101… # stacking
a = [Link]([1,2,3])
b = [Link]([4,5,6])

print([Link]((a,b))) # vertical stacking - row wise -> output array


print([Link]((a,b))) # horizontal stacking - column wise -> output array

[[1 2 3]
[4 5 6]]
[1 2 3 4 5 6]

In [102… # splitting
c = [Link]([[1,2,3], [4,5,6]])
print(c)

[[1 2 3]
[4 5 6]]

In [105… hsplit = [Link](c,3) # horizontal split -> input and output array are same di

for s in hsplit:
print(s)

[[1]
[4]]
[[2]
[5]]
[[3]
[6]]

In [106… vsplit = [Link](c,2) # vertical split -> input and output array are same dime

for s in vsplit:
print(s)

[[1 2 3]]
[[4 5 6]]

7 Mathematical Operations on Array


In [118… # FYI: list vs Array
# one of the reason we array instead of list : time taken by an operation on lis
%%time
numbers = list(range(1, 100000001))
result = [x+5 for x in numbers]

CPU times: total: 6.86 s


Wall time: 6.91 s

In [119… %%time
numbers = [Link](1,100000001)
result = numbers+5

[Link] 6/10
11/14/25, 3:30 PM numpy_tutorial_by_Rishabh_Mishra

CPU times: total: 2.19 s


Wall time: 2.21 s

In [109… a = [Link]([10,20,40,-30])
print(a)

[ 10 20 40 -30]

In [110… print(a+10) # add a value in array


print(a-30) # subs a value in array
print(a*2) # multiply by a value in array
print(a/10) # divide by a value in array

[ 20 30 50 -20]
[-20 -10 10 -60]
[ 20 40 80 -60]
[ 1. 2. 4. -3.]

In [112… b = [Link]([1,4,9])
print(b)

[1 4 9]

In [113… print([Link](b)) # square of all array elements


print([Link](b)) # square root of all array elements

[ 1 16 81]
[1. 2. 3.]

In [ ]: # HW: sin, tan, cos, sec

8 Mathematical Operations on Muliple Arrays


In [120… a = [Link]([1,2,3])
b = [Link]([4,5,6])
print(a)
print(b)

[1 2 3]
[4 5 6]

In [125… print([Link](a,b)) # add two arrays


print([Link](a,b)) # subtract two arrays
print([Link](a,b)) # multiply two arrays
print([Link](a,b)) # divide two arrays

[5 7 9]
[-3 -3 -3]
[ 4 10 18]
[0.25 0.4 0.5 ]

In [127… # Dot product: computes the inner product of the two vectors and sum the results
print([Link](a,b)) # dot product

32

In [128… print(a.T) # Transpose: Returns an array with axes transposed

[1 2 3]

In [129… t = [Link]([[1,2,3],
[4,5,6]])

[Link] 7/10
11/14/25, 3:30 PM numpy_tutorial_by_Rishabh_Mishra

print(t) # initial shape: 2,3


print(t.T) # shape after transpose: 3,2

[[1 2 3]
[4 5 6]]
[[1 4]
[2 5]
[3 6]]

9 Statistical Functions
In [130… a = [Link]([[1,2,3], [4,5,6]])
print(a)

[[1 2 3]
[4 5 6]]

In [131… print([Link](a)) # sum of all elements of an array

21

In [133… print([Link](a))
print([Link](a))
print([Link](a))
print([Link](a))
print([Link](a))

3.5
3.5
1.707825127659933
1
6

10 Array Comparison
In [136… a = [Link]([1,5,3])
b = [Link]([4,5,6])
print(a)
print(b)

[1 5 3]
[4 5 6]

In [137… print(a == b) # comparing each elements of an array


print(np.array_equal(a,b)) # comparing complete array

[False True False]


False

11 Broadcasting
In [ ]: # Broadcasting: is a mechanism that allows arithmetic operations to be performed
# it eliminates the need for explicit loops or reshaping operations, making code

# Broadcasting Rules:
# Compare shapes from right to left.
# Dimensions must match or be 1.

[Link] 8/10
11/14/25, 3:30 PM numpy_tutorial_by_Rishabh_Mishra

# If one array has fewer dimensions, pad its shape on the left with 1s.
# Any dimension equal to 1 can stretch to match the other array.

In [139… a = [Link]([1,2,3,4])
b = [Link]([5])
print(a+b) # adding 1D with 0ne-element array to 1D with multi-element arra

[6 7 8 9]

In [140… c = [Link]([[1,2,3], [4,5,6]])


d = [Link]([10,20,30])
print(c+d) # adding 2D with 6-element array (2,3) to 1D with 6-element arra

[[11 22 33]
[14 25 36]]

12 Handling with nan & inf


In [142… data1 = [Link]([1,2, [Link], 4, [Link]])
print(data1)

[ 1. 2. nan 4. inf]

In [143… print([Link](data1)) # check null value and returns boolean value

[False False True False False]

In [144… print(np.nan_to_num(data1)) # replace non-finite values within an array with

[1.00000000e+000 2.00000000e+000 0.00000000e+000 4.00000000e+000


1.79769313e+308]

save and load arrays


In [145… arr = [Link]([1,0,0,8])
print(arr)
[Link]('my_array.npy', arr)

[1 0 0 8]

In [147… loaded_arr = [Link]('my_array.npy')


print(loaded_arr)

[1 0 0 8]

In [ ]:

In [ ]: # Homework:
# 1. Create 5X5 matrix with values from 1 to 25
# a. extract last row,
# b. first column,
# c. sub matrix containing 3X3

# 2. Broadcasting: multiply shape- (3,1) matrix with shape- (3,)

In [ ]: # Free Data Analytics Resources: [Link]

In [ ]: # Next Recommended Courses: Pandas and Matplotlib

[Link] 9/10

You might also like