# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     name: python3
# ---

# + id="_9_yT1GKE3l_"
# %%capture
# If you're in Colab...
import sys
if 'google.colab' in sys.modules:
    # !pip install category_encoders==2.*

# + colab={"base_uri": "https://localhost:8080/"} id="kie7lxuAFCye" outputId="9390df6e-66db-48e9-baf4-a90d41850c8f"
#: Import data into DataFrame [df].
import pandas as pd

url_train = 'https://drive.google.com/uc?export=download&id=1tmYLQ1RwIgjI_d66PWnlXU-5D_VxtiQc'
df = pd.read_csv(url_train)
df = pd.read_csv(url_train, parse_dates=['Yr_Sold'], index_col='Yr_Sold')
### END DATA IMPORT

#Split single `df` into `X` and `y` [THE FEATURE MATRIX, AND THE TARGET... VECTOR?].

target = 'SalePrice'

y = df[target]
X = df.drop(columns=target)
### END SPLITTING

#Now - Split the `X` and `y` already sply into training, validation, and testing sets - Split single `df` into `X` and `y` based on the Year column.

mask = X.index.year  < 2009
X_train, y_train = X.loc[mask], y.loc[mask]
X_val, y_val = X.loc[~mask], y.loc[~mask]
### END TRAINING/VALIDATION/TEST SPLIT

### Calculate the baseline mean absolute error from the y_pred:

from sklearn.metrics import mean_absolute_error

#THIS IS ALSO WHERE THE y_pred is created: Look at other modules for different ways of doing this:
y_pred = [y_train.mean()] * len(y_train)
baseline_mae = mean_absolute_error(y_train, y_pred)
### END creation of the (OPTIONAL): ##print('Baseline MAE:', baseline_mae)


### IMPORT THE PIECES NEEDED FOR MAKING A PIPELINE FOR THE RIDGE REGRESSION
from sklearn.pipeline import make_pipeline
from category_encoders import OneHotEncoder
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import Ridge

### MAKE A PIPELINE TO Build and train A RIDGE Regression model.
model_r = make_pipeline(
    OneHotEncoder(use_cat_names=True),
    StandardScaler(), 
    Ridge(alpha=20, normalize=False)
)
model_r.fit(X_train, y_train);
### RIDGE REGRESSION MODEL IS ABOVE

### Compare the TRAINING AND VALIDATION Model Metrics / MAE.

model_r_training_mae = mean_absolute_error(y_train, model_r.predict(X_train))
model_r_validation_mae = mean_absolute_error(y_val, model_r.predict(X_val))

model_r_r2_score = model_r.score(X_val, y_val)
###OPTIONAL TO print('Linear Regression R^2:', model_lr_r2_score)

# + colab={"base_uri": "https://localhost:8080/"} id="0gQRrrWDFR0k" outputId="c605a042-dfb6-49c9-eebc-08cf29657658"
### GENERATE THE ARRAY OF PREDICTIONS = y_pred, for the test set below:
###Note: THIS IS IMPORTED AND CLEANED IN THE SAME WAY AS THE TRAINING/VAL SET.

url_test = 'https://drive.google.com/uc?export=download&id=1y9u8cOWprTjruw8E-ct1c7YaEytToqd_'
X_test = pd.read_csv(url_test)

X_test = pd.read_csv(url_test, parse_dates=['Yr_Sold'], index_col='Yr_Sold')
y_pred = model_r.predict(X_test)
print('My predictions:', y_pred[:3])


# + id="_--tmXmKHung"


StarcoderdataJupyterScriptsDedupFiltered