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

Assigniment 2 Machine Learning

This document outlines an assignment on machine learning concepts for a course. It includes 7 tasks covering lists, dictionaries, tuples, NumPy, functions, matplotlib, Pandas, image manipulation and video manipulation. Detailed instructions are provided for each task.

Uploaded by

shahfaisal gfg
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Assigniment 2 Machine Learning

This document outlines an assignment on machine learning concepts for a course. It includes 7 tasks covering lists, dictionaries, tuples, NumPy, functions, matplotlib, Pandas, image manipulation and video manipulation. Detailed instructions are provided for each task.

Uploaded by

shahfaisal gfg
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

University of Management and Technology

Machine Learning
Assignment 01 (CLO-02)
Course Instructor: Aqsa Afzal

Name: Roll Number:

Date: Dated:

Semester: Marks Obtained:

INSTRUCTIONS

✓ Complete the assignment in a group of two only.


✓ Late submissions will not be accepted under any circumstances.
✓ Prepare for a viva session, where you may be questioned on the content of your assignment.
✓ Ensure figures and diagrams are clearly labeled
✓ Submit your assignment in .ipynb format and hard form too.

Instructor Signature
Checking Libraries
Before checking the libraries, we will also install one more library. Run the followingcommand in your
terminal:
pip install python-mnist
There should be a confirmation message telling you that the library is installed success-fully.
Now, make a Python file with your text editor and run the following code to check if therelevant libraries
are installed on your machine:
import numpy as npimport
scipy as sp
from scipy.linalg import null_spacefrom
numpy.linalg import inv
from matplotlib.image import imreadimport
matplotlib.pyplot as plt import pandas as
pd
import math
from scipy import statsimport
random
from numpy.random import seed from
numpy.random import rand from
numpy.random import randn
from scipy.stats import pearsonr from
scipy.stats import ttest_ind from scipy.stats
import mannwhitneyufrom mnist import
MNIST
from sklearn.preprocessing import MinMaxScalerfrom
sklearn.svm import SVC
from sklearn import metrics from
skimage.feature import hog
from sklearn.decomposition import PCAfrom
scipy.io import wavfile
from scipy.io.wavfile import writeimport
wave
import sys

Task 1: Lists, Dictonaries, Tuples (10 Marks)

Lists
Given a list:
nums = [3, 4, 7, 8, 15]
Make another list named cubes and append the cubes of the given list in this list andprint it.

Dictionaries
You are given an empty dictionary:
dic = {}
Add the following data to the dictionary: ‘person’: 2, ‘cat’: 4, ‘spider’: 8, ‘horse’: 4as key value
pairs.
Use the ‘items’ method to loop over the dictionary and print the animals and theircorresponding
legs.
Sum the legs of each animal, and print the total at the end.

Tuples
Given the following tuple:
D = (1,15,4,[5,10])
Change the value in the list from ‘5’ to ‘3’.
Delete the tuple D.Given
another tuple:

E = (‘a’,‘p’,‘p’,‘l’,‘e’)
Print the number of occurences of ‘p’ in tuple E.
Print the index of ‘l’ in tuple E.

Task 2: Numpy (15 marks)


You should use all built in functions of numpy in this task, a list is available here

Convert matrix M into numpy array

• Use slicing to pull out the subarray consisting of the first 2 rows and columns 1 and 2.
• Store it in b which is a numpy array of shape (2, 2).

• Create an empty matrix ‘y’ with the same shape as ‘M’.


• Add the vector z to each column of the matrix M with an explicit loop and store it in y.
Given:

A = np.array([[1,2],[3,4]])

B = np.array([[5,6],[7,8]])

v = np.array([9,10])

Add the two matrices A and B.


Multiply the two matrices A and B.
Take the element wise square root of matrix A.
Take the dot product of the matrix A and vector v.
Compute sum of each column of A.
Print the transpose of B.

Task 3: Functions and For Loops (10 marks)

Function
Declare a function Compute that takes two arguments: distance and time, and use it tocalculate velocity.
Forloop
Declare a list even that contains all even numbers up till 16. Declare a function sum thattakes the list as an
argument and calculates the sum of all entries using a for loop.

Task 4: Matplotlib (15 marks)


Import the plotting function by the command:
import matplotlib.pyplot as plt

Plotting a single line


Compute the x and y coordinates for points on a sine curve and plot the points usingmatplotlib. Use the
function plt.show()

Plotting multiple lines


Compute the x and y coordinates for points on sine and cosine curves and plot them onthe same graph
using matplotlib. Add x and y labels to the graph as well.

Subplots
Compute the x and y coordinates for points on sine and cosine curves. Set up a subplotgrid that has height
2 and width 1, and set the first such subplot as active. Plot the sineand cosine graphs.
Hint: Use the plt.subplot() function

Task 5: Pandas DataFrame (15 marks)

Making a DataFrame
Create a dataframe pd that contains 5 rows and 4 columns, similar to the one given below:
Col Col Col Col
1 2 3 4
1 6 7 7
2 7 78 5
3 5 78 707
4 5 18 60
5 8 88 4

• Print only the first two rows of the dataframe.


• Print the second column.
• Change the name of the third column from “Col3” to “XYZ”.
• Add a new column to the dataframe and name it “Sum”.
• Sum the entries of each row and add the result in the column “Sum”.
Task 6: Image Manipulation (20 marks)

Importing Libraries
Run the following commands to import libraries

import matplotlib
from mpl_toolkits import mplot3d from
matplotlib import pyplot as pltfrom matplotlib
import cm
from matplotlib import image as mpimgfrom
matplotlib.pyplot import figure
%matplotlib inline import
seaborn as snsimport numpy
as np
import matplotlib.pylab as pl
from matplotlib.colors import ListedColormap

Loading an Image
Read image from file to memory using the given command:
img = np.array(mpimg.imread(‘path’))
Replace ‘path’ in the above command with the path of your image e.g. intro/cat.jpg
Now run the command:
img.setflags(write=1)
This allows us to manipulate the image

Tasks
Display your image using the plt.imshow function.

• Crop the image.


• Create 50 randomly placed markers on your image. You can check the followinglink for
learning about markers.

• Carry out color analysis by accessing the RGB values and plotting them. Use the
• seaborn library.
• View only the red values.
Hint: Set the blue and green values to 0

Task 7: Video Manipulation (15 marks)

Importing Libraries
Run the following commands to import libraries

import cv2
import numpy as np
Loading a Video
Create a VideoCapture object and read the input from camera:
video = cv2.VideoCapture(0)

Tasks
• Check if the camera is open.
• Read the video from camera feed.
• Write the video file into memory using the videoWriter function.

Page 7 of 7

You might also like