.squeeze function (C2W3_Lab_01_Model_Evaluation_and_Selection)

hi
Why does the lab use squeeze function in this code segment?

I explored that the function squeezes the dimension of an array. But as far as I understand mse is a float rather than an array so why do we need that function?

yhat = linear_model.predict(X_train_scaled)

print(f"training MSE (using sklearn function): {mean_squared_error(y_train, yhat) / 2}")

total_squared_error = 0

for i in range(len(yhat)):
squared_error_i = (yhat[i] - y_train[i])**2
total_squared_error += squared_error_i

mse = total_squared_error / (2*len(yhat))

print(f"training MSE (for-loop implementation): {mse.squeeze()}")

It’s a method for removing an axes from the array. It requires that the axes being squeezed only contains a single item. mse is actually an array containing a single number before you squeeze it. I added a print line to show mse before and after squeezing it.

1 Like

thank you very much for the response