Machine Learning Lab Manual
Machine Learning Lab Manual
LIBRARIES :
Machine learning programs often require various libraries depending on
the specific tasks and algorithms being used. Some commonly used
libraries in Python for machine learning are:
''' 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.
=================================
Explanation:
=================================
F : Friday
A : Absent
and
Then,
The probability that a student is absent given that today is Friday
P(A ∣ F)
Source 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,"%")
Output:
(base) C:\Users\STUDENT\Desktop\ml programs>python week1.py
The probability that it is Friday and that a student is absent : 0.03
The probability that it is Friday : 0.2
The probability that a student is absent given that today is Friday : 15.0
%
2. Extract the data from database using python.
=================================
Explanation:
=================================
---> Open Command prompt and then execute the following command
to enter into MySQL prompt.
Source 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 :")
# Printing the result
for x in result:
print(x);
# Commit the transaction
myconn.commit()
# Close the connection
myconn.close()
=================================
Explanation:
=================================
===> 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%).
Source code:
import random
# Loading data
data_iris = load_iris()
label_target = data_iris.target_names
print()
for i in range(10):
rn = random.randint(0,120)
print(data_iris.data[rn],"===>",label_target[data_iris.target[rn]])
X = data_iris.data
y = data_iris.target
try:
knn.fit(X_train, y_train)
for i in range(len(test_data)):
test_data[i] = float(test_data[i])
print()
v = knn.predict([test_data])
except:
Output
(base) C:\Users\STUDENT\Desktop\ml programs>python week3.py
******************************
=================================
Explanation:
=================================
===> To run this program you need to install the sklearn Module
===> Open Command propmt and then execute the following command
to install sklearn Module
Finally, you need to predict the class for the VAR1=0.906 and
VAR2=0.606
Source code:
import numpy as np
y=np.array([0,1,1,0,1,0,1,1,1])
i=0
for val in X:
print(val[0],"\t",val[1],"\t",y[i])
i+=1
print("="*20)
test_data = []
test_data.append(VAR1)
test_data.append(VAR2)
print("="*20)
Output:
The input data is
VAR1 VAR2 CLASS
1.713 1.586 0
0.18 1.786 1
0.353 1.24 1
0.94 1.566 0
1.486 0.759 1
1.266 1.106 0
1.54 0.419 1
0.459 1.799 1
0.773 0.186 1
====================
The Test data to predict
Enter Value for VAR1 :0.906
Enter Value for VAR2 :0.606
====================
The predicted Class is : [0]
5. 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
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
=================================
Explanation:
=================================
******************************
---> S : single
---> MR : medRisk
Source Code:
total_Records=10
numGolfRecords=4
unConditionalprobGolf=numGolfRecords / total_Records
numMedRiskSingle=2
numMedRisk=3
probMedRiskSingle=numMedRiskSingle/total_Records
probMedRisk=numMedRisk/total_Records
conditionalProb=(probMedRiskSingle/probMedRisk)
=================================
Explanation:
=================================
===> To run this program you need to install the pandas Module
===> To install, Open Command propmt and then execute the following
command
Source code:
import pandas as pd
import numpy as np
dataFrame = pd.read_csv('Age_Income.csv')
age = dataFrame['Age']
income = dataFrame['Income']
# number of points
num = np.size(age)
mean_age = np.mean(age)
mean_income = np.mean(income)
b1 = CD_ageincome / CD_ageage
b0 = mean_income - b1*mean_age
# to display coefficients
response_Vec = b0 + b1*age
# 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
Output:
Estimated Coefficients :
b0 = -14560.45016077166
b1 = 1550.7923748277433
7. Implement naive baye's theorem to classify the English text
=================================
Explanation:
=================================
===> To run this program you need to install the pandas Module
===> To install, Open Command propmt and then execute the following
command
===> Open Command propmt and then execute the following command
to install sklearn Module
Source 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_n
ames_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")
Output:
(base) C:\Users\STUDENT\Desktop\ml programs>python week7.py
The Total instances in the Dataset: 18
******** Accuracy Metrics *********
Accuracy : 0.4
Recall : 0.3333333333333333
Precision : 0.5
Confusion Matrix :
[[1 1]
[2 1]]
----------
Enter any statement to predict :i feel very happy
Statement is Positive
8.Implement an algorithm to demonstrate the significance of genetic
algorithm
import random
# Define parameters
POPULATION_SIZE = 100
GENERATIONS = 100
MUTATION_RATE = 0.1
CROSSOVER_RATE = 0.8
CAPACITY = 50
# Define items (weight, value)
items = [(10, 60), (20, 100), (30, 120), (40, 150), (50, 200)]
def generate_individual():
def fitness(individual):
return 0
else:
return total_value
def selection(population):
total_fitness = sum(fitness_values)
def mutation(individual):
for i in range(len(individual)):
individual[i] = 1 - individual[i]
return individual
new_population = []
new_population.append(best_individual)
while len(new_population) < POPULATION_SIZE:
child1 = mutation(child1)
child2 = mutation(child2)
new_population.extend([child1, child2])
population = new_population
best_fitness = fitness(best_solution)
Statements_data.csv
import pandas as pd
X = msglbl_data["Message"]
Y = msglbl_data.labelnum
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_n
ames_out())
clf.fit(Xtrain_dims, Ytrain)
prediction = clf.predict(Xtest_dims)
print(10*"-")
test_dims = count_vect.transform(test_stmt)
pred = clf.predict(test_dims)
if lbl == 1:
print("Statement is Positive")
else:
print("Statement is Negative")
Output:
Accuracy : 0.4
Recall : 0.3333333333333333
Precision : 0.5
Confusion Matrix :
[[1 1]
[2 1]]
----------
Statement is Positive