Deleting temporary variables. (C2W3_Lab_01_Model_Evaluation_and_Selection))

hi
why do we need this segment in the following code. What is the advantage of deleting these temp variables_?

del x_, y_ # Delete temporary variables

x_train, x_, y_train, y_ = train_test_split(x, y, test_size=0.40, random_state=1)

x_cv, x_test, y_cv, y_test = train_test_split(x_, y_, test_size=0.50, random_state=1)

del x_, y_

print(f"the shape of the training set (input) is: {x_train.shape}“)
print(f"the shape of the training set (target) is: {y_train.shape}\n”)
print(f"the shape of the cross validation set (input) is: {x_cv.shape}“)
print(f"the shape of the cross validation set (target) is: {y_cv.shape}\n”)
print(f"the shape of the test set (input) is: {x_test.shape}“)
print(f"the shape of the test set (target) is: {y_test.shape}”)

There’s no particular advantage other than just freeing up some memory.

Hello @mehmet_baki_deniz,

The del is to delete the variables placed next to it. If those variables are the last ones that reference to the data behind them, then those data can be removed from the memory for good. This action effectively allows the system to clear some memory out. It is one useful way when you are handling a large amount of data.

The first del, however, may not be necessary since writing something else to the variable has the same effect of getting rid of one referencing to the data originally behind. However, keeping the first del can communicate your intention clearly of deleting that variable.

Btw, I don’t think this is one of the Machine Learning Specialization’s lab, would you mind sharing the name of the course you are taking, so that I can help you move this topic back to the right place? My reply is based on the code you shared, but it might miss out the bigger picture since I don’t know the rest of the code.

Cheers,
Raymond

1 Like

hmm. interesting. I thought when I quit the program, the memory is automatically freed from the system. or maybe you guys are talking about the process during the execution of the program. So freeing space would be a wise decision especially for large data.

I copy and pasted the code from the optional lab of C2 of ML specialization for W3.

maybe this link to the lab may help

but I realized that I posted the question to week 2 of C2. It should be moved to week 3 of C2.

By the way, thank you both for your responses

I have moved this thread to Week 3 for you.

That’s true.

True and true.

Cheers,
Raymond

1 Like

thank you Raymond :slight_smile: