0% found this document useful (0 votes)
16 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)
16 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: c5-1 Roll No.: 03


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

Signature of the Staff In-charge with date

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

Arithmatic with NumPy Arrays:


Any arithmetic operations between equal-size arrays applies 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 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
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 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,long with the axis. If axis is not explicitly passed, it is taken as 0.
 Splitting NumPy Arrays
o Splitting is 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 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 (2, 4)

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

print(arr.shape)

import numpy as np [[ 1 2 3]

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


12])
[ 7 8 9]
newarr = arr.reshape(4, 3)
[10 11 12]]
print(newarr)

import numpy as np 1

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

for x in arr: 3

print(x)

import numpy as np The [1 2 3 4 5 6]

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

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

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

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

print(arr)

import numpy as np [array([1, 2]), array([3, 4]), array([5, 6])]

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

newarr = np.array_split(arr, 3)

print(newarr)

import numpy as np

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

x = np.where(arr == 4)

print(x)

import numpy as np

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

print(np.sort(arr))

import numpy as np

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

x = [True, False, True, False]

newarr = arr[x]

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, the overall average high and low temperatures,
and identify the months with the highest and lowest average temperatures.

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

Books/ Journals/ Websites referred:

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