AssertionError sur Jupyter (Python)

Résolu/Fermé
Ernani_2748 Messages postés 3 Date d'inscription dimanche 4 décembre 2022 Statut Membre Dernière intervention 6 décembre 2022 - Modifié le 5 déc. 2022 à 11:46
mamiemando Messages postés 33081 Date d'inscription jeudi 12 mai 2005 Statut Modérateur Dernière intervention 27 avril 2024 - 6 déc. 2022 à 12:51

Salut à vous

Je travaille sur un algorithme Python (dans Jupyter) qui m'affiche une erreur d'assertion. Je ne sais pas comment la résoudre. Voici l'algorithme:

import numpy as np
import matplotlib.pyplot as plt
from utils import *
import copy
import math
%matplotlib inline

# load the dataset
x_train, y_train = load_data()

# print x_train
print("Type of x_train:",type(x_train))
print("First five elements of x_train are:\n", x_train[:5]) 

# print y_train
print("Type of y_train:",type(y_train))
print("First five elements of y_train are:\n", y_train[:5])  

print ('The shape of x_train is:', x_train.shape)
print ('The shape of y_train is: ', y_train.shape)
print ('Number of training examples (m):', len(x_train))

# Create a scatter plot of the data. To change the markers to red "x",
# we used the 'marker' and 'c' parameters
plt.scatter(x_train, y_train, marker='x', c='r') 

# Set the title
plt.title("Profits vs. Population per city")
# Set the y-axis label
plt.ylabel('Profit in $10,000')
# Set the x-axis label
plt.xlabel('Population of City in 10,000s')
plt.show()

# Compute cost with some initial values for paramaters w, b
initial_w = 2
initial_b = 1

cost = compute_cost(x_train, y_train, initial_w, initial_b)
print(type(cost))
print(f'Cost at initial w:{cost:0.3f}')

# Public tests
from public_tests import *
compute_cost_test(compute_cost)

Et voici l'erreur:

 
AssertionError                            Traceback (most recent call last)
<ipython-input-85-0fe06c9a435b> in <module>
      9 # Public tests
     10 from public_tests import *
---> 11 compute_cost_test(compute_cost)

~/work/public_tests.py in compute_cost_test(target)
     17     initial_b = 1.0
     18     cost = target(x, y, initial_w, initial_b)
---> 19     assert cost == 2, f"Case 2: Cost must be 2 but got {cost}"
     20 
     21     # print("Using X with shape (5, 1)")

AssertionError: Case 2: Cost must be 2 but got 0.5

Pouvez-vous m'éclairer s'il vous plaît ?

Merci d'avance.

3 réponses

mamiemando Messages postés 33081 Date d'inscription jeudi 12 mai 2005 Statut Modérateur Dernière intervention 27 avril 2024 7 749
5 déc. 2022 à 11:49

Bonjour,

La plupart de tes fonctions (load_data, compute_cost, target) ne sont pas définies, donc difficile pour nous de t'aider. Tout ce que je peux te dire à ce stade, c'st que tu ta suite de test te dit que cost vaut 0.5 alors que la valeur attendue était 2.

Bonne chance

1
Ernani_2748 Messages postés 3 Date d'inscription dimanche 4 décembre 2022 Statut Membre Dernière intervention 6 décembre 2022
5 déc. 2022 à 18:34

Merci pour votre réponse.

0
mamiemando Messages postés 33081 Date d'inscription jeudi 12 mai 2005 Statut Modérateur Dernière intervention 27 avril 2024 7 749
5 déc. 2022 à 23:31

Heu, hum. Alors du coup on fait quoi ? On considère que ton problème est résolu ou tu veux nous donner des détails pour qu'on tente de t'aider ? :-D

0
Ernani_2748 Messages postés 3 Date d'inscription dimanche 4 décembre 2022 Statut Membre Dernière intervention 6 décembre 2022
6 déc. 2022 à 12:31

Je vais clore. Désolé pour le silence. J’essayais de comprendre le souci. Il s'agissait d'un problème d'indentation.

0
mamiemando Messages postés 33081 Date d'inscription jeudi 12 mai 2005 Statut Modérateur Dernière intervention 27 avril 2024 7 749
6 déc. 2022 à 12:51

Merci pour ton retour, bonne continuation

1