Ad24405 Ml Lab Record
Ad24405 Ml Lab Record
ENGINEERING AND
TECHNOLOGY, Tiruttani
(An Autonomous Institution)
Approved by AICTE, New Delhi & Affiliated to Anna University, Chennai.
Accredited by NAAC with “A++” Grade & An ISO 9001:2015 Certified Institution.
PRACTICAL RECORD
Year/Semester: II / 4
Name:
Register No.:
2025 - 2026
GRT INSTITUTE OF
ENGINEERING AND
TECHNOLOGY, Tiruttani
(An Autonomous Institution)
Approved by AICTE, New Delhi & Affiliated to Anna University, Chennai.
Accredited by NAAC with “A++” Grade & An ISO 9001:2015 Certified Institution.
Bonafide Certificate
NAME: …………………………….…….. PROGRAMME: ……………..
VISION:
MISSION:
A graduate of the Artificial Intelligence and Data Science Program will be able to:
PSO1: Develop data analytics and data visualization skills, skills pertaining to knowledge
acquisition, knowledge representation and knowledge engineering, and hence be capable of
coordinating complex projects.
PSO2: Create, select and apply the theoretical knowledge of AI and Data Analytics along
with practical Industrial tools and techniques to manage and solve wicked societal problems.
PSO3: Able to carry out fundamental research to cater the critical needs of the society
through cutting edge technologies of AI.
| AD24405 – MACHINE LEARNING LABORATORY
SYLLABUS
COURSE OBJECTIVES:
The student should be made:
• To understand the data sets and apply suitable algorithms for selecting the appropriate features for
analysis.
• To learn to implement supervised machine learning algorithms on standard datasets and evaluate the
performance.
• To experiment the unsupervised machine learning algorithms on standard datasets and evaluate the
performance.
• To build the graph-based learning models for standard data sets.
• To compare the performance of different ML algorithms and select the suitable one based on the
application.
LIST OF EXPERIMENTS
1. 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.
2. 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.
3. Build an Artificial Neural Network by implementing the Backpropagation algorithm and test the same
using appropriate data sets.
4. Write a program to implement the naïve Bayesian classifier for a sample training data set stored as a
.CSV file and compute the accuracy with a few test data sets.
5. Implement naïve Bayesian Classifier model to classify a set of documents and measure the accuracy,
precision, and recall.
6. Write a program to construct a Bayesian network to diagnose CORONA infection using standard WHO
Data Set.
7. Apply EM algorithm to cluster a set of data stored in a .CSV file. Use the same data set for clustering
using the k-Means algorithm. Compare the results of these two algorithms.
8. Write a program to implement k-Nearest Neighbour algorithm to classify the iris data set .print both
correct and wrong predictions.
9. Implement the non-parametric Locally Weighted Regression algorithm in order to fit data points. Select
an appropriate data set for your experiment and draw graphs.
10. Build a Support Vector Machine (SVM) model using the given dataset to classify the target variable.
Perform preprocessing, choose an appropriate kernel, train the model, and evaluate its performance
using suitable metrics.
COURSE OUTCOMES:
At the end of the course, the students will be able to:
CO1: Apply suitable algorithms for selecting the appropriate features for analysis.
CO2: Design Supervised machine learning algorithms on standard datasets and evaluate the performance.
CO3: Apply unsupervised machine learning algorithms on standard datasets and evaluate the performance.
CO4: Build the neural network learning models for standard data sets.
CO5: Assess and compare the performance of different ML algorithms and select the suitable one based on
the application.
INDEX
SUBJECT CODE / TITLE: AD24405 / MACHINE LEARNING LABORATORY
Implementation of Candidate –
1
Elimination algorithm
Implementation of Decision tree in
2
ID3 algorithm
Implementation of back propagation
3 algorithm to build an artificial neural
network
Implementation of Naïve Bayesian
4 classifier for a sample training
dataset and to compute accuracy
Implementation of naïve Bayesian
Classifier model to classify a set of
5
documents and to measure the
accuracy, precision, and recall
Construction of a Bayesian network
6 to diagnose CORONA infection
using standard WHO Data Set
Comparison of clustering in EM
7 algorithm and k-means algorithm
using the same data set
Implementation of k-Nearest
8 Neighbour algorithm to classify the
iris data set
Implementation of the non-
parametric Locally Weighted
9
Regression algorithm in order to fit
data points
Implementation of Support Vector
10 Machine (SVM) model to classify
the target variable.
CONTENT BEYOND SYLLABUS
MACHINE LEARNING
A subset of artificial intelligence known as machine learning focuses primarily on
the creation of algorithms that enable a computer to independently learn from data and
previous experiences, improve performance from experiences, and predict things.
8. Deployment: Once the model is trained and tuned, it can be deployed in a production
environment to make predictions on new data. This step requires integrating the
model into an existing software system or creating a new system for the model.
9. Monitoring and Maintenance: Finally, it is essential to monitor the model’s
performance in the production environment and perform maintenance tasks as
required. This involves monitoring for data drift, retraining the model as needed, and
updating the model as new data becomes available.
Example: If you train a model using labeled images of cats and dogs, it learns the features of
each. When shown a new image, it predicts whether it’s a cat or a dog.
There are two main categories of supervised learning that are mentioned below:
Classification - predicts categorical outputs, meaning it assigns data into predefined classes
like spam/non-spam emails or disease risk categories. These algorithms learn to map input
features to discrete labels.
Here are some classification algorithms:
• Logistic Regression
• Decision Tree
• Random Forest
• K-Nearest Neighbors (KNN)
• Naive Bayes
• Support Vector Machine (SVM)
Regression - predicts continuous values, such as house prices or product sales. It learns the
relationship between input features and a numerical target variable.
Here are some regression algorithms:
• Linear Regression
• Polynomial Regression
• Ridge Regression
• Lasso Regression
• Decision tree
• Random Forest
Example: If you have customer data without labels, the algorithm can group similar
customers based on purchase behavior useful for segmentation and marketing.
There are two main categories of unsupervised learning that are mentioned below:
Clustering - the process of grouping data points into clusters based on their similarity. This
technique is useful for identifying patterns and relationships in data without the need for
labeled examples.
Common techniques include:
• K-Means
• DBSCAN
• Mean-shift
Dimensionality reduction - helps reduce the number of features while preserving important
information.
Common techniques include:
• Principal Component Analysis
• Independent Component Analysis
• Reinforcement Learning
Reinforcement learning trains an agent to make a sequence of decisions through trial and
error. The agent interacts with the environment, receives feedback in the form of rewards or
penalties and learns optimal actions over time.
Example: An AI agent learning to play chess gets positive feedback for good moves and
negative for poor ones. Over time, it learns strategies to win more often.
Here are some of most common reinforcement learning algorithms:
• Q-learning
• SARSA (State-Action-Reward-State-Action
• Deep Q-learning
Challenges
• ML models learn from training data and if the data is biased, model’s decisions can
be unfair so it is important to select and monitor data carefully.
• Since it depends on large amounts of data, there is a risk of sensitive information
being exposed so protecting privacy is important.
• Complex ML models can be difficult to understand which makes it difficult to explain
why they make certain decisions. This can affect trust and accountability.
• Automation may replace some jobs so retraining and helping workers learn new skills
is important to adapt to these changes.
Applications
Machine Learning is used across industries to solve problems and improve services.
• In healthcare it diagnoses diseases, predicts outcomes and personalizes treatments
• In finance detects fraud, supports trading and assesses credit risk
• Recommends products, forecasts demand and analyzes customer behavior
• Powers self-driving cars, optimizes routes and predicts maintenance
• Recommends content and enables image/speech recognition
• Detects defects and predicts machine failures
AIM:
To implement and demonstrate the Candidate-Elimination algorithm, for a given set
of training data examples stored in a .CSV file, to output a description of the set of all
hypotheses consistent with the training examples.
ALGORITHM:
1. Load Data set.
2. Initialize General Hypothesis and Specific Hypothesis.
3. For each training example
if example is Positive example
if attribute_value == hypothesis_value:
Do Nothig
else:
replace attribute value with '?' (Basically generalizing it)
if example is Negative example
Make generalize hypothesis more specific.
DATA SET:
[Link]
PROGRAM:
import numpy as np
import pandas as pd
data = [Link](data=pd.read_csv('[Link]'))
print(data)
concepts = [Link]([Link][:,0:-1])
print(concepts)
target = [Link]([Link][:,-1])
print(target)
def learn(concepts, target):
specific_h = concepts[0].copy()
print("\nInitialization of specific_h and general_h")
print(specific_h)
for i, h in enumerate(concepts):
if target[i] == "Yes":
for x in range(len(specific_h)):
if h[x] != specific_h[x]:
specific_h[x] = '?'
general_h[x][x] = '?'
if target[i] == "No":
for x in range(len(specific_h)):
if h[x] != specific_h[x]:
general_h[x][x] = specific_h[x]
else:
general_h[x][x] = '?'
indices = [i for i, val in enumerate(general_h) if val == ['?', '?', '?', '?', '?', '?']]
for i in indices:
general_h.remove(['?', '?', '?', '?', '?', '?'])
return specific_h, general_h
OUTPUT:
RESULT:
Thus the Candidate-Elimination algorithm, to test all the hypotheses with the
training sets using python was executed and verified successfully.
Ex. No.: 2
IMPLEMENTATION OF DECISION TREE IN ID3 ALGORITHM
Date:
AIM:
To build Decision tree in ID3 algorithm to classify a new sample using python.
ALGORITHM:
1. Observe the dataset.
2. Import the necessary basic python libraries.
3. Read the dataset.
4. Calculate the Entropy of the whole dataset.
5. Calculate the Entropy of the filtered dataset.
6. Calculate the Information gain for the feature(outlook).
7. Finding the most informative feature (feature with highest information gain).
8. Adding a node to the tree.
9. Perform ID3 algorithm and generate a tree.
10. Finding unique classes of the label.
11. Predict from the tree.
12. Evaluate the test dataset.
13. Check the test dataset.
DATA SET:
[Link]
PROGRAM:
import numpy as np
import math
import csv
def read_data(filename):
with open(filename, 'r') as csvfile:
datareader = [Link](csvfile, delimiter=',')
headers = next(datareader)
metadata = []
traindata = []
class Node:
def __init__(self, attribute):
[Link] = attribute
[Link] = []
[Link] = ""
def __str__(self):
return [Link]
for x in range([Link][0]):
for y in range([Link][0]):
if data[y, col] == items[x]:
count[x] += 1
for x in range([Link][0]):
dict[items[x]] = [Link]((int(count[x]), [Link][1]), dtype="|S32")
pos = 0
for y in range([Link][0]):
if data[y, col] == items[x]:
dict[items[x]][pos] = data[y]
pos += 1
if delete:
dict[items[x]] = [Link](dict[items[x]], col, 1)
def entropy(S):
items = [Link](S)
if [Link] == 1:
return 0
for x in range([Link][0]):
counts[x] = sum(S == items[x]) / ([Link] * 1.0)
total_size = [Link][0]
entropies = [Link](([Link][0], 1))
intrinsic = [Link](([Link][0], 1))
for x in range([Link][0]):
ratio = dict[items[x]].shape[0]/(total_size * 1.0)
entropies[x] = ratio * entropy(dict[items[x]][:, -1])
intrinsic[x] = ratio * [Link](ratio, 2)
for x in range([Link][0]):
total_entropy -= entropies[x]
return total_entropy / iv
split = [Link](gains)
node = Node(metadata[split])
for x in range([Link][0]):
child = create_node(dict[items[x]], metadata)
[Link]((items[x], child))
return node
def empty(size):
s = ""
for x in range(size):
s += " "
return s
OUTPUT:
RESULT:
Thus the program to implement decision tree based ID3 algorithm using python was
executed and verified successfully.
AIM:
To implement the Back Propagation algorithm to build an Artificial Neural Network.
ALGORITHM:
1. Inputs X, arrive through the preconnected path.
2. Input is modelled using real weights W. The weights are usually randomly selected.
3. Calculate the output for every neuron from the input layer, to the hidden layers, to the
output layer.
4. Calculate the error in the outputs.
5. Travel back from the output layer to the hidden layer to adjust the weights such that the
errors is decreased.
6. Keep repeating the process until the desired output is achieved.
PROGRAM:
import numpy as np
X = [Link](([2, 9], [1, 5], [3, 6]), dtype=float) # X = (hours sleeping, hours studying)
y = [Link](([92], [86], [89]), dtype=float) # y = score on test
# scale units
X = X/[Link](X, axis=0) # maximum of X array
y = y/100 # max test score is 100
class Neural_Network(object):
def __init__(self):
[Link] = 2
[Link] = 1
[Link] = 3
self.W1 = [Link]([Link], [Link])
self.W2 = [Link]([Link], [Link])
NN = Neural_Network()
print ("\nInput: \n" + str(X))
print ("\nActual Output: \n" + str(y))
print ("\nPredicted Output: \n" + str([Link](X)))
print ("\nLoss: \n" + str([Link]([Link](y - [Link](X)))))
[Link](X, y)
OUTPUT:
RESULT:
Thus the Back propagation algorithm to build an Artificial Neural network was
implemented successfully.
AIM:
To implement Naïve Bayesian classifier for Tennis data set and to compute the
accuracy with few datasets.
ALGORITHM:
1. Convert the data set into a frequency table.
2. Create likelihood table by finding the probabilities like overcast probability = 0.29 and
probability of plating is 0.64.
3. Now, use Naive Bayesian equation to calculate the posterior probability for each class.
The class with the highest posterior probability is the outcome of prediction.
Problem:
Players will play if weather is sunny. Is this statement is correct?
We can solve it using above discussed method of posterior probability.
P(Yes | Sunny) = P( Sunny | Yes) * P(Yes) / P (Sunny)
Here we have
P (Sunny |Yes) = 3/9 = 0.33,
P(Sunny) = 5/14 = 0.36,
P( Yes)= 9/14 = 0.64
Now, P (Yes | Sunny) = 0.33 * 0.64 / 0.36 = 0.60, which has higher probability.
DATA SET:
[Link]
PROGRAM:
# import necessary libarities
import pandas as pd
from sklearn import tree
from [Link] import LabelEncoder
from sklearn.naive_bayes import GaussianNB
y = [Link][:,-1]
print("\nThe first 5 values of Train output is\n",[Link]())
le_Temperature = LabelEncoder()
[Link] = le_Temperature.fit_transform([Link])
le_Humidity = LabelEncoder()
[Link] = le_Humidity.fit_transform([Link])
le_Windy = LabelEncoder()
[Link] = le_Windy.fit_transform([Link])
le_PlayTennis = LabelEncoder()
y = le_PlayTennis.fit_transform(y)
print("\nNow the Train output is\n",y)
classifier = GaussianNB()
[Link](X_train,y_train)
OUTPUT:
RESULT:
Thus, the program to implement Naïve Bayesian classifier to compute the accuracy
with few datasets using python was executed and verified successfully.
AIM:
To classify a set of documents using Naïve Bayesian classifier and to measure the
accuracy, precision and recall.
ALGORITHM:
1. Import basic libraries.
2. Import the dataset.
3. Perform Data preprocessing.
4. Train the model.
5. Test and evaluate the model.
6. Visualizing the model.
7. Print the performance indicators.
DATA SET:
[Link]
PROGRAM:
import pandas as pd
msg = pd.read_csv('[Link]', names=['message', 'label'])
print("Total Instances of Dataset: ", [Link][0])
msg['labelnum'] = [Link]({'pos': 1, 'neg': 0})
X = [Link]
y = [Link]
from sklearn.model_selection import train_test_split
Xtrain, Xtest, ytrain, ytest = train_test_split(X, y)
from sklearn.feature_extraction.text import CountVectorizer
count_v = CountVectorizer()
Xtrain_dm = count_v.fit_transform(Xtrain)
Xtest_dm = count_v.transform(Xtest)
df = [Link](Xtrain_dm.toarray(),columns=count_v.get_feature_names_out())
print(df[0:5])
OUTPUT:
RESULT:
Thus the classification of a set of documents using Naïve Bayesian classifier was
done successfully and measured the accuracy, precision and recall.
AIM:
To construct a Bayesian network to diagnose corona infection using WHO data set.
ALGORITHM:
This Naive Bayes is broken down into 5 parts:
1. Separate by Class.
2. Summarize Dataset.
3. Summarize Data by Class.
4. Gaussian Probability Density Function.
5. Class Probabilities.
PROGRAM:
import pandas as pd
covid_19_data=pd.read_csv("/content/[Link]")
print(covid_19_data)
import warnings
[Link]("ignore",category=FutureWarning)
covid_19_data=pd.read_csv("/content/[Link]")
print(f'The shape of the dataframe is {covid_19_data.shape}')
print()
The shape of the dataframe is (5, 11)
print(covid_19_data.info())
print()
import numpy as np
covid_19_data.replace(to_replace='?',value=[Link],inplace=True)
print(covid_19_data.describe(include='all'))
print()
print(covid_19_data["Loss of Taste/Smell"].value_counts())
print(covid_19_data.isnull().sum())
OUTPUT:
RESULT:
Thus the python program to diagnose corona infection using Bayesian network was
implemented successfully.
AIM:
To compare the clustering in EM algorithm and K-means algorithm using the same data
set.
ALGORITHM:
The K-means implementation is as follows:
1. Choose the number of clusters k.
2. Select k random points from the data as centroids.
3. Assign all the points to the closest cluster centroid.
4. Recompute the centroids of newly formed clusters.
5. Repeat steps 3 and 4.
PROGRAM:
from [Link] import KMeans
from sklearn import preprocessing
from [Link] import GaussianMixture
from [Link] import load_iris
import [Link] as sm
import pandas as pd
import numpy as np
import [Link] as plt
dataset=load_iris()
# print(dataset)
X=[Link]([Link])
[Link]=['Sepal_Length','Sepal_Width','Petal_Length','Petal_Width']
y=[Link]([Link])
[Link]=['Targets']
# print(X)
[Link](figsize=(14,7))
colormap=[Link](['red','lime','black'])
# REAL PLOT
[Link](1,3,1)
[Link](X.Petal_Length,X.Petal_Width,c=colormap[[Link]],s=40)
[Link]('Real')
# K-PLOT
[Link](1,3,2)
model=KMeans(n_clusters=3)
[Link](X)
predY=[Link](model.labels_,[0,1,2]).astype(np.int64)
[Link](X.Petal_Length,X.Petal_Width,c=colormap[predY],s=40)
[Link]('KMeans')
# GMM PLOT
scaler=[Link]()
[Link](X)
xsa=[Link](X)
xs=[Link](xsa,columns=[Link])
gmm=GaussianMixture(n_components=3)
[Link](xs)
y_cluster_gmm=[Link](xs)
[Link](1,3,3)
[Link](X.Petal_Length,X.Petal_Width,c=colormap[y_cluster_gmm],s=40)
[Link]('GMM Classification')
[Link]()
OUTPUT:
RESULT:
Thus the program to compare clustering in EM and K-means algorithm with the
same dataset was performed successfully.
AIM:
To implement K-Nearest Neighbour algorithm to classify iris data set.
ALGORITHM:
1. Calculate the Information Gain of each feature.
2. Considering that all rows don’t belong to the same class, split the dataset S into subsets
using the feature for which the Information Gain is maximum.
3. Make a decision tree node using the feature with the maximum Information gain.
4. If all rows belong to the same class, make the current node as a leaf node with the class
as its label.
5. Repeat for the remaining features until we run out of all features, or the decision tree
has all leaf nodes.
PROGRAM:
from [Link] import load_iris
from [Link] import KNeighborsClassifier
from sklearn.model_selection import train_test_split
import numpy as np
dataset=load_iris()
#print(dataset)
X_train,X_test,y_train,y_test=train_test_split(dataset["data"],dataset["target"],random_state=0)
kn=KNeighborsClassifier(n_neighbors=1)
[Link](X_train,y_train)
for i in range(len(X_test)):
x=X_test[i]
x_new=[Link]([x])
prediction=[Link](x_new)
print("TARGET=",y_test[i],dataset["target_names"][y_test[i]],"PREDICTED=",prediction,
dataset["target_names"][prediction])
print([Link](X_test,y_test))
OUTPUT:
RESULT:
Thus the python program for K-Nearest Neighbour algorithm to classify the iris
data set was implemented successfully.
AIM:
To implement the non-parametric Locally Weighted Regression algorithm in order to fit
data points.
ALGORITHM:
1. Read the Given data Sample to X and the curve (linear or non linear) to Y.
2. Set the value for Smoothening parameter or Free parameter say τ.
3. Set the bias /Point of interest set x0 which is a subset of X.
4. Determine the weight matrix using :
6. Prediction = x0*β
PROGRAM:
from math import ceil
import numpy as np
from scipy import linalg
residuals = y - yest
s = [Link]([Link](residuals))
return yest
import math
n = 100
x = [Link](0, 2 * [Link], n)
y = [Link](x) + 0.3 * [Link](n)
f =0.25
iterations=3
yest = lowess(x, y, f, iterations)
OUTPUT:
RESULT:
Thus the non-parametric Locally Weighted Regression algorithm to fit data points
was implemented successfully.
AIM:
To Build a Support Vector Machine (SVM) model using the given dataset to classify the
target variable. Perform preprocessing, choose an appropriate kernel, train the model, and
evaluate its performance using suitable metrics.
ALGORITHM:
1. Data Preprocessing: The data will be cleaned, and features will be scaled (a highly
recommended step for SVMs, as they are not scale-invariant).
2. Choose an Appropriate Kernel: Depending on whether the data is linearly separable,
I will select an appropriate kernel (e.g., 'linear', 'rbf' for non-linear data, or 'poly').
3. Train the Model: The data will be split into training and testing sets, and the SVM
model will be trained using the training data.
4. Evaluate Performance: The model's performance will be evaluated on the test set
using suitable metrics such as accuracy, precision, recall, and F1-score.
PROGRAM:
import pandas as pd
from sklearn import datasets
from sklearn.model_selection import train_test_split
from [Link] import SVC
from [Link] import StandardScaler
from [Link] import accuracy_score, classification_report
# 1. Load the dataset (replace with your data loading) Example uses the Iris dataset from sklearn
iris = datasets.load_iris()
X = [Link]
y = [Link]
target_names = iris.target_names
# 2. Preprocess Data: Split and Scale
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)
scaler = StandardScaler()
X_train_scaled = scaler.fit_transform(X_train)
X_test_scaled = [Link](X_test)
# 3. Choose and Train the SVM Model 'rbf' (Radial Basis Function) kernel is a common choice for non-linear data
svm_model = SVC(kernel='rbf', random_state=42)
svm_model.fit(X_train_scaled, y_train)
OUTPUT:
RESULT:
Thus the python code for building the Support Vector Machine (SVM) model using
the given dataset to classify the target variable was done and Performed preprocessing, chosen
the appropriate kernel, trained the model, and evaluated its performance using the metrics
accuracy, precision, f1 score and recall.
Ex. No.: 1
LEAST SQUARE REGRESSION MODEL
Date:
AIM:
To Implement Least Square Regression Model.
ALGORITHM:
Step 1: Data Loading and Preprocessing
1. Import necessary libraries (pandas, numpy, [Link], LinearRegression
from sklearn).
2. Read the data from a CSV file ([Link]) and separate it into two variables:
X: the independent variable (the input values).
Y: the dependent variable (the target values).
Step 2: Manual Calculation of the Regression Line (Optional)
1. Calculate the mean of X and Y
2. Compute the slope (m)
3. Compute the intercept (c)
Step 3: Using sklearn Linear Regression (Modern Approach)
1. Initialize a LinearRegression model from sklearn.
2. Fit the model using X and Y.
3. The model computes the slope (m) and intercept (c) automatically using its built-in
method. model.coef_[0] gives the slope (m).
model.intercept_ gives the intercept (c).
Step 4: Making Predictions
Use the model to predict Y_pred (predicted Y values) based on the X values.
Step 5: Visualization
1. Plot the original data points (X, Y).
2. Plot the regression line by drawing a line for Y_pred (predicted values) against X.
Step 6: Output
1. Print the slope and intercept of the regression line.
2. Show a scatter plot of the data points along with the regression line to visualize the
model.
DATASET:
[Link]
32.50235 31.70701
53.4268 68.7776
61.53036 62.56238
47.47564 71.54663
59.81321 87.23093
55.14219 78.21152
52.2118 79.64197
39.29957 59.17149
48.10504 75.33124
52.55001 71.30088
45.41973 55.16568
54.35163 82.47885
44.16405 62.00892
58.16847 75.39287
56.72721 81.43619
48.95589 60.7236
44.6872 82.8925
60.29733 97.3799
45.61864 48.84715
38.81682 56.87721
66.18982 83.87856
65.41605 118.5912
47.48121 57.25182
41.57564 51.39174
51.84519 75.38065
59.37082 74.76556
57.31 95.45505
63.61556 95.22937
46.73762 79.05241
50.55676 83.43207
52.224 63.35879
35.56783 41.41289
42.43648 76.61734
58.16454 96.76957
57.50445 74.08413
45.44053 66.58814
61.89622 77.76848
33.09383 50.71959
36.43601 62.12457
37.67565 60.81025
44.55561 52.68298
43.31828 58.56982
50.07315 82.90598
43.87061 61.42471
62.99748 115.2442
32.66904 45.57059
40.1669 54.08405
53.57508 87.99445
33.86421 52.72549
64.70714 93.57612
38.11982 80.16628
44.50254 65.10171
40.59954 65.5623
41.72068 65.28089
51.08863 73.43464
36.43601 62.12457
41.10269 59.35885
41.18202 61.68404
50.18639 69.8476
52.37845 86.09829
50.13549 59.10884
33.64471 69.89968
39.5579 44.86249
56.13039 85.49807
57.36205 95.53669
60.26921 70.25193
35.67809 52.72173
31.58812 50.39267
53.66093 63.6424
46.68223 72.24725
43.10782 57.81251
70.34608 104.2571
44.49286 86.64202
57.50453 91.48678
36.93008 55.23166
55.80573 79.55044
38.95477 44.84712
56.90121 80.20752
56.8689 83.14275
34.33312 55.72349
59.04974 77.63418
57.78822 99.05141
54.28233 79.12065
51.08872 69.5889
50.28284 69.5105
44.21174 73.68756
38.00549 61.3669
32.94048 67.17066
53.69164 85.6682
68.76573 114.8539
46.23097 90.12357
68.31936 97.91982
50.03017 81.53699
49.23977 72.11183
50.03958 85.23201
48.14986 66.22496
PROGRAM:
print(m, c)
1.287357370010931 9.908606190326509
# Making predictions
Y_pred = m*X + c [Link](X, Y)
[Link](X, Y_pred, color='red')
# [Link]([min(X), max(X)], [min(Y_pred), max(Y_pred)], color='red')
[Link]()
RESULT:
Thus the Least Square Regression Model implemented successfully.
Ex. No.: 2
RULE BASED CLASSIFIER MODEL
Date:
AIM:
To Implement Rule Based Classifier Model.
PROBLEM STATEMENT:
Potential Customer Yield Calculation by Rule Based Classification
Problem
"A game company wants to create level-based new customer definitions (personas) by using
some features of its customers, and to create segments according to these new customer
definitions and to estimate how much the company can earn on average from new customers
based on these segments."
"For example: The company wants to determine how much a 25-year-old male user from
Turkey, who is an IOS user, can earn on average to the company."
Dataset
[Link] - data set contains the prices of the products sold by an international game
company and some demographic information of the users who buy these products. The data set
consists of records created in each sales transaction. This means that the table is not
singularized. In other words, a user with certain demographic characteristics may have made
more than one purchase.
DATASET:
[Link]
PROGRAM:
import pandas as pd
import [Link] as plt
import seaborn as sns
df = pd.read_csv("[Link]", encoding='utf-8')
def quick_info(dataframe):
print(" HEAD ")
print([Link](10))
print(" SHAPE ")
print([Link])
print(" COLUMNS ")
print([Link])
print(" DESRIBE ")
print([Link]().T)
print(" INFO ")
print([Link]())
print(" ANY NULL VALUES ")
print([Link]().[Link]())
print(" SUM OF NULL VALUES ")
print([Link]().sum())
quick_info(df)
SEX Ratio
female 2621 52.42
male 2379 47.58
COUNTRY Ratio
usa 2065 41.30
bra 1496 29.92
deu 455 9.10
tur 451 9.02
fra 303 6.06
can 230 4.60
count 5000.000000
mean 23.581400
std 8.995908
min 15.000000
0% 15.000000
25% 17.000000
50% 21.000000
75% 27.000000
100% 66.000000
max 66.000000
Name: AGE, dtype: float64
Return average earnings in terms of break down of COUNTRY, SOURCE, SEX, AGE in
descending order
agg_df =
[Link](["COUNTRY","SOURCE","SEX","AGE"]).agg({"PRICE":'mean'}).sort_values('P
RIC E',ascending=False)
#Convert index names to variable names by resetting index agg_
df.reset_index(inplace=True)
agg_df
COUNTRY SOURCE SEX AGE PRICE
0 bra android male 46 59.0
1 usa android male 36 59.0
2 fra android female 24 59.0
3 usa ios male 32 54.0
4 deu android female 36 49.0
... ... ... ... ... ...
343 usa ios female 38 19.0
344 usa ios female 30 19.0
345 can android female 27 19.0
346 fra android male 18 19.0
347 deu android male 26 9.0
#inclue_lowest made the first interval be left inclusive which allowed us to get the minimum
value (which is 15)
agg_df["AGE_CAT"] = [Link](agg_df["AGE"], bins=bins, labels=labels,
include_lowest=True) """
NOTE: This code snippet returns min, max values according to grouped 'AGE_CAT' and
useful for giving consistent intervals on both cut & qcut. (in terms of labelling) """
agg_df.groupby("AGE_CAT").agg({"AGE":['min','max']})
AGE
min max
AGE_CAT
15_18 15 18
19_23 19 23
24_30 24 30
31_40 31 40
41_66 41 66
agg_df
#Requested to define new level based customers and add them as variables to the dataset.
agg_df['CUSTOMER_LEVEL_BASED'] =
agg_df[["COUNTRY","SOURCE","SEX","AGE_CAT"]].apply(lambda x:
"_".join([Link](str)), axis=1)
#Since it has been said before that this dataset is not singularized, we need to singularize it.
agg_df = agg_df.groupby("CUSTOMER_LEVEL_BASED").agg({"PRICE":'mean'})
agg_df.reset_index(inplace=True)
#We had to reset index since we merged different columns into one (i.e. 'Singularization
process')
agg_df["CUSTOMER_LEVEL_BASED"].value_counts()
agg_df
CUSTOMER_LEVEL_BASED PRICE
0 bra_android_female_15_18 35.645303
1 bra_android_female_19_23 34.077340
2 bra_android_female_24_30 33.863946
3 bra_android_female_31_40 34.898326
4 bra_android_female_41_66 36.737179
... ... ...
104 usa_ios_male_15_18 33.983495
105 usa_ios_male_19_23 34.901872
106 usa_ios_male_24_30 34.838143
107 usa_ios_male_31_40 36.206324
108 usa_ios_male_41_66 35.750000
109 rows × 2 columns
#Requested to split new customers into "SEGMENTS" according to "PRICE" & Assign to
agg_df with a new column named "SEGMENTS"
labels = ['A', "B", 'C', "D", 'E']
#NOTE: qcut creates unequal size bins but frequency of samples is equal in each bin
agg_df["SEGMENT"] = [Link](agg_df['PRICE'], 5, labels=labels[::-1])
agg_df.sort_values(by="PRICE", ascending=False)
#Indicates that the 'SEGMENT' distribution is made according to the 'PRICE' distribution.
Parameters
country: origin of the customer device: customer's device
sex: customer's gender
age: customer's age Returns
Note
It is possible to do more checks before querying the input (For example, it could be extended
by checking whether all entries given in the dataset are valid)
"""
for _ in agg_df["CUSTOMER_LEVEL_BASED"]:
for ind, line in enumerate(agg_df["CUSTOMER_LEVEL_BASED"]):
min_age = agg_df['CUSTOMER_LEVEL_BASED'][ind].split('_')[3]
max_age = agg_df['CUSTOMER_LEVEL_BASED'][ind].split('_')[4]
#Which segment does a 20-year-old American male using IOS belong to and how much
income is expected to earn on average?
predict_income("usa",'ios','male', '20')
CUSTOMER_LEVEL_BASED PRICE SEGMENT
#Which segment does a 55-year-old Canadian male using ANDROID belong to and how much
income is expected to earn on average?
predict_income("can",'android','male', '50')
CUSTOMER_LEVEL_BASED PRICE SEGMENT
27 can_android_male_41_66 37.571429 A
RESULT:
Thus the Rule based Classifier model was implemented successfully.