如何阻止这个 while 循环无限循环?
请帮我弄清楚如何停止无限循环。这是向后消除的代码。我试图将这段代码放入我的作业中,因为如果我手动进行向后消除,则会浪费我很多时间。数据集有 78 个自变量,我不想手动完成。请帮忙。
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") ```
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") ```
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论