Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: 'for' loops and iteration in Linear Regression

I'm building a basic Linear regression model using the statsmodel package and here's what I'm trying to do:
Build a 'for' loop that checks the probabilities of each of the features, checks if they're greater than 0.05, if yes: drop the feature from training (& test) data, fit model again, and repeat till all probabilities are < 0.05.
Here's what I've done so far:

for x,y in zip(lrmodel.pvalues,xtrain.columns): 
   if x>0.05:
      xtrain = xtrain.drop(y,axis=1)
      xtest = xtest.drop(y,axis=1)
      lrmodel = sm.OLS(ytrain,xtrain).fit()
      finalmodel = lrmodel
    else:
      finalmodel = lrmodel

The problem with this loop is that it doesn't iterate over the pvalues, rather it removes all the probabilities>0.05 within a single shot.
If anyone could help me, I would be grateful. Thanks!

like image 821
PoojaV Avatar asked Nov 26 '25 08:11

PoojaV


1 Answers

I think you need a while loop on top of this:

while max(lrmodel.pvalues)>0.05:
    for x,y in zip(lrmodel.pvalues,xtrain.columns): 
        if x>0.05:
            xtrain = xtrain.drop(y,axis=1)
            xtest = xtest.drop(y,axis=1)
            lrmodel = sm.OLS(ytrain,xtrain).fit()
            break
# after all the values are less than 0.05, assign the model to final model
finalmodel = lrmodel

One potential problem of this is: you have to make sure all the values will be less than 0.05 eventually, otherwise you need an extra logic to terminate the loop. For example,

while len(lrmodel.pvalues)>0 and max(lrmodel.pvalues)>0.05:

like image 116
Kevin Fang Avatar answered Nov 27 '25 21:11

Kevin Fang



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!