This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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)] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def forward(self, x): | |
| '''Calculate perceptron output values.''' | |
| z = self.weights @ x.T | |
| z += self.bias | |
| return self.heaviside(z) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def heaviside(self, s): | |
| '''Heaviside step function.''' | |
| return (s >= 0).astype(np.int) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def predict(self, X): | |
| '''Predict the target value of the given instance.''' | |
| knn = self.neighbours(X) | |
| y = knn[:, -1] | |
| return y.mean() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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): |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def fit(self, X, y): | |
| '''Store to training data to be used when finding nearest neighbours.''' | |
| self.train_set = np.c_[X,y] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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] |
NewerOlder