ML Acoerecordiot
ML Acoerecordiot
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 :
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;
Next,Open Command propmt and then execute the following command to install mysql.connector package to
connect with mysql database through python.
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 :")
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
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
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 :
# 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]])
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......")
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
CODE :
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.
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
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))
Experiment-6:
Aim: Implement linear regression using python.
Description:
To run this program you need to install the pandas Module
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)
# 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
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
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
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
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
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.
# 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)
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
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()
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 :
Statements_data.csv