Practicals 2.odt

Download as odt, pdf, or txt
Download as odt, pdf, or txt
You are on page 1of 21

PRACTICAL: 1:

https://www.mldawn.com/train-a-perceptron-to-learn-the-and-gate-from-scratch-in-python/

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sat Jan 25 23:12:58 2020

@author: dhanush
"""

# Numpy is used for all mathematical operations


import numpy as np
# For plotting purposes
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import axes3d # 3 Dimensional plotting
from matplotlib import cm # For some fancy plotting ;-)

def Cross_Entropy(y_hat, y):


# There are 2 possibilities for the ground-truth: either 0 or
1
# Note that np.log() is actually the natural logarithm with e,
for its base
if y == 1:
return -np.log(y_hat)
else:
return -np.log(1 - y_hat)
# This is just the classic sigmoid function, given input z
def sigmoid(z):
return 1 / (1 + np.exp(-z))

def derivative_Cross_Entropy(y_hat, y):


# Again we account for 2 possibilities of y=0/1
if y == 1:
return -1/y_hat
else:
return 1 / (1 - y_hat)
# The derivative of sigmoid is quite straight-forward
def derivative_sigmoid(x):
return x*(1-x)

# Our data
X = np.array([[0, 0], [0, 5], [5, 0], [5, 5]])
# The ground truth (i.e., what AND returns and our perceptron
should learn to produce)
Y = np.array([0, 0, 0, 1])

#The size of each data point


area = 200
fig = plt.figure(figsize=(6, 6))
plt.title('The AND Gate', fontsize=20)
ax = fig.add_subplot(111)
# color red: is class 0 and color blue is class 1.
ax.scatter(0, 0, s=area, c='r', label="Class 0")
ax.scatter(0, 5, s=area, c='r', label="Class 0")
ax.scatter(5, 0, s=area, c='r', label="Class 0")
ax.scatter(5, 5, s=area, c='b', label="Class 1")
plt.grid()
plt.show()

# We consider a low and high range for our random weight


generation
# Remember we consider this rage to be small, so that during the
back-propagation
# Our gradients through the sigmoid unit will stay strong. This is
a major concern in
# Deep nets however!
low = -0.01
high = 0.01
# We are using uniform distribution for our random weight
generation.
W_2 = np.random.uniform(low=low, high=high, size=(1,))
W_1 = np.random.uniform(low=low, high=high, size=(1,))
W_0 = np.random.uniform(low=low, high=high, size=(1,))

# Number of our epochs. Every epoch is a complete sweep through


our data
Epoch = 2000
# The learning rate. Preferably a small value!
eta = 0.01

# E will contain the average cross-entropy error per epoch


E = []
# The actual training starts for as many times as Epoch.
for ep in range(Epoch):
# Shuffle the train_data X and its Labels Y. Almost always a
good practice to shuffle in the beginning of the epoch!
random_index = np.arange(X.shape[0])
# Now random_index has the same length as X and Y, and
contains a shuffled index list for X.
np.random.shuffle(random_index)
# e will record errors in an epoch. Then will be averaged, and
this average value will be added to E.
# We reset e[], in the beginning of each epoch
e = []
# This loop goes through the shuffled training data.
random_index makes sure for training data
# in X we are grabbing the correct ground truth from Y
for i in random_index:
# Grab the ith training data from X
x = X[i]
# Compute Z, which is the input to our sigmoidal unit
Z = W_1* x[0] + W_2* x[1] + W_0
# Apply sigmoid, to produce an output by the perceptron
Y_hat = sigmoid(Z)
# Compute the binary cross-entropy error for this ith data
point and add it to e[]
e.append(Cross_Entropy(Y_hat, Y[i]))
# Compute the gradients of our error function w.r.t. all 3
learnable parameters (i.e., the weights of our network)
dEdW_1 = derivative_Cross_Entropy(Y_hat,
Y[i])*derivative_sigmoid(Y_hat)*x[0]
dEdW_2 = derivative_Cross_Entropy(Y_hat, Y[i]) *
derivative_sigmoid(Y_hat) * x[1]
dEdW_0 = derivative_Cross_Entropy(Y_hat,
Y[i])*derivative_sigmoid(Y_hat)
# Update the parameters using the computed gradients. So,
we update after each individual data point => Stochastic
gradient descent!
W_0 = W_0 - eta * dEdW_0
W_1 = W_1 - eta*dEdW_1
W_2 = W_2 - eta* dEdW_2
# Every 500 epochs, we would like to visualise 3 things: 1)
The linear 2-Dimensional decision boundary in the input space
# 2) The actual hyper-plane in the 3-Dimensional space, which
by cutting through the input space,
# has produced our linear decision boundary, which is the same
as our Z (i.e., the input to our sigmoidal unit).
# Finally, the sigmoid() of this Z, which should squash the
hyper-plane between 0 and 1, by definition. NOTE: sigmoid(Z)
always
# squashes Z into a value in the range [0, 1]
if ep % 500 == 0:
# Generate a figure
fig = plt.figure(figsize=(10, 5))
plt.title('The AND Gate', fontsize=20)
# Insert a sub-figure and make sure it is capable of 3-
Dimensional rendering
ax = fig.add_subplot(131, projection='3d')
# Plot individual data points in this sub-figure
ax.scatter(0, 0, s=area, c='r', label="Class 0")
ax.scatter(0, 5, s=area, c='r', label="Class 0")
ax.scatter(5, 0, s=area, c='r', label="Class 0")
ax.scatter(5, 5, s=area, c='b', label="Class 1")
# Give a title to this sub-figure
plt.title('Decision Boundary Created by the Hyper-plane')
# The equation of Z = W2X2 + W1X1 + W0 and we know that
the linear decision boundary's equation is obtainable by setting Z
= 0
# So, W2X2 + W1X1 + W0 = 0, and by rearranging: x_2 = (-
W1/W2) * x1 - (W0/W2) which is a line with the slope (-W1/W2) and
the
# intercept - (W0/W2). Note that the weights are being
learned by the perceptron, however, we choose a range of values
for x1,
# and for each we compute x2. Why a range? because we want
to plot a continuous line here, and our x's are just discrete
values
# That can be either 0 or 5 (as shown in our dataset.)
x_1 = np.arange(-2, 5, 0.1)
W_1 * x[0] + W_2 * x[1] + W_0
x_2 = (-W_1/W_2) * x_1 - (W_0/W_2)
plt.grid()
plt.plot(x_2, x_1, '-k', marker='_', label="DB")
plt.xlabel('x1', fontsize=20)
plt.ylabel('x2', fontsize=20)
# Now we add the second sub-figure. This well wisualize
the hyper-plane X and the way it cuts through the input space
#ax = fig.add_subplot(132, projection='3d')
#x_0 = np.arange(-10, 10, 0.1)
# we need a mesh-grid for 3-Dimensional plotting
#X_0, X_1 = np.meshgrid(x_0, x_1)
# for every combination of points from X_0 and X_1, we
generate a value for Z in 3-Dimensions
#Z = X_0*W_1 + X_1*W_2 + W_0
# We use the wire_frame package so we could see behind
this hyper-plane. The stride arguements,
# determine the grid-size on this plane. The smaller their
values, the finer the grid on the hyper-plane
#ax.plot_wireframe(X_0, X_1, Z, rstride=10, cstride=10)
# We still want to visualize the linear decision boundary
computed in the previous sub-figure
#ax.scatter(x_2, x_1, 0, marker='_', c='k')
# Again plot our data points as well for this sub-figure
#ax.scatter(0, 0, 0, marker='o', c='r', s=100)
#ax.scatter(0, 5, 0, marker='o', c='r', s=100)
#ax.scatter(5, 0, 0, marker='o', c='r', s=100)
#ax.scatter(5, 5, 0, marker='o', c='b', s=100)
#plt.xlabel('x1', fontsize=20)
#plt.ylabel('x2', fontsize=20)
#plt.title('The Hyper-plane Cutting through Input Space')
#plt.grid()
# Add the last sub-figure that will show the power of
sigmoid(), and highlights how Z gets squashed between 0 and 1
#ax = fig.add_subplot(133, projection='3d')
# This is really cool! The cm package will color the
figure generated by sigmoid(Z), based on the value of sigmoid(Z).
# Brighter colors are closer to 1 and darker ones are
closer to 0! This way you can see how the sigmoid(Z), which is the
final
# Output of our perceptron y_hat, will assign values
closer to 1 for our positive examples and values closer to 0 for
our negative
# examples.
#my_col = cm.jet(sigmoid(Z) / np.amax(sigmoid(Z)))
#ax.plot_surface(X_0, X_1, sigmoid(Z), facecolors=my_col)
# Again we want to see the linear decision boundary
produced in the first sub-figure with our actual training examples
#ax.scatter(x_2, x_1, 0, marker='_', c='k')
#ax.scatter(0, 0, 0, marker='o', c='r', s=100)
#ax.scatter(0, 5, 0, marker='o', c='r', s=100)
#ax.scatter(5, 0, 0, marker='o', c='r', s=100)
#ax.scatter(5, 5, 0, marker='o', c='b', s=100)
#plt.title('The Hyper-plane after Applying Sigmoid()')
#plt.xlabel('x1', fontsize=20)
#plt.ylabel('x2', fontsize=20)
#plt.grid()
plt.show()
# Now e has the errors for every training example in our
training set through out the current epoch. We average that and
add it to E[]
E.append(np.mean(e))

plt.figure()
plt.grid()
plt.xlabel("Epochs During Training")
plt.ylabel("Binary Cross-Entropy Error")
plt.plot(E, c='r')
plt.show()
OUTPUT:

PRACTICAL 2:

https://towardsdatascience.com/implementing-the-xor-gate-using-
backpropagation-in-neural-networks-c1f255b4f20d

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sat Jan 25 23:48:33 2020

@author: dhanush
"""
import numpy as np
#np.random.seed(0)

def sigmoid (x):


return 1/(1 + np.exp(-x))

def sigmoid_derivative(x):
return x * (1 - x)

#Input datasets
inputs = np.array([[0,0],[0,1],[1,0],[1,1]])
expected_output = np.array([[0],[1],[1],[0]])

epochs = 10000
lr = 0.1
inputLayerNeurons, hiddenLayerNeurons, outputLayerNeurons = 2,2,1

#Random weights and bias initialization


hidden_weights =
np.random.uniform(size=(inputLayerNeurons,hiddenLayerNeurons))
hidden_bias =np.random.uniform(size=(1,hiddenLayerNeurons))
output_weights =
np.random.uniform(size=(hiddenLayerNeurons,outputLayerNeurons))
output_bias = np.random.uniform(size=(1,outputLayerNeurons))

print("Initial hidden weights: ",end='')


print(*hidden_weights)
print("Initial hidden biases: ",end='')
print(*hidden_bias)
print("Initial output weights: ",end='')
print(*output_weights)
print("Initial output biases: ",end='')
print(*output_bias)

#Training algorithm
for _ in range(epochs):
#Forward Propagation
hidden_layer_activation = np.dot(inputs,hidden_weights)
hidden_layer_activation += hidden_bias
hidden_layer_output = sigmoid(hidden_layer_activation)

output_layer_activation =
np.dot(hidden_layer_output,output_weights)
output_layer_activation += output_bias
predicted_output = sigmoid(output_layer_activation)

#Backpropagation
error = expected_output - predicted_output
d_predicted_output = error *
sigmoid_derivative(predicted_output)

error_hidden_layer = d_predicted_output.dot(output_weights.T)
d_hidden_layer = error_hidden_layer *
sigmoid_derivative(hidden_layer_output)

#Updating Weights and Biases


output_weights +=
hidden_layer_output.T.dot(d_predicted_output) * lr
output_bias += np.sum(d_predicted_output,axis=0,keepdims=True)
* lr
hidden_weights += inputs.T.dot(d_hidden_layer) * lr
hidden_bias += np.sum(d_hidden_layer,axis=0,keepdims=True) *
lr

print("Final hidden weights: ",end='')


print(*hidden_weights)
print("Final hidden bias: ",end='')
print(*hidden_bias)
print("Final output weights: ",end='')
print(*output_weights)
print("Final output bias: ",end='')
print(*output_bias)

print("\nOutput from neural network after 10,000 epochs: ",end='')


print(*predicted_output)

OUTPUT:
Initial hidden weights: [0.72952612 0.56162152] [0.66028489
0.86485965]
Initial hidden biases: [0.97254347 0.67007999]
Initial output weights: [0.33179675] [0.54454469]
Initial output biases: [0.77141425]
Final hidden weights: [3.6110791 5.86418656] [3.60780624
5.84610256]
Final hidden bias: [-5.51828085 -2.42003941]
Final output weights: [-8.0441085] [7.38997746]
Final output bias: [-3.30845684]

Output from neural network after 10,000 epochs: [0.06082214]


[0.94329807] [0.94335588] [0.06173174]

PRACTICAL 3:
HyperParamter:
https://www.geeksforgeeks.org/svm-hyperparameter-tuning-using-
gridsearchcv-ml/

import pandas as pd
import numpy as np
from sklearn.metrics import classification_report,
confusion_matrix
from sklearn.datasets import load_breast_cancer
from sklearn.svm import SVC
cancer = load_breast_cancer()

# The data set is presented in a dictionary form:


print(cancer.keys())
df_feat = pd.DataFrame(cancer['data'],
columns = cancer['feature_names'])
# cancer column is our target
df_target = pd.DataFrame(cancer['target'],
columns =['Cancer'])

print("Feature Variables: ")


print(df_feat.info())

print("Dataframe looks like : ")


print(df_feat.head())

from sklearn.model_selection import train_test_split


X_train, X_test, y_train, y_test = train_test_split(
df_feat, np.ravel(df_target),
test_size = 0.30, random_state = 101)

# train the model on train set


model = SVC()
model.fit(X_train, y_train)
# print prediction results
predictions = model.predict(X_test)
print(classification_report(y_test, predictions))

from sklearn.model_selection import GridSearchCV


# defining parameter range
param_grid = {'C': [0.1, 1, 10, 100, 1000],
'gamma': [1, 0.1, 0.01, 0.001, 0.0001],
'kernel': ['rbf']}
grid = GridSearchCV(SVC(), param_grid, refit = True, verbose = 3)
# fitting the model for grid search
grid.fit(X_train, y_train)
# print best parameter after tuning
print(grid.best_params_)
# print how our model looks after hyper-parameter tuning
print(grid.best_estimator_)
grid_predictions = grid.predict(X_test)
# print classification report
print(classification_report(y_test, grid_predictions))

OUTPUT:
dict_keys(['target_names', 'DESCR', 'target', 'filename', 'data',
'feature_names'])
Feature Variables:
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 569 entries, 0 to 568
Data columns (total 30 columns):
mean radius 569 non-null float64
mean texture 569 non-null float64
mean perimeter 569 non-null float64
mean area 569 non-null float64
mean smoothness 569 non-null float64
mean compactness 569 non-null float64
mean concavity 569 non-null float64
mean concave points 569 non-null float64
mean symmetry 569 non-null float64
mean fractal dimension 569 non-null float64
radius error 569 non-null float64
texture error 569 non-null float64
perimeter error 569 non-null float64
area error 569 non-null float64
smoothness error 569 non-null float64
compactness error 569 non-null float64
concavity error 569 non-null float64
concave points error 569 non-null float64
symmetry error 569 non-null float64
fractal dimension error 569 non-null float64
worst radius 569 non-null float64
worst texture 569 non-null float64
worst perimeter 569 non-null float64
worst area 569 non-null float64
worst smoothness 569 non-null float64
worst compactness 569 non-null float64
worst concavity 569 non-null float64
worst concave points 569 non-null float64
worst symmetry 569 non-null float64
worst fractal dimension 569 non-null float64
dtypes: float64(30)
memory usage: 133.4 KB
None
Dataframe looks like :
mean radius mean texture ... worst symmetry worst fractal
dimension
0 17.99 10.38 ... 0.4601
0.11890
1 20.57 17.77 ... 0.2750
0.08902
2 19.69 21.25 ... 0.3613
0.08758
3 11.42 20.38 ... 0.6638
0.17300
4 20.29 14.34 ... 0.2364
0.07678

[5 rows x 30 columns]
precision recall f1-score support

0 0.95 0.85 0.90 66


1 0.91 0.97 0.94 105
accuracy 0.92 171
macro avg 0.93 0.91 0.92 171
weighted avg 0.93 0.92 0.92 171

Fitting 5 folds for each of 25 candidates, totalling 125 fits


[CV] kernel=rbf, gamma=1,
C=0.1 ......................................
[CV] .......... kernel=rbf, gamma=1, C=0.1, score=0.637, total=
0.1s
[CV] kernel=rbf, gamma=1,
C=0.1 ......................................
[CV] .......... kernel=rbf, gamma=1, C=0.1, score=0.637, total=
0.1s
[CV] kernel=rbf, gamma=1,
C=0.1 ......................................
[Parallel(n_jobs=1)]: Using backend SequentialBackend with 1
concurrent workers.
[Parallel(n_jobs=1)]: Done 1 out of 1 | elapsed: 0.1s
remaining: 0.0s
[Parallel(n_jobs=1)]: Done 2 out of 2 | elapsed: 0.1s
remaining: 0.0s
[CV] .......... kernel=rbf, gamma=1, C=0.1, score=0.625, total=
0.1s
[CV] kernel=rbf, gamma=1,
C=0.1 ......................................
[CV] .......... kernel=rbf, gamma=1, C=0.1, score=0.633, total=
0.1s
[CV] kernel=rbf, gamma=1,
C=0.1 ......................................
[CV] .......... kernel=rbf, gamma=1, C=0.1, score=0.633, total=
0.0s
[CV] kernel=rbf, gamma=0.1,
C=0.1 ....................................
[CV] ........ kernel=rbf, gamma=0.1, C=0.1, score=0.637, total=
0.0s
[CV] kernel=rbf, gamma=0.1,
C=0.1 ....................................
[CV] ........ kernel=rbf, gamma=0.1, C=0.1, score=0.637, total=
0.1s
[CV] kernel=rbf, gamma=0.1,
C=0.1 ....................................
[CV] ........ kernel=rbf, gamma=0.1, C=0.1, score=0.625, total=
0.0s
[CV] kernel=rbf, gamma=0.1,
C=0.1 ....................................
[CV] ........ kernel=rbf, gamma=0.1, C=0.1, score=0.633, total=
0.0s
[CV] kernel=rbf, gamma=0.1,
C=0.1 ....................................
[CV] ........ kernel=rbf, gamma=0.1, C=0.1, score=0.633, total=
0.0s
[CV] kernel=rbf, gamma=0.01,
C=0.1 ...................................
[CV] ....... kernel=rbf, gamma=0.01, C=0.1, score=0.637, total=
0.0s
[CV] kernel=rbf, gamma=0.01,
C=0.1 ...................................
[CV] ....... kernel=rbf, gamma=0.01, C=0.1, score=0.637, total=
0.1s
[CV] kernel=rbf, gamma=0.01,
C=0.1 ...................................
[CV] ....... kernel=rbf, gamma=0.01, C=0.1, score=0.625, total=
0.0s
[CV] kernel=rbf, gamma=0.01,
C=0.1 ...................................
[CV] ....... kernel=rbf, gamma=0.01, C=0.1, score=0.633, total=
0.0s
[CV] kernel=rbf, gamma=0.01,
C=0.1 ...................................
[CV] ....... kernel=rbf, gamma=0.01, C=0.1, score=0.633, total=
0.0s
[CV] kernel=rbf, gamma=0.001,
C=0.1 ..................................
[CV] ...... kernel=rbf, gamma=0.001, C=0.1, score=0.637, total=
0.0s
[CV] kernel=rbf, gamma=0.001,
C=0.1 ..................................
[CV] ...... kernel=rbf, gamma=0.001, C=0.1, score=0.637, total=
0.0s
[CV] kernel=rbf, gamma=0.001,
C=0.1 ..................................
[CV] ...... kernel=rbf, gamma=0.001, C=0.1, score=0.625, total=
0.0s
[CV] kernel=rbf, gamma=0.001,
C=0.1 ..................................
[CV] ...... kernel=rbf, gamma=0.001, C=0.1, score=0.633, total=
0.0s
[CV] kernel=rbf, gamma=0.001,
C=0.1 ..................................
[CV] ...... kernel=rbf, gamma=0.001, C=0.1, score=0.633, total=
0.0s
[CV] kernel=rbf, gamma=0.0001,
C=0.1 .................................
[CV] ..... kernel=rbf, gamma=0.0001, C=0.1, score=0.887, total=
0.0s
[CV] kernel=rbf, gamma=0.0001,
C=0.1 .................................
[CV] ..... kernel=rbf, gamma=0.0001, C=0.1, score=0.938, total=
0.0s
[CV] kernel=rbf, gamma=0.0001,
C=0.1 .................................
[CV] ..... kernel=rbf, gamma=0.0001, C=0.1, score=0.963, total=
0.0s
[CV] kernel=rbf, gamma=0.0001,
C=0.1 .................................
[CV] ..... kernel=rbf, gamma=0.0001, C=0.1, score=0.962, total=
0.0s
[CV] kernel=rbf, gamma=0.0001,
C=0.1 .................................
[CV] ..... kernel=rbf, gamma=0.0001, C=0.1, score=0.886, total=
0.0s
[CV] kernel=rbf, gamma=1,
C=1 ........................................
[CV] ............ kernel=rbf, gamma=1, C=1, score=0.637, total=
0.0s
[CV] kernel=rbf, gamma=1,
C=1 ........................................
[CV] ............ kernel=rbf, gamma=1, C=1, score=0.637, total=
0.0s
[CV] kernel=rbf, gamma=1,
C=1 ........................................
[CV] ............ kernel=rbf, gamma=1, C=1, score=0.625, total=
0.0s
[CV] kernel=rbf, gamma=1,
C=1 ........................................
[CV] ............ kernel=rbf, gamma=1, C=1, score=0.633, total=
0.0s
[CV] kernel=rbf, gamma=1,
C=1 ........................................
[CV] ............ kernel=rbf, gamma=1, C=1, score=0.633, total=
0.0s
[CV] kernel=rbf, gamma=0.1,
C=1 ......................................
[CV] .......... kernel=rbf, gamma=0.1, C=1, score=0.637, total=
0.0s
[CV] kernel=rbf, gamma=0.1,
C=1 ......................................
[CV] .......... kernel=rbf, gamma=0.1, C=1, score=0.637, total=
0.1s
[CV] kernel=rbf, gamma=0.1,
C=1 ......................................
[CV] .......... kernel=rbf, gamma=0.1, C=1, score=0.625, total=
0.0s
[CV] kernel=rbf, gamma=0.1,
C=1 ......................................
[CV] .......... kernel=rbf, gamma=0.1, C=1, score=0.633, total=
0.1s
[CV] kernel=rbf, gamma=0.1,
C=1 ......................................
[CV] .......... kernel=rbf, gamma=0.1, C=1, score=0.633, total=
0.0s
[CV] kernel=rbf, gamma=0.01,
C=1 .....................................
[CV] ......... kernel=rbf, gamma=0.01, C=1, score=0.637, total=
0.0s
[CV] kernel=rbf, gamma=0.01,
C=1 .....................................
[CV] ......... kernel=rbf, gamma=0.01, C=1, score=0.637, total=
0.0s
[CV] kernel=rbf, gamma=0.01,
C=1 .....................................
[CV] ......... kernel=rbf, gamma=0.01, C=1, score=0.625, total=
0.0s
[CV] kernel=rbf, gamma=0.01,
C=1 .....................................
[CV] ......... kernel=rbf, gamma=0.01, C=1, score=0.633, total=
0.0s
[CV] kernel=rbf, gamma=0.01,
C=1 .....................................
[CV] ......... kernel=rbf, gamma=0.01, C=1, score=0.633, total=
0.0s
[CV] kernel=rbf, gamma=0.001,
C=1 ....................................
[CV] ........ kernel=rbf, gamma=0.001, C=1, score=0.900, total=
0.0s
[CV] kernel=rbf, gamma=0.001,
C=1 ....................................
[CV] ........ kernel=rbf, gamma=0.001, C=1, score=0.912, total=
0.0s
[CV] kernel=rbf, gamma=0.001,
C=1 ....................................
[CV] ........ kernel=rbf, gamma=0.001, C=1, score=0.925, total=
0.0s
[CV] kernel=rbf, gamma=0.001,
C=1 ....................................
[CV] ........ kernel=rbf, gamma=0.001, C=1, score=0.962, total=
0.0s
[CV] kernel=rbf, gamma=0.001,
C=1 ....................................
[CV] ........ kernel=rbf, gamma=0.001, C=1, score=0.937, total=
0.0s
[CV] kernel=rbf, gamma=0.0001,
C=1 ...................................
[CV] ....... kernel=rbf, gamma=0.0001, C=1, score=0.912, total=
0.0s
[CV] kernel=rbf, gamma=0.0001,
C=1 ...................................
[CV] ....... kernel=rbf, gamma=0.0001, C=1, score=0.950, total=
0.0s
[CV] kernel=rbf, gamma=0.0001,
C=1 ...................................
[CV] ....... kernel=rbf, gamma=0.0001, C=1, score=0.975, total=
0.0s
[CV] kernel=rbf, gamma=0.0001,
C=1 ...................................
[CV] ....... kernel=rbf, gamma=0.0001, C=1, score=0.962, total=
0.0s
[CV] kernel=rbf, gamma=0.0001,
C=1 ...................................
[CV] ....... kernel=rbf, gamma=0.0001, C=1, score=0.937, total=
0.0s
[CV] kernel=rbf, gamma=1,
C=10 .......................................
[CV] ........... kernel=rbf, gamma=1, C=10, score=0.637, total=
0.0s
[CV] kernel=rbf, gamma=1,
C=10 .......................................
[CV] ........... kernel=rbf, gamma=1, C=10, score=0.637, total=
0.1s
[CV] kernel=rbf, gamma=1,
C=10 .......................................
[CV] ........... kernel=rbf, gamma=1, C=10, score=0.625, total=
0.1s
[CV] kernel=rbf, gamma=1,
C=10 .......................................
[CV] ........... kernel=rbf, gamma=1, C=10, score=0.633, total=
0.1s
[CV] kernel=rbf, gamma=1,
C=10 .......................................
[CV] ........... kernel=rbf, gamma=1, C=10, score=0.633, total=
0.1s
[CV] kernel=rbf, gamma=0.1,
C=10 .....................................
[CV] ......... kernel=rbf, gamma=0.1, C=10, score=0.637, total=
0.1s
[CV] kernel=rbf, gamma=0.1,
C=10 .....................................
[CV] ......... kernel=rbf, gamma=0.1, C=10, score=0.637, total=
0.0s
[CV] kernel=rbf, gamma=0.1,
C=10 .....................................
[CV] ......... kernel=rbf, gamma=0.1, C=10, score=0.625, total=
0.1s
[CV] kernel=rbf, gamma=0.1,
C=10 .....................................
[CV] ......... kernel=rbf, gamma=0.1, C=10, score=0.633, total=
0.1s
[CV] kernel=rbf, gamma=0.1,
C=10 .....................................
[CV] ......... kernel=rbf, gamma=0.1, C=10, score=0.633, total=
0.1s
[CV] kernel=rbf, gamma=0.01,
C=10 ....................................
[CV] ........ kernel=rbf, gamma=0.01, C=10, score=0.637, total=
0.0s
[CV] kernel=rbf, gamma=0.01,
C=10 ....................................
[CV] ........ kernel=rbf, gamma=0.01, C=10, score=0.637, total=
0.0s
[CV] kernel=rbf, gamma=0.01,
C=10 ....................................
[CV] ........ kernel=rbf, gamma=0.01, C=10, score=0.613, total=
0.0s
[CV] kernel=rbf, gamma=0.01,
C=10 ....................................
[CV] ........ kernel=rbf, gamma=0.01, C=10, score=0.633, total=
0.0s
[CV] kernel=rbf, gamma=0.01,
C=10 ....................................
[CV] ........ kernel=rbf, gamma=0.01, C=10, score=0.633, total=
0.0s
[CV] kernel=rbf, gamma=0.001,
C=10 ...................................
[CV] ....... kernel=rbf, gamma=0.001, C=10, score=0.887, total=
0.0s
[CV] kernel=rbf, gamma=0.001,
C=10 ...................................
[CV] ....... kernel=rbf, gamma=0.001, C=10, score=0.912, total=
0.0s
[CV] kernel=rbf, gamma=0.001,
C=10 ...................................
[CV] ....... kernel=rbf, gamma=0.001, C=10, score=0.900, total=
0.0s
[CV] kernel=rbf, gamma=0.001,
C=10 ...................................
[CV] ....... kernel=rbf, gamma=0.001, C=10, score=0.937, total=
0.0s
[CV] kernel=rbf, gamma=0.001,
C=10 ...................................
[CV] ....... kernel=rbf, gamma=0.001, C=10, score=0.924, total=
0.0s
[CV] kernel=rbf, gamma=0.0001,
C=10 ..................................
[CV] ...... kernel=rbf, gamma=0.0001, C=10, score=0.950, total=
0.0s
[CV] kernel=rbf, gamma=0.0001,
C=10 ..................................
[CV] ...... kernel=rbf, gamma=0.0001, C=10, score=0.912, total=
0.0s
[CV] kernel=rbf, gamma=0.0001,
C=10 ..................................
[CV] ...... kernel=rbf, gamma=0.0001, C=10, score=0.975, total=
0.0s
[CV] kernel=rbf, gamma=0.0001,
C=10 ..................................
[CV] ...... kernel=rbf, gamma=0.0001, C=10, score=0.949, total=
0.0s
[CV] kernel=rbf, gamma=0.0001,
C=10 ..................................
[CV] ...... kernel=rbf, gamma=0.0001, C=10, score=0.949, total=
0.0s
[CV] kernel=rbf, gamma=1,
C=100 ......................................
[CV] .......... kernel=rbf, gamma=1, C=100, score=0.637, total=
0.1s
[CV] kernel=rbf, gamma=1,
C=100 ......................................
[CV] .......... kernel=rbf, gamma=1, C=100, score=0.637, total=
0.0s
[CV] kernel=rbf, gamma=1,
C=100 ......................................
[CV] .......... kernel=rbf, gamma=1, C=100, score=0.625, total=
0.1s
[CV] kernel=rbf, gamma=1,
C=100 ......................................
[CV] .......... kernel=rbf, gamma=1, C=100, score=0.633, total=
0.1s
[CV] kernel=rbf, gamma=1,
C=100 ......................................
[CV] .......... kernel=rbf, gamma=1, C=100, score=0.633, total=
0.0s
[CV] kernel=rbf, gamma=0.1,
C=100 ....................................
[CV] ........ kernel=rbf, gamma=0.1, C=100, score=0.637, total=
0.0s
[CV] kernel=rbf, gamma=0.1,
C=100 ....................................
[CV] ........ kernel=rbf, gamma=0.1, C=100, score=0.637, total=
0.0s
[CV] kernel=rbf, gamma=0.1,
C=100 ....................................
[CV] ........ kernel=rbf, gamma=0.1, C=100, score=0.625, total=
0.0s
[CV] kernel=rbf, gamma=0.1,
C=100 ....................................
[CV] ........ kernel=rbf, gamma=0.1, C=100, score=0.633, total=
0.1s
[CV] kernel=rbf, gamma=0.1,
C=100 ....................................
[CV] ........ kernel=rbf, gamma=0.1, C=100, score=0.633, total=
0.0s
[CV] kernel=rbf, gamma=0.01,
C=100 ...................................
[CV] ....... kernel=rbf, gamma=0.01, C=100, score=0.637, total=
0.0s
[CV] kernel=rbf, gamma=0.01,
C=100 ...................................
[CV] ....... kernel=rbf, gamma=0.01, C=100, score=0.637, total=
0.0s
[CV] kernel=rbf, gamma=0.01,
C=100 ...................................
[CV] ....... kernel=rbf, gamma=0.01, C=100, score=0.613, total=
0.0s
[CV] kernel=rbf, gamma=0.01,
C=100 ...................................
[CV] ....... kernel=rbf, gamma=0.01, C=100, score=0.633, total=
0.0s
[CV] kernel=rbf, gamma=0.01,
C=100 ...................................
[CV] ....... kernel=rbf, gamma=0.01, C=100, score=0.633, total=
0.0s
[CV] kernel=rbf, gamma=0.001,
C=100 ..................................
[CV] ...... kernel=rbf, gamma=0.001, C=100, score=0.887, total=
0.0s
[CV] kernel=rbf, gamma=0.001,
C=100 ..................................
[CV] ...... kernel=rbf, gamma=0.001, C=100, score=0.912, total=
0.0s
[CV] kernel=rbf, gamma=0.001,
C=100 ..................................
[CV] ...... kernel=rbf, gamma=0.001, C=100, score=0.900, total=
0.0s
[CV] kernel=rbf, gamma=0.001,
C=100 ..................................
[CV] ...... kernel=rbf, gamma=0.001, C=100, score=0.937, total=
0.0s
[CV] kernel=rbf, gamma=0.001,
C=100 ..................................
[CV] ...... kernel=rbf, gamma=0.001, C=100, score=0.924, total=
0.0s
[CV] kernel=rbf, gamma=0.0001,
C=100 .................................
[CV] ..... kernel=rbf, gamma=0.0001, C=100, score=0.925, total=
0.0s
[CV] kernel=rbf, gamma=0.0001,
C=100 .................................
[CV] ..... kernel=rbf, gamma=0.0001, C=100, score=0.912, total=
0.0s
[CV] kernel=rbf, gamma=0.0001,
C=100 .................................
[CV] ..... kernel=rbf, gamma=0.0001, C=100, score=0.975, total=
0.0s
[CV] kernel=rbf, gamma=0.0001,
C=100 .................................
[CV] ..... kernel=rbf, gamma=0.0001, C=100, score=0.937, total=
0.0s
[CV] kernel=rbf, gamma=0.0001,
C=100 .................................
[CV] ..... kernel=rbf, gamma=0.0001, C=100, score=0.949, total=
0.0s
[CV] kernel=rbf, gamma=1,
C=1000 .....................................
[CV] ......... kernel=rbf, gamma=1, C=1000, score=0.637, total=
0.1s
[CV] kernel=rbf, gamma=1,
C=1000 .....................................
[CV] ......... kernel=rbf, gamma=1, C=1000, score=0.637, total=
0.1s
[CV] kernel=rbf, gamma=1,
C=1000 .....................................
[CV] ......... kernel=rbf, gamma=1, C=1000, score=0.625, total=
0.1s
[CV] kernel=rbf, gamma=1,
C=1000 .....................................
[CV] ......... kernel=rbf, gamma=1, C=1000, score=0.633, total=
0.0s
[CV] kernel=rbf, gamma=1,
C=1000 .....................................
[CV] ......... kernel=rbf, gamma=1, C=1000, score=0.633, total=
0.1s
[CV] kernel=rbf, gamma=0.1,
C=1000 ...................................
[CV] ....... kernel=rbf, gamma=0.1, C=1000, score=0.637, total=
0.1s
[CV] kernel=rbf, gamma=0.1,
C=1000 ...................................
[CV] ....... kernel=rbf, gamma=0.1, C=1000, score=0.637, total=
0.1s
[CV] kernel=rbf, gamma=0.1,
C=1000 ...................................
[CV] ....... kernel=rbf, gamma=0.1, C=1000, score=0.625, total=
0.0s
[CV] kernel=rbf, gamma=0.1,
C=1000 ...................................
[CV] ....... kernel=rbf, gamma=0.1, C=1000, score=0.633, total=
0.1s
[CV] kernel=rbf, gamma=0.1,
C=1000 ...................................
[CV] ....... kernel=rbf, gamma=0.1, C=1000, score=0.633, total=
0.1s
[CV] kernel=rbf, gamma=0.01,
C=1000 ..................................
[CV] ...... kernel=rbf, gamma=0.01, C=1000, score=0.637, total=
0.0s
[CV] kernel=rbf, gamma=0.01,
C=1000 ..................................
[CV] ...... kernel=rbf, gamma=0.01, C=1000, score=0.637, total=
0.1s
[CV] kernel=rbf, gamma=0.01,
C=1000 ..................................
[CV] ...... kernel=rbf, gamma=0.01, C=1000, score=0.613, total=
0.0s
[CV] kernel=rbf, gamma=0.01,
C=1000 ..................................
[CV] ...... kernel=rbf, gamma=0.01, C=1000, score=0.633, total=
0.0s
[CV] kernel=rbf, gamma=0.01,
C=1000 ..................................
[CV] ...... kernel=rbf, gamma=0.01, C=1000, score=0.633, total=
0.0s
[CV] kernel=rbf, gamma=0.001,
C=1000 .................................
[CV] ..... kernel=rbf, gamma=0.001, C=1000, score=0.887, total=
0.0s
[CV] kernel=rbf, gamma=0.001,
C=1000 .................................
[CV] ..... kernel=rbf, gamma=0.001, C=1000, score=0.912, total=
0.0s
[CV] kernel=rbf, gamma=0.001,
C=1000 .................................
[CV] ..... kernel=rbf, gamma=0.001, C=1000, score=0.900, total=
0.0s
[CV] kernel=rbf, gamma=0.001,
C=1000 .................................
[CV] ..... kernel=rbf, gamma=0.001, C=1000, score=0.937, total=
0.0s
[CV] kernel=rbf, gamma=0.001,
C=1000 .................................
[CV] ..... kernel=rbf, gamma=0.001, C=1000, score=0.924, total=
0.0s
[CV] kernel=rbf, gamma=0.0001,
C=1000 ................................
[CV] .... kernel=rbf, gamma=0.0001, C=1000, score=0.938, total=
0.0s
[CV] kernel=rbf, gamma=0.0001,
C=1000 ................................
[CV] .... kernel=rbf, gamma=0.0001, C=1000, score=0.912, total=
0.0s
[CV] kernel=rbf, gamma=0.0001,
C=1000 ................................
[CV] .... kernel=rbf, gamma=0.0001, C=1000, score=0.963, total=
0.0s
[CV] kernel=rbf, gamma=0.0001,
C=1000 ................................
[CV] .... kernel=rbf, gamma=0.0001, C=1000, score=0.924, total=
0.0s
[CV] kernel=rbf, gamma=0.0001,
C=1000 ................................
[CV] .... kernel=rbf, gamma=0.0001, C=1000, score=0.962, total=
0.0s
{'kernel': 'rbf', 'gamma': 0.0001, 'C': 1}
SVC(C=1, break_ties=False, cache_size=200, class_weight=None,
coef0=0.0,
decision_function_shape='ovr', degree=3, gamma=0.0001,
kernel='rbf',
max_iter=-1, probability=False, random_state=None,
shrinking=True,
tol=0.001, verbose=False)
precision recall f1-score support

0 0.94 0.89 0.91 66


1 0.94 0.96 0.95 105

accuracy 0.94 171


macro avg 0.94 0.93 0.93 171
weighted avg 0.94 0.94 0.94 171

[Parallel(n_jobs=1)]: Done 125 out of 125 | elapsed: 5.8s


finished
PRACTICAL4:
NAIVE BAYES

from sklearn.datasets import load_iris


from sklearn.model_selection import train_test_split
from sklearn.naive_bayes import GaussianNB
X, y = load_iris(return_X_y=True)
X_train, X_test, y_train, y_test = train_test_split(X, y,
test_size=0.5, random_state=0)
gnb = GaussianNB()
y_pred = gnb.fit(X_train, y_train).predict(X_test)
print("Number of mislabeled points out of a total",
(X_test.shape[0])," points: ",(y_test != y_pred).sum())

OUTPUT:
Number of mislabeled points out of a total 75 points : 4

You might also like