Python Stepwise Regression to Predict Column Value

Viewed 30

I have a data frame 'df'. For the 'Purchase' column, 0 = no purchase and 1 = yes purchase.

I am trying to develop a stepwise regression model to predict 'Spending' using the filtered datasets (Purchase = 1). Is there a way to do it? Also, my output shows 0 errors in regression statistics, but I predict there should be some errors. Is there something that I am doing wrong?

Dataframe: df

sequence_number US  source_a    source_c    source_b    source_d    source_e    source_m    source_o    source_h    ... source_x    source_w    Freq    last_update_days_ago    1st_update_days_ago Web order   Gender=male Address_is_res  Purchase    Spending
0   1   1   0   0   1   0   0   0   0   0   ... 0   0   2   3662    3662    1   0   1   1   128
1   2   1   0   0   0   0   1   0   0   0   ... 0   0   0   2900    2900    1   1   0   0   0
2   3   1   0   0   0   0   0   0   0   0   ... 0   0   2   3883    3914    0   0   0   1   127
3   4   1   0   1   0   0   0   0   0   0   ... 0   0   1   829 829 0   1   0   0   0
4   5   1   0   1   0   0   0   0   0   0   ... 0   0   1   869 869 0   0   0   0   0

My code:

# Partition the data: Training set (800 records), validation set (700 records), test set (500 records)
X = pd.DataFrame(df)
y = df['Purchase']
X_train, X_rem, y_train, y_rem = train_test_split(X, y, train_size = 0.4)
X_valid, X_test, y_valid, y_test = train_test_split(X_rem, y_rem, test_size = 500/1200)

# Filtered datasets: Subsets of the training and validation sets filtering for Purchase = 1
X_train_purchases = X_train[(X_train['Purchase'] == 1)]
X_valid_purchases = X_valid[(X_valid['Purchase'] == 1)]

# Multiple linear regression (Stepwise regression)
def train_model(variables):
    if len(variables) == 0:
        return None
        model = LinearRegression()
        model.fit(X_train[variables], y_train)
        return model

def score_model(model, variables):
    if len(variables) == 0:
        return AIC_score(y_train, [y_train.mean()] * len(y_train), model, df = 1)
        return AIC_score(y_train, model.predict(X_train[variables]), model)
    best_model, best_variables = stepwise_selection(X_train_purchases.columns, train_model, score_model, verbose = True)
    print(best_variables)

# Evalation of training performance
regressionSummary(y_train, best_model.predict(X_train[best_variables]))

# Evalation of validation performance
regressionSummary(y_valid, best_model.predict(X_valid[best_variables]))

Output:

Regression statistics

               Mean Error (ME) : -0.0000
Root Mean Squared Error (RMSE) : 0.0000
     Mean Absolute Error (MAE) : 0.0000

Regression statistics

               Mean Error (ME) : -0.0000
Root Mean Squared Error (RMSE) : 0.0000
     Mean Absolute Error (MAE) : 0.0000
0 Answers
Related