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

LAB-Skill Advanced Course Machine Learning With Python Experiments

Uploaded by

prasatya0706
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)
88 views

LAB-Skill Advanced Course Machine Learning With Python Experiments

Uploaded by

prasatya0706
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/ 23

SKILL ADVANCED COURSE - MACHINE LEARNING WITH PYTHON LAB

(IV B.Tech. – I Semester) R20

Experiment-1: Implement and demonstrate the FIND-S algorithm for finding the most specific hypothesis
based on a given set of training data samples. Read the training data from a .CSV file.
Program:

Data File (enjoysports.csv)

Output
['Sunny', 'Warm', 'Normal', 'Strong', 'Warm', 'Same']
['Sunny', 'Warm', '?', 'Strong', 'Warm', 'Same']
['Sunny', 'Warm', '?', 'Strong', 'Warm', 'Same']
['Sunny', 'Warm', '?', 'Strong', '?', '?']
SKILL ADVANCED COURSE - MACHINE LEARNING WITH PYTHON LAB
(IV B.Tech. – I Semester) R20

Experiment-2: For a given set of training data examples stored in a .CSV file, implement and demonstrate the
Candidate-Elimination algorithm to output a description of the set of all hypotheses consistent with the
training examples.

Output
Final Specific Hypothesis S:
['Sunny' 'Warm' '?' 'Strong' '?' '?']
Final General Hypotheses G:
[['Sunny', '?', '?', '?', '?', '?'], ['?', 'Warm', '?', '?', '?', '?']]
SKILL ADVANCED COURSE - MACHINE LEARNING WITH PYTHON LAB
(IV B.Tech. – I Semester) R20

Experiment-3: Write a program to demonstrate the working of the decision tree based ID3 algorithm. Use an
appropriate data set for building the decision tree and apply this knowledge to classify a new sample.
SKILL ADVANCED COURSE - MACHINE LEARNING WITH PYTHON LAB
(IV B.Tech. – I Semester) R20

Output
Decision Tree: {'Sky': {'Rainy': 'No', 'Sunny': 'Yes'}}
Prediction for the sample: EnjoySport = No
SKILL ADVANCED COURSE - MACHINE LEARNING WITH PYTHON LAB
(IV B.Tech. – I Semester) R20

Experiment-4: Exercises to solve real-world problems using the following machine learning methods: a)
Linear Regression b) Logistic Regression c) Binary Classifier.
1. Linear Regression: Predicting House Prices

Output:
Predicted Prices: [493656.71641791]
RMSE: 6343.283582096
SKILL ADVANCED COURSE - MACHINE LEARNING WITH PYTHON LAB
(IV B.Tech. – I Semester) R20

2. Logistic Regression: Predicting Customer Churn

Output
Predicted churn: [1 1]
Accuracy: 0.5
Classification Report:
precision recall f1-score support
0 0.00 0.00 0.00 1
1 0.50 1.00 0.67 1
accuracy 0.50 2
macro avg 0.25 0.50 0.33 2
weighted avg 0.25 0.50 0.33 2
SKILL ADVANCED COURSE - MACHINE LEARNING WITH PYTHON LAB
(IV B.Tech. – I Semester) R20

3. Binary Classifier: Email Spam Detection using Naïve Bayes

Output
Predicted spam classification: [0]
Accuracy: 0.0
Classification Report:
precision recall f1-score support
0 0.00 0.00 0.00 0.0
1 0.00 0.00 0.00 1.0
accuracy 0.00 1.0
macro avg 0.00 0.00 0.00 1.0
weighted avg 0.00 0.00 0.00 1.0
SKILL ADVANCED COURSE - MACHINE LEARNING WITH PYTHON LAB
(IV B.Tech. – I Semester) R20

Experiment-5: Develop a program for Bias, Variance, Remove duplicates, Cross Validation.
SKILL ADVANCED COURSE - MACHINE LEARNING WITH PYTHON LAB
(IV B.Tech. – I Semester) R20

Output
Data after removing duplicates:
X1 X2 Y
0 1 1 10
1 2 2 20
2 3 3 30
3 4 4 40
4 5 5 50
6 6 6 60
Linear Model MSE: 8.204153414298523e-29
Polynomial Model MSE: 5.467346741706781
Cross-Validation MSE: 130.0
SKILL ADVANCED COURSE - MACHINE LEARNING WITH PYTHON LAB
(IV B.Tech. – I Semester) R20

Experiment-6: Write a program to implement Categorical Encoding, One-hot Encoding.


SKILL ADVANCED COURSE - MACHINE LEARNING WITH PYTHON LAB
(IV B.Tech. – I Semester) R20

Output
Original Data:
City Weather Temperature
0 New York Sunny 30
1 Los Angeles Rainy 25
2 Chicago Cloudy 20
3 New York Sunny 28
4 Chicago Cloudy 22

Data after Label Encoding:


City Weather Temperature City_Label_Encoded Weather_Label_Encoded
0 New York Sunny 30 2 2
1 Los Angeles Rainy 25 1 1
2 Chicago Cloudy 20 0 0
3 New York Sunny 28 2 2
4 Chicago Cloudy 22 0 0

Data after One-Hot Encoding (using Pandas get_dummies):


City_Chicago City_Los Angeles City_New York Weather_Cloudy \
0 False False True False
1 False True False False
2 True False False True
3 False False True False
4 True False False True

Weather_Rainy Weather_Sunny
0 False True
1 True False
2 False False
3 False True
4 False False

City One-Hot Encoded Data (using Scikit-learn):


City_0 City_1 City_2
0 0.0 0.0 1.0
1 0.0 1.0 0.0
2 1.0 0.0 0.0
3 0.0 0.0 1.0
4 1.0 0.0 0.0

Weather One-Hot Encoded Data (using Scikit-learn):


Weather_0 Weather_1 Weather_2
0 0.0 0.0 1.0
1 0.0 1.0 0.0
2 1.0 0.0 0.0
3 0.0 0.0 1.0
4 1.0 0.0 0.0
SKILL ADVANCED COURSE - MACHINE LEARNING WITH PYTHON LAB
(IV B.Tech. – I Semester) R20

Experiment-7: Build an Artificial Neural Network by implementing the Back propagation algorithm and test
the same using appropriate data sets.
Process:
1. Initialize network parameters: Weights and biases.
2. Feedforward pass: Calculate activations using weights, biases, and the activation function.
3. Backpropagation: Compute gradients for weights and biases using the loss function and chain rule.
4. Update weights: Use the gradients to adjust weights (using gradient descent).
5. Train and Test: Train the network using a dataset and evaluate its performance.
SKILL ADVANCED COURSE - MACHINE LEARNING WITH PYTHON LAB
(IV B.Tech. – I Semester) R20
SKILL ADVANCED COURSE - MACHINE LEARNING WITH PYTHON LAB
(IV B.Tech. – I Semester) R20

Experiment-8: Write a program to implement k-Nearest Neighbor algorithm to classify the iris data set. Print
both correct and wrong predictions.

Output
Model accuracy: 100.00%
Predictions on the test set:
Correct prediction: versicolor (Predicted: versicolor, Actual: versicolor)
Correct prediction: setosa (Predicted: setosa, Actual: setosa)
Correct prediction: virginica (Predicted: virginica, Actual: virginica)
Correct prediction: versicolor (Predicted: versicolor, Actual: versicolor)
Correct prediction: versicolor (Predicted: versicolor, Actual: versicolor)
Correct prediction: setosa (Predicted: setosa, Actual: setosa)
Correct prediction: versicolor (Predicted: versicolor, Actual: versicolor)
Correct prediction: virginica (Predicted: virginica, Actual: virginica)
Correct prediction: versicolor (Predicted: versicolor, Actual: versicolor)
Correct prediction: versicolor (Predicted: versicolor, Actual: versicolor)
Correct prediction: virginica (Predicted: virginica, Actual: virginica)
Correct prediction: setosa (Predicted: setosa, Actual: setosa)
Correct prediction: setosa (Predicted: setosa, Actual: setosa)
SKILL ADVANCED COURSE - MACHINE LEARNING WITH PYTHON LAB
(IV B.Tech. – I Semester) R20

Experiment-12: Exploratory Data Analysis for Classification using Pandas or Matplotlib.


Exploratory Data Analysis of Iris Dataset for Classification using Pandas or Matplotlib
SKILL ADVANCED COURSE - MACHINE LEARNING WITH PYTHON LAB
(IV B.Tech. – I Semester) R20

Output
sepal length (cm) sepal width (cm) petal length (cm) petal width (cm) \
0 5.1 3.5 1.4 0.2
1 4.9 3.0 1.4 0.2
2 4.7 3.2 1.3 0.2
3 4.6 3.1 1.5 0.2
4 5.0 3.6 1.4 0.2

target
0 setosa
1 setosa
2 setosa
3 setosa
4 setosa
sepal length (cm) sepal width (cm) petal length (cm) \
count 150.000000 150.000000 150.000000
mean 5.843333 3.057333 3.758000
std 0.828066 0.435866 1.765298
min 4.300000 2.000000 1.000000
25% 5.100000 2.800000 1.600000
50% 5.800000 3.000000 4.350000
75% 6.400000 3.300000 5.100000
max 7.900000 4.400000 6.900000

petal width (cm)


count 150.000000
mean 1.199333
std 0.762238
min 0.100000
25% 0.300000
50% 1.300000
75% 1.800000
max 2.500000

Missing values:
sepal length (cm) 0
sepal width (cm) 0
petal length (cm) 0
petal width (cm) 0
target 0
dtype: int64

Class distribution:
target
setosa 50
versicolor 50
virginica 50
Name: count, dtype: int64
SKILL ADVANCED COURSE - MACHINE LEARNING WITH PYTHON LAB
(IV B.Tech. – I Semester) R20

Pair plot to visualize relationships between features, colored by target class

Plot histograms for each feature


SKILL ADVANCED COURSE - MACHINE LEARNING WITH PYTHON LAB
(IV B.Tech. – I Semester) R20

Box plot for each feature, grouped by target

Correlation matrix
SKILL ADVANCED COURSE - MACHINE LEARNING WITH PYTHON LAB
(IV B.Tech. – I Semester) R20

Experiment-14: Write a program to Implement Support Vector Machines and Principal Component Analysis.
SKILL ADVANCED COURSE - MACHINE LEARNING WITH PYTHON LAB
(IV B.Tech. – I Semester) R20

Visualize original data set


SKILL ADVANCED COURSE - MACHINE LEARNING WITH PYTHON LAB
(IV B.Tech. – I Semester) R20

Model Accuracy: 97.77777777777777 %

Classification Report:
precision recall f1-score support

0 1.00 1.00 1.00 19


1 0.93 1.00 0.96 13
2 1.00 0.92 0.96 13

accuracy 0.98 45
macro avg 0.98 0.97 0.97 45
weighted avg 0.98 0.98 0.98 45
SKILL ADVANCED COURSE - MACHINE LEARNING WITH PYTHON LAB
(IV B.Tech. – I Semester) R20

Experiment-15: Write a program to Implement Principal Component Analysis.


SKILL ADVANCED COURSE - MACHINE LEARNING WITH PYTHON LAB
(IV B.Tech. – I Semester) R20

Explained Variance by Each Principal Component:


Principal Component 1: 92.46%
Principal Component 2: 5.31%
The following experiments are not considered:
Experiment-9: Implement the non-parametric Locally Weighted Regression algorithm to fit data points.
Select appropriate data set for your experiment and draw graphs.
Experiment-10: Assuming a set of documents that need to be classified, use the naïve Bayesian Classifier
model to perform this task. Built-in Java classes/API can be used to write the program. Calculate the accuracy,
precision, and recall for your data set.
Experiment-11: Apply EM algorithm to cluster a Heart Disease Data Set. Use the same data set for clustering
using k-Means algorithm. Compare the results of these two algorithms and comment on the quality of
clustering. You can add Java/Python ML library classes/API in the program.
Experiment-13: Write a Python program to construct a Bayesian network considering medical data. Use this
model to demonstrate the diagnosis of heart patients using standard Heart Disease Data Set.

You might also like