Functions in MP Neuron Class Help

https://www.guvi.in/courses-video?course=dl_primitive_neurons#
Help me to resolve the error I am using Spyder
CODE:

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)
            
        print("\nY_pred = ", len(Y_pred), "\n")
        print("\nY      = ", len(Y), "\n")
        accuracy[b] = accuracy_score(Y_pred, Y)
            
        best_b = max(accuracy, key=accuracy.get)
        self.b = best_b
        print("Optional Value of b = ", best_b)
        print("Heighest accuracy   = ". accuracy[best_b])

print("\nX_binarised_train = ", len(X_binarised_train))
print("\nY_train           = ", len(Y_train), "\n")
mp_neuron = MPNEURON()
mp_neuron.fit(X_binarised_train, Y_train)

ERROR:

X_binarised_train =  512 


Y_train           =  512 


Y_pred =  569 


Y      =  512 

Traceback (most recent call last):

  File "G:\## PHD ##\GUVI COURSE\2-MP Neurons &Perceptron using Python Class.py", line 128, in <module>
    mp_neuron.fit(X_binarised_train, Y_train)

  File "G:\## PHD ##\GUVI COURSE\2-MP Neurons &Perceptron using Python Class.py", line 118, in fit
    accuracy[b] = accuracy_score(Y_pred, Y)

  File "C:\Users\Jeyanthi\Anaconda\lib\site-packages\sklearn\metrics\classification.py", line 176, in accuracy_score
    y_type, y_true, y_pred = _check_targets(y_true, y_pred)

  File "C:\Users\Jeyanthi\Anaconda\lib\site-packages\sklearn\metrics\classification.py", line 71, in _check_targets
    check_consistent_length(y_true, y_pred)

  File "C:\Users\Jeyanthi\Anaconda\lib\site-packages\sklearn\utils\validation.py", line 235, in check_consistent_length
    " samples: %r" % [int(l) for l in lengths])

ValueError: Found input variables with inconsistent numbers of samples: [569, 512]

This is strange.

And this should go inside for loop.

Can you share link to your notebook?

PYTHON CODE

Sir I am using Sypder, everything is ok only MPNEURON class is giving me error
’ ’ ’
ValueError: Found input variables with inconsistent numbers of samples: [569, 512]
’ ’ ’

Its difficult to identify the error with information above.

See, you are calling fit with X_binarised_train which has 512 elements.
Within fit function, there is a call to model returning Y_pred. Y_pred is the model output for each of the 512 entries in X, so Y_pred should also be off length 512. But for some reason (which is not clear to me from the code you have shared), Y_pred is of length 569 which is incorrect.

May be within model, can you print, shape of X before this line.

Again, it will be easier, if you can share the code (with correct indentation).

Brother in your predict function you are returning a numpy array of small y. What is small y?

1 Like

Thanks for solving the error. :grinning: :grinning: :grinning:
Code worked. I had made typographic error in predict function return statement. I simply changed the small y into caps Y in return. Everything works fine and good, Thanks brother.

Thanks for helping :grinning: :grinning: :grinning: me to identify the error sir.

1 Like