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

Numpy

The document provides an overview of the Numpy library, highlighting its advantages such as faster mathematical operations compared to standard Python lists. It includes examples of creating and manipulating Numpy arrays, performing mathematical operations, and utilizing built-in functions for analysis. Additionally, it covers array indexing, reshaping, and matrix operations.

Uploaded by

ngf2314
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

Numpy

The document provides an overview of the Numpy library, highlighting its advantages such as faster mathematical operations compared to standard Python lists. It includes examples of creating and manipulating Numpy arrays, performing mathematical operations, and utilizing built-in functions for analysis. Additionally, it covers array indexing, reshaping, and matrix operations.

Uploaded by

ngf2314
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

Numpy

October 24, 2025

Numpy Library - Stands for Numerical Python - Advantages of Numpy Arrays - Allows Several
Mathematical operations - Faster operations

[1]: # Importing Numpy Library


import numpy as np

List Vs Numpy Array - Time Taken

[2]: from time import process_time

[3]: # Time Taken by a list


python_list = [i for i in range(1000000)]
start_time = process_time()
python_list = [i+5 for i in python_list]
end_time = process_time()
print(end_time - start_time)

0.09375

[4]: # Time Taken by a list


python_list = [Link]([i for i in range(1000000)])
start_time = process_time()
python_list += 5
end_time = process_time()
print(end_time - start_time)

0.0
Numpy Arrays

[5]: # List
list1 = [1,2,3,4,5]
print(list1)
type(list1)

[1, 2, 3, 4, 5]

[5]: list

[6]: # Numpy Array


np_array = [Link]([1,2,3,4,5])

1
print(np_array)
type(np_array)

[1 2 3 4 5]

[6]: [Link]

[7]: # Creating a 1-dim array


a = [Link]([1,2,3,4])
print(a)
[Link]

[1 2 3 4]

[7]: (4,)

[8]: # Creating a 2-dim array


b = [Link]([(1,2,3,4),(5,6,7,8)])
print(b)
[Link]

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

[8]: (2, 4)

[9]: # Change data types


c = [Link]([(1,2,3,4), (5,6,7,8)], dtype=float)
print(c)

[[1. 2. 3. 4.]
[5. 6. 7. 8.]]
Creating Special arrays

[10]: # Create a numpy array of zeros


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

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

[11]: # Create a numpy array of ones


y = [Link]((3,3))
print(y)

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

2
[12]: # Create a numpy array of a particular value
z = [Link]((3,4), 5)
print(z)

[[5 5 5 5]
[5 5 5 5]
[5 5 5 5]]

[13]: # Create an identity matrix


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

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

[14]: # Create a numpy array with random values between 0 and 1


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

[[0.15763017 0.31949418]
[0.87838617 0.59438146]
[0.84969114 0.77307157]]

[15]: # Create a numpy array with random values between 0 and 1


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

[[0.1406787 0.55319765]
[0.64825126 0.23065925]
[0.60282465 0.38588639]]

[16]: # create a numpy array with random integer values within specific range
c = [Link](10,100,(3,3))
print(c)

[[62 17 58]
[12 79 83]
[17 88 57]]

[17]: # create a numpy array with evenly spaced values


d = [Link](10,30,6)
print(d)

[10. 14. 18. 22. 26. 30.]

[18]: # create a numpy array with Values from spacific range with specific step
e = [Link](0,10,3)
print(e)

[0 3 6 9]

3
[19]: # Convert a list to a numpy array
list2 = [10, 20, 30, 20, 50]
np_array = [Link](list2)
print(np_array)
type(np_array)

[10 20 30 20 50]

[19]: [Link]

Analyzing a numpy array

[20]: c = [Link](10,100, (5,5))


print(c)

[[10 24 26 40 38]
[36 76 95 50 34]
[32 76 31 13 12]
[53 17 39 41 87]
[44 20 45 71 34]]

[21]: # Array dimension


print([Link])

(5, 5)

[22]: # Number of dimensions


print([Link])

[23]: # Number of elemnts in an array


print([Link])

25

[24]: # Checking the data type of values in the array


print([Link])

int32
Mathematical operations on a array

[25]: list1 = [1,2,3,4,5]


list2 = [6,7,8,9,10]
print(list1 + list2) # this make concatenate or join two list not sumation

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

[26]: a = [Link](0,10,(3,3))
b = [Link](10,20,(3,3))

4
[27]: print(a)

[[9 0 8]
[4 4 3]
[2 3 0]]

[28]: print(b)

[[10 16 19]
[16 19 12]
[12 17 13]]

[29]: print(a + b)

[[19 16 27]
[20 23 15]
[14 20 13]]

[30]: print(a - b)

[[ -1 -16 -11]
[-12 -15 -9]
[-10 -14 -13]]

[31]: print(a * b)

[[ 90 0 152]
[ 64 76 36]
[ 24 51 0]]

[32]: print(a / b)

[[0.9 0. 0.42105263]
[0.25 0.21052632 0.25 ]
[0.16666667 0.17647059 0. ]]

[33]: print([Link](a,b))

[[19 16 27]
[20 23 15]
[14 20 13]]

[34]: print([Link](a,b))

[[ -1 -16 -11]
[-12 -15 -9]
[-10 -14 -13]]

[35]: print([Link](a,b))

5
[[ 90 0 152]
[ 64 76 36]
[ 24 51 0]]

[36]: print([Link](a,b))

[[0.9 0. 0.42105263]
[0.25 0.21052632 0.25 ]
[0.16666667 0.17647059 0. ]]
Built-in Functions

[37]: print([Link](a))
print([Link](a))
print([Link](a))
print([Link](a))
print([Link](a))

9
0
33
2.943920288775949
3.6666666666666665
Indexing and Slicing

[38]: arr = [Link]([10, 20, 30, 40, 50])


print(arr[0]) # First element
print(arr[-1]) # Last element
print(arr[1:4]) # Slice elements 1–3

10
50
[20 30 40]

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


print(arr2[0, 0]) # Element in row 0, column 0
print(arr2[:, 1]) # All rows, column 1
print(arr2[1, :]) # Row 1, all columns

1
[2 5]
[4 5 6]
Reshaping Arrays

[40]: arr = [Link](1, 10)


arr_reshaped = [Link](3, 3)
print(arr_reshaped)

[[1 2 3]
[4 5 6]

6
[7 8 9]]
Transpose

[41]: arr = [Link](10,20,(2,3))


print(arr)
trans = [Link](arr)
print(trans)
print([Link])

[[12 15 11]
[11 15 14]]
[[12 11]
[15 15]
[11 14]]
(3, 2)

[42]: # another way to find transpose


trans2 = arr.T
print(trans2)
print([Link])

[[12 11]
[15 15]
[11 14]]
(3, 2)
Combining Arrays

[43]: a = [Link]([[1, 2], [3, 4]])


b = [Link]([[5, 6]])
print([Link]((a, b))) # Vertical stack
print([Link]((a, b.T))) # Horizontal stack

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

[44]: arr = [Link]([10, 20, 30, 40, 50])


print(arr[arr > 25])

[30 40 50]
Matrix Operations

[45]: A = [Link]([[1, 2], [3, 4]])


B = [Link]([[5, 6], [7, 8]])

7
[46]: print([Link](A, B)) # Matrix multiplication

[[19 22]
[43 50]]

[47]: print([Link](A)) # Inverse of matrix

[[-2. 1. ]
[ 1.5 -0.5]]

[48]: print([Link](A)) # Determinant

-2.0000000000000004

[ ]:

[ ]:

[ ]:

You might also like