FML_lab_manual
FML_lab_manual
Laboratory Manual
(Fundamentals of Machine Learning)
(4341603)
DTE’s Vision:
• To provide globally competitive technical education;
• Remove geographical imbalances and inconsistencies;
• Develop student friendly resources with a special focus on girls’ education and
support to weaker sections;
• Develop programs relevant to industry and create a vibrant pool of technical professionals.
DTE’s Mission:
• Quality technical and professional education with continuous improvement of all the
resources and personnel
• To allocate competent and dedicated human resources and infrastructure to the
institutions for providing world-class professional education to become a Global Leader
(“Vishwa Guru”)
Institute’s Vision:
• To be a leading technical institute that provides transformative education to learners for
achieving competency as per the needs of industry and society, thus contributing to
nation building
Institute’s Mission:
• To provide a conducive learning environment to nurture learners.
• To act as a catalyst for achieving academic excellence by bringing stake holders on same
platform.
• To be committed towards continuous improvement and enrichment of learners by a
holistic approach to education so as to enable them to be successful individuals and
responsible citizens of India.
Department’s Vision:
• To acquire quality Education, Research and Development in the field of Information
technology meeting the global standards and comply with the ever-growing technology
Department’s Mission:
• The graduates of our department will be efficient in technical and ethical responsibilities
to become globally recognised by pursuing opportunities for higher studies and real time
problem solving.
• Project development in association with the Government, Industries and Professionals
will be done that can meet industrial needs.
Fundamentals of Machine Learning (4341603)
Certificate
Place: Ahmedabad
Date: /05/2025
2. Problem analysis: Identify and analyse well-defined engineering problems using codified
standard methods.
4. Engineering Tools, Experimentation and Testing: Apply modern engineering tools and
appropriate technique to conduct standard tests and measurements.
7. Life-long learning: Ability to analyse individual needs and engage in updating in the context
of technological changes in field of engineering.
3 | Page
3 | Page
Fundamentals of Machine Learning (4341603)
a) CO1: -To understand the need of machine learning for various problem solving.
b) CO2: - Prepare machine leaning model and learning the evaluation methods.
c) CO3: - Evaluate various supervised learning algorithms using appropriate dataset.
d) CO4: -Evaluate various unsupervised learning algorithms using appropriate dataset.
e) CO5:-To understand the use of various existing machine learning libraries.
7. ML Project
Use the following dataset as music.csv
4 | Page
4 | Page
Fundamentals of Machine Learning (4341603)
5 | Page
5 | Page
Fundamentals of Machine Learning (4341603)
a) Student will learn to automate variety of task making system more efficient and cost
effective
b) Student will learn efficient handling of data that will cater to better data analytics
c) Student will lean to implement machine learning approaches to varied field of
applications from healthcare to e-commerce.
6 | Page
6 | Page
Fundamentals of Machine Learning (4341603)
Sr Marks
No.
Practical Outcome/Title of experiment Page Date Sign
(25)
ML Project
Use the following dataset as music.csv
7 | Page
Fundamentals of Machine Learning (4341603)
8 | Page
Fundamentals of Machine Learning (4341603)
Introduction :
Numerical computing with Python refers to the use of Python programming language for
performing mathematical and scientific computations. Python is a high- level
programming language that is easy to learn, has a clear and concise syntax, and is widely
used in scientific computing. Python provides several libraries that are useful for
numerical computing, including:
2. SciPy: SciPy is a library for scientific computing in Python. It provides functions for
optimization, signal processing, and statistics, among other things.
4. Pandas: Pandas is a library for data analysis in Python. It provides data structures for
working with structured data, such as tables and time series.
5. SymPy: SymPy is a library for symbolic mathematics in Python. It provides tools for
working with mathematical expressions and equations symbolically, rather than
numerically.
Numerical computing with Python is used in a wide range of scientific and engineering
applications, including data analysis, image processing, and simulation. It is also used in
machine learning and deep learning applications, where numerical computations are
performed on large datasets.
Numpy
→ Installing Numpy
pip install numpy
9 | Page
Fundamentals of Machine Learning (4341603)
Code:
import numpy as np
a = np.zeros((3, 3))
print(a)
Output:
→ Array indexing and slicing: NumPy arrays can be indexed and sliced like
regular Python lists, but also support more advanced indexing and slicing
operations. For example, to extract the first row of a 2-dimensional array a, you
can use:
Code:
row = a[0,:]
print(row)
Output:
Code:
x=np.array([4,9,16,25])
b = np.sqrt(x)
print(b)
Output:
x=np.array([4,9,16,25])
total = np.sum(x)
print(total)
10 | Page
Fundamentals of Machine Learning (4341603)
Output:
Code:
x=np.array([4,9,16,25])
y=np.array([1,2,3,4])
C = np.dot(x,y)
print(C)
Output:
Matplotlib
→ Installing Library
pip install matplotlib
→ Importing Library
import matplotlib.pyplot as plt
→ Creating a line plot: Line plots are a simple way to visualize a sequence of data
points. To create a line plot in Matplotlib, you can use the plot function. For
example, to plot a sine wave from 0 to 20, you can use:
Code:
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(0, 2*np.pi, 100)
y = np.sin(x)
plt.plot(x, y)
plt.show()
Output:
11 | Page
Fundamentals of Machine Learning (4341603)
Output:
→ Creating a scatter plot: Scatter plots are a useful way to visualize the relationship
between two variables. To create a scatter plot in Matplotlib, you can use the
scatter function. For example, to plot the relationship between two variables x and
y, you can use:
Code:
x= np.random.randn(100)
y = 2*x + np.random.randn(100)
plt.scatter(x, y)
plt.show()
Output:
→ Creating a bar plot: Bar plots are a useful way to compare the values of different
categories. To create a bar plot in Matplotlib, you can use the bar or barh
function. For example, to plot the number of students who prefer different
subjects, you can use:
Code:
12 | Page
Fundamentals of Machine Learning (4341603)
Output:
Code:
scores = np.random.randn(100)
plt.hist(scores, bins=10)
plt.show()
Output:
13 | Page
Fundamentals of Machine Learning (4341603)
Code:
x = np.random.randn(100)
y = 2*x + np.random.randn(100)
plt.plot(x, y, color='red', linestyle='dashed', linewidth=2)
plt.title('Sine wave’)
plt.xlabel('x’)
plt.ylabel('y’)
plt.show()
Output:
14 | Page
Fundamentals of Machine Learning (4341603)
15 | Page
Fundamentals of Machine Learning (4341603)
Practical No. 2 : Introduction to Pandas for data import and export (Excel,
CSV etc.
Pandas is a popular library in Python for data analysis and manipulation. It provides a
range of powerful tools for working with structured data, including the ability to import
and export data in various formats.
Pandas
→ Installing Library
pip install pandas
→ Importing Library
import pandas as pd
Code:
import pandas as pd
df = pd.read_csv(‘hello.csv’)
print(df.head())
Output:
Code:
import pandas as pd
df = pd.read_excel(‘hello.xlsx’);
print(df.head())
Output:
Required Dependency
pip install openpyxl
16 | Page
Fundamentals of Machine Learning (4341603)
Code:
import pandas as pd
#list of name, degree, score
nme=["aparna", "pankaj", "sudhir", "Geeku"]
deg = ["MBAА", "ВСА", "M.Tech", "MBA"]
scr = [90, 40, 80, 98]
#dictionary of lists
dict = {'name': nme, 'degree': deg, 'score': scr}
df = pd.DataFrame(dict)
df.to_csv(‘hello.csv’)
Output:
Code:
import pandas as pd
dct ={'ID':{0:23,1:24,2:25,3:26,4:27},'Name':{0:'ram',1:'deep',2:'yash',3:'aman',4:'arjun’
},'Marks':{0:89,1:70,2:56,3:66,4:90},'Grade':{0:'B',1:'C',2:'E',3:'D',4:'A’}}
data = pd.DataFrame(dct)
data.to_excel('hello.xlsx’)
Output:
17 | Page
Fundamentals of Machine Learning (4341603)
18 | Page
Fundamentals of Machine Learning (4341603)
Scikit-learn :
Scikit-learn, also known as sklearn, is a popular Python library used for machine learning
and statistical modeling. It provides a range of tools for data preprocessing, feature
extraction, supervised and unsupervised learning, and model evaluation.
Scikit-learn is built on top of other popular Python libraries such as NumPy, SciPy, and
matplotlib, and provides a consistent interface for various machine learning algorithms.
1. Data preprocessing: Scikit-learn provides a range of tools for data preprocessing, such
as scaling, normalization, and feature selection.
2. Supervised learning: Scikit-learn provides a range of algorithms for supervised
learning, including linear regression, logistic regression, decision trees, and support
vector machines.
3. Unsupervised learning: Scikit-learn provides a range of algorithms for unsupervised
learning, including clustering, dimensionality reduction, and anomaly detection.
4. Model selection and evaluation: Scikit-learn provides tools for model selection and
evaluation, including cross-validation, hyperparameter tuning, and metrics for
evaluating model performance.
1. Prepare the data: Load and preprocess the data using Scikit-learn's data preprocessing
tools.
2. Choose a model: Select a model that is appropriate for your problem, based on the
type of data and the task at hand.
3. Train the model: Fit the model to the training data using Scikit-learn's fit function.
4. Evaluate the model: Evaluate the performance of the model using Scikit-learn's
evaluation metrics.
5. Tune the model: Fine-tune the model by adjusting its hyperparameters using Scikit-
learn shyperparameter tuning tools.
6. Use the model: Use the trained model to make predictions on new data.
→ Loading a dataset Scikit-learn provides a number of built-in datasets that you can
use for testing and practicing. For example, to load the iris dataset, you can use:
19 | Page
Fundamentals of Machine Learning (4341603)
Code:
Output:
Before training a model, it's important to split the data into training and test sets to
evaluate the model's performance on new, unseen data. Scikit-learn provides a function
for doing this, called train_test_split. For example, to split the iris dataset into training
and test sets with 80% of the data used for training, you can use
Code:
Output:
→ Training a model
Scikit-learn provides a wide range of machine learning models that you can use for
different tasks. For example, to train a logistic regression model on the iris dataset, you
can use
20 | Page
Fundamentals of Machine Learning (4341603)
Code:
Output:
→ Evaluating a model
Once you’ve trained a model, you can evaluate its performance on the test set using a
variety of metrics. Scikit-learn provides functions for computing many of these
metrics, such as accuracy, precision, recall, and F1 score. For example, to compute the
accuracy of the logistic regression model on the test set, you can use:
Code:
Output:
21 | Page
Fundamentals of Machine Learning (4341603)
22 | Page
Fundamentals of Machine Learning (4341603)
Algorithm:
Code:
23 | Page
Fundamentals of Machine Learning (4341603)
Output:
24 | Page
Fundamentals of Machine Learning (4341603)
25 | Page
Fundamentals of Machine Learning (4341603)
Practical No. 5 : Import Pima indian diabetes data Apply SelectKBest and
chi2 for feature selection Identify the best features
Code:
Output:
26 | Page
Fundamentals of Machine Learning (4341603)
27 | Page
Fundamentals of Machine Learning (4341603)
Code:
Output:
28 | Page
Fundamentals of Machine Learning (4341603)
29 | Page
Fundamentals of Machine Learning (4341603)
Code:
Output:
30 | Page
Fundamentals of Machine Learning (4341603)
31 | Page
Fundamentals of Machine Learning (4341603)
Code:
Output:
32 | Page
Fundamentals of Machine Learning (4341603)
33 | Page
Fundamentals of Machine Learning (4341603)
Code:
Output:
Code:
Output:
Code:
34 | Page
Fundamentals of Machine Learning (4341603)
Output:
Code:
Output:
35 | Page
Fundamentals of Machine Learning (4341603)
36 | Page
Fundamentals of Machine Learning (4341603)
Solution:
Code:
Output:
Code:
37 | Page
Fundamentals of Machine Learning (4341603)
Output:
c. Plot the price of house with respect to area using matplotlib library
Code:
38 | Page
Fundamentals of Machine Learning (4341603)
Output:
Code:
Output:
39 | Page
Fundamentals of Machine Learning (4341603)
40 | Page
Fundamentals of Machine Learning (4341603)
Code:
Output:
41 | Page
Fundamentals of Machine Learning (4341603)
42 | Page
Fundamentals of Machine Learning (4341603)
Code:
Output:
Code:
Output:
43 | Page
Fundamentals of Machine Learning (4341603)
Code:
Output:
c. Find out the data instances in each class(use groupby and size)
Code:
44 | Page
Fundamentals of Machine Learning (4341603)
Output:
Code:
Output:
45 | Page
Fundamentals of Machine Learning (4341603)
Output:
46 | Page
Fundamentals of Machine Learning (4341603)
Code:
Output:
g. Apply K-NN and k means clustering to check accuracy and decide which is
better.
Code:
47 | Page
Fundamentals of Machine Learning (4341603)
Output:
48 | Page