FAQ: Perceptron - Step 1: Weighted Sum

This community-built FAQ covers the “Step 1: Weighted Sum” exercise from the lesson “Perceptron”.

Paths and Courses
This exercise can be found in the following Codecademy content:

FAQs on the exercise Step 1: Weighted Sum

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

What is the context of Self in perceptrons?

May I know why do we need to write 15 lines of code when we can done away with just 2 lines if the objective is to print an output with 2 inputs? (Python Newbee I am)
Also, what’s the need of class here when function is enough to do the job?
Thanks in advance.

Why not extract num_inputs directly from the weights variable? It is just the length of the weights variable. This is easier for the user as it requires one less inputs.

class Perceptron:
  def __init__(self, weights=[2,1]):
    self.num_inputs = len(weights)
    self.weights = weights

Why not use the dot product to calculate the weighted sum with numpy? This can be done in one line of code and is faster than a for loop.

import numpy as np

def weighted_sum(self, inputs):
    weighted_sum = np.dot(inputs,self.weights)
    return weighted_sum

The concept of self means what in the perceptron

class Perceptron:
def init(self, num_inputs=2, weights=[3,1]):
self.num_inputs = num_inputs
self.weights = weights

def weighted_sum(self, inputs):
weighted_sum = 0
for i in range(self.num_inputs):
weighted_sum += self.weights[i]*inputs[i]
#complete this loop
return weighted_sum

cool_perceptron = Perceptron()
print(cool_perceptron.weighted_sum([24, 55]))