Created
February 15, 2019 16:55
-
-
Save Niranjankumar-c/cb10a1aab3708d61ca9e591f2a17bcaa to your computer and use it in GitHub Desktop.
Implementation of MP Neuron using Python
This file contains 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
class MPNeuron: | |
def __init__(self): | |
self.b = None | |
def model(self, x): | |
return(sum(x) >= self.b) | |
def predict(self, X): | |
Y = [] | |
for x in X: | |
result = self.model(x) | |
Y.append(result) | |
return np.array(Y) | |
def fit(self, X, Y): | |
accuracy = {} | |
for b in range(X.shape[1] + 1): | |
self.b = b | |
Y_pred = self.predict(X) | |
accuracy[b] = accuracy_score(Y_pred, Y) | |
best_b = max(accuracy, key = accuracy.get) | |
self.b = best_b | |
print('Optimal value of b is', best_b) | |
print('Highest accuracy is', accuracy[best_b]) | |
#Calling the class MPNeuron | |
mp_neuron = MPNeuron() | |
#Calling the fit method inside the class on the training data | |
mp_neuron.fit(X_binarised_train, Y_train) | |
#testing the model on the test data. | |
Y_test_pred = mp_neuron.predict(X_binarised_test) | |
accuracy_test = accuracy_score(Y_test_pred, Y_test) | |
#print the accuracy of the test data | |
print(accuracy_test) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment