0% found this document useful (0 votes)
70 views13 pages

AD-502 Machine Learning Lab - Exp 1-10

Uploaded by

Himanshu Pawar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
70 views13 pages

AD-502 Machine Learning Lab - Exp 1-10

Uploaded by

Himanshu Pawar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

MACHINE LEARNING LABORATORY -1-

CHAMELI DEVI GROUP OF INSTITUTIONS, INDORE

LABORATORY MANUAL

Machine Learning
AD502

V SEM (AI&DS)

DEPARTMENT OF
ARTIFICIAL INTELLIGENCE AND DATA SCIENCE

CHAMELI DEVI GROUP OF INSTITUTIONS, INDORE. DEPARTMENT OF AI & DS


MACHINE LEARNING LABORATORY -2-

CHAMELI DEVI GROUP OF INSTITUTIONS


INDORE (M.P.)

DEPARTMENT OF
ARTIFICIAL INTELLIGENCE AND DATA SCIENCE

CERTIFICATE

This is to certify that Mr./Ms……………………………………………………………… with RGTU Enrollment No. 0832

..………………………….. has satisfactorily completed the course of experiments in Machine Learning laboratory, as

prescribed by Rajiv Gandhi Proudyogiki Vishwavidyalaya, Bhopal for V Semester of the Artificial Intelligence

and Data Science Department during the year 2024-25

Signature of
Faculty In-charge

CHAMELI DEVI GROUP OF INSTITUTIONS, INDORE. DEPARTMENT OF AI & DS


MACHINE LEARNING LABORATORY -3-

DEPARTMENT OF
ARTIFICIAL INTELLIGENCE AND DATA SCIENCE
2024-25

List of Experiments

Student Name: - Enrollment No.: -

Exp. Conduction Staff


List of Experiments
No. date Signature
Write a program to print checkerboard pattern of NXN
1.
dimensions using NumPy (Take input for N=8).
Write a program to print transpose of a matrix in a single
2.
line in Python.
Perform data manipulation with Pandas (Create Data
3.
Frame).
4. Perform data manipulation with Pandas (Read CSV file ).

Perform data pre-processing to handle missing values


5.
and categorical features.

6. Implement a linear regression model.

7. Implement a polynomial regression model.

8. Implement logistic regression model.

9. Implement the K- Nearest neighbors algorithm.

10. Implement SVM algorithm..

CHAMELI DEVI GROUP OF INSTITUTIONS, INDORE. DEPARTMENT OF AI & DS


MACHINE LEARNING LABORATORY -4-
EXPT. No. -1. Write a program to print checkerboard pattern having NXN dimensions using NumPy (Take
input for N=8)

Aim: To understand the concept of NumPy in python


Theory: NumPy is an array-processing package that is used for general-purpose and provides a high-
performance multidimensional array object, and tools to work on an array. It is the fundamental package for
scientific computing and is used as an efficient multi-dimensional container of generic data.

Code:

Output:

010101010
101010101
010101010
101010101
010101010
101010101
010101010
101010101
010101010

Viva Question:
1. What is NumPy in python?
2. Differentiate between NumPy and List.
3. How NumPy is useful in Machine Learning?
4. How slicing works in NumPy?
5. What is the default datatype of integer element in NumPy array?

CHAMELI DEVI GROUP OF INSTITUTIONS, INDORE. DEPARTMENT OF AI & DS


MACHINE LEARNING LABORATORY -5-
EXPT. No. -2. Write a program to print transpose of a matrix in a single line in Python

Aim: To understand the concept of matrix in NumPy using python.


Theory: Nested loop is used to find the Transpose of a matrix. But there are some interesting ways to do the
same in a single line. In Python, we can implement a matrix as a nested list (list inside a list). Each element is
treated as a row of the matrix. For example m = [[1, 2], [3, 4], [5, 6]] represents a matrix of 3 rows and 2
columns. The transpose of the matrix can be found by interchanging the rows and columns.

Code:

Output:

Viva Question:
1. What are the advantages of NumPy?
2. What is range function in Python?
3. Define module in Python.
4. Explain some statistic methods in NumPy.
5. What is the use of arange function NumPy?

CHAMELI DEVI GROUP OF INSTITUTIONS, INDORE. DEPARTMENT OF AI & DS


MACHINE LEARNING LABORATORY -6-
EXPT. No. -3. Perform data manipulation with Pandas (Create Data Frame)

Aim: To understand the concept of pandas to create Data Frame.


Theory: A DataFrame is a widely used data structure of pandas and works with a two-dimensional array with
labeled axes (rows and columns) DataFrame is defined as a standard way to store data and has two different
indexes, i.e., row index and column index.
Create Data Frame for Employee-
Ename Age Salary Designation Location
Ajay 28 40000 Project Engineer Indore
Chetna 24 35000 HR Indore
Karan 26 39000 Data Analyst Pune
Richa 25 34000 HR Trainee Pune

Perform Following Operations-


1. Create Data Frame
2. Select Ename
3. Add one more row
4. Add one column Gender
5. Rename Column from Designation to Profile

Code:

Output:

Viva Question:

1. Define “pandas” in Python.


2. What is “DataFrame” in “pandas”?
3. How will you create an empty “DataFrame” in pandas?
4. How will you add a column in to a “DataFrame” using “pandas”?
5. What are the different ways to create a “DataFrame” in “pandas”?

CHAMELI DEVI GROUP OF INSTITUTIONS, INDORE. DEPARTMENT OF AI & DS


MACHINE LEARNING LABORATORY -7-
EXPT. No.- 4. Perform data manipulation with Pandas (Read csv file)

Aim: To understand the concept of pandas to data manipulation.


Theory: pandas is a library written for the Python programming language for data manipulation and analysis.
In particular, it offers data structures and operations for manipulating numerical tables.
Read dataset (odi_new.csv) from local disk and it is a csv file and perform below operations-
1. To view few columns and rows in data to understand it
2. Type of data in each column
3. View number of rows and columns
4. To see the description of data
5. Show top 10 rows
6. Show below 10 rows

Code:

Output:

Viva Question:
1. What are the uses of “pandas” in Machine Learning?
2. Which is the standard data missing marker in “pandas”?
3. What is PEP8?
4. What is the difference between dataset and data frame?
5. What is the role of “unique()” function in “pandas”?

CHAMELI DEVI GROUP OF INSTITUTIONS, INDORE. DEPARTMENT OF AI & DS


MACHINE LEARNING LABORATORY -8-
EXPT. No.- 5. Perform data pre-processing to handle missing values and categorical features.

Aim: To understand the concept and need of data preprocessing.


Theory: Pre-processing refers to the transformations applied to our data before feeding it to the algorithm.
Data pre-processing is a technique that is used to convert the raw data into a clean data set. In other words,
whenever the data is gathered from different sources it is collected in raw format which is not feasible for the
analysis. For achieving better results from the applied model in Machine Learning projects the format of the
data must be in a proper manner. Some specified Machine Learning model needs information in a specified
format, for example, Random Forest algorithm does not support null values, therefore, to execute random
forest algorithm null values have to be managed from the original raw data set.
Another aspect is that data set should be formatted in such a way that more than one Machine Learning and
Deep Learning algorithms are executed in one data set, and best out of them is chosen.

Code:

Output:

Viva Question:
1. What is the need of data preprocessing?
2. Define categorical feature.
3. What is “OneHotEncoder”?
4. What is “Fit” method?
5. What is “Transform” in Machine Learning?

CHAMELI DEVI GROUP OF INSTITUTIONS, INDORE. DEPARTMENT OF AI & DS


MACHINE LEARNING LABORATORY -9-
EXPT. No.- 6 . Implement Linear Regression model. (Read Data named Salary_data).

Aim: To understand the concept Linear Regression.


Theory: This is one of the most common and interesting type of Regression technique. Here we predict a
target variable Y based on the input variable X. A linear relationship should exist between target variable and
predictor and so comes the name Linear Regression.
Consider predicting the salary of an employee based on his/her age. We can easily identify that there seems to
be a correlation between employee’s age and salary (more the age more is the salary). The hypothesis of
linear regression is- Y= a + bX
Y represents salary, X is employee’s age and a & b are the coefficients of equation. So, in order to predict Y
(salary) of given X (age), we need to know the values of a and b (the model’s coefficients).

Code:

Viva Question:
1. What is Linear Regression?
2. What is predictor variable in machine learning?
3. What is the best ratio for training and test data?
4. What is the use of “fit-transform” method?
5. What is the use of “labelEncoder”?

CHAMELI DEVI GROUP OF INSTITUTIONS, INDORE. DEPARTMENT OF AI & DS


MACHINE LEARNING LABORATORY - 10 -
EXPT. No.- 7. Implement Polynomial Regression model. ( Read Data named positions_salaries)

Aim: To understand the concept Polynomial Regression.


Theory: In polynomial regression, we transform the original features into polynomial features of a given
degree and then apply Linear Regression on it. Consider the above linear model Y = a+bX is transformed to
something like – Y=a + bX + cX2
It is still a linear model but the curve is now quadratic rather than a line. Scikit-Learn provide Polynomial
Features class to transform the features.

Code:

Output:

Viva Question:
1. What is polynomial feature?
2. What is irreducible error?
3. Define bias in learning algorithm.
4. Where we can use polynomial regression?
5. What is the use of scatter plot?

CHAMELI DEVI GROUP OF INSTITUTIONS, INDORE. DEPARTMENT OF AI & DS


MACHINE LEARNING LABORATORY - 11 -
EXPT. No.- 8 . Implement Logistic Regression model. (Read Data Social_network_ads)

Aim: To understand the concept of Logistic Regression.


Theory: Logistic regression is a fundamental classification technique. It belongs to the group of linear
classifiers and is somewhat similar to polynomial and linear regression. Logistic regression is fast and
relatively uncomplicated, and it’s convenient for you to interpret the results. Although it’s essentially a
method for binary classification, it can also be applied to multiclass problems.
Code:

Viva Question:
1. What is “overfitting”?
2. What is “underfitting”?
3. What is “bestfit” in learning algorithm?
4. Define bias-variance trade-off.
5. Define variance.

CHAMELI DEVI GROUP OF INSTITUTIONS, INDORE. DEPARTMENT OF AI & DS


MACHINE LEARNING LABORATORY - 12 -
EXPT. No.- 9 . Implement K- Nearest neighbors algorithm ( Read Data named Social_network_ads)

Aim: To understand the concept of K-Nearest Neighbors.


Theory: KNN can be used for both classification and regression predictive problems. However, it is more
widely used in classification problems in the industry. To evaluate any technique, we generally look at 3
important aspects:
1. Ease to interpret output
2. Calculation time
3. Predictive Power
KNN makes predictions using the training dataset directly.
Predictions are made for a new instance (x) by searching through the entire training set for the K most similar
instances (the neighbors) and summarizing the output variable for those K instances. For regression this might
be the mean output variable, in classification this might be the mode (or most common) class value.
To determine which of the K instances in the training dataset are most similar to a new input a distance
measure is used.
Code:

Output:

Viva Question:
1. What is use of StandardScaler?
2. What are the parameters for KNeighborsClassifier?
3. What is confusion matrix?
4. Define cross validation.
5. How we can balance bias and variance?

CHAMELI DEVI GROUP OF INSTITUTIONS, INDORE. DEPARTMENT OF AI & DS


MACHINE LEARNING LABORATORY - 13 -
EXPT. No.- 10. Implement SVM algorithm. (Read Data named Social_network_ads)

Aim: To understand the concept of Support Vector Machine.


Theory: “Support Vector Machine” (SVM) is a supervised machine learning algorithm which can be used for
both classification or regression challenges. However, it is mostly used in classification problems. In this
algorithm, we plot each data item as a point in n-dimensional space (where n is number of features you have)
with the value of each feature being the value of a particular coordinate. Then, we perform classification by
finding the hyper-plane that differentiate the two classes very well.
Code:

Output:

Viva Question:
1. What is hyper plane?
2. How to choose the best hyper plane?
3. What is kernel in SVR?
4. In which situation kernel is used?
5. What is random state?

CHAMELI DEVI GROUP OF INSTITUTIONS, INDORE. DEPARTMENT OF AI & DS

You might also like