forwardselection py import statsmodels api as sm from statsmodels formula api import ols bwt sm datasets get_rdataset (birthwt ","mass"data ftv (bwt['ftv '>= astype (intptl (bwt['ptl '>= astype (intremaining_features {'lwt ''age '' (ui)''smoke '' (ht)''ftv ''ptl 'selected_features [while remaining_features pf [#list of ( value feature for in remaining_features temp selected_features [ftemporary list of features formula 'bwt~'+join(tempfit ols(formula ,data=bwtfit (pvalfit pvalues - if pval pf append (pval , )if pf#if not empty pf sortreverse =true(best_pval best_f pf pop (remaining_features remove best_f print ('feature {with pvalue { }format (best_f best_pval )selected_features append best_f elsebreak feature feature feature feature (uiwith pvalue - (htwith pvalue - lwt with pvalue - smoke with pvalue - in backward elimination we start with the complete model (all features includedand at each stepwe remove the variable with the highest -valueas long as it is not significant (greater than we leave it as an exercise to verify that the order in which the features are removed isageftv and ptl in this caseforward selection and backward elimination result in the same modelbut this need not be the case in general this way of model selection has the advantage of being easy to use and of treating the question of variable selection in systematic manner the main drawback is that variables are included or deleted based on purely statistical criteriawithout taking into account the aim of the study this usually leads to model which may be satisfactory from statistical point of viewbut in which the variables are not necessarily the most relevant when it comes to understanding and interpreting the data in the study of coursewe can choose to investigate any combination of featuresnot just the ones suggested by the above variable selection methods for examplelet us see if the mother' weighther ageher raceand whether she smokes explain the baby' birthweight backward elimination