String formatting in C1_W1_Lab05_Gradient_Descent_Soln

Print cost every at intervals 10 times or as many iterations if < 10

    if i% math.ceil(num_iters/10) == 0:
        print(f"Iteration {i:4}: Cost {J_history[-1]:0.2e} ",
              f"dj_dw: {dj_dw: 0.3e}, dj_db: {dj_db: 0.3e}  ",
              f"w: {w: 0.3e}, b:{b: 0.5e}")

can someone explain what {I:4} or dj_db:0.3e or so on do

They specify the format your want to print the numbers. Please check out this page for examples of some popular uses of string formatting, and this page for the full list of format specifiers.

You may try to run some of those examples and modify them to see the different effects!


P.S. edited the title of this thread to reflect this is about string formatting

1 Like