How to stop this while loop from looping infinitely?

61 views Asked by At

Please help me figure how to stop the infinite loop. This is a code for backward elimination. Im trying to put this code in my homework because if I do backward elimination manually it will waste a lot of my time. The dataset has 78 independent variable, I dont want to do it manually. please help.

greater_than_5_per = x_train_new.columns[lm.pvalues > 0.05]
i = 1
while(len(greater_than_5_per)!=0):
 print("iteration {}".format(i))
 new_col = x_train_new.columns[lm.pvalues < 0.05]
 print("num of new columns is {}\nnew columns in iteration are\n{}".format(len(new_col),new_col))
 x_train_new = x_train_new[new_col]
 x_train_new, lm = build_model(x_train_new,y_train)
 #----------------------------------------------------------
 if 'const' in x_train_new.columns[lm.pvalues > 0.05]:
     greater_than_5_per = x_train_new.columns[lm.pvalues > 0.05].drop('const')
 i+=1
 print("\n\n******************************************\n") ```
0

There are 0 answers