<jupyter_start><jupyter_text>Live Coding 1: Simple Prediction Notebook Propulsion Academy, 2022            Goal: Revision on a simple prediction model using Scikit Learn         Outline:                             Set up             Data Exploration and Cleaning             Modeling             Model Evaluation             Conclusion                Topics Trained: Notebook Layout, Data Cleaning, Modelling and Model Evaluation             Module 7, Machine Learning Engineering |        Day 1, Data Science Project Development |         Live Coding 1, Simple Prediction Notebook  [Introduction](P0) This Notebook is a minimal example of a regression experiment on the California Housing Dataset. It is inspired from the exercise from day 2 of the [Machine Learning Module](https://monolith.propulsion-home.ch/backend/api/momentum/materials/ds-materials/04_MachineLearning/day2/pages/07_exercises.html).The modeling and data cleaning are very simple, so that you can focus on MLOps concepts  [Set up](P0)  Packages<jupyter_code>import numpy as np
import pandas as pd

from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LinearRegression
from sklearn import metrics
from sklearn.pipeline import Pipeline<jupyter_output><empty_output><jupyter_text>User Dependent Variables<jupyter_code>data_path = "../data/raw/california_housing_0.csv"<jupyter_output><empty_output><jupyter_text>[Data Preparation](P0)<jupyter_code>data = pd.read_csv(data_path)<jupyter_output><empty_output><jupyter_text>Data Exploration<jupyter_code>data.head()
data.shape
data.isna().sum()<jupyter_output><empty_output><jupyter_text>Data Cleaning<jupyter_code>data = data.drop(columns="total_bedrooms")<jupyter_output><empty_output><jupyter_text>Train-Test Split<jupyter_code>data_train, data_test = train_test_split(data, test_size=0.33, random_state=0)
data_train.shape, data_test.shape
# # Select X and y values (predictor and outcome)
X_train = data_train.drop(columns="median_house_value")
y_train = data_train["median_house_value"]
X_test = data_test.drop(columns="median_house_value")
y_test = data_test["median_house_value"]
X_train.shape, X_test.shape<jupyter_output><empty_output><jupyter_text>[Modelling](P0)  Pipeline Definition<jupyter_code>sc = StandardScaler()
lin_reg = LinearRegression()
pipeline_mlr = Pipeline([("data_scaling", sc), ("estimator", lin_reg)])<jupyter_output><empty_output><jupyter_text>Model Fit<jupyter_code>pipeline_mlr.fit(X_train, y_train)<jupyter_output><empty_output><jupyter_text>[Model Evaluation](P0)<jupyter_code>predictions_mlr = pipeline_mlr.predict(X_test)
# Test score
pipeline_mlr.score(X_test, y_test)
print("MAE", metrics.mean_absolute_error(y_test, predictions_mlr))
print("MSE", metrics.mean_squared_error(y_test, predictions_mlr))
print("RMSE", np.sqrt(metrics.mean_squared_error(y_test, predictions_mlr)))
print("Explained Var Score", metrics.explained_variance_score(y_test, predictions_mlr))<jupyter_output>MAE 46722.86268024486
MSE 4155629700.8375745
RMSE 64464.17377766952
Explained Var Score 0.6304406174521564
{'fraction_non_alphanumeric': 0.09090909090909091, 'fraction_numerical': 0.02880921895006402, 'mean_word_length': 6.422802850356295, 'pattern_counts': {'":': 0, '<': 36, '<?xml version=': 0, '>': 36, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 3, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '216838', 'license': 'FTL', 'n_tokens_mistral': 1086, 'n_tokens_neox': 998, 'n_words': 208, 'path': 'notebooks/MLE_D1_LC1_California_Prediction.ipynb', 'repo_name': 'miraculixx/california-housing-prediction', 'size': '3124'}