0% found this document useful (0 votes)
55 views

ML Lab1,2

The document discusses two machine learning experiments: 1) Applying Bayes' rule in Python to calculate the probability that a student is absent given it is Friday, which is 15%. 2) Different SQL SELECT operations that can be performed from Python on a MySQL database, including fetching all rows, limited rows, and using WHERE clauses to fetch specific rows based on conditions. 3) Steps for connecting to a MySQL database from Python and executing SELECT queries to fetch data, including getting a cursor, executing queries, extracting rows, iterating over rows, and closing connections.

Uploaded by

kirsagar akash
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)
55 views

ML Lab1,2

The document discusses two machine learning experiments: 1) Applying Bayes' rule in Python to calculate the probability that a student is absent given it is Friday, which is 15%. 2) Different SQL SELECT operations that can be performed from Python on a MySQL database, including fetching all rows, limited rows, and using WHERE clauses to fetch specific rows based on conditions. 3) Steps for connecting to a MySQL database from Python and executing SELECT queries to fetch data, including getting a cursor, executing queries, extracting rows, iterating over rows, and closing connections.

Uploaded by

kirsagar akash
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/ 5

Machine Learning Lab

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,

P(Singer & Male) = P(Male) x P(Singer / Male)

What is Bayes rule ?

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 )

What is Bayes classifier?

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)

# Therefore the result is:

bayesResult=(probAbsentFriday/probFriday)

print(bayesResult * 100)

Output: 15
Machine Learning Lab

Experiment-2

Extract the data from database using python

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

This article demonstrates how to select rows of a MySQL table in Python.

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.

 Steps to fetch rows from a MySQL database table

Follow these steps: –

How to Select from a MySQL table using Python

1. Connect to MySQL from Python

Python MySQL database connection to connect to MySQL database from


Python using MySQL Connector module

2. Define a SQL SELECT Query

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

For example, SELECT col1, col2,…colnN FROM MySQL_table WHERE id =


10; . This will return row number 10.

3. Get Cursor Object from Connection

Next, use a  connection.cursor()  method to create a cursor object. This


method creates a new  MySQLCursor  object.

4. Execute the SELECT query using execute() method

Execute the select query using the  cursor.execute()  method.

5. Extract all rows from a result

After successfully executing a Select operation, Use the fetchall() method of a


cursor object to get all rows from a query result. it returns a list of rows.

6. Iterate each row

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.)

7. Close the cursor object and database connection object

use  cursor.clsoe()  and  connection.clsoe()  method to close open


connections after your work completes.

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)

# To close the connection

conn.close()
Machine Learning Lab

You might also like