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 i
s 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:
=================================
===> First You need to Create a Table (students) in Mysql Database (Sa
mpleDB)
---> Open Command prompt and then execute the following command to
enter into MySQL prompt.
And then, you need to execute the following commands at MySQL prom
pt to create table in the database.
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 int
o training(70%) and test set(30%).
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)
Source code:
import random
# Loading data
data_iris = load_iris()
label_target = data_iris.target_names
print()
print("*"*30)
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 = KNeighborsClassifier(nn)
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
******************************
'''Aim: Given the following data, which specify classifications for nine o
mbinationsof VAR1 and VAR2 predict a classification for a case where
VAR1=0.906and VAR2=0.606, using the result of k-means clustering wi
th 3 means (i.e., 3centroids)
=================================
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
[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])
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
P(S ∩ MR) = The number of MedRisk with Single records / total num
ber of Records
= 2 / 10 = 0.2
and
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)
Output:
(base) C:\Users\STUDENT\Desktop\ml programs>python week5.py
=================================
Explanation:
=================================
===> To run this program you need to install the pandas Module
===> To install, Open Command propmt and then execute the following
command
===> To install, Open Command propmt and then execute the following
command
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
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
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)
child1 = mutation(child1)
child2 = mutation(child2)
new_population.extend([child1, child2])
population = new_population
best_fitness = fitness(best_solution)
Output:
10. ''' Implement the finite words classification system using Back-pr
opagation algorithm
11.
12. =================================
13. Explanation:
14. =================================
15.
16. ===> To run this program you need to install the pandas Module
17.
18. ---> pandas Module is used to read csv files
19.
20. ===> To install, Open Command propmt and then execute the fol
lowing command
21.
22. ---> pip install pandas
23.
24.
25. And, then you need to install the sklearn Module
26.
27. ===> Open Command propmt and then execute the following co
mmand to install sklearn Module
28.
29. ---> pip install scikit-learn
30.
31. ===> Open Command propmt and then execute the following co
mmand to install sklearn-neuralnetwork Module
32.
33. ---> pip install scikit-neuralnetwork
34.
35.
36. Finally, you need to create dataset called "Statements_data.csv" fi
le.
Statements_data.csv
Source code:
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 = MLPClassifier(solver='lbfgs', alpha=1e-5,hidden_layer_sizes=(5,
2), random_state=1)
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