Machine Learning
Laboratory
[Link]
23N266
23N266 pg. 1
1. Write a Python program to draw a scatter graph taking a random distribution in X and Y
and plotted against each other.
import [Link] as plt
import numpy as np
import random
import math
Xs = [Link].standard_normal(100)
Ys= [Link].standard_normal(100)
[Link](Xs, Ys , cmap='blue')
[Link]("Xs")
[Link]("Ys")
[Link]()
C:\Users\yeshw\AppData\Local\Temp\ipykernel_35440\[Link]:
UserWarning: No data for colormapping provided via 'c'. Parameters
'cmap' will be ignored
[Link](Xs, Ys , cmap='blue')
1. Write a Python program to draw a scatter plot with empty circles taking a random
distribution in X and Y and plotted against each other.
[Link](Xs , Ys,facecolors = "none" ,edgecolors="r")
<[Link] at 0x1edbb66bbf0>
23N266 pg. 2
1. Write a Python program to draw a scatter plot using random distributions to generate
balls of different sizes.
colors = [[Link](1,4) for i in range(100)]
areas = [[Link] * [Link](5, 12)**2 for i in range(100)]
[Link](Xs, Ys ,s = areas ,c= colors , alpha= .9 )
<[Link] at 0x1edbb727dd0>
23N266 pg. 3
1. Write a Python program to draw a scatter plot comparing two subject marks of
Mathematics and Science
math_marks = [88, 92, 80, 89, 100, 80, 60, 100, 80, 34]
science_marks = [35, 79, 79, 48, 100, 88, 32, 45, 20, 30]
marks_range = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
[Link](marks_range , math_marks ,c = "r" , label = "Math Mark")
[Link](marks_range, science_marks,c = "b" , label = "Science
Mark")
[Link]("Maths Vs Science")
[Link]("Marks Range")
[Link]('Marks Scored')
[Link]()
<[Link] at 0x1edbb649fa0>
23N266 pg. 4
1. Write a Python program to draw a scatter plot for three different groups comparing
weights and heights
w1=[67,57.2,59.6,59.64,55.8,61.2,60.45,61,56.23,56]
b1=[101.7,197.6,98.3,125.1,113.7,157.7,136,148.9,125.3,114.9]
w2=[61.9,64,62.1,64.2,62.3,65.4,62.4,61.4,62.5,63.6]
b2=[152.8,155.3,135.1,125.2,151.3,135,182.2,195.9,165.1,125.1]
w3=[68.2,67.2,68.4,68.7,71,71.3,70.8,70,71.1,71.7]
b3=[165.8,170.9,192.8,135.4,161.4,136.1,167.1,235.1,181.1,177.3]
w = [Link]((w1,w2,w3))
b= [Link]((b1,b2,b3))
[Link](w, b , marker= "*" , c="g")
[Link]("Weights")
[Link]("Height")
[Link]("Weight vs Height")
Text(0.5, 1.0, 'Weight vs Height')
23N266 pg. 5
1. Write a Python program to draw a scatter plot to find sea level rise in past 100 years.
years = [Link](100)
sea_l = sorted([ [Link].standard_normal() for _ in range(100)])
[Link](sea_l , years , c="b" , s=10)
<[Link] at 0x1edbb5b5880>
23N266 pg. 6
Barchart
import [Link] as plt
1. Write a Python programming to display a bar chart of the popularity of programming
Languages.
Programming = ["Java", "Python", "PHP", "JavaScript", "C#", "C++"]
Popularity = [ 22.2, 17.6, 8.8, 8, 7.7, 6.7]
x= [ i for i, _ in enumerate(Programming)]
[Link](x, Popularity , color ="blue")
[Link](x, Programming)
plt.minorticks_on()
[Link](which='major' ,linestyle = "-" ,linewidth = '.5' , color =
'red')
[Link](which='minor' ,linestyle = ":" ,linewidth = '.5' , color =
'black')
[Link]("Languages ")
[Link]("Popularity ")
[Link]("Programming and Languages")
Text(0.5, 1.0, 'Programming and Languages')
23N266 pg. 7
1. Write a Python programming to display a horizontal bar chart of the popularity of
programming Languages
[Link](x, Popularity , color = "red")
[Link](x, Programming)
plt.minorticks_on()
[Link](which= "minor" , linestyle = ":" , linewidth = ".5" , color =
"black")
[Link](which= "major" , linestyle = "-" , linewidth = ".5" , color =
"blue")
23N266 pg. 8
1. Write a Python programming to display a bar chart of the popularity of programming
Languages. Use uniform color.
Programming = ["Java", "Python", "PHP", "JavaScript", "C#", "C++"]
Popularity = [ 22.2, 17.6, 8.8, 8, 7.7, 6.7]
x= [ i for i, _ in enumerate(Programming)]
[Link](x, Popularity , color =(.4, .6 , 0,1))
[Link](x, Programming)
plt.minorticks_on()
[Link](which='major' ,linestyle = "-" ,linewidth = '.5' , color =
'red')
[Link](which='minor' ,linestyle = ":" ,linewidth = '.5' , color =
'black')
[Link]("Languages ")
[Link]("Popularity ")
[Link]("Programming and Languages")
Text(0.5, 1.0, 'Programming and Languages')
23N266 pg. 9
1. Write a Python programming to display a bar chart of the popularity of programming
Languages. Use different color for each bar.
Programming = ["Java", "Python", "PHP", "JavaScript", "C#", "C++"]
colors = [ "red" , "green", "black" , "blue", "yellow" , "cyan"]
Popularity = [ 22.2, 17.6, 8.8, 8, 7.7, 6.7]
x= [ i for i, _ in enumerate(Programming)]
[Link](x, Popularity , color =colors)
[Link](x, Programming)
plt.minorticks_on()
[Link](which='major' ,linestyle = "-" ,linewidth = '.5' , color =
'red')
[Link](which='minor' ,linestyle = ":" ,linewidth = '.5' , color =
'black')
[Link]("Languages ")
[Link]("Popularity ")
[Link]("Programming and Languages")
Text(0.5, 1.0, 'Programming and Languages')
23N266 pg. 10
1. Write a Python programming to display a bar chart of the popularity of programming
Languages. Attach a text label above each bar displaying its popularity (float value).
[Link](x, Popularity, color=colors)
[Link](x, Programming)
# Add text labels above each bar
for i, v in enumerate(Popularity):
[Link](i, v + 0.5, '%.3f' % float(v), ha='center')
plt.minorticks_on()
[Link](which='major', linestyle="-", linewidth='.5', color='red')
[Link](which='minor', linestyle=":", linewidth='.5', color='black')
[Link]("Languages")
[Link]("Popularity")
[Link]("Programming Languages Popularity")
Text(0.5, 1.0, 'Programming Languages Popularity')
23N266 pg. 11
1. Write a Python program to create bar plot of scores by group and gender. Use multiple X
values on the same chart for men and women.
men=(22, 30, 35, 35, 26)
women = (25, 32, 30, 35, 29)
group = 5
index = [Link](group)
bar_width = 0.35
[Link](index, men, bar_width, label='Men')
[Link](index + bar_width, women, bar_width, label='Women')
[Link]('Group')
[Link]('Scores')
[Link]('Scores by Group and Gender')
[Link](index + bar_width/2, ['A', 'B', 'C', 'D', 'E'])
[Link]()
<[Link] at 0x1edbcf9ed20>
23N266 pg. 12
1. Write a Python program to create bar plot from a DataFrame.
# Create sample dataframe
import pandas as pd
data = {
'a': [2, 4, 6, 8, 10],
'b': [4, 2, 4, 2, 2],
'c': [8, 3, 7, 6, 4],
'd': [5, 4, 4, 4, 3],
'e': [7, 2, 7, 8, 3],
'f': [6, 6, 8, 6, 2]
}
df = [Link](data)
# Plot bar chart
[Link](x='a', y=['b','c','d','e','f'], kind='bar', width=0.8)
[Link]('a values')
[Link]('Values')
[Link]('Bar Plot from DataFrame')
[Link](loc='center left', bbox_to_anchor=(1, 0.5))
plt.tight_layout()
23N266 pg. 13
1. Write a Python program to create bar plots with error bars on the same figure.
N = 5
menMeans = (54.74, 42.35, 67.37, 58.24, 30.25)
menStd = (4, 3, 4, 1, 5)
# the x locations for the groups
ind = [Link](N)
# the width of the bars
width = 0.35
[Link](ind, menMeans, width, yerr=menStd, color='red')
[Link]('Mean Velocity')
[Link]('Groups')
[Link]('Mean Velocities with Error Bars')
[Link](True, linestyle='--', alpha=0.7)
23N266 pg. 14
1. Write a Python program to create a stacked bar plot with error bars. Note: Use bottom to
stack the women?s bars on top of the men?s bars.
# Width of the bars
width = 0.35
# Men's data is already defined
menMeans = men
womenMeans = women
menStd = (4, 3, 4, 1, 5)
womenStd = (3, 5, 2, 3, 3)
# Create stacked bar chart
[Link](figsize=(10, 6))
p1 = [Link](index, menMeans, width, yerr=menStd, label='Men',
color='skyblue')
p2 = [Link](index, womenMeans, width, yerr=womenStd, bottom=menMeans,
label='Women', color='lightpink')
[Link]('Scores')
[Link]('Groups')
[Link]('Scores by Group and Gender')
[Link](index, ['A', 'B', 'C', 'D', 'E'])
23N266 pg. 15
[Link]()
[Link](True, linestyle='--', alpha=0.8)
PIE CHART
1. Write a Python programming to create a pie chart of the popularity of programming
Languages.
[Link](Popularity, labels=Programming, autopct='%1.1f%%',
shadow=True)
[Link]('equal')
[Link]("Programming Languages Popularity")
[Link]()
23N266 pg. 16
1. Write a Python programming to create a pie chart of gold medal achievements of five
most successful countries in 2016 Summer Olympics. Read the data from a csv file.
medal_data = {
'country': ['United States', 'Great Britain', 'China', 'Russia',
'Germany'],
'gold_medal': [46, 27, 26, 19, 17]
}
df_medals = [Link](medal_data)
# Create pie chart
[Link](figsize=(5, 4))
[Link](df_medals['gold_medal'],
labels=df_medals['country'],
autopct='%1.1f%%',
shadow=True,
startangle=90)
[Link]('Gold Medal Distribution in 2016 Summer Olympics')
[Link]('equal')
(-1.0999999392244875,
1.099999635044311,
-1.0999928170440252,
1.0999996579544773)
23N266 pg. 17
23N266 pg. 18