Practicals 2.odt
Practicals 2.odt
Practicals 2.odt
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
"""
# 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])
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_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
#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)
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]
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()
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
OUTPUT:
Number of mislabeled points out of a total 75 points : 4