Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lernen Challenge: Evaluating the Perceptron | Neural Network from Scratch
Introduction to Neural Networks

book
Challenge: Evaluating the Perceptron

To evaluate the previously created perceptron, you will use a dataset containing two input features and two distinct classes (0 and 1):

This dataset is balanced, with 500 samples from class 1 and 500 samples from class 0. Therefore, accuracy is a sufficient metric for evaluation in this case, which can be calculated using the accuracy_score() function:

python
accuracy_score(y_true, y_pred)

y_true represents the actual labels, while y_pred represents the predicted labels.

The dataset is stored in perceptron.py as two NumPy arrays: X (input features) and y (corresponding labels), so they will be simply imported. This file also contains model, which is the instance of the Perceptron class you previously created.

Aufgabe

Swipe to start coding

Obtain predictions from the trained model and evaluate its performance:

  1. Split the dataset into training (80%) and testing (20%) sets.
  2. Train the model for 10 epochs with a learning rate of 0.01.
  3. Obtain predictions for all examples in the test set.
  4. Calculate the accuracy by comparing the predicted labels with the actual test labels.

Lösung

import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
import os
os.system('wget https://codefinity-content-media.s3.eu-west-1.amazonaws.com/f9fc718f-c98b-470d-ba78-d84ef16ba45f/section_2/perceptron.py 2>/dev/null')
from perceptron import X, y, model

# 1. Split the dataset (80% for training set and 20% for test set)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=10)
# 2. Train the model for 10 epochs with a learning rate of 0.01
model.fit(X_train, y_train, 10, 0.01)
# 3. Obtain predictions for all examples in the test set
predictions = np.array([model.forward(test_input)[0] for test_input in X_test])
# Rounding predictions (e.g., 0.83 rounds to 1, and 0.1 to 0)
predicted_labels = np.round(predictions)
# 4. Calculate the accuracy on the test set
print(accuracy_score(y_test, predicted_labels))
War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 2. Kapitel 12
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
import os
os.system('wget https://codefinity-content-media.s3.eu-west-1.amazonaws.com/f9fc718f-c98b-470d-ba78-d84ef16ba45f/section_2/perceptron.py 2>/dev/null')
from perceptron import X, y, model

# 1. Split the dataset (80% for training set and 20% for test set)
X_train, X_test, y_train, y_test = ___(___, ___, ___=___, random_state=10)
# 2. Train the model for 10 epochs with a learning rate of 0.01
model.___(X_train, y_train, ___, ___)
# 3. Obtain predictions for all examples in the test set
predictions = np.array([model.___(___)[0] for test_input in ___])
# Rounding predictions (e.g., 0.83 rounds to 1, and 0.1 to 0)
predicted_labels = np.round(predictions)
# 4. Calculate the accuracy on the test set
print(___(___, ___))

Fragen Sie AI

expand
ChatGPT

Fragen Sie alles oder probieren Sie eine der vorgeschlagenen Fragen, um unser Gespräch zu beginnen

some-alt