0% found this document useful (0 votes)
2 views33 pages

M.tech ML Using Python Lab

The document outlines various machine learning experiments, including exercises on Linear and Logistic Regression, Support Vector Machines, Exploratory Data Analysis, Bias and Variance, Perceptron networks, Decision Trees using the ID3 algorithm, and Artificial Neural Networks with backpropagation. Each experiment includes source code and aims to solve real-world problems or demonstrate specific machine learning concepts. The document serves as a comprehensive guide for implementing and understanding different machine learning techniques.
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)
2 views33 pages

M.tech ML Using Python Lab

The document outlines various machine learning experiments, including exercises on Linear and Logistic Regression, Support Vector Machines, Exploratory Data Analysis, Bias and Variance, Perceptron networks, Decision Trees using the ID3 algorithm, and Artificial Neural Networks with backpropagation. Each experiment includes source code and aims to solve real-world problems or demonstrate specific machine learning concepts. The document serves as a comprehensive guide for implementing and understanding different machine learning techniques.
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

Exp1: Exercises to solve the real-world problems using the following machine learning

methods:

a) Linear Regression b) Logistic Regression.


Aim:To solve the real-world problems using machine learning of Linear and Logistic
regression.
Source code:
a) LinearRegression

import numpy as np
import pandas as pd
import [Link] as mtp
fromsklearn.linear_modelimport
LinearRegressionfrom
sklearn.model_selectionimport train_test_split
data_set=pd.read_csv(r'[Link]')
#print(data_set)
x=data_set.iloc[:,:-1].values
y=data_set.iloc[:,:-1].values

x_train , x_test ,y_train , y_test=train_test_split(x,y,test_size=1/3


,random_state=0)
regressor =LinearRegression() [Link](x_train,y_train)

y_pred = [Link](x_test)
x_pred = [Link](x_train)
[Link](x_train,y_train,color="green")
[Link](x_train,x_pred,color="red")
[Link]("salary vs experence(TrainingDataset }")
[Link]("Years of Experence")
[Link]("salary (in rupee)")
[Link]()
[Link](x_test,y_test,color="blue")
[Link](x_train,x_pred,color="red")
[Link]("salary vs experence(Training Dataset }")
[Link]("Years of Experence")
[Link]("salary (in rupee)")
[Link]()
Out put:
b)Logistic regression:

import numpy as nm
import [Link] as mtp
import pandas as pd
from sklearn.model_selection import train_test_split
from [Link] import StandardScaler
from sklearn.linear_model import LogisticRegression
from [Link] import confusion_matrix
from [Link] import ListedColormap
from [Link] import ListedColormap

#importing datasets
data_set= pd.read_csv('user_data.csv')
#print(data_set)
x= data_set.iloc[:, [3,4]].values
y= data_set.iloc[:, 5].values
x_train, x_test, y_train, y_test= train_test_split(x, y, test_size= 0.25,
random_state=0)
st_x= StandardScaler()
x_train= st_x.fit_transform(x_train)
x_test= st_x.transform(x_test)
classifier= LogisticRegression(random_state=0)
[Link](x_train, y_train)
LogisticRegression(C=1.0, class_weight=None, dual=False,
fit_intercept=True,
intercept_scaling=1, l1_ratio=None, max_iter=100,
multi_class='warn', n_jobs=None, penalty='l2',
random_state=0, solver='warn', tol=0.0001, verbose=0,
warm_start=False)
y_pred= [Link](x_test)
cm= confusion_matrix(y_test,y_pred)
x_set, y_set = x_train, y_train
x1, x2 = [Link]([Link](start = x_set[:, 0].min() - 1, stop =
x_set[:, 0].max() + 1, step =0.01),
[Link](start = x_set[:, 1].min() - 1, stop = x_set[:, 1].max() + 1,
step = 0.01))

[Link](x1, x2, [Link]([Link]([[Link](),


[Link]()]).T).reshape([Link]),
alpha = 0.75, cmap = ListedColormap(('purple','green' )))
[Link]([Link](), [Link]())
[Link]([Link](), [Link]())
for i, j in enumerate([Link](y_set)):
[Link](x_set[y_set == j, 0], x_set[y_set == j, 1],
c = ListedColormap(('purple', 'green'))(i), label = j)
[Link]('Logistic Regression (Training set)')
[Link]('Age')
[Link]('Estimated Salary')
[Link]()
[Link]()

x_set, y_set = x_test, y_test


x1, x2 = [Link]([Link](start = x_set[:, 0].min() - 1, stop =
x_set[:, 0].max() + 1, step =0.01),
[Link](start = x_set[:, 1].min() - 1, stop = x_set[:, 1].max() + 1,
step = 0.01))
[Link](x1, x2, [Link]([Link]([[Link](),
[Link]()]).T).reshape([Link]),
alpha = 0.75, cmap = ListedColormap(('purple','green' )))
[Link]([Link](), [Link]())
[Link]([Link](), [Link]())
for i, j in enumerate([Link](y_set)):
[Link](x_set[y_set == j, 0], x_set[y_set == j, 1],
c = ListedColormap(('purple', 'green'))(i), label = j)
[Link]('Logistic Regression (Test set)')
[Link]('Age')
[Link]('Estimated Salary')
[Link]()
[Link]()

Out put:

Exp2: Write a program to Implement Support Vector Machines.


Aim:To implement support vector machines.
Source Code:

import numpy as np
import pandas as pd from
sklearn import svm
from [Link] import PCA
from [Link] import load_iris
from sklearn.model_selection import train_test_split
from [Link] import accuracy_score
iris = load_iris()
X = [Link]
y = [Link]
X_train, X_test, y_train, y_test = train_test_split(X, y,
test_size=0.3, random_state=42)
pca = PCA(n_components=2)
print(pca)

X_train_pca = pca.fit_transform(X_train)
X_test_pca = [Link](X_test)
svm_clf = [Link](kernel='linear')
svm_clf.fit(X_train_pca, y_train)
y_pred = svm_clf.predict(X_test_pca)
accuracy = accuracy_score(y_test, y_pred)
print('Accuracy: {:.2f}'.format(accuracy))

OUTPUT:
PCA(n_components=2)

Accuracy: 0.98
Exp3: Exploratory Data Analysis for Classification using Pandas or
Matplotlib.
Aim:Data Analysis for Classification using pandas or Matplotlib.

Source Code:

import pandas as pd
import [Link] as plt
# Load the data into a Pandas dataframe
data = pd.read_csv('[Link]')
# Get a summary of the data
print([Link]())
# Plot histograms of the numerical features
[Link](bins=10, figsize=(20,15))
[Link]()
# Plot a scatter matrix of the numerical features
from [Link] import scatter_matrix
scatter_matrix(data, figsize=(20,15))
[Link]()
# Plot a bar chart of the loan purposes
data['loan_purpose'].value_counts().plot(kind='bar')
[Link]()
# Plot a pie chart of the labels data['label\t\
t'].value_counts().plot(kind='pie', autopct='%1.1f%%') [Link]()

#DATA SET DOWNLOAD

Exp4: Develop a program for Bias, Variance, Remove duplicates,


Cross Validation.
Aim:A program for Bias, Variance, Remove duplicates, Cross
Validation.
Source Code:
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from [Link] import mean_squared_error
# generate some sample data
X = [Link](100, 10)
y = [Link](100)
# split the data into training and test sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)
# train a linear regression model on the training data
model = LinearRegression()
[Link](X_train, y_train)
# calculate the mean squared error on the test data
y_pred = [Link](X_test)
mse = mean_squared_error(y_test, y_pred)
print(f"Mean squared error: {mse:.3f}")
# calculate the bias and variance
y_pred_train = [Link](X_train)
bias = [Link]((y_pred_train - y_train) ** 2)
variance = [Link]((y_pred - y_test) ** 2)
print(f"Bias: {bias:.3f}")
print(f"Variance: {variance:.3f}")
# remove duplicates from the data
X_no_duplicates, indices = [Link](X, axis=0, return_index=True)
y_no_duplicates = y[indices]
print(f"Number of duplicates removed: {[Link][0] -
X_no_duplicates.shape[0]}")
# perform k-fold cross-validation
from sklearn.model_selection import KFold
kf = KFold(n_splits=5)
mse_scores = []
for train_index, test_index in [Link](X):
X_train, X_test = X[train_index], X[test_index]
y_train, y_test = y[train_index], y[test_index]
model = LinearRegression()
[Link](X_train, y_train)
y_pred = [Link](X_test)
mse = mean_squared_error(y_test, y_pred)
mse_scores.append(mse)
print(f"Cross-validation mean squared error:
{[Link](mse_scores):.3f}")

OUTPUT: -
Mean squared error:
0.096 Bias: 0.064

Variance: 0.096
Number of duplicates removed: 0
Cross-validation mean squared error: 0.087
Exp5:Write a program to simulate a program for perception network for
pattern classification and function Approximation.

Aim:To simulate thepattern Classification.

Source Code:
from [Link] import load_breast_cancer
from sklearn.linear_model import Perceptron
from sklearn.model_selection import train_test_split
from sklearn import metrics
from [Link] import accuracy_score
from [Link] import confusion_matrix
# Load the Breast Cancer dataset
data = load_breast_cancer()
X = [Link]
y = [Link]
print([Link])
print([Link])
#Split the dataset into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3,
random_state=42)
print(X_train.shape)
print(X_test.shape)
print(y_train.shape)
print(y_test.shape)
# Create a Perceptron model
clf = Perceptron(max_iter=1000, eta0=0.1)
# Train the model
[Link](X_train, y_train)
# Make predictions
y_pred = [Link](X_test)
print(y_pred)
# Evaluate accuracy
accuracy = accuracy_score(y_test, y_pred)
print("Accuracy:", accuracy)
# Calculate confusion matrix
conf_matrix = confusion_matrix(y_test, y_pred)
cm_display = [Link](confusion_matrix = conf_matrix,
display_labels = [False, True])
cm_display.plot()
[Link]()

OUTPUT:
Exp6: Write a program to demonstrate the working of the decision tree
based ID3 algorithm. Use an appropriatedata set for building the decision
tree and apply this knowledge to classify a new sample.

Aim: To demonstrate the working of the decision tree based ID3 algorithm.

Source Code:

import math
import csv
def load_csv(filename):
lines=[Link](open(filename,"r"));
dataset = list(lines)
headers = [Link](0)
return dataset,headers
class Node:
def init (self,attribute):
[Link]=attribute
[Link]=[]
[Link]=""
def subtables(data,col,delete):
dic={}
coldata=[row[col] for row in data] attr=list(set(coldata))
counts=[0]*len(attr)
r=len(data)
c=len(data[0])
for x in range(len(attr)):
for y in range(r):
if data[y][col]==attr[x]:
counts[x]+=1
for x in range(len(attr)):
dic[attr[x]]=[[0 for i in range(c)] for j in range(counts[x])]
pos=0
for y in range(r):
if data[y][col]==attr[x]:
if delete:
del data[y][col]
dic[attr[x]][pos]=data[y]
pos+=1
return attr,dic
def entropy(S):
attr=list(set(S))
if len(attr)==1:
return 0
counts=[0,0]
for i in range(2):
counts[i]=sum([1 for x in S if attr[i]==x])/(len(S)*1.0)
sums=0
for cnt in counts:
sums+=-1*cnt*[Link](cnt,2)
return sums
def compute_gain(data,col):
attr,dic = subtables(data,col,delete=False)
total_size=len(data)
entropies=[0]*len(attr)
ratio=[0]*len(attr)
total_entropy=entropy([row[-1] for row in data])
for x in range(len(attr)):
ratio[x]=len(dic[attr[x]])/(total_size*1.0)
entropies[x]=entropy([row[-1] for row in dic[attr[x]]])
total_entropy-=ratio[x]*entropies[x]
return total_entropy
def build_tree(data,features): lastcol=[row[-1] for row in data]
if(len(set(lastcol)))==1:
node=Node("")
[Link]=lastcol[0]
return node
n=len(data[0])-1
gains=[0]*n
for col in range(n):
gains[col]=compute_gain(data,col)
split=[Link](max(gains))
node=Node(features[split])
fea = features[:split]+features[split+1:]
attr,dic=subtables(data,split,delete=True)

for x in range(len(attr)):
child=build_tree(dic[attr[x]],fea)
[Link]((attr[x],child))
return node
def print_tree(node,level):
if [Link]!="":
print("
"*level,[Link]) return
print(" "*level,[Link])
for value,n in [Link]:
print(" "*(level+1),value)
print_tree(n,level+2)
def classify(node,x_test,features):
if [Link]!="":
print([Link])
return
pos=[Link]([Link])
for value, n in [Link]:
if x_test[pos]==value:
classify(n,x_test,features)
'''Main program'''
#This is main program that calls previously defined functions
dataset,features=load_csv("[Link]")
node1=build_tree(dataset,features)
print("The decision tree for the dataset using ID3 algorithmis")
print_tree(node1,0)
#load second dataset to test the model
testdata,features=load_csv("data3_test.csv")
for xtest in testdata:
print("\n The test instance:",xtest)
print("The label for test instance:",end="")
classify(node1,xtest,features)
OUTPUT:
The decision tree for the dataset using ID3 algorithms
Outlook
Overcast
Yes
Rain
Wind
Strong
No
Weak
Yes
Sunny
Humidity
High
Exp7: Build an Artificial Neural Network by implementing the Back
propagation algorithm and test the sameusing appropriate data sets.

Aim:To build an artificial neural network by implementing the back


Propagation algorithm.
Source code:
import numpy as np
X = [Link](([2, 9], [1, 5], [3, 6]), dtype=float)
y = [Link](([92], [86], [89]), dtype=float)
X = X/[Link](X,axis=0) # maximum of X array longitudinally
y = y/100
#Sigmoid Function
def sigmoid (x):
return (1/(1 + [Link](-x)))
#Derivative of Sigmoid Function
def derivatives_sigmoid(x):
return x * (1 - x)
#Variable initialization
epoch=7000 #Setting training iterations
lr=0.1 #Setting learning rate
inputlayer_neurons = 2 #number of features in data set
hiddenlayer_neurons = 3 #number of hidden layers neurons
output_neurons = 1
#weight and bias initialization
wh=[Link](size=(inputlayer_neurons,hiddenlayer_neuro
ns))
bh=[Link](size=(1,hiddenlayer_neurons))
wout=[Link](size=(hiddenlayer_neurons,output_neurons
))
bout=[Link](size=(1,output_neurons))
# draws a random range of numbers uniformly of dim x*y
#Forward Propagation
for i in range(epoch):
hinp1=[Link](X,wh)
hinp=hinp1 + bh
hlayer_act = sigmoid(hinp)
outinp1=[Link](hlayer_act,wout)
outinp= outinp1+ bout
output = sigmoid(outinp)
#Backpropagation
EO = y-output
outgrad = derivatives_sigmoid(output)
d_output = EO* outgrad
EH = d_output.dot(wout.T)
hiddengrad = derivatives_sigmoid(hlayer_act)
#how much hidden layer wts contributed to error
d_hiddenlayer = EH * hiddengrad
wout += hlayer_act.[Link](d_output) *lr
# dotproduct of nextlayererror and currentlayerop
bout += [Link](d_output, axis=0,keepdims=True) *lr
wh += [Link](d_hiddenlayer) *lr
#bh += [Link](d_hiddenlayer, axis=0,keepdims=True) *lr
print("Input: \n" + str(X))
print("Actual Output: \n" + str(y))

print("Predicted Output: \n",output)

OUTPUT:

Input:
[[0.66666667 1. ]
[0.33333333 0.55555556]
[1. 0.66666667]]
Actual Output:
[[0.92]
[0.86]
[0.89]]
Predicted Output:
[[0.82871308]
[0.82163793]
[0.82736521]]
Exp8:Write a program to implement the Naïve Bayesian classifier for Iris data set compute
the accuracy.

Aim:To implement the Naïve Bayesian classifier for Iris data set compute the accuracy.

Source Code:

from sklearn.model_selection import train_test_split


from [Link] Bayesian import Navie Bayesian
Classifier
from [Link] import classification_report,confusion_matrix
from sklearn import datasets
#Load the Iris dataset
iris=datasets.load_iris()
#The x variable contains the first four columns of the datset,while y
contains the labels
x=[Link]
y=[Link]
print('sepal-length','speal-width','petal-length','petal-width')
print(x)
print('class: 0-Iris-Setosa, 1-Iris-Versicolor, 2- Iris-Virginica')
print(y)
x_train,x_test,y_train,y_test=train_test_split(x,y,test_size=0.30)
classifier= Navie Bayesian
Classifier(n_Bayesian=5).fit(x_train,y_train)
#To make predictions on our test data
y_pred=[Link](x_test)
#Display the results
print("Results of Classification using K-NN with k=5")
for r in range(0,len(x_test)):
print("Sample:",str(x_test[r]),"Actual-
label:",str(y_test[r]),"Predicted-label:",str(y_pred[r]))
print("Classification Accuracy:",[Link](x_test,y_test));
#"For evaluating an algorithm,confusion matrix,precision,recall"
print('Confusion matrix is as follows')
print(confusion_matrix(y_test,y_pred))
print('Accuracy Matrics')
print(classification_report(y_test,y_pred))
OUTPUT:
sepal-length speal-width petal-length
petal-width [[5.1 3.5 1.4 0.2]
[4.9 3. 1.4 0.2]
[4.7 3.2 1.3 0.2]
[4.6 3.1 1.5 0.2]
[5. 3.6 1.4 0.2]
[5.4 3.9 1.7 0.4]
[4.6 3.4 1.4 0.3]
[5. 3.4 1.5 0.2]
[4.4 2.9 1.4 0.2]
[4.9 3.1 1.5 0.1]
[5.4 3.7 1.5 0.2]

…………………………………………………………..
……………………………………………………………
class: 0-Iris-Setosa, 1-Iris-Versicolor, 2- Iris-Virginica
[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0000000000000111111111111111111111111
1111111111111111111111111122222222222
2222222222222222222222222222222222222
2 2]
Results of Classification using K-NN with k=5
Sample: [5.7 3. 4.2 1.2] Actual-label: 1 Predicted-label: 1
Sample: [5.9 3. 5.1 1.8] Actual-label: 2 Predicted-label: 2
Sample: [6.8 2.8 4.8 1.4] Actual-label: 1 Predicted-label: 1
Sample: [5.4 3.9 1.3 0.4] Actual-label: 0 Predicted-label: 0
Sample: [6.3 3.4 5.6 2.4] Actual-label: 2 Predicted-label: 2
Sample: [6.5 3. 5.8 2.2] Actual-label: 2 Predicted-label: 2
Sample: [7.7 2.6 6.9 2.3] Actual-label: 2 Predicted-label: 2
Sample: [6.3 2.5 4.9 1.5] Actual-label: 1 Predicted-label: 2
Sample: [7.3 2.9 6.3 1.8] Actual-label: 2 Predicted-label: 2
Sample: [4.7 3.2 1.3 0.2] Actual-label: 0 Predicted-label: 0
Sample: [4.9 2.5 4.5 1.7] Actual-label: 2 Predicted-label: 1
………………………………………………………………………..
…………………………………………………….
Classification Accuracy:
0.9333333333333333

Confusion matrix is as
follows [[12 0 0]
[ 0 15 1]
[ 0 2 15]]
Accuracy Matrics
precision recall f1-score support

0 1.00 1.00 1.00 12


1 0.88 0.94 0.91 16
2 0.94 0.88 0.91 17
Exp9: Assuming a set of documents that need to be classified, use the
naïve Bayesian Classifier model to perform this task. Built-in Java
classes/API can be used to write the [Link] the accuracy,
precision, and recall for your data set.
Aim: Use the naïve Bayesian Classifier model to perform this task. Built-in
Java classes/API can be used to write the [Link] the accuracy,
precision, and recall for your data set.
Source Code:

import pandas as pd
msg = pd.read_csv(r'[Link]', names=['message','label'])
print("Total Instances of Dataset: ", [Link][0])
msg['labelnum'] = [Link]({'pos': 1, 'neg': 0})
X = [Link]
y = [Link]
from sklearn.model_selection import train_test_split
Xtrain, Xtest, ytrain, ytest = train_test_split(X, y)
from sklearn.feature_extraction.text import CountVectorizer
count_v = CountVectorizer()
Xtrain_dm = count_v.fit_transform(Xtrain)
Xtest_dm = count_v.transform(Xtest)
df =
[Link](Xtrain_dm.toarray(),columns=count_v.get_feature_na
mes_out())
print(df[0:5])
from sklearn.naive_bayes import MultinomialNB
clf = MultinomialNB()
[Link](Xtrain_dm, ytrain)
pred = [Link](Xtest_dm)
for doc, p in zip(Xtrain, pred):
p = 'pos' if p == 1 else 'neg'
print("%s -> %s" % (doc, p))
from [Link] import accuracy_score, confusion_matrix,
precision_score,recall_score
print('Accuracy Metrics: \n')
print('Accuracy: ', accuracy_score(ytest, pred))
print('Recall: ', recall_score(ytest, pred))
print('Precision: ', precision_score(ytest, pred))
print('Confusion Matrix: \n', confusion_matrix(ytest, pred))

#DATA SET DOWNLOAD


OUTPUT:
Total Instances of Dataset: 18
about am amazing an and awesome bad beers best
boss ... today \ 0 0 0 0 0 0 0 0 0
0 0 ... 0
1 0 0 0 0 0 0 0 0 0 0 ... 0
2 0 1 0 0 1 0 0 0 0 0 ... 0
3 0 0 0 0 0 0 0 0 0 0 ... 0
4 0 0 0 0 0 0 0 0 0 0 ... 0

tomorrow very view we went what will with work


0 1 0 0 1 0 0 1 0 0
1 0 0 0 0 0 0 0 1 0
2 0 0 0 0 0 0 0 0 0
3 0 0 0 0 0 0 0 0 0
4 0 0 0 0 0 1 0 0 0

[5 rows x 47 columns]

We will have good fun


tomorrow -> pos I can't deal
with this -> pos
I am sick and tired of this
place -> neg He is my sworn
enemy -> neg
What a great holiday -
> pos Accuracy
Metrics:

Accuracy: 0.8

Recall: 1.0
Precision: 0.6666666666666666
Confusion
Matrix: [[2 1]
[0 2]]
importnumpyasnp
from[Link]importKMe
ans
import[Link]asplt
from[Link]importGaus
sianMixture
importpandasaspd
X=pd.read_csv("[Link]")
x1=X['chol'].values
x2=X['trestbps'].values
X=[Link](list(zip(x1,x2))).res
hape(len(x1),2)
#codeforEM
gmm=GaussianMixture(n_compon
ents=3)
[Link](X)
em_predictions=[Link](X)
print("\nEMpredictions")
print(em_predictions)
print("mean:\n",gmm.means_)
print('\n')
print("Covariances\
n",gmm.covariances_)
Exp10:Apply EM algorithm to cluster a Heart Disease Data Set. Use
the same data set for clustering using k- Means algorithm. Compare
the results of these two algorithms and comment on the quality of
clustering. You can add Java/Python ML library classes/API in the
program.

SourceCode:
[1]
importnumpyasnp
[Link]
import [Link] as plt
[Link]
import pandas as pd
X=pd.read_csv("[Link]")
x1=X['chol'].values
x2=X['trestbps'].values
X=[Link](list(zip(x1,x2))).reshape(len(x1),2) #code for
EM
gmm=GaussianMixture(n_components=
3) [Link](X)
em_predictions = [Link](X)
print("\nEM predictions")
print(em_predictions) print("mean:\
n",gmm.means_)print('\n')
print("Covariances\
n",gmm.covariances_)
output:
[2]
print(X)
[Link]('Exceptation Maximum')
[Link](X[:,0],X[:,1],c=em_predictions,s=50)
[Link]()

importmatplotlib.pyplotasplt1 kmeans
= KMeans(n_clusters=3) [Link](X)
print(kmeans.cluster_centers_)
print(kmeans.labels_)
[Link]('KMEANS')
[Link](X[:,0], X[:,1], c=kmeans.labels_, cmap='rainbow')
[Link](kmeans.cluster_centers_[:,0],kmeans.cluster_centers_[:,1]
,color='black')
Output:

Experiment-11:
Write a program to implement k-Nearest Neighbor algorithm to classify
the iris data set. Print both correct and wrong predictions.
Source code:
import numpy as np

import pandas as pd

from [Link] import KNeighborsClassifier

from sklearn.model_selection import train_test_split

from sklearn import metrics

names = ['sepal-length', 'sepal-width', 'petal-length', 'petal-width',


'Class']

# Read dataset to pandas dataframe

dataset = pd.read_csv("[Link]", names=names)

X = [Link][:, :-1]

y = [Link][:, -1]

print([Link]())

Xtrain, Xtest, ytrain, ytest = train_test_split(X, y, test_size=0.10)

classifier = KNeighborsClassifier(n_neighbors=5).fit(Xtrain, ytrain)

ypred = [Link](Xtest)
i = 0

print ("\
n----------------------------------------------------------------------
---")

print ('%-25s %-25s %-25s' % ('Original Label', 'Predicted Label',


'Correct/Wrong'))

print
("---------------------------------------------------------------------
----")

for label in ytest:

print ('%-25s %-25s' % (label, ypred[i]), end="")

if (label == ypred[i]):

print (' %-25s' % ('Correct'))

else:

print (' %-25s' % ('Wrong'))

i = i + 1

print
("---------------------------------------------------------------------
----")

print("\nConfusion Matrix:\n",metrics.confusion_matrix(ytest, ypred))

print
("---------------------------------------------------------------------
----")

print("\nClassification Report:\n",metrics.classification_report(ytest,
ypred))

print
("---------------------------------------------------------------------
----")

print('Accuracy of the classifer is %0.2f' %


metrics.accuracy_score(ytest,ypred))
print
("---------------------------------------------------------------------
----")

Output:
sepal-length sepal-width petal-length petal-width

0 5.1 3.5 1.4 0.2

1 4.9 3.0 1.4 0.2

2 4.7 3.2 1.3 0.2

3 4.6 3.1 1.5 0.2

4 5.0 3.6 1.4 0.2

-----------------------------------------------------------------------
--

Original Label Predicted Label Correct/Wrong

-----------------------------------------------------------------------
--

Iris-versicolor Iris-versicolor Correct

Iris-virginica Iris-versicolor Wrong

Iris-virginica Iris-virginica Correct

Iris-versicolor Iris-versicolor Correct

Iris-setosa Iris-setosa Correct

Iris-versicolor Iris-versicolor Correct

Iris-setosa Iris-setosa Correct

Iris-setosa Iris-setosa Correct


Iris-virginica Iris-virginica Correct

Iris-virginica Iris-versicolor Wrong

Iris-virginica Iris-virginica Correct

Iris-setosa Iris-setosa Correct

Iris-virginica Iris-virginica Correct

Iris-virginica Iris-virginica Correct

Iris-versicolor Iris-versicolor Correct

-----------------------------------------------------------------------
--

Confusion Matrix:

[[4 0 0]

[0 4 0]

[0 2 5]]

-----------------------------------------------------------------------
--

Classification Report:

precision recall f1-score support

Iris-setosa 1.00 1.00 1.00 4

Iris-versicolor 0.67 1.00 0.80 4

Iris-virginica 1.00 0.71 0.83 7

avg / total 0.91 0.87 0.87 15


-----------------------------------------------------------------------
--

Accuracy of the classifer is 0.87

importnumpyasnp
from[Link]importKMea
ns
import[Link]asplt
from[Link]importGaus
sianMixture
importpandasaspd
X=pd.read_csv("[Link]")
x1=X['chol'].values
x2=X['trestbps'].values
X=[Link](list(zip(x1,x2))).res
hape(len(x1),2)
#codeforEM
gmm=GaussianMixture(n_compon
ents=3)
[Link](X)
em_predictions=[Link](X)
print("\nEMpredictions")
print(em_predictions)
print("mean:\n",gmm.means_)
print('\n')
print("Covariances\
n",gmm.covariances_)
***THE END****

You might also like