ML Lab1,2
ML Lab1,2
Experiment-1
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 theprobability
that a student is absent given that today is Friday? Apply Baye’s rule in python to get
the result. (Ans: 15%)
Machine learning is a method of data analysis that automates analytical model building of
data set. Using the implemented algorithms that iteratively learn from data, machine learning
allows computers to find hidden insights without being explicitly programmed where to look.
Naive bayes algorithm is one of the most popular machines learning technique. In this article
we will look how to implement Naive bayes algorithm using python.
Before someone can understand Bayes’ theorem, they need to know a couple of related
concepts first, namely, the idea of Conditional Probability, and Bayes’ Rule.
Conditional Probability is just what is the probability that something will happen, given that
something else has already happened.
Let say we have a collection of people. Some of them are singers. They are either male or
female. If we select a random sample, what is the probability that this person is a male? what
is the probability that this person is a male and singer? Conditional Probability is the best
option here. We can calculate probability like,
We can simply define Bayes rule like this. Let A1, A2, …, An be a set of mutually exclusive
events that together form the sample space S. Let B be any event from the same sample
space, such that P(B) > 0. Then, P( Ak | B ) = P( Ak ∩ B ) / P( A1 ∩ B ) + P( A2 ∩ B ) + . . .
+ P( An ∩ B )
Naive Bayes classifiers are a family of simple probabilistic classifiers based on applying
Bayes’ theorem with strong (naive) independence assumptions between the features in
machine learning. Basically we can use above theories and equations for classification
problem.
AIM: To find the probability that a student is absent given that today is Friday.
probAbsentFriday=0.03
probFriday=0.2
# bayes Formula
#p(Absent|Friday)=p(Friday|Absent)p(Absent)/p(Friday)
Machine Learning Lab
#p(Friday|Absent)=p(Friday∩Absent)/p(Absent)
bayesResult=(probAbsentFriday/probFriday)
print(bayesResult * 100)
Output: 15
Machine Learning Lab
Experiment-2
You’ll learn the following MySQL SELECT operations from Python using a ‘MySQL
Connector Python’ module.
Execute the SELECT query and process the result set returned by the query in
Python.
Use Python variables in a where clause of a SELECT query to pass dynamic
values.
Use fetchall(), fetchmany(), and fetchone() methods of a cursor class to fetch
all or limited rows from a table.
Python Select from MySQL Table
You’ll learn the following MySQL SELECT operations from Python using a ‘MySQL
Connector Python’ module.
Execute the SELECT query and process the result set returned by the query in
Python.
Use Python variables in a where clause of a SELECT query to pass dynamic
values.
Use fetchall() , fetchmany() , and fetchone() methods of a cursor class to fetch
all or limited rows from a table.
Next, prepare a SQL SELECT query to fetch rows from a table. You can select
all or limited rows based on your requirement. If the where condition is used,
then it decides the number of rows to fetch.
Machine Learning Lab
Iterate a row list using a for loop and access each row individually (Access each
row’s column data using a column name or index number.)
import pymysql
def mysqlconnect():
# To connect MySQL database
conn = pymysql.connect(
host='localhost',
user='root',
password = "pass",
db='College',
)
cur = conn.cursor()
cur.execute("select @@version")
output = cur.fetchall()
print(output)
conn.close()
Machine Learning Lab