Chapter 14 worked answers: Preparing for Machine Learning and Deep Learning
On this page
Compare your reasoning after an attempt. Different valid implementations are welcome when they preserve the observation set and the stated meaning.
B2-14-E01 · Evaluate a numeric baseline
Calculate MAE and RMSE in minutes and verify one error by hand.
Absolute errors are 2,4,7; squared errors are 4,16,49. Their means give the reported measures.
actual=np.array([9.,15.,18.]);prediction=np.array([11.,11.,11.])
mae=np.abs(actual-prediction).mean();rmse=np.sqrt(((actual-prediction)**2).mean())
assert np.isclose(mae,13/3) and np.isclose(rmse,np.sqrt(23))
print(mae,rmse)4.333333333333333 4.795831523312719
B2-14-E02 · Reject preprocessing leakage
Learn the centering value from training only and transform evaluation with it.
The evaluation distribution must not determine the learned center used to assess the procedure.
train=np.array([10.,20.]);evaluation=np.array([100.,200.])
center=train.mean();transformed=evaluation-center
assert center==15 and transformed.tolist()==[85,185]
print(center,transformed)15.0 [ 85. 185.]
B2-14-E03 · Align targets by ID
Construct y in the feature-row order and verify [2,1].
Equal lengths are insufficient; IDs establish the correspondence.
features=pd.DataFrame({"id":["b","a"],"x":[20,10]}).set_index("id")
target=pd.Series([1,2],index=["a","b"])
y=target.reindex(features.index).to_numpy()
assert y.tolist()==[2,1]
print(y)[2 1]
Return to practice
Change one supplied input, predict the effect and rerun your own implementation. Return to the exercise prompts to identify a task you can now explain independently.