Lightning AI Studios: Never set up a local environment again →

Log in or create a free Lightning.ai account to track your progress and access additional course materials  

1.6 Implementing a Perceptron in Python. Parts 1-3

References

What we covered in this video lecture

In this video, we implemented our first machine learning algorithm — a perceptron classifier — in Python. Along the way, we used pandas to load the data from a text file, NumPy to check the class label distribution, and matplotlib to visualize the results.

Additional resources if you want to learn more

If you need help setting up your computing environment, we recommend checking out the previous lecture (1.5 Setting Up Our Computing Environment).

We implemented the Perceptron using an objected-oriented programming paradigm and Python classes. While it is possible to implement a Perceptron using a purely functional approach, the goal was to set the stage for the later units where we will implement deep neural networks. Most deep learning frameworks, including PyTorch, also use a similar paradigm. If you are new to object-oriented programming or Python classes,

If you need help getting started with NumPy and matplotlib, check out Sebastian’s comprehensive tutorial blog and video series: Scientific Computing in Python: Introduction to NumPy and Matplotlib.

Log in or create a free Lightning.ai account to access:

  • Quizzes
  • Completion badges
  • Progress tracking
  • Additional downloadable content
  • Additional AI education resources
  • Notifications when new units are released
  • Free cloud computing credits

Quiz: 1.6 Implementing a Perceptron in Python. Part 1

Using the pandas.read_csv(..., sep="\t") we load data from a dataset where …

Incorrect. Using default settings .read_csv reads from comma-separated files, but by specifying sep="\t", it expects tab-separated columns.

Correct. Using default settings .read_csv reads from comma-separated files, but by specifying sep="\t", it expects tab-separated columns.

Incorrect. Here, we refer to the columns of the datasets, not the rows. (Hint: also, we provide the extra argument sep="\t" .)

Incorrect. Here, we refer to the columns of the datasets, not the rows.

If we have a dataset with 25 class labels from class 1 and 25 class labels from class 2, the shape of y_train is

Correct. The 1-dimensional label array consists of 50 labels corresponding to the 50 training examples.

Incorrect. Hint: you are missing half of the training examples.

Incorrect. Hint: the label array should be 1-dimensional.

Incorrect. Hint: the label array should be 1-dimensional.

Incorrect. Hint: the label array should be 1-dimensional.

If we have a label array with 25 class labels from class 1 and 25 class labels from class 2, np.bincount will return the following output:

Incorrect. Hint: you are close, but Python & NumPy start the index at 0 …

Correct. However, if the class labels were 0 and 1, then array([25, 25]) would be correct.

Incorrect. Hint: were you thinking about an array shape?

Incorrect. Hint: were you thinking about the array shape?

We used the following code, X_train[y_train == 1, 0], in the plotting portion to select

Incorrect. Hint: in `X_train[?, ?]`, the first position inside the square brackets selects for the rows, and the second index selects for the columns.

Incorrect. Hint: If we we are just interested in the first feature of *all* training examples, why do we use `y_train`?

Incorrect. You were close. Hint: check which class label we select for.

Correct. y_train == 1 is a selection mask for all data points that belong to class 1. Using that mask, we then further select the first feature, the feature at index 0: X_train[... == 1, 0]

Please answer all questions to proceed.

Quiz: 1.6 Implementing a Perceptron in Python. Part 2

Suppose we use the variable num_weights (number or weights) for initializing the perceptron via [0.0 for _ in range(num_weights)]. Which of the following is true?

Incorrect. The number of weights does not depend on the number of training examples (dataset size).

Incorrect. This would only be true for a 1-dimensional dataset.

Correct. Each feature has an associated weight.

Suppose we have a 3-dimensional dataset and compute the forward pass in the perceptron via ppn.forward([1.1, 2.2, 3.3]), where [1.1, 2.2, 3.3] are the 3 feature values corresponding to a single training example. The output that is returned is a

Correct. Independent of the number of features, the perceptron outputs the predicted class label of the perceptron.

Incorrect. Hint: Think about what computation happens inside the perceptron when we compute the weighted sum …

Incorrect. Hint: Think about what computation happens inside the perceptron when we compute the weighted sum …

Suppose we start with the initial weights of [0.0, 0.0], the true label is 0, and the predicted labels is 1. Given a training example [-1.2, 2.4] what does the weight vector look like after the update?

Incorrect. Since the true label is different from the predicted label, the weight vector will be different after the update.

Correct, since the error is `0-1= -1`, we update the weight vector by the negative feature vector.

Incorrect. Hint: You may have computed the error incorrectly.

Incorrect. Hint: You may have computed the error incorrectly.

Please answer all questions to proceed.

Quiz: 1.6 Implementing a Perceptron in Python. Part 3

The accuracy is a value between (Check all that apply):

Correct. The training accuracy is the number of correct predictions divided by the total number of training examples.

Correct. We can express the training accuracy as a percentage of correct predictions. It is computed as the number of correct predictions divided by the total number of training examples times 100%.

Incorrect. Hint: the training accuracy is computed by dividing the number of correct predictions by the total number of examples in the dataset.

Incorrect. Hint: the training accuracy is computed by dividing the number of correct predictions by the total number of examples in the dataset.

Please answer all questions to proceed.
Watch Video 1