0% found this document useful (0 votes)
63 views5 pages

Handout 2 Machine Learning-Matplotlib and Pandas: Plot Function

This document contains 10 questions and solutions about machine learning concepts using Python libraries like Matplotlib and Pandas. The questions cover topics like plotting lines, scatter plots, and creating DataFrames from CSV files. The solutions demonstrate how to label axes, set plot titles, create cross tabulations from DataFrames, and read/preview data from CSV files.

Uploaded by

Arun Rangrej
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)
63 views5 pages

Handout 2 Machine Learning-Matplotlib and Pandas: Plot Function

This document contains 10 questions and solutions about machine learning concepts using Python libraries like Matplotlib and Pandas. The questions cover topics like plotting lines, scatter plots, and creating DataFrames from CSV files. The solutions demonstrate how to label axes, set plot titles, create cross tabulations from DataFrames, and read/preview data from CSV files.

Uploaded by

Arun Rangrej
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/ 5

HANDOUT 2

Machine Learning-Matplotlib and Pandas

Q1. Write a Python program to draw a line using given axis values with suitable label in the x
axis, y axis and a title.

Solution: import matplotlib.pyplot as plt


# x axis values
x = [1,2,3]
# y axis values
y = [2,4,1]
# Plot lines and/or markers to the Axes.
plt.plot(x, y)
# Set the x axis label of the current axis.
plt.xlabel('x - axis')
# Set the y axis label of the current axis.
plt.ylabel('y - axis')
# Set a title
plt.title('Sample graph!')
# Display a figure.
plt.show()

( A line plot can be created by calling the plot() function and passing the x-axis data for
the regular interval, and y-axis for the observations.)

Q2. Write a Python program to draw a line with suitable label in the x axis, y axis(3 times of
x) and a title.
Solution:
import matplotlib.pyplot as plt
X = range(1, 50)
Y = [value * 3 for value in X]
print("Values of X:")
print(*range(1,50))
print("Values of Y (thrice of X):")
print(Y)
# Plot lines and/or markers to the Axes.
plt.plot(X, Y)
# Set the x axis label of the current axis.
plt.xlabel('x - axis')
# Set the y axis label of the current axis.
plt.ylabel('y - axis')
# Set a title
plt.title('Draw a line.')
# Display the figure.
plt.show()

Q3. Write a Python program to plot quantities which have an x and y position making use
of pylab

Solution:
import numpy as np
import pylab as pl
# Make an array of x values
x1 = [2, 3, 5, 6, 8]
# Make an array of y values for each x value
y1 = [1, 5, 10, 18, 20]
# Make an array of x values
x2 = [3, 4, 6, 7, 9]
# Make an array of y values for each x value
y2 = [2, 6, 11, 20, 22]
# set new axes limits
pl.axis([0, 10, 0, 30])
# use pylab to plot x and y as red circles
pl.plot(x1, y1,'b*', x2, y2, 'ro')
# show the plot on the screen
pl.show()

Q4. Write a Python program to plot several lines with different format styles in one
command using arrays

Solution: import numpy as np


import matplotlib.pyplot as plt
# Sampled time at 200ms intervals
t = np.arange(0., 5., 0.2)
# green dashes, blue squares and red triangles
plt.plot(t, t, 'g--', t, t**2, 'bs', t, t**3, 'r^')
plt.show()

Q5. Write a Python program to create multiple plots

Solution:
import matplotlib.pyplot as plt
fig = plt.figure()
fig.subplots_adjust(bottom=0.020, left=0.020, top = 0.400,right=0.500)
plt.subplot(2, 1, 1)
plt.xticks(()), plt.yticks(())
plt.subplot(2, 3, 4)
plt.xticks(())
plt.yticks(())
plt.subplot(2, 3, 5)
plt.xticks(())
plt.yticks(())
plt.subplot(2, 3, 6)
plt.xticks(())
plt.yticks(())
plt.show()

Q6. Write a Python program to display a DataFrame having values for Name, Exam,
Subject and Result.

Solution: import pandas as pd


import numpy as np
d = { 'Name':['Alisa','Bobby','Cathrine','Alisa','Bobby','Cathrine'],
'Exam':['Semester 1','Semester 1','Semester 1', Semester2', 'Semester
2','Semester 2'], 'Subject':
['Mathematics','Mathematics','Mathematics','Science','Science',
'Mathematics'],
'Result':['Pass','Pass','Fail','Pass','Fail','Pass']}
df = pd.DataFrame(d,columns=['Name','Exam','Subject','Result'])
df

Q7. Write a Python program to create a two-way cross tab for above DataFrame having
for Subject vs Results summary.

Solution: pd.crosstab(df.Subject, df.Result,margins=True)

Q8. Write a Python program to create a three-way cross tab for above DataFrame having
for Subject,Exams vs Results summary.
Solution:

pd.crosstab([df.Subject, df.Exam],df.Result, margins=True)

Q9. Write a Python program to create a scatter plot for given x and y values.

Solution:
import matplotlib.pyplot as plt
x = [5,7,8,7,2,17,2,9,4,11,12,9,6]
y = [99,86,87,88,111,86,103,87,94,78,77,85,86]
plt.scatter(x,y)
plt.show()

Q10 . Write a python program to read data from a csv file.(diabetes.csv)


Solution:
a) # Load libraries
import pandas as pd
# load dataset
pima = pd.read_csv('D://diabetes.csv')
X = list(pima.columns)
print("List of Attributes:", X)
X.remove('Outcome') #Remove the class attribute
print("Predicting Attributes:", X)
X=pima[X]
Y=pima.Outcome
pima.head()

b) import numpy as np
import pandas as pd
from pandas import DataFrame
df= pd.read_csv('D://weather.csv')
cols = list(df.columns)
print("List of Attributes:",cols)

You might also like