ML Lab Record
ML Lab Record
INTRODUCTION TO LAB:
Machine Learning is used anywhere from automating mundane tasks to offering intelligent insights,
industries in every sector try to benefit from it. You may already be using a device that utilizes it. For
example, a wearable fitness tracker like Fitbit, or an intelligent home assistant like Google Home. But
there are much more examples of ML in use.
Prediction:Machine learning can also be used in the prediction systems. Considering the loan
example, to compute the probability of a fault, the system will need to classify the available
data ingroups.
Image recognition:Machine learning can be used for face detection in an image as well. There
is aseparate category for each person in a database of several people.
Speech Recognition:It is the translation of spoken words into the text. It is used in voice
searches and more. Voice user interfaces include voice dialing, call routing, and appliance
control. It can also be used a simple data entry and the preparation of structured documents.
Medical diagnoses:ML is trained to recognize cancerous tissues.
Financial industry:andtrading:companies use ML in fraud investigations and credit checks.
1. Supervised Learning
2. Unsupervised Learning
3. Reinforcement Learning
In Supervised learning, an AI system is presented with data which is labeled, which means that each
datatagged with the correct label.
The goal is to approximate the mapping function so well that when you have new input data (x) that
youcan predict the output variables (Y) for that data.
As shown in the above example, we have initially taken some data and marked them as ‘Spam’ or ‘Not
Spam’. This labeled data is used by the training supervised model, this data is used to train the model.
Once it is trained we can test our model by testing it with some test new mails and checking of the
model is able to predict the right output.
Types of Supervised learning
In unsupervised learning, an AI system is presented with unlabeled, uncategorized data and the
system’s algorithms act on the data without prior training. The output is dependent upon the coded
algorithms. Subjecting a system to unsupervised learning is one way of testing AI.
Clustering: A clustering problem is where you want to discover the inherent groupings in the
data,such as grouping customers by purchasing behavior.
Association: An association rule learning problem is where you want to discover rules that
describe large portions of your data, such as people that buy X also tend to buy Y.
A reinforcement learning algorithm, or agent, learns by interacting with its environment. The agent
receives rewards by performing correctly and penalties for performing incorrectly. The agent learns
without intervention from a human by maximizing its reward and minimizing its penalty. It is a type of
dynamic programming that trains algorithms using a system of reward and punishment.
in the above example, we can see that the agent is given 2 options i.e. a path with water or a path
with fire. A reinforcement algorithm works on reward a system i.e. if the agent uses the fire path
then the rewards are subtracted and agent tries to learn that it should avoid the fire path. If it had
chosen the water path or the safe path then some points would have been added to the reward
points, the agent then would try to learn what path is safe and what path isn’t.
It is basically leveraging the rewards obtained; the agent improves its environment knowledge to
select thenext action.
PROGRAM 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.
SOURCE CODE:
absgivenfri=abonfri/prothatfri
print("The probability that the student is absent given that today is Fridays is",absgivenfri)
O/P:
Method - I
SOURCE CODE:
import mysql.connector
my_database=mysql.connector.connect(host="localhost", user="root", password="root",
database="mysql")
cursor=my_database.cursor()
sql="insert into player1(name, jersey_no,age,score)values(%s,%s,%s,%s)"
player2=[('sachin',10,20,50),('kohile',20,30,100),('dhoni',40,35,110)]
cursur.executemany(sql,player2)
my_database.commit()
cursor.execute("select * from player1")
cursor.fetchall()
output:
Method - II
=================================
Explanation:
=================================
===> 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.
And then, you need to execute the following commands at MySQL prompt to create table in the
database.
===> Next,Open Command propmt and then execute the following command to install mysql.connector
package to connect with mysql database through python.
===============================
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()
Output:
PROGRAM 3: IMPLEMENT K-NEAREST NEIGHBORS CLASSIFICATION
USINGPYTHON
SOURCE CODE:
print(__doc__)
import numpy as np
n_neighbors=15
#Loading data
iris=load_iris()
#print(irisData)
#print(X)
y=iris.target
cmap_light=ListedColormap(['#FFAAAA','#AAFFAA','#AAAAFF'])
cmap_bold=ListedColormap(['#FF0000','#00FF00','#0000FF'])
clf=KNeighborsClassifier(n_neighbors,weights=weights)
clf.fit(X,y)
xx,yy=np.meshgrid(np.arange(x_min,x_max,h),np.arange(y_min,y_max,h))
Z=clf.predict(np.c_[xx.ravel(),yy.ravel()])
plt.figure()
plt.pcolormesh(xx,yy,Z,cmap=cmap_light,shading='auto')
plt.xlim(xx.min(),xx.max())
plt.ylim(yy.min(),yy.max())
plt.show()
Output:
SOURCE CODE:
# Import necessary modules
# Loading data
irisData = load_iris()
#print(irisData)
X = irisData.data
y = irisData.target
print(X_test)
print(y_test)
knn = KNeighborsClassifier(n_neighbors=7,weights='distance')
knn.fit(X_train, y_train)
print(knn.predict(X_test))
output:
PROGRAM-4: Given the following data, which specify classifications for nine
combinations of VAR1 and VAR2 predict a classification for a case where
VAR1=0.906 and VAR2=0.606, using the result of k-means clustering with 3 means
(i.e., 3centroids)
SOURCE CODE:
import numpy as np
import pandas as pd
data=pd.read_csv("D:\ML LAB\Lab_Final\Lab_Final\kmeansdata.csv")
print(data)
var1=pd.DataFrame(data['VAR1'])
var2=pd.DataFrame(data['VAR2'])
kmeans=KMeans(3)
pred_var2=kmeans.fit_predict(var1)
print(pred_var2)
plt.scatter(var1,var2)
plt.show()
CSV FILE:
OUTPUT:
PROGRAM 5: The Following Training Examples Map Descriptions Of Individuals Onto High,
Medium And LowCredit-Worthiness.
SOURCE CODE:
import pandas as pd
data=pd.read_csv("D:\ML LAB\Lab_Final\Lab_Final\Credit-Worthiness.csv")
def unc_prob(val,attr):
val_count=0
if ele==val:
val_count+=1
return val_count/len(data[attr])
def cond_prob(val1,attr1,val2,attr2):
val_count1,data_count=0,0
if ele2==val2:
data_count+=1
if ele1==val1:
val_count1+=1
return val_count1/data_count
inp_value,inp_attr=input("Enter the value name and attribute name for which you want to find
unconditional probability with a space in between").split()
ele_unc_prob=unc_prob(inp_value,inp_attr)
ele_cond_prob=cond_prob(inp_value1,inp_attr1,inp_value2,inp_attr2)
print(ele_unc_prob)
print(ele_cond_prob)
CSV FILE:
OUTPUT:
PROGRAM 6: IMPLEMENT LINEAR REGRESSION USINGPYTHON.
SOURCE CODE:
# A program to illustrate linear regression
# Import Libraries
import pandas as pd
import matplotlib.pyplot as plt
from sklearn import linear_model
# Load Dataset
data=pd.read_csv('D:\ML LAB\Lab_Final\Lab_Final\weight-height.csv')
# Quick view about the data
#print(data)
#data.plot(kind='scatter',x='Height',y='Weight')
#data.plot(kind='box')
#plt.show()
#print(data.corr())
# Change to DataFrame variables
Height=pd.DataFrame(data['Height'])
Weight=pd.DataFrame(data['Weight'])
print(Weight)
print(Height)
# Build Linear Regression Model
lm=linear_model.LinearRegression()
model=lm.fit(Height,Weight)
print(model.coef_)
print(model.intercept_)
print(model.score(Height,Weight))#Evaluate the model
Height_new=pd.DataFrame([65,60,68])
Weight_new=model.predict(Height_new)
Weight_new=pd.DataFrame(Weight_new)
df=pd.concat([Height_new,Weight_new],axis=1,keys=['Height_new','Weight_new'])
print(df)
# Visualize the result
data.plot(kind='scatter',x='Height',y='Weight')
#Plotting the regression line
plt.plot(Height,model.predict(Height),color='red',linewidth=2)
# Plotting the predicted values
plt.scatter(Height_new,Weight_new,color='red')
plt.show()
CSV FILE:
OUTPUT:
PROGRAM 7: IMPLEMENT NAÏVE BAYES THEOREM TO CLASSIFY THE
ENGLISHTEXT
SOURCE CODE:
data = fetch_20newsgroups()
print(data)
text_categories = data.target_names
print(test_data.data[5])
predicted_categories = model.predict(test_data.data)
print(np.array(test_data.target_names)[predicted_categories])
plt.xlabel("true labels")
plt.ylabel("predicted label")
plt.show()
OUTPUT:
PROGRAM 8: IMPLEMENT AN ALGORITHM TO DEMONSTRATE THE
SIGNIFICANCE OFGENETICALGORITHM
SOURCE CODE:
import numpy
def cal_pop_fitness(equation_inputs, pop):
fitness = numpy.sum(pop*equation_inputs, axis=1)
return fitness
def select_mating_pool(pop, fitness, num_parents):
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)
crossover_point = numpy.uint8(offspring_size[1]/2)
for k in range(offspring_size[0]):
parent1_idx = k%parents.shape[0]
parent2_idx = (k+1)%parents.shape[0]
offspring[k, 0:crossover_point] = parents[parent1_idx, 0:crossover_point]
offspring[k, crossover_point:] = parents[parent2_idx, crossover_point:]
return offspring
def mutation(offspring_crossover):
for idx in range(offspring_crossover.shape[0]):
random_value = numpy.random.uniform(-1.0, 1.0, 1)
offspring_crossover[idx, 4] = offspring_crossover[idx, 4] + random_value
return offspring_crossover
PROGRAM 9: IMPLEMENT THE FINITE WORDS CLASSIFICATION SYSTEM USING
BACK-PROPAGATIONALGORITHM
SOURCE CODE:
import pandas as pd
X = msg.message
y = msg.labelnum
count_v = CountVectorizer()
Xtrain_dm = count_v.fit_transform(Xtrain)
Xtest_dm = count_v.transform(Xtest)
df = pd.DataFrame(Xtrain_dm.toarray(),columns=count_v.get_feature_names())
print(df[0:5])
clf = MultinomialNB()
clf.fit(Xtrain_dm, ytrain)
pred = clf.predict(Xtest_dm)
CSV FILE:
OUTPUT: