Artificial Intelligence and Machine Learning
ISBN 9788119221196

Highlights

Notes

  

Chapter 3: Study of Python Libraries: Continued

Matplotlib: ◀◀◀

Matplotlib is a python library/package used for 2-dimensional graphics. Matplotlib is a comprehensive library for creating static, animated, and interactive visualizations in Python.

Implementation of matplotlib library with linewidth and linestyle attribute.

import matplotlib.pyplot as pl

x = [1, 2, 3, 4]

y = [9, 8, 7, 6]

pl.xlabel(“X-axis”)

pl.ylabel(“Y-axis”)

pl.plot(x,y,’r’,linewidth = 3, linestyle = ‘dashdot’)

pl.show()

Output:

Performing marker and various line style attribute:

Line style as dashed

pl.xlabel(“X-axis”)

pl.ylabel(“Y-axis”)

pl.plot(x,y,’r’,linewidth = 3, linestyle = ‘dashed’)

pl.show()

Output:

Marker as diamond.

pl.xlabel(“X-axis”)

pl.ylabel(“Y-axis”)

pl.plot(x,y,’r’,marker=‘d’)

pl.show()

Output:

pl.xlabel(“X-axis”)

pl.ylabel(“Y-axis”)

pl.plot(x,y,’r’,marker=‘D’)

pl.show()

Output:

Marker as Square

pl.xlabel(“X-axis”)

pl.ylabel(“Y-axis”)

pl.plot(x,y,’r’,marker=‘s’)

pl.show()

Output:

With no Marker

pl.xlabel(“X-axis”)

pl.ylabel(“Y-axis”)

pl.plot(x,y,’r’,marker=‘‘)

pl.show()

Output:

with Marker as Square and Markeredgecolor

pl.xlabel(“X-axis”)

pl.ylabel(“Y-axis”)

pl.plot(x,y,marker=‘s’, markeredgecolor=‘red’)

pl.show()

Output:

With marker as + and markeredgecolor

pl.xlabel(“X-axis”)

pl.ylabel(“Y-axis”)

pl.plot(x,y,marker=‘+’, markeredgecolor=‘red’)

pl.show()

Output:

pl.xlabel(“X-axis”)

pl.ylabel(“Y-axis”)

pl.title(“Student”)

pl.plot(x,y,marker=‘+’, markeredgecolor=‘red’)

pl.show()

Output:

With label attribute

pl.xlabel(“X-axis”)

pl.ylabel(“Y-axis”)

pl.plot(x,y,linewidth=3, label=“sub1”)

pl.legend()

pl.show()

Output:

Demonstration of Grid view:

x2 = [6, 9, 11]

y = [5, 8, 10]

x = [12, 6, 6]

y2 = [6, 14, 8]

pl.xlabel(“X-axis”)

pl.ylabel(“Y-axis”)

pl.plot(x,y,linewidth=3, label=“sub1”)

pl.plot(x2,y2,linewidth=3, label=“sub2”)

pl.legend()

pl.show()

Output:

Demonstration of grid view with color attribute

from matplotlib import style

style.use(‘ggplot’)

pl.xlabel(“X-axis”)

pl.ylabel(“Y-axis”)

pl.plot(x,y,linewidth=3, label=“sub1”)

pl.plot(x2,y2,linewidth=3, label=“sub2”)

pl.legend()

pl.grid(True, color = ‘k’)

pl.show()

Demonstration of bar graph:

x = [1,2,3,4]

y = [9,8,7,6]

pl.xlabel(“X-axis”)

pl.ylabel(“Y-axis”)

pl.bar(x,y, width=[0.1,0.2,0.3,0.4], color = ‘b’)

pl.show()

Output:

Demonstration of bar graph with different color:

x = [1,2,3,4]

y = [9,8,7,6]

pl.xlabel(“X-axis”)

pl.ylabel(“Y-axis”)

pl.bar(x,y, width=[0.1,0.2,0.3,0.4], color = [‘r’,’g’,’b’,’k’])

pl.show()

Output:

Demonstration of Histogram

population_age = [50,30,60,76,34,29,90,100,9,8,5,23,65,34,21,54,87,98,43,56,45,99,12,10,44,35,101]

bins = [0,10,20,30,40,50,60,70,80,90,101]

pl.hist(population_age,bins,histtype = ‘bar’, rwidth=0.5)

pl.xlabel(“X-axis”)

pl.ylabel(“Y-axis”)

pl.title(“Hostogram”)

pl.show()

Output:

x = [1,2,3,4,5,6]

y = [4,6,7,6,1,4]

pl.scatter(x,y)

pl.xlabel(“X-axis”)

pl.ylabel(“Y-axis”)

pl.show()

Output:

Demonstration of Pie chart:

slices = [8,3,3,12]

subjects = [‘Math’,’Science’,’German’,’Hindi’]

cols = [‘g’,’m’,’r’,’b’]

pl.pie(slices, labels = subjects, colors = cols, startangle = 120)

pl.show()

Output:

slices = [8,3,3,12]

subjects = [‘Math’,’Science’,’German’,’Hindi’]

cols = [‘g’,’m’,’r’,’b’]

pl.pie(slices, labels = subjects, colors = cols, startangle = 12)

pl.show()

Output:

slices = [8,3,3,12]

subjects = [‘Math’,’Science’,’German’,’Hindi’]

cols = [‘g’,’m’,’r’,’b’]

pl.pie(slices, labels = subjects, colors = cols, startangle = 120, shadow = True, autopct = ‘%1.1f%%’)

pl.show()

Output:

Scikit Learn ◀◀◀

from numpy import poly1d

p = poly1d([9,8,7])

p

Output: 2

print(p)

Output: 9 x + 8 x + 7

print(p*p)

Output:

4 3 2

81 x + 144 x + 190 x + 112 x + 49

print(2*p)

Output:

2

18 x + 16 x + 14

p.r

Output: array([-0.44444444+0.7617394j, -0.44444444-0.7617394j])

import scipy.special as spl

import numpy as np

a = 8

spl.cbrt(a)

Output: 2.0

x = spl.sindg(90)

x

Output:1.0

y = spl.cosdg(0)

y

Output: 1.0

z = spl.tandg(45)

z

Output: 1.0

r = spl.perm(6,2)

r

Output: 30.0

x = 4

spl.exp10(x)

Output: 10000.0

spl.exp2(2)

Output: 4.0

f = lambda x: x**3

f(3)

Output: 27

u = lambda x,y: x**2 + y**2 + 2*x*y

u(4,5)

Output: 81

from scipy import integrate

f = lambda x: x**3

i = integrate.quad(f,1,2)

i

Output: (3.7500000000000004, 4.1633363423443377e-14)

import math

print(math.radians(180))

Output: 3.141592653589793