0% found this document useful (0 votes)
13 views7 pages

Experiment No-7

Uploaded by

abhinay.p
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views7 pages

Experiment No-7

Uploaded by

abhinay.p
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

K. J.

Somaiya College of Engineering, Mumbai-77

(A Constituent College of Somaiya Vidyavihar University)

Department of Science and Humanities

Batch: Roll No.:


Experiment / assignment / tutorial No.
Grade: AA / AB / BB / BC / CC / CD /DD

Signature of the Staff In-charge with date

IT Title: NumPy library of Python

AIM: To explore the Numpy library of Python


______________________________________________________________________
Expected OUTCOME of Experiment:

CO5: Use Numpy Library functions


_____________________________________________________________________
Resource Needed: Python IDE
_____________________________________________________________________
Theory:
NumPy: A Python library used for working with arrays.
• It also has functions for working in the domain of linear algebra, Fourier transform,
and matrices.
• NumPy stands for Numerical Python.
• The array object in NumPy is called ndarray, it provides a lot of supporting functions
that make working with ndarray very easy.

For Installation of NumPy:


• pip install numpy

Example
import numpy

arr=numpy.array([1, 2, 3, 4, 5])

print(arr)
output:?

For the creation of NumPy ndarray Object:


• NumPy is used to work with arrays. The array object in NumPy is
called ndarray.

PP Sem I/ 2024-2025
K. J. Somaiya College of Engineering, Mumbai-77

(A Constituent College of Somaiya Vidyavihar University)

Department of Science and Humanities

• We can create a NumPy ndarray object by using the array() function.


Example:

import numpy as np
arr=np.array([1, 2, 3, 4, 5])
print(arr)
print(type(arr))

Creating ndarrays:

array = np.array([[0,1,2],[2,3,4]])
output:
[[0 1 2]
[2 3 4]]

array = np.zeros((2,3))
[[0. 0. 0.]
[0. 0. 0.]]

array = np.ones((2,3))
[[1. 1. 1.]
[1. 1. 1.]]

array = np.eye(3)
[[1. 0. 0.]
[0. 1. 0.]
[0. 0. 1.]]

array = np.arange(0, 10, 2)


[0, 2, 4, 6, 8]

array = np.random.randint(0, 10, (3,3))


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

Slicing arrays
Slicing in Python means taking elements from one given index to another given index.
We pass slice instead of index like this: [start: end].
We can also define the step, like this: [start:end: step].

Arithmetic with NumPy Arrays:


Any arithmetic operations between equal-size arrays apply the operation element-wise
arr = np.array([[1., 2., 3.], [4., 5., 6.]])
print(arr)

PP Sem I/ 2024-2025
K. J. Somaiya College of Engineering, Mumbai-77

(A Constituent College of Somaiya Vidyavihar University)

Department of Science and Humanities

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

print(arr * arr)
[[ 1. 4. 9.]
[16. 25. 36.]]

print(arr - arr)
[[0. 0. 0.]
[0. 0. 0.]]

● Shape of an Array
o The shape of an array is the number of elements in each dimension.
● Reshaping arrays
o Reshaping means changing the shape of an array.
o The shape of an array is the number of elements in each dimension.
o By reshaping we can add or remove dimensions or change the number of
elements
o in each dimension.
● Iterating Arrays
o Iterating means going through elements one by one.
o As we deal with multi-dimensional arrays in numpy, we can do this using the
basic
o for loop of Python. If we iterate on a 1-D array it will go through each
element
o one by one.
● Joining NumPy Arrays
o Joining means putting the contents of two or more arrays in a single array.
o In SQL we join tables based on a key, whereas in NumPy we join arrays by
axes.
o We pass a sequence of arrays that we want to join to the concatenate()
function, along with the axis. If the axis is not explicitly passed, it is taken as
0.
● Splitting NumPy Arrays
o Splitting is the reverse operation of Joining.
o Joining merges multiple arrays into one and Splitting breaks one array into
multiple.
o We use array_split() for splitting arrays, we pass it the array we want to split
and the number of splits.
● NumPy Searching Arrays
o You can search an array for a certain value, and return the indexes that get a
match.
o To search an array, use the where() method.
● Sorting Arrays

PP Sem I/ 2024-2025
K. J. Somaiya College of Engineering, Mumbai-77

(A Constituent College of Somaiya Vidyavihar University)

Department of Science and Humanities

o Sorting means putting elements in an ordered sequence.


o An ordered sequence is any sequence that has an order corresponding to
elements, like numeric or alphabetical, ascending or descending.
o The NumPy ndarray object has a function called sort(), that will sort a
specified array.
● NumPy Filter Array
o Getting some elements out of an existing array and creating a new array out
of them is called filtering. In NumPy, you filter an array using a boolean
index list.

1. Problem statement:

Python Code Output

import numpy as np

arr = np.array([[1, 2, 3, 4], [5, 6, 7, 8]]) (2,4)

print(arr.shape)

import numpy as np

arr = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]) [[ 1 2 3]

newarr = arr.reshape(4, 3) [ 4 5 6]

print(newarr) [ 7 8 9]

[10 11 12]]

import numpy as np

arr = np.array([1, 2, 3]) 1

for x in arr: 2

print(x) 3

import numpy as np The

arr1 = np.array([1, 2, 3]) [1 2 3 4 5 6]

arr2 = np.array([4, 5, 6])

arr = np.concatenate((arr1, arr2))

print(arr)

PP Sem I/ 2024-2025
K. J. Somaiya College of Engineering, Mumbai-77

(A Constituent College of Somaiya Vidyavihar University)

Department of Science and Humanities

import numpy as np [array([1, 2]),

arr = np.array([1, 2, 3, 4, 5, 6]) array([3, 4]),

newarr = np.array_split(arr, 3) array([5, 6])]

print(newarr)

import numpy as np

arr = np.array([1, 2, 3, 4, 5, 4, 4]) (array([3, 5, 6], dtype=int64),)

x = np.where(arr == 4)

print(x)

import numpy as np import numpy as nparr = np.array

arr = np.array([3, 2, 0, 1]) ([3, 2, 0, 1])print(np.sort(arr))

print(np.sort(arr))

import numpy as np

arr = np.array([41, 42, 43, 44])

x = [True, False, True, False]

newarr = arr[x] [41 43]

print(newarr)

2. Write a Python program to calculate the sum of all columns in a 2D NumPy array.

3. Create two NumPy arrays representing monthly high and low temperatures for a year.
Calculate the monthly average temperatures, and the overall average high and low
temperatures, and identify the months with the highest and lowest average temperatures.

Books/ Journals/ Websites referred:

PP Sem I/ 2024-2025
K. J. Somaiya College of Engineering, Mumbai-77

(A Constituent College of Somaiya Vidyavihar University)

Department of Science and Humanities

1. Reema Thareja, Python Programming: Using Problem-Solving Approach, Oxford


University Press, First Edition 2017, India
2. Sheetal Taneja and Naveen Kumar, Python Programming: A modular Approach, Pearson
India, Second Edition 2018, India

Implementation details:

Output(s):

PP Sem I/ 2024-2025
K. J. Somaiya College of Engineering, Mumbai-77

(A Constituent College of Somaiya Vidyavihar University)

Department of Science and Humanities

Conclusion:

Post Lab Descriptive Questions

1. Generate a random integer from 0 to 100 using the NumPy random function.
2. Explain the slicing of 2-D Array

Date: _____________ Signature of faculty in-charge

PP Sem I/ 2024-2025

You might also like