Skip to content

Instantly share code, notes, and snippets.

View TristanBester's full-sized avatar
🔥
Focusing

Tristan Bester TristanBester

🔥
Focusing
View GitHub Profile
def backprop(self, x, y):
nabla_w = [0] * len(self.weights)
nabla_b = [0] * len(self.biases)
a = x
activations = [x]
sums = [None]
for i in range(len(self.weights)):
a = np.dot(self.weights[i], a) + self.biases[i]
sums.append(a)
def SGD(self, training_data, epochs, mini_batch_size, eta):
for j in range(epochs):
for x,y in training_data:
delta_nabla_b, delta_nabla_w = self.backprop(x, y)
self.weights = [w - (eta*dw).reshape(w.shape) for w, dw in zip(self.weights, delta_nabla_w)]
self.biases = [b - eta*nb for b, nb in zip(self.biases, delta_nabla_b)]
def feedforward(self, a):
for i in range(len(self.weights)):
a = np.dot(self.weights[i], a) + self.biases[i]
a = self.sigmoid(a)
return a
def fit(self, X, Y, n_iters):
'''Fit the model to the given data.'''
for i in range(n_iters):
for x,y in zip(X,Y):
pred = self.forward(x)
diff = y - pred
for row in range(self.weights.shape[0]):
for col in range(self.weights.shape[1]):
self.weights[row][col] += self.lr * diff[row] * x[col]
def forward(self, x):
'''Calculate perceptron output values.'''
z = self.weights @ x.T
z += self.bias
return self.heaviside(z)
def heaviside(self, s):
'''Heaviside step function.'''
return (s >= 0).astype(np.int)
def predict(self, X):
'''Predict the target value of the given instance.'''
knn = self.neighbours(X)
y = knn[:, -1]
return y.mean()
def predict_proba(self, sample):
'''Predict the class probabilities of the given instance.'''
knn = self.neighbours(sample)
y = knn[:, -1].astype(int)
counts = np.bincount(y)
counts = counts/float(sum(counts))
return counts
def predict(self, X):
def fit(self, X, y):
'''Store to training data to be used when finding nearest neighbours.'''
self.train_set = np.c_[X,y]
def neighbours(self, sample):
'''Find the k-nearest neighbours of the given instance.'''
dists = [(idx, self.__euclidean_distance(x, sample)) for idx,x in enumerate(self.train_set[:, :-1])]
sorted_dists = sorted(dists, key=lambda tup: tup[1])
neighbour_idx = [sorted_dists[i][0] for i in range(self.n_neighbours)]
return self.train_set[neighbour_idx]