Ex7 HTML
Ex7 HTML
Balasubramanian S
19Z237
Aim:
Desgin a Perceptron
In [ ]: import pandas as pd
from sklearn import datasets
boston = datasets.load_boston()
X = boston.data
Y = boston.target
print(X.shape)
print(Y.shape)
data = {
'x1': [0.3, 0.7, 0.3],
'x2': [0.2, 0.1, -0.6],
'O': [0.5, 0.4, -0.1]
}
dataset = pd.DataFrame(data)
(506, 13)
(506,)
/Users/Balasubramanian/Desktop/ML/balasubramanian/lib/python3.9/site-packages/sklear
n/utils/deprecation.py:87: FutureWarning: Function load_boston is deprecated; `load_b
oston` is deprecated in 1.0 and will be removed in 1.2.
The Boston housing prices dataset has an ethical problem. You can refer to
the documentation of this function for further details.
In this special case, you can fetch the dataset from the original
source::
import pandas as pd
import numpy as np
data_url = "http://lib.stat.cmu.edu/datasets/boston"
raw_df = pd.read_csv(data_url, sep="\s+", skiprows=22, header=None)
data = np.hstack([raw_df.values[::2, :], raw_df.values[1::2, :2]])
target = raw_df.values[1::2, 2]
warnings.warn(msg, category=FutureWarning)
def calculateY(self):
print(len(self.dataset))
self.Y = [0]*(len(self.dataset))
for index, row in self.dataset.iterrows():
summ = 0
for i in range(1, 13):
summ += self.weights[i] * row[i]
summ += self.weights[0]
self.Y[index] = round(summ, 3)
# print("Y: ", self.Y)
def calculateY_O2(self):
self.Y_O2 = [0]*(len(self.dataset))
for i in range(len(self.Y)):
self.Y_O2[i] = round((self.Y[i]-self.output[i])**2, 3)
# print("Y_O2: ", self.Y_O2)
def calculateSSE(self):
sum = 0
for i in self.Y_O2:
sum += i
print("Error: ", sum)
def updateWeights(self):
for i in range(len(self.weights)):
summation_value = 0
for index, row in self.dataset.iterrows():
if(i == 0):
summation_value += (self.output[index] -
self.Y[index])
else:
summation_value += (self.output[index] -
self.Y[index])*row[i-1]
summation_value *= -2
self.weights[i] = self.weights[i] - round(summation_value, 3)
print("-------------------------------------------------------------------")
self.calculateY()
self.calculateY_O2()
self.calculateSSE()
print((boston_frame.iloc[:, 0:13]))
neuron = Perceptron((boston_frame.iloc[:, 0:13]),
list(boston_frame['TARGET'].values), [0.1, 0.1, 0.1, 0.1, 0.1, 0.
CRIM ZN INDUS CHAS NOX RM AGE DIS RAD TAX \
0 0.00632 18.0 2.31 0.0 0.538 6.575 65.2 4.0900 1.0 296.0
1 0.02731 0.0 7.07 0.0 0.469 6.421 78.9 4.9671 2.0 242.0
2 0.02729 0.0 7.07 0.0 0.469 7.185 61.1 4.9671 2.0 242.0
3 0.03237 0.0 2.18 0.0 0.458 6.998 45.8 6.0622 3.0 222.0
4 0.06905 0.0 2.18 0.0 0.458 7.147 54.2 6.0622 3.0 222.0
.. ... ... ... ... ... ... ... ... ... ...
501 0.06263 0.0 11.93 0.0 0.573 6.593 69.1 2.4786 1.0 273.0
502 0.04527 0.0 11.93 0.0 0.573 6.120 76.7 2.2875 1.0 273.0
503 0.06076 0.0 11.93 0.0 0.573 6.976 91.0 2.1675 1.0 273.0
504 0.10959 0.0 11.93 0.0 0.573 6.794 89.3 2.3889 1.0 273.0
505 0.04741 0.0 11.93 0.0 0.573 6.030 80.8 2.5050 1.0 273.0
PTRATIO B LSTAT
0 15.3 396.90 4.98
1 17.8 396.90 9.14
2 17.8 392.83 4.03
3 18.7 394.63 2.94
4 18.7 396.90 5.33
.. ... ... ...
501 21.0 391.99 9.67
502 21.0 396.90 9.08
503 21.0 396.90 5.64
504 21.0 393.45 6.48
505 21.0 396.90 7.88
In [ ]: print(len(Perceptron.Y))
0