0% found this document useful (0 votes)
13 views18 pages

ML Acoerecordiot

The document describes an experiment to calculate probabilities from a credit risk dataset. It contains 10 records mapping individual descriptions to high, medium, and low credit risk. The unconditional probability of 'golf' is calculated to be 0.4. The conditional probability of 'single' given 'medRisk' is calculated to be 0.66666.

Uploaded by

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

ML Acoerecordiot

The document describes an experiment to calculate probabilities from a credit risk dataset. It contains 10 records mapping individual descriptions to high, medium, and low credit risk. The unconditional probability of 'golf' is calculated to be 0.4. The conditional probability of 'single' given 'medRisk' is calculated to be 0.66666.

Uploaded by

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

Exp No: Page No:

Date:

Experiment-1
Aim: The probability that it is Friday and that a student is absent is 3%. Since there are 5 school days in
a week, the probability that it is Friday is 20%. What is the probability that a student is absent given that
today is Friday? Apply Baye’s rule in python to get the result.

Description:
F : Friday
A : Absent
Based on the given problem statement,
The probability that it is Friday and that a student is absent is 3% i.e
P(A ∩ F)= 3% = 3 / 100 = 0.03 and
The probability that it is Friday is 20% i.e
P(F)=20% = 20/100 = 0.2 Then,
The probability that a student is absent given that today is
Friday P(A ∣ F)
By the definition of Baye's rule( conditional probability ), we have
P(A ∣ F) = P(A ∩ F) / P(F)

CODE :

# The probability that it is Friday and that a student is absent is 3%


pAF=0.03
print("The probability that it is Friday and that a student is absent :",pAF)

# The probability that it is Friday is 20%


pF=0.2
print("The probability that it is Friday : ",pF)

# The probability that a student is absent given that today is Friday


pResult=(pAF/pF)

# Display the Result


print("The probability that a student is absent given that today is Friday : ",pResult * 100,"%")

ADITYA COLLEGE OF ENGINEERING Reg No:


Exp No: Page No:
Date:

Experiment-2
Aim: Extract the data from database using python.
Description:
First You need to Create a Table (students) in Mysql Database (SampleDB)
Open Command prompt and then execute the following command to enter into MySQL prompt.
--> mysql -u root -p
And then, you need to execute the following commands at MySQL prompt to create table in the database.
--> create database SampleDB;
--> use SampleDB;

--> CREATE TABLE students (sid VARCHAR(10),sname VARCHAR(10),age int);


--> INSERT INTO students VALUES('s521','Jhon Bob',23);
--> INSERT INTO students VALUES('s522','Dilly',22);
--> INSERT INTO students VALUES('s523','Kenney',25);
--> INSERT INTO students VALUES('s524','Herny',26);

Next,Open Command propmt and then execute the following command to install mysql.connector package to
connect with mysql database through python.

--> pip install mysql.connector (Windows)


--> sudo apt-get install mysql.connector (linux)

CODE :

import mysql.connector
# Create the connection object
myconn = mysql.connector.connect(host = "localhost", user = "root",passwd =
"",database="SampleDB")
# Creating the cursor object cur
= myconn.cursor()
# Executing the query cur.execute("select
* from students")
# Fetching the rows from the cursor object result
= cur.fetchall()
print("Student Details are :")

ADITYA COLLEGE OF ENGINEERING Reg No:


Exp No: Page No:
Date:

# Printing the result


for x in result:
print(x);
# Commit the transaction
myconn.commit()

# Close the connection


myconn.close()

ADITYA COLLEGE OF ENGINEERING Reg No:


Exp No: Page No:
Date:

Experiment-3:
Aim: Implement k-nearest neighbours classification using python.

Description:
To run this program you need to install the sklearn Module
Open Command propmt and then execute the following command to install sklearn Module

pip install scikit-learn

In this program, we are going to use iris dataset.And this dataset Split into training(70%) and test set(30%).
The iris dataset conatins the following features

---> sepal length (cm)


---> sepal width (cm)
---> petal length (cm)
---> petal width (cm)

The Sample data in iris dataset format is [5.4 3.4 1.7 0.2]

Where
5.4 ---> sepal length (cm)
3.4 ---> sepal width (cm)
1.7 ---> petal length (cm)
0.2 ---> petal width (cm)

CODE :

# Import necessary modules


from sklearn.neighbors import
KNeighborsClassifier from sklearn.model_selection
import train_test_split from sklearn.datasets import
load_iris
import random

# Loading data
data_iris = load_iris()
# To get list of target names
label_target = data_iris.target_names
print()
print("Sample Data from Iris Dataset")
print("*"*30)
# to display the sample data from the iris
dataset for i in range(10):
rn = random.randint(0,120)
print(data_iris.data[rn],"===>",label_target[data_iris.target[rn]])

ADITYA COLLEGE OF ENGINEERING Reg No:


Exp No: Page No:
Date:

# Create feature and target


arrays X = data_iris.data
y = data_iris.target

# Split into training and test set


X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size = 0.3, random_state=1)

print("The Training dataset length:


",len(X_train)) print("The Testing dataset length:
",len(X_test)) try:
nn = int(input("Enter number of neighbors :"))
knn = KNeighborsClassifier(nn)

knn.fit(X_train, y_train)
# to display the score
print("The Score is :",knn.score(X_test, y_test))
# To get test data from the user
test_data = input("Enter Test Data :").split(",")
for i in range(len(test_data)):
test_data[i] = float(test_data[i])
print( )
v= knn.predict([test_data]) print("Predicted
output is :",label_target[v])
except:
print("Please supply valid input......")

ADITYA COLLEGE OF ENGINEERING Reg No:


Exp No: Page No:
Date:

Experiment-4:
Aim: Given the following data, which specify classifications for nine ombinationsof VAR1 and VAR2
predict a classification for a case where VAR1=0.906and VAR2=0.606, using the result of k-means
clustering with 3 means (i.e., 3centroids)

Description:
To run this program you need to install the sklearn Module
Open Command propmt and then execute the following command to install sklearn Module

pip install scikit-learn

In this program, we are going to use the following data


VAR1 VAR2 CLASS
1.713 1.586 0
0.180 1.786 1
0.353 1.240 1
0.940 1.566 0
1.486 0.759 1
1.266 1.106 0
1.540 0.419 1
0.459 1.799 1
0.773 0.186 1

And, we need apply k-means clustering with 3 means (i.e., 3 centroids)


Finally, you need to predict the class for the VAR1=0.906 and VAR2=0.606

CODE :

from sklearn.cluster import KMeans


import numpy as np
X = np.array([[1.713,1.586], [0.180,1.786], [0.353,1.240],
[0.940,1.566], [1.486,0.759], [1.266,1.106],[1.540,0.419],[0.459,1.799],[0.773,0.186]])
y=np.array([0,1,1,0,1,0,1,1,1])
kmeans = KMeans(n_clusters=3, random_state=0).fit(X,y)
print("The input data is ")
print("VAR1 \t VAR2 \t CLASS")
i=0
for val in X: print(val[0],"\
t",val[1],"\t",y[i]) i+=1
print("="*20)
# To get test data from the user
print("The Test data to predict ")
test_data = []
VAR1 = float(input("Enter Value for VAR1 :"))

ADITYA COLLEGE OF ENGINEERING Reg No:


Exp No: Page No:
Date:

VAR2 = float(input("Enter Value for VAR2 :"))


test_data.append(VAR1)
test_data.append(VAR2)
print("="*20)
print("The predicted Class is : ",kmeans.predict([test_data]))

ADITYA COLLEGE OF ENGINEERING Reg No:


Exp No: Page No:
Date:

Experiment-5:
Aim: The following training examples map descriptions of individuals onto high, medium and low
credit-worthiness. Find the unconditional probability of 'golf' and the conditional probability of 'single'
given 'medRisk' in the dataset.

Description:
The following training examples map descriptions of individuals onto high, medium and low credit-worthiness.

medium skiing design single twenties no -> highRisk


high golf trading married forties yes -> lowRisk
low speedway transport married thirties yes -> medRisk
medium football banking single thirties yes -> lowRisk
high flying media married fifties yes -> highRisk
low football security single twenties no -> medRisk
medium golf media single thirties yes -> medRisk
medium golf transport married forties yes -> lowRisk
high skiing banking single thirties yes -> highRisk
low golf unemployed married forties yes -> highRisk

Input attributes are (from left to right) income, recreation, job, status, age-group, home-owner. Find the
unconditional probability of 'golf' and the conditional probability of 'single' given 'medRisk' in the dataset

In the given data set,

The total number of records are 10.


The number of records which contains 'golf' are 4.
Then, the Unconditional probability of golf :

= The number of records which contains 'golf' / total number of records


= 4 / 10
= 0.4

To find the Conditional probability of single given medRisk,


---> S : single
---> MR : medRisk
---> By the definition of Baye's rule( conditional probability ), we have
P(S ∣ MR) = P(S ∩ MR) / P(MR)
Based on the given problem statement,
P(S ∩ MR) = The number of MedRisk with Single records / total number of Records
= 2 / 10 = 0.2 and
P(MR) = The number of records with MedRisk /total number of Records
= 3 / 10 = 0.3

Then, the Conditional probability of single given medRisk


P(S ∣ MR) = 0.2 / 0.3 = 0.66666

ADITYA COLLEGE OF ENGINEERING Reg No:


Exp No: Page No:
Date:

CODE :

total_Records=10
numGolfRecords=4
unConditionalprobGolf=numGolfRecords / total_Records
print("Unconditional probability of golf: ={}".format(unConditionalprobGolf))
#conditional probability of 'single' given 'medRisk'
numMedRiskSingle=2
numMedRisk=3
probMedRiskSingle=numMedRiskSingle/total_Records
probMedRisk=numMedRisk/total_Records
conditionalProb=(probMedRiskSingle/probMedRisk)
print("Conditional probability of single given medRisk: = {}".format(conditionalProb))

ADITYA COLLEGE OF ENGINEERING Reg No:


Exp No: Page No:
Date:

Experiment-6:
Aim: Implement linear regression using python.

Description:
To run this program you need to install the pandas Module

pandas Module is used to read csv files


To install, Open Command propmt and then execute the following command
---> pip install pandas
And, then you need to install the matplotlib Module
matplotlib Module is used to plot the graphs
To install, Open Command propmt and then execute the following command
---> pip install matplotlib
Finally, you need to create dataset called "Age_Income.csv" file.

Code :
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
# To read data from Age_Income.csv file
dataFrame = pd.read_csv('Age_Income.csv')
# To place data in to age and income vectors
age = dataFrame['Age']
income = dataFrame['Income']

# number of points
num = np.size(age)
# To find the mean of age and income vector
mean_age = np.mean(age)
mean_income = np.mean(income)

# calculating cross-deviation and deviation about age


CD_ageincome = np.sum(income*age) - num*mean_income*mean_age
CD_ageage = np.sum(age*age) - num*mean_age*mean_age

# calculating regression coefficients


b1 = CD_ageincome / CD_ageage
b0 = mean_income - b1*mean_age

ADITYA COLLEGE OF ENGINEERING Reg No:


Exp No: Page No:
Date:

# to display coefficients
print("Estimated Coefficients :")
print("b0 = ",b0,"\nb1 = ",b1)
# To plot the actual points as scatter plot
plt.scatter(age, income, color = "b",marker = "o")
# TO predict response vector
response_Vec = b0 + b1*age
# To plot the regression line
plt.plot(age, response_Vec, color = "r")
# Placing labels
plt.xlabel('Age')
plt.ylabel('Income')
# To display plot
plt.show()

Age_Income.csv
Age, Income
25, 25000
23, 22000
24, 26000
28, 29000
34, 38600
32, 36500
42, 41000
55, 81000
45, 47500

ADITYA COLLEGE OF ENGINEERING Reg No:


Exp No: Page No:
Date:

Experiment-7
Aim: Implement naive baye's theorem to classify the English text.

Description:
To run this program you need to install the pandas Module
pandas Module is used to read csv files
To install, Open Command propmt and then execute the following command
---> pip install pandas
And, then you need to install the sklearn Module
Open Command propmt and then execute the following command to install sklearn Module
---> pip install scikit-learn
Finally, you need to create dataset called "Statements_data.csv" file.

CODE :

import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.naive_bayes import MultinomialNB
from sklearn.metrics import accuracy_score, confusion_matrix, precision_score, recall_score

msglbl_data = pd.read_csv('Statements_data.csv', names=['Message', 'Label'])


print("The Total instances in the Dataset: ", msglbl_data.shape[0])
msglbl_data['labelnum'] = msglbl_data.Label.map({'pos': 1, 'neg': 0})
# place the data in X and Y Vectors
X = msglbl_data["Message"]
Y = msglbl_data.labelnum
# to split the data into train se and test set
Xtrain, Xtest, Ytrain, Ytest = train_test_split(X, Y)

count_vect = CountVectorizer()
Xtrain_dims = count_vect.fit_transform(Xtrain)
Xtest_dims = count_vect.transform(Xtest)
df = pd.DataFrame(Xtrain_dims.toarray(),columns=count_vect.get_feature_names_out())
clf = MultinomialNB()
# to fit the train data into model
clf.fit(Xtrain_dims, Ytrain)
# to predict the test data

ADITYA COLLEGE OF ENGINEERING Reg No:


Exp No: Page No:
Date:

prediction = clf.predict(Xtest_dims)
print('******** Accuracy Metrics *********')
print('Accuracy : ', accuracy_score(Ytest, prediction))
print('Recall : ', recall_score(Ytest, prediction))
print('Precision : ',precision_score(Ytest, prediction))
print('Confusion Matrix : \n', confusion_matrix(Ytest, prediction))
print(10*"-")
# to predict the input statement
test_stmt = [input("Enter any statement to predict :")]
test_dims = count_vect.transform(test_stmt)
pred = clf.predict(test_dims)
for stmt,lbl in zip(test_stmt,pred):
if lbl == 1:
print("Statement is Positive")
else:
print("Statement is Negative")

Statements_data.csv

This is very good place,pos


I like this biryani,pos
I feel very happy,pos
This is my best work,pos
I do not like this restaurant,neg
I am tired of this stuff,neg
I can't deal with this,neg
What an idea it is,pos
My place is horrible,neg
This is an awesome place,pos
I do not like the taste of this juice,neg
I love to sing,pos
I am sick and tired,neg
I love to dance,pos
What a great holiday,pos
That is a bad locality to stay,neg
We will have good fun tomorrow,pos
I hate this food,neg

ADITYA COLLEGE OF ENGINEERING Reg No:


Exp No: Page No:
Date:

Experiment-8
Aim: Implement an algorithm to demonstrate the significance of genetic algorithm.

Description:
ALGORITHM:
1) Randomly initialize populations p
2) Determine fitness of population
3) Untill convergence repeat:
a) Select parents from population
b) Crossover and generate new population
c) Perform mutation on new population
d) Calculate fitness for new population

USES OF GENETIC ALGORITHM:


They are Robust
Provide optimisation over large space state.
Unlike traditional AI, they do not break on slight change in input or presence of noise.

CODE :

Import numpy
Def cal_pop_fitness(equation_inputs, pop):
# Calculating the fitness value of each solution in the current population.
# The fitness function calulates the sum of products between each input and its
corresponding weight.

fitness = numpy.sum(pop*equation_inputs,axis=1) return fitness


defselect_mating_pool(pop, fitness, num_parents):

# Selecting the best individuals in the current generation as parents for producing the
offspring of the next generation.
parents = numpy.empty((num_parents,pop.shape[1]))
for parent_num in range(num_parents):
max_fitness_idx = numpy.where(fitness == numpy.max(fitness)) max_fitness_idx =
max_fitness_idx[0][0]
parents[parent_num, :] = pop[max_fitness_idx, :]
fitness[max_fitness_idx] = -99999999999
return parents
def crossover(parents,offspring_size): offspring =
numpy.empty(offspring_size)

# The point at which crossover takes place between two parents. Usually, it is at the center.
crossover_point = numpy.uint8(offspring_size[1]/2)

ADITYA COLLEGE OF ENGINEERING Reg No:


Exp No: Page No:
Date:

for k in range(offspring_size[0]):
# Index of the first parent to
mate. parent1_idx = k%parents.shape[0]
# Index of the second parent to mate.
parent2_idx = (k+1)%parents.shape[0]
# The new offspring will have its first half of its genes taken from the first
parent. offspring[k, 0:crossover_point] = parents[parent1_idx,
0:crossover_point]
# The new offspring will have its second half of its genes taken from the second parent.
offspring[k, crossover_point:] = parents[parent2_idx, crossover_point:]
return offspring
def mutation(offspring_crossover, num_mutations=1):
mutations_counter = numpy.uint8(offspring_crossover.shape[1] / num_mutations)
# Mutation changes a number of genes as defined by the num_mutations
argument. The changes are random.
for id x in
range(offspring_crossover.shape[0]):
gene_idx = mutations_counter - 1
formutation_num in range(num_mutations):
# The random value to be added to the gene.
random_value = numpy.random.uniform(-1.0, 1.0,
1)
offspring_crossover[idx, gene_idx] = offspring_crossover[idx,
gene_idx] + random_value
gene_idx = gene_idx +
mutations_counter return
offspring_crossover
import numpy
"""
The y=target is to maximize this
equation ASAP: y =
w1x1+w2x2+w3x3+w4x4+w5x5+6wx6
where (x1,x2,x3,x4,x5,x6)=(4,-2,3.5,5,-
11,-4.7)
What are the best values for the 6 weights w1 to w6?
We are going to use the genetic algorithm for the best possible values after a
number of generations.
"""
# Inputs of the equation.
equation_inputs = [4,-
2,3.5,5,-11,-4.7]
# Number of the weights we are looking to
optimize. num_weights = len(equation_inputs)
Genetic algorithm
parameters: Mating

ADITYA COLLEGE OF ENGINEERING Reg No:


Exp No: Page No:
Date:

pool size
Population size
"""
sol_per_pop = 8
num_parents_mating = 4
# Defining the population size.
pop_size = (sol_per_pop,num_weights) # The population will have sol_per_pop
chromosome where each chromosome has num_weights genes.
#Creating the initial population.
new_population = numpy.random.uniform(low=-4.0, high=4.0,
size=pop_size) print(new_population)
"""
new_population[0, :] = [2.4, 0.7, 8, -2, 5, 1.1]
new_population[1, :] = [-0.4, 2.7, 5, -1, 7, 0.1]
new_population[2, :] = [-1, 2, 2, -3, 2,0.9]
new_population[3, :]=[4, 7, 12, 6.1, 1.4, -4]
new_population[4, :] = [3.1, 4, 0, 2.4, 4.8,0]
new_population[5, :] = [-2, 3, -7,6, 3,3]

crossover. offspring_crossover =
crossover(parents,
offspring_size=(pop_size[0]-parents.shape[0], num_weights))
print("Crossover")
print(offspring_cr
ossover)
# Adding some variations to the offspring using mutation.
offspring_mutation = mutation(offspring_crossover,
num_mutations=2) print("Mutation")
print(offspring_mutation)
# Creating the new population based on the parents and offspring.
new_population[0:parents.shape[0], :] = parents
new_population[parents.shape[0]:, :] = offspring_mutation
# Getting the best solution after iterating finishing all generations.
#At first, the fitness is calculated for each solution in the final
generation. fitness = cal_pop_fitness(equation_inputs,
new_population)
# Then return the index of that solution corresponding to the
best fitness. best_match_idx = numpy.where(fitness ==
numpy.max(fitness))
print("Best solution : ", new_population[best_match_idx, :])
print("Best solution fitness : ", fitness[best_match_idx])
importmatplotlib.pyplotmatplotlib.pyplot.plot(best_outputs)
matplotlib.pyplot.xlabel("Iteration")
matplotlib.pyplot.ylabel("Fitness")
matplotlib.pyplot.show()

ADITYA COLLEGE OF ENGINEERING Reg No:


Exp No: Page No:
Date:

Experiment-9
Aim: Implement the finite words classification system using Back-propagation algorithm.

Description:
To run this program you need to install the pandas Module
pandas Module is used to read csv files
To install, Open Command propmt and then execute the following command pip
install pandasAnd, then you need to install the sklearn Module
Open Command propmt and then execute the following command to install sklearn Module pip install
scikit-learn
Open Command propmt and then execute the following command to install sklearn-neuralnetwork Module
pip install scikit-neuralnetwork
Finally, you need to create dataset called "Statements_data.csv"

CODE :

import pandas as pdfrom sklearn.model_selection


import train_test_splitfrom sklearn.feature_extraction.text import
CountVectorizerfrom sklearn.neural_network
import MLPClassifierfrom sklearn.metrics import accuracy_score, confusion_matrix, precision_score,
recall_scoremsglbl_data = pd.read_csv('Statements_data.csv', names=['Message', 'Label']) print("The Total
instances in the Dataset: ", msglbl_data.shape[0])
msglbl_data['labelnum'] = msglbl_data.Label.map({'pos': 1, 'neg': 0}) #
place the data in X and Y
VectorsX = msglbl_data["Message"]Y = msglbl_data.labelnum # to
split the data into train se and test
setXtrain, Xtest, Ytrain, Ytest = train_test_split(X, Y)
count_vect = CountVectorizer()Xtrain_dims = count_vect.fit_transform(Xtrain) Xtest_dims =
count_vect.transform(Xtest)df =
pd.DataFrame(Xtrain_dims.toarray(),columns=count_vect.get_feature_names_out())clf =
MLPClassifier(solver='lbfgs', alpha=1e-5,hidden_layer_sizes=(5, 2), random_state=1)
# to fit the train data into modelclf.fit(Xtrain_dims, Ytrain)
# to predict the test dataprediction = clf.predict(Xtest_dims)
print('******** Accuracy Metrics *********')print('Accuracy : ', accuracy_score(Ytest, prediction))
print('Recall : ', recall_score(Ytest, prediction))
print('Precision : ',precision_score(Ytest, prediction))print('Confusion Matrix : \n',
confusion_matrix(Ytest, prediction))
print(10*"-")

ADITYA COLLEGE OF ENGINEERING Reg No:


Exp No: Page No:
Date:

# to predict the input


statementtest_stmt = [input("Enter any statement to predict :")] test_dims =
count_vect.transform(test_stmt)pred = clf.predict(test_dims) for stmt,lbl in
zip(test_stmt,pred):
if lbl == 1:
print("Statement is Positive")
else:
print("Statement is Negative")

Statements_data.csv

This is very good place,pos


I like this biryani,pos
I feel very happy,pos
This is my best work,pos
I do not like this restaurant,neg
I am tired of this stuff,neg
I can't deal with this,neg
What an idea it is,pos
My place is horrible,neg
This is an awesome place,pos
I do not like the taste of this juice,neg
I love to sing,pos
I am sick and tired,neg
I love to dance,pos
What a great holiday,pos
That is a bad locality to stay,neg
We will have good fun tomorrow,pos
I hate this food,neg

ADITYA COLLEGE OF ENGINEERING Reg No:

You might also like