Question: Code debug help # your code here allowed_factors = ['Age', 'Weight', 'Height', 'Neck', 'Chest', 'Abdomen', 'Hip', 'Thigh', 'Knee', 'Ankle', 'Biceps', 'Forearm','Wrist'] # Initialize the

Code debug help

 


# your code here
allowed_factors = ['Age', 'Weight', 'Height', 'Neck', 'Chest', 'Abdomen', 'Hip', 'Thigh', 'Knee', 'Ankle', 'Biceps', 'Forearm','Wrist']
# Initialize the model with no predictors
best =  ['', 0]

class Model_Null:
    def __init__(self, response):
        self.endog_names = response
        self.exog_names = ["Intercept"]

class Model_Null_Regression:
    def __init__(self, response):
        self.model = Model_Null(response)
        self.rsq_adjusted = 0
       
def generate_formula(last_model, next_feature):
    formula = last_model.endog_names + "~"
    for i in range(len(last_model.exog_names)-1):
        formula = formula + last_model.exog_names[i+1] + "+"
    formula = formula + next_feature
    return formula

def forward_stepwise_selection(last_model, allowed_factors, df):
    current_best = (None, None, 0)
    remaining_features = [feature for feature in allowed_factors if feature not in last_model.model.exog_names[1:]]
   
    for feature in remaining_features:
        model = smf.ols(generate_formula(last_model.model, feature), data=df).fit(disp=0)
        if model.rsquared_adj > current_best[2]:
            current_best = (feature, model, model.rsquared_adj)
    return current_best


train_bmi1 = forward_stepwise_selection(Model_Null_Regression('Density'), allowed_factors, train_fat)[1]

train_bmi2 = forward_stepwise_selection(train_bmi1, allowed_factors, train_fat)[1]
train_bmi3 = forward_stepwise_selection(train_bmi2, allowed_factors, train_fat)[1]
train_bmi4 = forward_stepwise_selection(train_bmi3, allowed_factors, train_fat)[1]
train_bmi5 = forward_stepwise_selection(train_bmi4, allowed_factors, train_fat)[1]

3i. Plot [5 pts]
Plot your resulting  adjusted ????2
  vs number of predictors (k=1,2,3,4,5) and overlay the  adjusted ????2
  for the test data. Call the list of the five adjusted r-squared values from the five train_bmi# models as adjr2_train and the one from the test data as adjr2_test.# plot resulting adjusted rsquared vs number of predictors (k=1,2,3,4,5)


# Adjusted R^2 Formula = 1- [(1-R^2)*(n-1)/(n-k-1)]; R^2 sample R-squared, N: Total Sample Size, p: # of Independent variables

# When deep=True, data is copied but actual Python objects will not be copied recursively, only the reference to the object.
#This is in contrast to copy.deepcopy in the Standard Library, which recursively copies object data.
test_df = df.copy(deep=True)
#test_df.columns.drop("mpg")


def calculate_adjusted_rsq(test_df, linear_model):
   
    response = linear_model.model.endog_names
    features = linear_model.model.exog_names[1:]
   
   
    y_test = test_df[response] # response = y
   
    yhat_test = linear_model.predict(test_df[features]) # y_hat = predicted value of y
   
    residual_squared_test = (y_test - yhat_test)**2
   
    RSS_test = residual_squared_test.sum() # Residual Sum of Squares RSS
   
    mean_test = y_test.mean() # y_bar aka mean
   
    deviance_squared_test = (y_test-mean_test)**2
   
    TSS_test = deviance_squared_test.sum() # Total Sum of Squares TSS
   
    # R^2 = 1-RSS/TSS
    Rsq_test = 1- (RSS_test/TSS_test)
   
    n= len(y_test) # total sample size
    k= len(features)
   
    adjustedRsq_test = 1-[(1-Rsq_test)*(n-1)/(n-k-1)]
   
    return adjustedRsq_test
   
k = [1, 2, 3, 4, 5]
k1 = forward_stepwise_selection(Model_Null_Regression("Density"), allowed_factors, train_fat)
k2 = forward_stepwise_selection(train_bmi1, allowed_factors, train_fat)
k3 = forward_stepwise_selection(train_bmi2, allowed_factors, train_fat)
k4 = forward_stepwise_selection(train_bmi3, allowed_factors, train_fat)
k5 = forward_stepwise_selection(train_bmi4, allowed_factors, train_fat)

adjr2_train = [k1[2], k2[2], k3[2], k4[2], k5[2]]

#set up an initial value for adjr2_test
adjr2_test = []

adjr2_test.append(calculate_adjusted_rsq(test_df, train_bmi1))
adjr2_test.append(calculate_adjusted_rsq(test_df, train_bmi2))
adjr2_test.append(calculate_adjusted_rsq(test_df, train_bmi3))
adjr2_test.append(calculate_adjusted_rsq(test_df, train_bmi4))
adjr2_test.append(calculate_adjusted_rsq(test_df, train_bmi5))
   
print(adjr2_train)
print(adjr2_test)
   
 

I am trying to code above code, but encountered an error, please help me debug.

Traceback (most recent call last) /opt/conda/lib/python3.7/site-packages/pandas/core/indexes/base.py in get_loc(self, key, method, tolerance) KeyError

Traceback (most recent call last) /opt/conda/lib/python3.7/site-packages/pandas/core/indexes/base.py in get_loc(self, key, method, tolerance) KeyError 2645 -> 2646 2647 return self._engine.get_loc (key) except KeyError: pandas/_libs/index.pyx in pandas._libs.index. IndexEngine.get_loc() pandas/_libs/index.pyx in pandas._libs.index. IndexEngine.get_loc() pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyobjectHashTable.get_item() pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyobjectHashTable.get_item() KeyError: 'Density' During handling of the above exception, another exception occurred: KeyError try: in 55 adjr2_test = [] 56 57 adjr2_test.append(calculate_adjusted_rsq(test_df, train_bmi1)) 58 adjr2_test.append(calculate_adjusted_rsq (test_df, train_bmi2)) 59 adjr2_test.append(calculate_adjusted_rsq(test_df, train_bmi3)) in calculate_adjusted_rsq(test_df, linear_model) 19 20 ---> 21 y_test= test_df [response] # response = y 22 23 yhat_test = linear_model.predict(test_df [features]) # y_hat = predicted value of y 2798 2799 -> 2800 /opt/conda/lib/python3.7/site-packages/pandas/core/frame.py in getitem_(self, key) if self.columns.nlevels > 1: return self._getitem_multilevel (key) indexer self.columns.get_loc(key) if is integer (indexer): indexer = [indexer] /opt/conda/lib/python3.7/site-packages/pandas/core/indexes/base.py in get_loc(self, key, method, tolerance) 2801 2802 Traceback (most recent call last) 2646 2647 -> 2648 2649 2650 return self._engine.get_loc (key) except KeyError: return self._engine.get_loc(self._maybe_cast_indexer (key)) indexer = self.get_indexer ([key], method-method, tolerance-tolerance) if indexer.ndim > 1 or indexer.size > 1: pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc() pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc() KeyError: 'Density' pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyobjectHashTable.get_item() pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable. PyobjectHashTable.get_item()

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!