Exp 9
Exp 9
AIM:-
To create a python program to perform operations of class and objects.
THEORY:-
Pyplot is a Matplotlib module which provides a MATLAB-like interface.
Matplotlib is designed to be as usable as MATLAB, with the ability to use Python
and the advantage of being free and open-source. Each pyplot function makes
some change to a figure: e.g., creates a figure, creates a plotting area in a figure,
plots some lines in a plotting area, decorates the plot with labels, etc. The various
plots we can utilize using Pyplot are Line Plot, Histogram, Scatter, 3D Plot, Image,
Contour, and Polar.
1. Import Matplotlib and write the code to generate the following chart.
Hint:
fig1 = plt.figure()
ax1 = fig1.add_axes([0,0,1,1])
ax2 = fig1.add_axes([0.2,0.5,.4,.4])
CODE:-
import matplotlib.pyplot as plt
import math
fig1 = plt.figure()
ax1 = fig1.add_axes([0,0,1,1])
ax2 = fig1.add_axes([0.2,0.5,0.4,0.4])
y1 = [2*pow(i, 2)+2 for i in range(0, 100)]
y2 = [41, 41, 41, 41, 41]
x1 = [i for i in range(0, 100)]
x2 = [41, 42, 43, 45, 46]
ax1.plot(x1, y1)
ax2.plot(x2, y2)
plt.show()
OUTPUT:-
3. Given the following information, draw line charts that show the performance of each of the
players across all seasons of IPL (2008-2022).
ViratKohli=[165,246,307,557,364,634,359,505,973,308,530,464,466,405,309]
DineshKarthik=[145, 288, 278, 282, 238, 510, 325, 141, 335, 361, 498, 253, 169, 223, 287]
GlennMaxwell = [0, 0, 0, 0, 6, 36, 552, 145, 179, 310, 169, 108, 513, 268]
CODE:-
import matplotlib.pyplot as plt
ViratKohli=[165,246,307,557,364,634,359,505,973,308,530,464,466,405,309]
DineshKarthik=[145, 288, 278, 282, 238, 510, 325, 141, 335, 361, 498, 253, 169,
223, 287]
GlennMaxwell = [0, 0, 0, 0, 6, 0, 36, 552, 145, 179, 310, 169, 108, 513, 268]
x = [2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020,
2021, 2022]
plt.figure(0)
plt.plot(x, ViratKohli)
plt.show()
plt.figure(1)
plt.plot(x, DineshKarthik)
plt.show()
print(len(GlennMaxwell))
plt.figure(2)
plt.plot(x, GlennMaxwell)
plt.show()
OUTPUT:-
4. Draw a scatter plot to show the performance of DK over the different seasons. Vary the size
of the circles/dots with respect to the scores (Bigger the scores, larger the circle).
CODE:-
import matplotlib.pyplot as plt
DineshKarthik=[145, 288, 278, 282, 238, 510, 325, 141, 335, 361, 498, 253, 169,
223, 287]
s = [n for n in DineshKarthik]
plt.scatter(x, DineshKarthik, s = s)
plt.show()
OUPUT:-
5. Generate a histogram that shows scores of ViratKohli spread across the ranges(0-100,100-
200,.......,900-1000).
CODE:-
import matplotlib.pyplot as plt
ViratKohli=[165,246,307,557,364,634,359,505,973,308,530,464,466,405,309]
plt.hist(ViratKohli)
plt.show()
OUTPUT:-
6. Visualize the performances of Virat, DK and Maxwell using scatterplot. Plot each of their
performances against Ranges=[200,300,400,500,600,700,800,900,1000]. Use a different color
for each player.
CODE:-
import matplotlib.pyplot as plt
ViratKohli=[165,246,307,557,364,634,359,505,973,308,530,464,466,405,309]
DineshKarthik=[145, 288, 278, 282, 238, 510, 325, 141, 335, 361, 498, 253, 169,
223, 287]
GlennMaxwell = [0, 0, 0, 0, 6, 0, 36, 552, 145, 179, 310, 169, 108, 513, 268]
s = [i for i in range(len(ViratKohli))]
plt.scatter(s , ViratKohli, c= 'red')
plt.scatter(s, DineshKarthik, c = 'black')
plt.scatter(s, GlennMaxwell, c = 'green')
plt.show()
OUTPUT:-
CODE:-
import matplotlib.pyplot as plt
team= ["RR", "LSG", "KKR", "PBKS", "DC", "CSK", "MI", "SRH", "RCB", "GT"]
sixes = [113,115,113,110,106,103,100,97,95,75]
fig = plt.figure(figsize = (10, 5))
plt.bar(team, sixes)
plt.show()
OUTPUT:-
Asarray - numpy.asarray()function is used when we want to convert input to an array. Input can
be lists, lists of tuples, tuples, tuples of tuples, tuples of lists and arrays.
Empty - is used to create a new array of given shape and type, without initializing entries.
Empty_like - Return a new array with the same shape and type as a given array.
ii) Illustrate the following numpy.linalg functions. For each function, create a different 2D array
(Vary the shapes and values of the array)
ANS:
numpy.linalg.eig(a) : This function is used to compute the eigenvalues and right eigenvectors of
a square array.
numpy.linalg.eigvals() : Compute the eigenvalues of a general matrix.
numpy.linalg.eigvalsh(a[, UPLO]) : Compute the eigenvalues of a complex Hermitian or real
symmetric matrix.
numpy.vdot(vector_a, vector_b) : Returns the dot product of vectors a and b. If first argument is
complex the complex conjugate of the first argument(this is where vdot() differs working of dot()
method) is used for the calculation of the dot product.
numpy.linalg.solve() : Solve a linear matrix equation, or system of linear scalar
equations.Computes the “exact” solution, x, of the well-determined, i.e., full rank, linear matrix
equation ax = b.
numpy.linalg.lstsq() : Return the least-squares solution to a linear matrix equation.Solves the
equation a x = b by computing a vector x that minimizes the Euclidean 2-norm || b – a x ||^2.
diag
dot
trace
Det
CODE:-
import numpy as np
a = np.array([[1,2], [5,6]])
b = np.array([[1,2,3,4], [5,6,7,8]])
print(a.diagonal())
print(a.dot(b))
print(a.trace())
print(np.linalg.det(a))
OUTPUT:-
[1 6]
[[11 14 17 20]
[35 46 57 68]]
7
-3.999999999999999
9. I)Create a 2D boolean array, where the size of the dimensions are 3 and 4 respectively.
Transform the array into 2 X 6 , 4 X 3 and 6 X 2 arrays and display them.
CODE:-
import numpy as np
a = np.array([[1,2,3],[4,5,6],[7,8,9],[10,11,12]])
print(np.reshape(a, (2,6)))
print(np.reshape(a, (4,3)))
print(np.reshape(a, (6,2)))
OUTPUT:-
[[ 1 2 3 4 5 6]
[ 7 8 9 10 11 12]]
[[ 1 2 3]
[ 4 5 6]
[ 7 8 9]
[10 11 12]]
[[ 1 2]
[ 3 4]
[ 5 6]
[ 7 8]
[ 9 10]
[11 12]]
Ii) Create 2 1D arrays and fill them with random values between 0 and 100. Find and display the
common values in both the arrays.
CODE:-
from numpy import random
a=random.randint(20, size=(10))
b=random.randint(20, size=(10))
print(np.intersect1d(a, b))
OUTPUT:-
[ 8 14]
iii) Create an array of random values and find the sum of the elements in the array, minimum
and maximum element. Also display the sorted array.
CODE:-
from numpy import random
import numpy as np
x = random.randint(20, size = (10))
print("Sum is :", x.sum())
print("Minimum Value is :", np.min(x))
print("Maximum Value is :", np.max(x))
OUTPUT:-
Sum is : 124
Minimum Value is : 2
Maximum Value is : 18
iv) Given the following matrix, create the equivalent boolean matrix.
CODE:-
import numpy as np
a = [[1,1,0],[0,0,1],[1,1,1],[0,0,1]]
x = np.array(a)
print(x > 0)
OUTPUT:-
[[ True True False]
[False False True]
[ True True True]
[False False True]]
10. Create a 6 x 5 matrix with random values between from 10 to 50 and find and display the indices
of even numbers.
CODE:-
import numpy as np
x = np.random.randint(10, 50, size = (6, 5))
m = x[x % 2 == 0]
print(m)
OUTPUT:-
[24 18 32 36 22 44 44 46 18 34 20 18 28 44 10]
RESULT:-
Thus the above program is executed successfully.