# Implementation of Perceptron Algorithm for AND Logic Gate with 2-bit Binary Input

In the field of Machine Learning, the Perceptron is a Supervised Learning Algorithm for binary classifiers. The Perceptron Model implements the following function: For a particular choice of the weight vector and bias parameter , the model predicts output for the corresponding input vector .

AND logical function truth table for 2-bit binary variables, i.e, the input vector and the corresponding output    0 0 0
0 1 0
1 0 0
1 1 1

Now for the corresponding weight vector of the input vector , the associated Perceptron Function can be defined as:  For the implementation, considered weight parameters are and the bias parameter is .

Python Implementation:

 # importing Python library  import numpy as np     # define Unit Step Function  def unitStep(v):      if v >= 0:          return 1     else:          return 0    # design Perceptron Model  def perceptronModel(x, w, b):      v = np.dot(w, x) + b      y = unitStep(v)      return y     # AND Logic Function  # w1 = 1, w2 = 1, b = -1.5  def AND_logicFunction(x):      w = np.array([1, 1])      b = -1.5     return perceptronModel(x, w, b)     # testing the Perceptron Model  test1 = np.array([0, 1])  test2 = np.array([1, 1])  test3 = np.array([0, 0])  test4 = np.array([1, 0])     print("AND({}, {}) = {}".format(0, 1, AND_logicFunction(test1)))  print("AND({}, {}) = {}".format(1, 1, AND_logicFunction(test2)))  print("AND({}, {}) = {}".format(0, 0, AND_logicFunction(test3)))  print("AND({}, {}) = {}".format(1, 0, AND_logicFunction(test4)))

Output:

AND(0, 1) = 0
AND(1, 1) = 1
AND(0, 0) = 0
AND(1, 0) = 0


Here, the model predicted output ( ) for each of the test inputs are exactly matched with the AND logic gate conventional output ( ) according to the truth table for 2-bit binary input.
Hence, it is verified that the perceptron algorithm for AND logic gate is correctly implemented.

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Previous
Next