0% found this document useful (0 votes)
9 views

Unit-V Python_BCC402

Complete python notes for acedmics.

Uploaded by

mohit gola
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)
9 views

Unit-V Python_BCC402

Complete python notes for acedmics.

Uploaded by

mohit gola
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/ 20

PYTHON PROGRAMMING

UNIT-V
@IT 2024
NumPy

@IT 2024
NumPy

• Stands for Numerical Python


• Is the fundamental package required for high performance computing and data analysis
• NumPy is so important for numerical computations in Python is because it is designed
for efficiency on large arrays of data.
• It provides
• ndarray for creating multiple dimensional arrays
• Standard math functions for fast operations on entire arrays of data without
having to write loops
• NumPy Arrays are important because they enable you to express batch operations
on data without writing any for loops. We call this vectorization.

@IT 2024
NumPy ndarray vs list

• One of the key features of NumPy is its N-dimensional array object, or ndarray, which is
a fast, flexible container for large datasets in Python.
• Whenever you see “array,” “NumPy array,” or “ndarray” in the text, with few exceptions
they all refer to the same thing: the ndarray object.
• NumPy-based algorithms are generally 10 to 100 times faster (or more) than their pure
Python counterparts and use significantly less memory.
import numpy as np
my_arr = np.arange(1000000)
my_list = list(range(1000000))

@IT 2024
ndarray
• ndarray is used for storage of homogeneous data
• i.e., all elements the same type
• Every array must have a shape and a dtype
• Supports convenient slicing, indexing and efficient vectorized computation

import numpy as np
data1 = [6, 7.5, 8, 0, 1]
arr1 = np.array(data1)
print(arr1)
print(arr1.dtype)
print(arr1.shape)
print(arr1.ndim)

@IT 2024
Creating a Numpy Array
• Arrays in Numpy can be created by multiple ways, with various number of Ranks, defining the size of the Array.
• Arrays can also be created with the use of various data types such as lists, tuples, etc.
• The type of the resultant array is deduced from the type of the elements in the sequences.
Note: Type of array can be explicitly defined while creating the array.

# Python program for


# Creation of Arrays
import numpy as np
# Creating a rank 1 Array arr = np.array([1, 2, 3]) Output
print("Array with Rank 1: \n",arr)
Array with Rank 1:
# Creating a rank 2 Array [1 2 3]
arr = np.array([[1, 2, 3], [4, 5, 6]])
print("Array with Rank 2: \n", arr)
Array with Rank 2:
[[1 2 3] [4 5 6]]
# Creating an array from tuple
arr = np.array((1, 3, 2)) Array created using passed tuple:
print("\nArray created using " "passed tuple:\n", arr) [1 3 2]

@IT 2024
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)
[[1. 2. 3.]
[4. 5. 6.]]
print(arr * arr)
[[ 1. 4. 9.]
[16. 25. 36.]]
print(arr - arr)
[[0. 0. 0.]
[0. 0. 0.]]

@IT 2024
Arithmatic with NumPy Arrays

• Arithmetic operations with scalars propagate the scalar argument to each


element in the array:
arr = np.array([[1., 2., 3.], [4., 5., 6.]])
print(arr)
[[1. 2. 3.]
[4. 5. 6.]]

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

arr2 = np.array([[0., 4., 1.], [7., 2., 12.]])


print(arr2)
[[ 0. 4. 1.]
[ 7. 2. 12.]]
print(arr2 > arr)
[[False True False]
[ True False True]] @IT 2024
NumPy Array Indexing
• Array indexing is the same as accessing an array element.
• You can access an array element by referring to its index number.
• The indexes in NumPy arrays start with 0, meaning that the first element has index 0, and the second has index
1 etc.

Example : Get the first element from the following array:

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

Example : Get third and fourth elements from the following array and add them.

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

@IT 2024
NumPy Array Indexing
Access 2-D Arrays
• To access elements from 2-D arrays we can use comma separated integers representing the dimension and the
index of the element.
• Think of 2-D arrays like a table with rows and columns, where the dimension represents the row and the index
represents the column.

Example : Access the element on the first row, second column:


import numpy as np
arr = np.array([[1,2,3,4,5], [6,7,8,9,10]])
print('2nd element on 1st row: ', arr[0, 1])

Example : Access the element on the 2nd row, 5th column:


import numpy as np
arr = np.array([[1,2,3,4,5], [6,7,8,9,10]])
print('5th element on 2nd row: ', arr[1, 4])

@IT 2024
NumPy Array Slicing
• 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].
• If we don't pass start its considered 0
• If we don't pass end its considered length of array in that dimension
• If we don't pass step its considered 1

Example:-Slice elements from index 1 to index 5 from the following array:

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

Example -Return every other element from the entire array(Use of Step)

import numpy as np
arr = np.array([1, 2, 3, 4, 5, 6, 7])
print(arr[::2])
@IT 2024
NumPy Array Slicing
• 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].
• If we don't pass start its considered 0
• If we don't pass end its considered length of array in that dimension
• If we don't pass step its considered 1

Example:-Slice elements from index 1 to index 5 from the following array:

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

Example -Return every other element from the entire array(Use of Step)

import numpy as np
arr = np.array([1, 2, 3, 4, 5, 6, 7])
print(arr[::2])
@IT 2024
Matplotlib

@IT 2024
Introduction to Matplotlib in Python

• Matplotlib is a comprehensive library for creating static, animated, and interactive


visualizations in Python.
• Matplotlib was created by John D. Hunter.
• Matplotlib is open source and we can use it freely.
• It is widely used for generating plots, histograms, power spectra, bar charts, error
charts, scatterplots, and more.

@IT 2024
Introduction to Matplotlib in Python

• There are five key plots that are used for data visualization.

@IT 2024
The General Concept of Matplotlib

• A Matplotlib figure can be categorized into various parts as below:

@IT 2024
Built-in functions in Matplotlib
Myplot module:
Most of the Matplotlib utilities lies under the pyplot submodule, and are usually imported under
the plt alias:

import matplotlib.pyplot as plt

Example:
Draw a line in a diagram from position (0,0) to position (6,250):

import matplotlib.pyplot as plt


import numpy as np
xpoints = np.array([0, 6])
ypoints = np.array([0, 250])
#ploting our canvas
plt.plot(xpoints, ypoints)
#display the graph
plt.show()
@IT 2024
Built-in functions in Matplotlib
1. plot()
• The plot() function is used to draw points (markers) in a diagram.
• By default, the plot() function draws a line from point to point.

• Syntax
plot(x,y) #Parameter 1 & 2 is an array containing the points on the x and y-axis
Example 1:
Draw a line in a diagram from position (1,3) to position (8,10):

import matplotlib.pyplot as plt


import numpy as np

xpoints = np.array([1, 8])


ypoints = np.array([3, 10])

plt.plot(xpoints, ypoints)
plt.show() @IT 2024
Built-in functions in Matplotlib
Example 2:
Draw a line in a diagram from position (1, 3) to (2, 8) then to (6, 1) and finally to position (8, 10):

import matplotlib.pyplot as plt


import numpy as np

xpoints = np.array([1, 2, 6, 8])


ypoints = np.array([3, 8, 1, 10])

plt.plot(xpoints, ypoints)
plt.show()

@IT 2024
@IT 2024

You might also like