# Stub of the Trees Lab page: the exercises, without the solutions. Solutions: https://morrison-lab.github.io/lds/chapters/trees-lab.html

# Exercise 1
# #### Lab: the scale of sales
#
# Use the [`Carseats` data](#dat-carseats).
# What are the [sample mean](https://morrison-lab.github.io/sds/exploratory-descriptive.html#def-sample-mean) and [sample standard deviation](https://morrison-lab.github.io/sds/exploratory-descriptive.html#def-sample-sd) of `Sales`?
#
# ::: {.callout-tip}
#
# #### Hint
#
# [Read the file](https://morrison-lab.github.io/lds/chapters/programming-primer.html#exm-prog-pandas) into a table,
# then apply a summary function to one column.
#
# :::



# Exercise 2
# #### Lab: stores by shelving location
#
# Use the [`Carseats` data](#dat-carseats).
# How many stores have each value of `ShelveLoc`?
#
# ::: {.callout-tip}
#
# #### Hint
#
# [Count the rows](https://morrison-lab.github.io/lds/chapters/programming-primer.html#exm-prog-pandas) with each value of a column using a pandas method in Python, or `count()` in R.
#
# :::



# Exercise 3
# #### Lab: a histogram of sales
#
# Use the [`Carseats` data](#dat-carseats).
# Draw a histogram of `Sales`.
#
# ::: {.callout-tip}
#
# #### Hint
#
# [Draw a histogram](https://morrison-lab.github.io/lds/chapters/programming-primer.html#exm-prog-plots) of one column, as in the histogram of the year of each car in the primer.
#
# :::



# Exercise 4
# #### Lab: sales against price
#
# Use the [`Carseats` data](#dat-carseats).
# Draw a scatterplot of `Sales` against `Price`.
#
# ::: {.callout-tip}
#
# #### Hint
#
# [Draw a scatterplot](https://morrison-lab.github.io/lds/chapters/programming-primer.html#exm-prog-plots) of one column against another, as in the plot of acceleration against horsepower in the primer.
#
# :::



# Exercise 5
# #### Lab: define a high-sales indicator
#
# Read the Carseats data from [`data/carseats.csv`](https://morrison-lab.github.io/lds/chapters/data/carseats.csv),
# which are simulated sales of child car seats at $400$ stores.
# Add a variable `High` that is `"Yes"` for a store whose `Sales` (in thousands of units) exceed $8$ and `"No"` otherwise.
#
# ::: {.callout-tip}
#
# #### Hint
#
# The [data frame functions](https://morrison-lab.github.io/lds/chapters/programming-primer.html#exm-prog-pandas) read the file,
# and [`.assign`](https://morrison-lab.github.io/lds/chapters/programming-primer.html#exm-prog-pandas-more) adds a column computed from another one.
# In R, `mutate()` does the same.
#
# :::



# Exercise 6
# #### Lab: split the Carseats data
#
# Split the Carseats stores from Exercise 5 at random into a [training set](https://morrison-lab.github.io/lds/chapters/big-ideas.html#def-training-set) and a [test set](https://morrison-lab.github.io/lds/chapters/big-ideas.html#def-test-set) of equal size,
# using every variable except `Sales` as a [feature](https://morrison-lab.github.io/lds/chapters/anatomy.html#def-feature) of `High`.
#
# ::: {.callout-tip}
#
# #### Hint
#
# The [random number functions](https://morrison-lab.github.io/lds/chapters/programming-primer.html#exm-prog-random) draw row positions without repeats,
# and a [selection by position](https://morrison-lab.github.io/lds/chapters/programming-primer.html#exm-prog-pandas) keeps those rows.
#
# :::



# Exercise 7
# #### Lab: fit a classification tree
#
# [Fit](https://morrison-lab.github.io/lds/chapters/big-ideas.html#def-fitted-model) a [classification](https://morrison-lab.github.io/lds/chapters/anatomy.html#def-classification) tree of `High` on the other variables in the [training set](https://morrison-lab.github.io/lds/chapters/big-ideas.html#def-training-set) from Exercise 6,
# with at most $5$ levels of splits.
#
# ::: {.callout-tip}
#
# #### Hint
#
# Every [estimator](https://morrison-lab.github.io/sds/estimation.html#def-estimator) in [scikit-learn](https://morrison-lab.github.io/lds/chapters/programming-primer.html#exm-prog-sklearn) is [fit](https://morrison-lab.github.io/lds/chapters/big-ideas.html#def-fitted-model) with `.fit` on a [matrix](https://morrison-lab.github.io/mds/linear-algebra-matrices.html#def-matrix) of [features](https://morrison-lab.github.io/lds/chapters/anatomy.html#def-feature) and a vector of [outcomes](https://morrison-lab.github.io/lds/chapters/anatomy.html#def-output-variable),
# so a column of text must first be [recoded to indicator columns](https://morrison-lab.github.io/lds/chapters/programming-primer.html#exm-prog-pandas-more).
# In R, the `decision_tree()` specification of [tidymodels](https://morrison-lab.github.io/lds/chapters/programming-primer.html#exm-prog-tidymodels) is fit with `fit()`.
#
# :::



# Exercise 8
# #### Lab: test the classification tree
#
# Report the [error rate](https://morrison-lab.github.io/lds/chapters/first-model.html#def-error-rate) of the tree from Exercise 7 on the [test set](https://morrison-lab.github.io/lds/chapters/big-ideas.html#def-test-set).
#
# ::: {.callout-tip}
#
# #### Hint
#
# The [`.predict` method](https://morrison-lab.github.io/lds/chapters/programming-primer.html#exm-prog-sklearn) returns [predicted](https://morrison-lab.github.io/lds/chapters/course-overview.html#def-prediction) [classes](https://morrison-lab.github.io/sds/classification.html#def-class),
# and the [error rate](https://morrison-lab.github.io/lds/chapters/first-model.html#def-error-rate) is the share that differ from the actual ones.
# In R, `predict()` on a [tidymodels](https://morrison-lab.github.io/lds/chapters/programming-primer.html#exm-prog-tidymodels) workflow plays the same role as `.predict`.
#
# :::



# Exercise 9
# #### Lab: split the Boston data
#
# Download the Boston data from [`data/boston.csv`](https://morrison-lab.github.io/lds/chapters/data/boston.csv),
# which has $506$ Boston neighborhoods from the 1970 census.
# Its [response](https://morrison-lab.github.io/lds/chapters/anatomy.html#def-output-variable) `medv` is the median home value, in thousands of dollars.
# Split the neighborhoods at random into a [training set](https://morrison-lab.github.io/lds/chapters/big-ideas.html#def-training-set) and a [test set](https://morrison-lab.github.io/lds/chapters/big-ideas.html#def-test-set) of equal size.
#
# ::: {.callout-tip}
#
# #### Hint
#
# The [data frame functions](https://morrison-lab.github.io/lds/chapters/programming-primer.html#exm-prog-pandas) read the file,
# and the [random number functions](https://morrison-lab.github.io/lds/chapters/programming-primer.html#exm-prog-random) draw the row positions of the [training set](https://morrison-lab.github.io/lds/chapters/big-ideas.html#def-training-set).
#
# :::



# Exercise 10
# #### Lab: fit a regression tree
#
# [Fit](https://morrison-lab.github.io/lds/chapters/big-ideas.html#def-fitted-model) a [regression](https://morrison-lab.github.io/lds/chapters/anatomy.html#def-regression) tree of `medv` on the other variables in the [training set](https://morrison-lab.github.io/lds/chapters/big-ideas.html#def-training-set) from Exercise 9,
# with at most $4$ levels of splits.
#
# ::: {.callout-tip}
#
# #### Hint
#
# The [regression tree estimator](https://morrison-lab.github.io/lds/chapters/programming-primer.html#exm-prog-sklearn) is [fit](https://morrison-lab.github.io/lds/chapters/big-ideas.html#def-fitted-model) with `.fit`,
# and one of its settings limits the number of levels of splits.
# In R, the `decision_tree()` specification of [tidymodels](https://morrison-lab.github.io/lds/chapters/programming-primer.html#exm-prog-tidymodels) is fit with `fit()`.
#
# :::



# Exercise 11
# #### Lab: test the regression tree
#
# Report the [mean squared error](https://morrison-lab.github.io/lds/chapters/anatomy.html#def-mse) of the tree from Exercise 10 on the [test set](https://morrison-lab.github.io/lds/chapters/big-ideas.html#def-test-set).
#
# ::: {.callout-tip}
#
# #### Hint
#
# The [`.predict` method](https://morrison-lab.github.io/lds/chapters/programming-primer.html#exm-prog-sklearn) and the [error function](https://morrison-lab.github.io/lds/chapters/programming-primer.html#exm-prog-sklearn) of scikit-learn give the [mean squared error](https://morrison-lab.github.io/lds/chapters/anatomy.html#def-mse) on the [held-out](https://morrison-lab.github.io/lds/chapters/big-ideas.html#def-test-set) rows.
# In R, `predict()` on a [tidymodels](https://morrison-lab.github.io/lds/chapters/programming-primer.html#exm-prog-tidymodels) workflow plays the same role as `.predict`.
#
# :::



# Exercise 12
# #### Lab: fit a random forest
#
# [Fit](https://morrison-lab.github.io/lds/chapters/big-ideas.html#def-fitted-model) a random forest of $500$ [regression](https://morrison-lab.github.io/lds/chapters/anatomy.html#def-regression) trees of `medv` on the other variables in the [training set](https://morrison-lab.github.io/lds/chapters/big-ideas.html#def-training-set) from Exercise 9. <!-- check-term-links: allow -->
# At each split, let the forest choose among $6$ of the $12$ [predictors](https://morrison-lab.github.io/lds/chapters/anatomy.html#def-feature), picked at random.
#
# ::: {.callout-tip}
#
# #### Hint
#
# Every [estimator](https://morrison-lab.github.io/sds/estimation.html#def-estimator) in [scikit-learn](https://morrison-lab.github.io/lds/chapters/programming-primer.html#exm-prog-sklearn) is [fit](https://morrison-lab.github.io/lds/chapters/big-ideas.html#def-fitted-model) with `.fit`,
# and a random forest has settings for the number of trees and for the number of [predictors](https://morrison-lab.github.io/lds/chapters/anatomy.html#def-feature) tried at each split.
# In R, the `rand_forest()` specification of [tidymodels](https://morrison-lab.github.io/lds/chapters/programming-primer.html#exm-prog-tidymodels) has the same settings and is fit with `fit()`.
#
# :::



# Exercise 13
# #### Lab: test the random forest
#
# Report the [mean squared error](https://morrison-lab.github.io/lds/chapters/anatomy.html#def-mse) of the forest from Exercise 12 on the [test set](https://morrison-lab.github.io/lds/chapters/big-ideas.html#def-test-set).
#
# ::: {.callout-tip}
#
# #### Hint
#
# The [`.predict` method](https://morrison-lab.github.io/lds/chapters/programming-primer.html#exm-prog-sklearn) and the [error function](https://morrison-lab.github.io/lds/chapters/programming-primer.html#exm-prog-sklearn) of scikit-learn give the [mean squared error](https://morrison-lab.github.io/lds/chapters/anatomy.html#def-mse) on the [held-out](https://morrison-lab.github.io/lds/chapters/big-ideas.html#def-test-set) rows.
# In R, `predict()` on a [tidymodels](https://morrison-lab.github.io/lds/chapters/programming-primer.html#exm-prog-tidymodels) workflow plays the same role as `.predict`.
#
# :::



# Exercise 14
# #### Lab: fit a boosted regression
#
# [Fit](https://morrison-lab.github.io/lds/chapters/big-ideas.html#def-fitted-model) a boosted ensemble of $5000$ [regression](https://morrison-lab.github.io/lds/chapters/anatomy.html#def-regression) trees of `medv` on the other variables in the [training set](https://morrison-lab.github.io/lds/chapters/big-ideas.html#def-training-set) from Exercise 9.
# Give each tree at most $4$ levels of splits,
# and use the [learning rate](https://morrison-lab.github.io/lds/chapters/gradient-descent.html#def-learning-rate) $0.2$.
#
# ::: {.callout-tip}
#
# #### Hint
#
# Every [estimator](https://morrison-lab.github.io/sds/estimation.html#def-estimator) in [scikit-learn](https://morrison-lab.github.io/lds/chapters/programming-primer.html#exm-prog-sklearn) is [fit](https://morrison-lab.github.io/lds/chapters/big-ideas.html#def-fitted-model) with `.fit`;
# a boosted ensemble has settings for the number of trees, the number of levels of each tree and the [learning rate](https://morrison-lab.github.io/lds/chapters/gradient-descent.html#def-learning-rate).
# In R, the `boost_tree()` specification of [tidymodels](https://morrison-lab.github.io/lds/chapters/programming-primer.html#exm-prog-tidymodels) has the same settings and is fit with `fit()`.
#
# :::



# Exercise 15
# #### Lab: test the boosted regression
#
# Report the [mean squared error](https://morrison-lab.github.io/lds/chapters/anatomy.html#def-mse) of the boosted ensemble from Exercise 14 on the [test set](https://morrison-lab.github.io/lds/chapters/big-ideas.html#def-test-set).
#
# ::: {.callout-tip}
#
# #### Hint
#
# The [`.predict` method](https://morrison-lab.github.io/lds/chapters/programming-primer.html#exm-prog-sklearn) and the [error function](https://morrison-lab.github.io/lds/chapters/programming-primer.html#exm-prog-sklearn) of scikit-learn give the [mean squared error](https://morrison-lab.github.io/lds/chapters/anatomy.html#def-mse) on the [held-out](https://morrison-lab.github.io/lds/chapters/big-ideas.html#def-test-set) rows.
# In R, `predict()` on a [tidymodels](https://morrison-lab.github.io/lds/chapters/programming-primer.html#exm-prog-tidymodels) workflow plays the same role as `.predict`.
#
# :::


