Skip to content

Commit 66b6f68

Browse files
fit consistency in reg1
1 parent 18b984f commit 66b6f68

File tree

1 file changed

+15
-17
lines changed

1 file changed

+15
-17
lines changed

source/regression1.md

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -603,13 +603,13 @@ and rename the parameter column to be more readable.
603603

604604
```{code-cell} ipython3
605605
# fit the GridSearchCV object
606-
sacr_fit = sacr_gridsearch.fit(
606+
sacr_gridsearch.fit(
607607
sacramento_train[["sqft"]], # A single-column data frame
608608
sacramento_train["price"] # A series
609609
)
610610
611611
# Retrieve the CV scores
612-
sacr_results = pd.DataFrame(sacr_fit.cv_results_)[[
612+
sacr_results = pd.DataFrame(sacr_gridsearch.cv_results_)[[
613613
"param_kneighborsregressor__n_neighbors",
614614
"mean_test_score",
615615
"std_test_score"
@@ -689,7 +689,7 @@ Note that it is still useful to visualize the results as we did above
689689
since this provides additional information on how the model performance varies.
690690

691691
```{code-cell} ipython3
692-
sacr_fit.best_params_
692+
sacr_gridsearch.best_params_
693693
```
694694

695695
+++
@@ -835,7 +835,7 @@ model uses a different default scoring metric than the RMSPE.
835835
from sklearn.metrics import mean_squared_error
836836
837837
sacr_preds = sacramento_test.assign(
838-
predicted = sacr_fit.predict(sacramento_test)
838+
predicted = sacr_gridsearch.predict(sacramento_test)
839839
)
840840
RMSPE = mean_squared_error(
841841
y_true = sacr_preds["price"],
@@ -891,7 +891,7 @@ sqft_prediction_grid = pd.DataFrame({
891891
})
892892
# Predict the price for each of the sqft values in the grid
893893
sacr_preds = sqft_prediction_grid.assign(
894-
predicted = sacr_fit.predict(sqft_prediction_grid)
894+
predicted = sacr_gridsearch.predict(sqft_prediction_grid)
895895
)
896896
897897
# Plot all the houses
@@ -1012,18 +1012,19 @@ param_grid = {
10121012
"kneighborsregressor__n_neighbors": range(1, 50),
10131013
}
10141014
1015-
sacr_fit = GridSearchCV(
1015+
sacr_gridsearch = GridSearchCV(
10161016
estimator=sacr_pipeline,
10171017
param_grid=param_grid,
10181018
cv=5,
10191019
scoring="neg_root_mean_squared_error"
1020-
).fit(
1021-
sacramento_train[["sqft", "beds"]],
1022-
sacramento_train["price"]
1023-
)
1020+
)
1021+
sacr_gridsearch.fit(
1022+
sacramento_train[["sqft", "beds"]],
1023+
sacramento_train["price"]
1024+
)
10241025
10251026
# retrieve the CV scores
1026-
sacr_results = pd.DataFrame(sacr_fit.cv_results_)[[
1027+
sacr_results = pd.DataFrame(sacr_gridsearch.cv_results_)[[
10271028
"param_kneighborsregressor__n_neighbors",
10281029
"mean_test_score",
10291030
"std_test_score"
@@ -1035,13 +1036,10 @@ sacr_results = (
10351036
.rename(columns={"param_kneighborsregressor__n_neighbors" : "n_neighbors"})
10361037
.drop(columns=["std_test_score"])
10371038
)
1038-
10391039
sacr_results["mean_test_score"] = -sacr_results["mean_test_score"]
10401040
10411041
# show only the row of minimum RMSPE
1042-
sacr_results[
1043-
sacr_results["mean_test_score"] == sacr_results["mean_test_score"].min()
1044-
]
1042+
sacr_results.nsmallest(1, "mean_test_score")
10451043
```
10461044

10471045
```{code-cell} ipython3
@@ -1072,7 +1070,7 @@ to compute the RMSPE.
10721070

10731071
```{code-cell} ipython3
10741072
sacr_preds = sacramento_test.assign(
1075-
predicted = sacr_fit.predict(sacramento_test)
1073+
predicted = sacr_gridsearch.predict(sacramento_test)
10761074
)
10771075
RMSPE_mult = mean_squared_error(
10781076
y_true = sacr_preds["price"],
@@ -1109,7 +1107,7 @@ xygrid = np.array(np.meshgrid(xvals, yvals)).reshape(2, -1).T
11091107
xygrid = pd.DataFrame(xygrid, columns=["sqft", "beds"])
11101108
11111109
# add prediction
1112-
knnPredGrid = sacr_fit.predict(xygrid)
1110+
knnPredGrid = sacr_gridsearch.predict(xygrid)
11131111
11141112
fig = px.scatter_3d(
11151113
sacramento_train,

0 commit comments

Comments
 (0)