<jupyter_start><jupyter_text>Make ols model with clusters as features<jupyter_code>train.dtypes
train.head()
X_train.dtypes
# Create Model
ols_model1 = ols(formula='log_error ~ area_cluster_la_newer + area_cluster_la_older + area_cluster_northwest_costal + area_cluster_palmdale_landcaster + area_cluster_santa_clarita + area_cluster_se_coast + size_cluster_a + size_cluster_b + size_cluster_c + size_cluster_d + size_cluster_e + size_cluster_f + size_cluster_g + price_cluster_a + price_cluster_b + price_cluster_c + price_cluster_d + price_cluster_e', data=train).fit()

# Make predictions
ols_yhat = ols_model1.predict(X_train)
X_train['mvp_prdictions']=ols_yhat

#  DataFrame for evaluating model 
ols_eval = y_train.copy()
validate_eval = y_validate.copy()
ols_eval = pd.DataFrame(ols_eval)
validate_eval = pd.DataFrame(validate_eval)
ols_eval.rename(columns={'logerror': 'actual'}, inplace=True)
validate_eval.rename(columns={'logerror': 'actual'}, inplace=True)
validate_eval['ols_yhat'] = ols_model1.predict(X_validate)


# Add baseline - median home value
ols_eval['baseline_yhat'] = ols_eval['actual'].mean()
validate_eval['baseline_yhat'] = ols_eval['actual'].mean()
validate_eval['ols_yhat'] = ols_model1.predict(X_validate)

#  Compute the RMSE  and R2 for  ols model and baseline 
baseline_RMSE = (sqrt(mean_squared_error(ols_eval.actual, ols_eval.baseline_yhat)))
ols_RMSE = (sqrt(mean_squared_error(ols_eval.actual, ols_eval.ols_yhat)))

pct_change=round(((ols_RMSE-baseline_RMSE)/baseline_RMSE)*100, 2)
rmse_validate = (sqrt(mean_squared_error(validate_eval.actual, validate_eval.ols_yhat)))
baseline_r2 = (r2_score(ols_eval.actual, ols_eval.baseline_yhat))
ols_train_r2 = (r2_score(ols_eval.actual, ols_eval.ols_yhat))
ols_validate_r2 = (r2_score(validate_eval.actual, validate_eval.ols_yhat))

#Output Findings

print(f'My model has value: {ols_RMSE < baseline_RMSE}')
print()
print(f'Baseline RMSE: {baseline_RMSE}')
print(f'My model train RMSE: {ols_RMSE}')
print(f'My model validate RMSE: {rmse_validate}')
print(f'RMSE difference baseline to model train: {baseline_RMSE- ols_RMSE}')
print(f'RMSE difference baseline to model validate: {baseline_RMSE- rmse_validate}')
print(f'RMSE difference train to validate: {ols_RMSE- rmse_validate}')
print(f'RMSE improvement: {pct_change}%')
print()
print(f'Baseline R2: {baseline_r2}')
print(f'Model train  R2: {ols_train_r2}')
print(f'Model Validate R2: {ols_validate_r2}')

# Add baseline - median home value
ols_eval['baseline_yhat'] = ols_eval['actual'].mean()
validate_eval['baseline_yhat'] = ols_eval['actual'].mean()

ols_eval
ols_eval

# Add model prediction
ols_eval['ols_yhat'] = ols_model1.predict(X_train)

ols_eval.head()
ols_eval<jupyter_output><empty_output><jupyter_text>-[] Wranlge need to add clusters and encoding to x_validate<jupyter_code>X_validate.head()
validate_eval['ols_yhat'] = ols_model1.predict(X_validate)<jupyter_output><empty_output><jupyter_text>Calculate Baseline RMSE<jupyter_code># Calculate and Add Residuals Column for Plotting
ols_eval['residuals'] = ols_eval.ols_yhat - ols_eval.actual
validate_eval['residuals'] = validate_eval.ols_yhat - validate_eval.actual

#  Compute the RMSE  and R2 for  ols model and baseline 
baseline_RMSE = (sqrt(mean_squared_error(ols_eval.actual, ols_eval.baseline_yhat)))
ols_RMSE = (sqrt(mean_squared_error(ols_eval.actual, ols_eval.ols_yhat)))

baseline_RMSE
ols_eval
baseline_RMSE
pct_change=round(((ols_RMSE-baseline_RMSE)/baseline_RMSE)*100, 2)
rmse_validate = (sqrt(mean_squared_error(validate_eval.actual, validate_eval.ols_yhat)))
baseline_r2 = (r2_score(ols_eval.actual, ols_eval.baseline_yhat))
ols_train_r2 = (r2_score(ols_eval.actual, ols_eval.ols_yhat))
ols_validate_r2 = (r2_score(validate_eval.actual, validate_eval.ols_yhat))

#Output Findings

print(f'My model has value: {ols_RMSE < baseline_RMSE}')
print()
print(f'Baseline RMSE: {baseline_RMSE}')
print(f'My model train RMSE: {ols_RMSE}')
print(f'My model validate RMSE: {rmse_validate}')
print(f'RMSE difference baseline to model train: {baseline_RMSE- ols_RMSE}')
print(f'RMSE difference baseline to model validate: {baseline_RMSE- rmse_validate}')
print(f'RMSE difference train to validate: {ols_RMSE- rmse_validate}')
print(f'RMSE improvement: {pct_change}%')
print()
print(f'Baseline R2: {baseline_r2}')
print(f'Model train  R2: {ols_train_r2}')
print(f'Model Validate R2: {ols_validate_r2}')
train.dtypes
# Create Model
ols_model3 = ols(formula='log_error ~ area_cluster_la_newer + area_cluster_la_older + area_cluster_northwest_costal + area_cluster_palmdale_landcaster + area_cluster_santa_clarita + area_cluster_se_coast + taxamount + taxvaluedollarcnt + structuretaxvaluedollarcnt  + landtaxvaluedollarcnt + acres + age', data=train).fit()

# Make predictions
ols3_yhat = ols_model3.predict(X_train)
ols_eval['ols3_yhat']=ols3_yhat
X_train['ols3_prdictions']=ols3_yhat

ols_eval.head()
ols_eval.head()
ols_eval
#Calculate Evaluation metrics
baseline_RMSE = (sqrt(mean_squared_error(ols_eval.actual, ols_eval.baseline_yhat)))
ols_RMSE = (sqrt(mean_squared_error(ols_eval.actual, ols_eval.ols_yhat)))
ols3_RMSE = (sqrt(mean_squared_error(ols_eval.actual, ols_eval.ols3_yhat)))
pct_change_baseline=(((ols_RMSE-baseline_RMSE)/baseline_RMSE)*100)
pct_change_last_model=(((ols3_RMSE-ols_RMSE)/ols_RMSE)*100)
#rmse_validate = (sqrt(mean_squared_error(validate_eval.actual, validate_eval.ols2_yhat)))
baseline_r2 = (r2_score(ols_eval.actual, ols_eval.baseline_yhat))
ols3_train_r2 = (r2_score(ols_eval.actual, ols_eval.ols3_yhat))
#ols2_validate_r2 = (r2_score(validate_eval.actual, validate_eval.ols2_yhat))
# Display findings
print(f'My model has value: {ols_RMSE < baseline_RMSE}')
print(f'My model beats previous model: {ols3_RMSE < ols_RMSE}')
print()
print(f'Baseline RMSE: {baseline_RMSE}')
print(f'Model 1 RMSE: {ols_RMSE}')
print(f'Current model train RMSE: {ols3_RMSE}')
#print(f'Currennt model validate RMSE: {rmse_validate}')
print()
print(f'Current model RMSE difference from baseline: {baseline_RMSE- ols_RMSE}')
#print(f'RMSE difference train to validate: {ols2_RMSE- rmse_validate}')
print(f'Current model baseline RMSE improvement: {pct_change_baseline}%')
#print(f'Current model RMSE improvement from last model: {pct_change_last_model}%')
print()
print(f'Baseline R2: {baseline_r2}')
print(f'Model train  R2: {ols3_train_r2}')
#print(f'Model Validate R2: {ols2_validate_r2}')
train.dtypes
# Create Model
ols_model2 = ols(formula='log_error ~ area_cluster_la_newer + area_cluster_la_older + area_cluster_northwest_costal + area_cluster_palmdale_landcaster + area_cluster_santa_clarita + area_cluster_se_coast + bathroomcnt + bedroomcnt + calculatedfinishedsquarefeet + acres + age', data=train).fit()

# Make predictions
ols2_yhat = ols_model2.predict(X_train)
ols_eval['ols2_yhat']=ols2_yhat
X_train['ols2_prdictions']=ols2_yhat

#Calculate Evaluation metrics
baseline_RMSE = (sqrt(mean_squared_error(ols_eval.actual, ols_eval.baseline_yhat)))
ols_RMSE = (sqrt(mean_squared_error(ols_eval.actual, ols_eval.ols_yhat)))
ols2_RMSE = (sqrt(mean_squared_error(ols_eval.actual, ols_eval.ols2_yhat)))
pct_change_baseline=(((ols_RMSE-baseline_RMSE)/baseline_RMSE)*100)
pct_change_last_model=(((ols2_RMSE-ols_RMSE)/ols_RMSE)*100)
#rmse_validate = (sqrt(mean_squared_error(validate_eval.actual, validate_eval.ols2_yhat)))
baseline_r2 = (r2_score(ols_eval.actual, ols_eval.baseline_yhat))
ols2_train_r2 = (r2_score(ols_eval.actual, ols_eval.ols2_yhat))
#ols2_validate_r2 = (r2_score(validate_eval.actual, validate_eval.ols2_yhat))
# Display findings
print(f'My model has value: {ols_RMSE < baseline_RMSE}')
print(f'My model beats previous model: {ols2_RMSE < ols_RMSE}')
print()
print(f'Baseline RMSE: {baseline_RMSE}')
print(f'Model 1 RMSE: {ols_RMSE}')
print(f'Current model train RMSE: {ols2_RMSE}')
#print(f'Current model validate RMSE: {rmse_validate}')
print()
print(f'Current model RMSE difference from baseline: {baseline_RMSE- ols_RMSE}')
#print(f'RMSE difference train to validate: {ols2_RMSE- rmse_validate}')
print(f'Current model baseline RMSE improvement: {pct_change_baseline}%')
print(f'Current model RMSE improvement from last model: {pct_change_last_model}%')
print()
print(f'Baseline R2: {baseline_r2}')
print(f'Model train  R2: {ols2_train_r2}')
#print(f'Model Validate R2: {ols2_validate_r2}')
# Create Model
ols_model4 = ols(formula='log_error ~ taxvaluedollarcnt + structuretaxvaluedollarcnt + taxvaluedollarcnt + landtaxvaluedollarcnt + acres + age', data=train).fit()

# Make predictions
ols4_yhat = ols_model4.predict(X_train)
ols_eval['ols4_yhat']=ols4_yhat
X_train['ols4_prdictions']=ols4_yhat

#Calculate Evaluation metrics
baseline_RMSE = (sqrt(mean_squared_error(ols_eval.actual, ols_eval.baseline_yhat)))
ols_RMSE = (sqrt(mean_squared_error(ols_eval.actual, ols_eval.ols_yhat)))
ols2_RMSE = (sqrt(mean_squared_error(ols_eval.actual, ols_eval.ols2_yhat)))
ols3_RMSE = (sqrt(mean_squared_error(ols_eval.actual, ols_eval.ols3_yhat)))
ols4_RMSE = (sqrt(mean_squared_error(ols_eval.actual, ols_eval.ols4_yhat)))

pct_change_baseline=(((ols4_RMSE-baseline_RMSE)/baseline_RMSE)*100)
pct_change_last_model=(((ols4_RMSE-ols_RMSE)/ols_RMSE)*100)
#rmse_validate = (sqrt(mean_squared_error(validate_eval.actual, validate_eval.ols2_yhat)))
baseline_r2 = (r2_score(ols_eval.actual, ols_eval.baseline_yhat))
ols2_train_r2 = (r2_score(ols_eval.actual, ols_eval.ols2_yhat))
ols3_train_r2 = (r2_score(ols_eval.actual, ols_eval.ols3_yhat))
ols4_train_r2 = (r2_score(ols_eval.actual, ols_eval.ols4_yhat))
#ols2_validate_r2 = (r2_score(validate_eval.actual, validate_eval.ols2_yhat))
# Display findings
print(f'My model has value: {ols_RMSE < baseline_RMSE}')
print(f'My model beats previous best model: {ols4_RMSE < ols_RMSE}')
print()
print(f'Baseline RMSE: {baseline_RMSE}')
print(f'Model 1 RMSE: {ols_RMSE}')
print(f'Current model train RMSE: {ols4_RMSE}')
#print(f'Current model validate RMSE: {rmse_validate}')
print()
print(f'Current model RMSE difference from baseline: {baseline_RMSE- ols4_RMSE}')
#print(f'RMSE difference train to validate: {ols2_RMSE- rmse_validate}')
print(f'Current model baseline RMSE improvement: {pct_change_baseline}%')
print(f'Current model RMSE improvement from last model: {pct_change_last_model}%')
print()
print(f'Baseline R2: {baseline_r2}')
print(f'Model train  R2: {ols4_train_r2}')
#print(f'Model Validate R2: {ols2_validate_r2}')
baseline_r2
# Create Model
ols_model5 = ols(formula='log_error ~ area_cluster_la_newer + area_cluster_la_older + area_cluster_northwest_costal + area_cluster_palmdale_landcaster + area_cluster_santa_clarita + area_cluster_se_coast + size_cluster_a + size_cluster_b + size_cluster_c + size_cluster_d + size_cluster_e + size_cluster_f + size_cluster_g + price_cluster_a + price_cluster_b + price_cluster_c + price_cluster_d + price_cluster_e + taxvaluedollarcnt + structuretaxvaluedollarcnt + taxvaluedollarcnt + landtaxvaluedollarcnt', data=train).fit()

# Make predictions
ols5_yhat = ols_model5.predict(X_train)
X_train['mvp_prdictions']=ols5_yhat
ols_eval['ols5_yhat']=ols5_yhat

ols5_validate_yhat = ols_model5.predict(X_validate)
X_validate['ols5_prdictions']=ols5_validate_yhat
ols_eval['ols5_yhat']=ols5_yhat

validate_eval.head()
validate_eval['ols5_yhat']=ols5_validate_yhat
ols_eval.head()
ols_eval.head()
validate_eval.head()
#Calculate Evaluation metrics
baseline_RMSE = (sqrt(mean_squared_error(ols_eval.actual, ols_eval.baseline_yhat)))
ols_RMSE = (sqrt(mean_squared_error(ols_eval.actual, ols_eval.ols_yhat)))
ols5_RMSE = (sqrt(mean_squared_error(ols_eval.actual, ols_eval.ols5_yhat)))
pct_change_baseline=(((ols5_RMSE-baseline_RMSE)/baseline_RMSE)*100)
pct_change_last_model=(((ols5_RMSE-ols_RMSE)/ols_RMSE)*100)
rmse_validate = (sqrt(mean_squared_error(validate_eval.actual, validate_eval.ols5_yhat)))
baseline_r2 = (r2_score(ols_eval.actual, ols_eval.baseline_yhat))
ols5_train_r2 = (r2_score(ols_eval.actual, ols_eval.ols5_yhat))
ols5_validate_r2 = (r2_score(validate_eval.actual, validate_eval.ols5_yhat))
# Display findings
print(f'My model has value: {ols_RMSE < baseline_RMSE}')
print(f'My model beats previous model: {ols5_RMSE < ols_RMSE}')
print()
print(f'Baseline RMSE: {baseline_RMSE}')
print(f'Model 1 RMSE: {ols_RMSE}')
print(f'Current model train RMSE: {ols5_RMSE}')
print(f'Current model validate RMSE: {rmse_validate}')
print()
print(f'Current model train RMSE difference from baseline: {baseline_RMSE- ols5_RMSE}')
print(f'Current model validate RMSE difference from baseline: {baseline_RMSE- rmse_validate}')
print()

print(f'Current model RMSE difference from best model: {ols5_RMSE- ols_RMSE}')
print()

print(f'RMSE difference train to validate: {ols2_RMSE- rmse_validate}')
print(f'Current model baseline RMSE improvement: {pct_change_baseline}%')
print(f'Current model RMSE improvement from last model: {pct_change_last_model}%')
print()
print(f'Baseline R2: {baseline_r2}')
print(f'Model train  R2: {ols3_train_r2}')
print(f'Model Validate R2: {ols5_validate_r2}')<jupyter_output>My model has value: True
My model beats previous model: True

Baseline RMSE: 0.1747252013031296
Model 1 RMSE: 0.17434797056175116
Current model train RMSE: 0.1743396579027326
Current model validate RMSE: 0.16987508165511148

Current model train RMSE difference from baseline: 0.0003855434003970093
Current model validate RMSE difference from baseline: 0.004850119648018125

Current model RMSE difference from best model: -8.312659018572255e-06

RMSE difference train to validate: 0.004575292374718848
Current model baseline RMSE improvement: -0.22065700741596664%
Current model RMSE improvement from last model: -0.004767855336536911%

Baseline R2: 0.0
Model train  R2: 0.0023658827981120067
Model Validate R2: 0.00322978970601584
{'fraction_non_alphanumeric': 0.11556363636363637, 'fraction_numerical': 0.032581818181818184, 'mean_word_length': 9.292664670658683, 'pattern_counts': {'":': 0, '<': 22, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '339135', 'license': 'MIT', 'n_tokens_mistral': 5563, 'n_tokens_neox': 5169, 'n_words': 925, 'path': 'jg_model.ipynb', 'repo_name': 'Zillow-Project-JG-DN/clustering-project-jgdn', 'size': '13750'}