<jupyter_start><jupyter_text>Exercises 1) Select a dataset with a continuous target variable.<jupyter_code>df = data('swiss')
df.head()
df.info()
df.shape
target = 'Education'<jupyter_output><empty_output><jupyter_text>2) Be sure your data is prepared (no missing values, numeric datatypes) and split into samples.<jupyter_code>from wrangle import train_validate_test_split
X_train, y_train, X_validate, y_validate, X_test, y_test = train_validate_test_split(df, target)
X_train.head(20)<jupyter_output><empty_output><jupyter_text>3) Work through all of the steps outlined in the lesson, from setting the baseline to selected a model and evaluating the final model on your test data.  OLS Model<jupyter_code># We need y_train and y_validate to be dataframes to append the new columns with predicted values. 
y_train = pd.DataFrame(y_train)
y_validate = pd.DataFrame(y_validate)

# 1. Predict Education_pred_mean
Education_pred_mean = y_train['Education'].mean()
y_train['Education_pred_mean'] = Education_pred_mean
y_validate['Education_pred_mean'] = Education_pred_mean

# 2. compute Education_pred_median
Education_pred_median = y_train['Education'].median()
y_train['Education_pred_median'] = Education_pred_median
y_validate['Education_pred_median'] = Education_pred_median

# 3. RMSE of Education_pred_mean
rmse_train = mean_squared_error(y_train.Education, y_train.Education_pred_mean)**1/2
rmse_validate = mean_squared_error(y_validate.Education, y_validate.Education_pred_mean)**1/2

print("RMSE using Mean\nTrain/In-Sample: ", round(rmse_train, 2), 
      "\nValidate/Out-of-Sample: ", round(rmse_validate, 2))

# 4. RMSE of Education_pred_median
rmse_train = mean_squared_error(y_train.Education, y_train.Education_pred_median)**1/2
rmse_validate = mean_squared_error(y_validate.Education, y_validate.Education_pred_median)**1/2

print("RMSE using Median\nTrain/In-Sample: ", round(rmse_train, 2), 
      "\nValidate/Out-of-Sample: ", round(rmse_validate, 2))

# plot to visualize actual vs predicted. 
plt.hist(y_train.Education, color='blue', alpha=.5, label="Actual Education", bins = 20)
plt.hist(y_train.Education_pred_mean, bins=1, color='red', alpha=.5, rwidth=100, label="Predicted Education - Mean")
plt.hist(y_train.Education_pred_median, bins=1, color='orange', alpha=.5, rwidth=100, label="Predicted Education - Median")
plt.xlabel("% education beyond primary school")
plt.ylabel("number of French-speaking provinces")
plt.legend()
plt.show()
# create the model object
lm = LinearRegression(normalize=True)

# fit the model to our training data. We must specify the column in y_train, 
# since we have converted it to a dataframe from a series! 
lm.fit(X_train, y_train.Education)

# predict train
y_train['Education_pred_lm'] = lm.predict(X_train)

# evaluate: rmse
rmse_train = mean_squared_error(y_train.Education, y_train.Education_pred_lm)**1/2

# predict validate
y_validate['Education_pred_lm'] = lm.predict(X_validate)

# evaluate: rmse
rmse_validate = mean_squared_error(y_validate.Education, y_validate.Education_pred_lm)**1/2

print("RMSE for OLS using LinearRegression\nTraining/In-Sample: ", rmse_train, 
      "\nValidation/Out-of-Sample: ", rmse_validate)<jupyter_output>RMSE for OLS using LinearRegression
Training/In-Sample:  6.4754581875207755 
Validation/Out-of-Sample:  23.784802831710376<jupyter_text>LassoLars<jupyter_code># create the model object
lars = LassoLars(alpha=1.0)

# fit the model to our training data. We must specify the column in y_train, 
# since we have converted it to a dataframe from a series! 
lars.fit(X_train, y_train.Education)

# predict train
y_train['Education_pred_lars'] = lars.predict(X_train)

# evaluate: rmse
rmse_train = mean_squared_error(y_train.Education, y_train.Education_pred_lars)**1/2

# predict validate
y_validate['Education_pred_lars'] = lars.predict(X_validate)

# evaluate: rmse
rmse_validate = mean_squared_error(y_validate.Education, y_validate.Education_pred_lars)**1/2

print("RMSE for Lasso + Lars\nTraining/In-Sample: ", rmse_train, 
      "\nValidation/Out-of-Sample: ", rmse_validate)<jupyter_output>RMSE for Lasso + Lars
Training/In-Sample:  20.500799999999998 
Validation/Out-of-Sample:  53.64253333333332<jupyter_text>TweedieRegressor (GLM)<jupyter_code># create the model object
glm = TweedieRegressor(power=1, alpha=0)

# fit the model to our training data. We must specify the column in y_train, 
# since we have converted it to a dataframe from a series! 
glm.fit(X_train, y_train.Education)

# predict train
y_train['Education_pred_glm'] = glm.predict(X_train)

# evaluate: rmse
rmse_train = mean_squared_error(y_train.Education, y_train.Education_pred_glm)**1/2

# predict validate
y_validate['Education_pred_glm'] = glm.predict(X_validate)

# evaluate: rmse
rmse_validate = mean_squared_error(y_validate.Education, y_validate.Education_pred_glm)**1/2

print("RMSE for GLM using Tweedie, power=1 & alpha=0\nTraining/In-Sample: ", rmse_train, 
      "\nValidation/Out-of-Sample: ", rmse_validate)<jupyter_output>RMSE for GLM using Tweedie, power=1 & alpha=0
Training/In-Sample:  5.795364632256172 
Validation/Out-of-Sample:  27.362263848895527<jupyter_text>Polynomial Regression<jupyter_code># make the polynomial features to get a new set of features
pf = PolynomialFeatures(degree=2)

# fit and transform X_train_scaled
X_train_degree2 = pf.fit_transform(X_train)

# transform X_validate_scaled & X_test_scaled
X_validate_degree2 = pf.transform(X_validate)
X_test_degree2 = pf.transform(X_test)<jupyter_output><empty_output><jupyter_text>LinearRegression<jupyter_code># create the model object
lm2 = LinearRegression(normalize=True)

# fit the model to our training data. We must specify the column in y_train, 
# since we have converted it to a dataframe from a series! 
lm2.fit(X_train_degree2, y_train.Education)

# predict train
y_train['Education_pred_lm2'] = lm2.predict(X_train_degree2)

# evaluate: rmse
rmse_train = mean_squared_error(y_train.Education, y_train.Education_pred_lm2)**1/2

# predict validate
y_validate['Education_pred_lm2'] = lm2.predict(X_validate_degree2)

# evaluate: rmse
rmse_validate = mean_squared_error(y_validate.Education, y_validate.Education_pred_lm2)**1/2

print("RMSE for Polynomial Model, degrees=2\nTraining/In-Sample: ", rmse_train, 
      "\nValidation/Out-of-Sample: ", rmse_validate)<jupyter_output>RMSE for Polynomial Model, degrees=2
Training/In-Sample:  0.3529139611801625 
Validation/Out-of-Sample:  82.26378895254446<jupyter_text>Evaluate<jupyter_code># y_validate.head()
plt.figure(figsize=(16,8))
plt.plot(y_validate.Education, y_validate.Education_pred_mean, alpha=.5, color="gray", label='_nolegend_')
plt.annotate("Baseline: Predict Using Mean", (16, 9.5))
plt.plot(y_validate.Education, y_validate.Education, alpha=.5, color="blue", label='_nolegend_')
plt.annotate("The Ideal Line: Predicted = Actual", (.5, 3.5), rotation=15.5)

plt.scatter(y_validate.Education, y_validate.Education_pred_lm, 
            alpha=.5, color="red", s=100, label="Model: LinearRegression")
plt.scatter(y_validate.Education, y_validate.Education_pred_glm, 
            alpha=.5, color="yellow", s=100, label="Model: TweedieRegressor")
plt.scatter(y_validate.Education, y_validate.Education_pred_lm2, 
            alpha=.5, color="green", s=100, label="Model 2nd degree Polynomial")
plt.legend()
plt.xlabel("Actual Final Grade")
plt.ylabel("Predicted Final Grade")
plt.title("Where are predictions more extreme? More modest?")
# plt.annotate("The polynomial model appears to overreact to noise", (2.0, -10))
# plt.annotate("The OLS model (LinearRegression)\n appears to be most consistent", (15.5, 3))
plt.show()<jupyter_output><empty_output><jupyter_text>Residuals Plot<jupyter_code># y_validate.head()
plt.figure(figsize=(16,8))
plt.axhline(label="No Error")
plt.scatter(y_validate.Education, y_validate.Education_pred_lm-y_validate.Education, 
            alpha=.5, color="red", s=100, label="Model: LinearRegression")
plt.scatter(y_validate.Education, y_validate.Education_pred_glm-y_validate.Education, 
            alpha=.5, color="yellow", s=100, label="Model: TweedieRegressor")
plt.scatter(y_validate.Education, y_validate.Education_pred_lm2-y_validate.Education, 
            alpha=.5, color="green", s=100, label="Model 2nd degree Polynomial")
plt.legend()
plt.xlabel("Actual Final Grade")
plt.ylabel("Residual/Error: Predicted Grade - Actual Grade")
plt.title("Do the size of errors change as the actual value changes?")
plt.annotate("The polynomial model appears to overreact to noise", (2.0, -10))
plt.annotate("The OLS model (LinearRegression)\n appears to be most consistent", (15.5, 3))
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11462769018298999, 'fraction_numerical': 0.031073771435147887, 'mean_word_length': 6.569686411149826, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '621445', 'license': 'MIT', 'n_tokens_mistral': 3370, 'n_tokens_neox': 2976, 'n_words': 640, 'path': 'modeling_skeleton_exercises.ipynb', 'repo_name': 'ljackson707/Regression', 'size': '8689'}