W2_A2_Squeeze function in Logistic_Regression_model

Hi folks,

In the Week 2 programming assignment, there’s a piece of sample code like below:

# Plot learning curve (with costs)
costs = np.squeeze(logistic_regression_model['costs'])

I tried to remove the use of np.squeeze() and revised the code like this:

costs = logistic_regression_model['costs']

The revised code works just fine.
I wonder why the np.squeeze() is used here.


HI @WinniePooh
Welcome to the community

we make squeeze as it’s remove single-dimensional entries from the shape of an array.in this assignment we are sure np.squeeze() isn’t useful(to make the code more general to be used) here as it implemented for you but if you doing an personal project and stack the values of cost in 1 row and n columns of cost values shape is (1,n) …the graph code wouldn’t be run and would raise an error so we make np.squeeze(…) to make sure that the values of cost would be in the list of values with dimension (,n) without (1,n) shape as plt.plot(x) code line when make graph use x[0] , x[1] indexing … and that would raise an error as there isn’t an index 1 so to convert it to a list of values make sure to use squeeze function.


That makes sense.