---
title: "Neural Networks Lab (stub)"
format: html
---

Stub of the Neural Networks Lab page: the exercises, without the solutions. Solutions: https://morrison-lab.github.io/lds/chapters/neural-nets-lab.html

#### Lab: players with a missing salary

Use the [`Hitters` data](#dat-hitters).
How many players have a missing `Salary`?

::: {.callout-tip}
#### Hint

[Count the missing values of a column](https://morrison-lab.github.io/lds/chapters/programming-primer.html#exm-prog-pandas) with a pandas method in Python, or with `is.na()` in R.

:::

```{python}
# your code here
```

#### Lab: salary and career hits

Use the [`Hitters` data](#dat-hitters).
Among the players with a recorded `Salary`,
what is the [sample correlation](https://morrison-lab.github.io/sds/exploratory-descriptive.html#def-pearson-r) between `Salary` and `CHits`?

::: {.callout-tip}
#### Hint

[Drop the rows with a missing value](https://morrison-lab.github.io/lds/chapters/programming-primer.html#exm-prog-pandas) before you compute it,
because a missing value makes the result missing in R and is skipped in pandas.

:::

```{python}
# your code here
```

#### Lab: a histogram of salary

Use the [`Hitters` data](#dat-hitters).
Draw a histogram of `Salary` for the players with a recorded `Salary`.

::: {.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.

:::

```{python}
# your code here
```

#### Lab: salary against career hits

Use the [`Hitters` data](#dat-hitters).
Draw a scatterplot of `Salary` against `CHits` for the players with a recorded `Salary`.

::: {.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.

:::

```{python}
# your code here
```

#### Lab: load the Hitters data

Read the Hitters data,
which record the `Salary` in thousands of dollars of major league baseball players,
and keep the $16$ numeric [predictors](https://morrison-lab.github.io/lds/chapters/anatomy.html#def-feature) of `Salary`.
Drop the players whose `Salary` is missing.

::: {.callout-tip}

#### Hint

The [data frame functions](https://morrison-lab.github.io/lds/chapters/programming-primer.html#exm-prog-pandas) read the file and select columns,
and [`.dropna`](https://morrison-lab.github.io/lds/chapters/programming-primer.html#exm-prog-pandas-more) removes the rows with a missing value.
In R, `drop_na()` does the same.

:::

```{python}
# your code here
```

#### Lab: split the Hitters data

Split the players from Exercise 5 at random into a [training set](https://morrison-lab.github.io/lds/chapters/big-ideas.html#def-training-set) with two thirds of them and a [test set](https://morrison-lab.github.io/lds/chapters/big-ideas.html#def-test-set) with the rest.

::: {.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.

:::

```{python}
# your code here
```

#### Lab: standardize the Hitters predictors

Standardize each [predictor](https://morrison-lab.github.io/lds/chapters/anatomy.html#def-feature) in the [training set](https://morrison-lab.github.io/lds/chapters/big-ideas.html#def-training-set) and the [test set](https://morrison-lab.github.io/lds/chapters/big-ideas.html#def-test-set) from Exercise 6 with its [mean](https://morrison-lab.github.io/sds/exploratory-descriptive.html#def-sample-mean) and [standard deviation](https://morrison-lab.github.io/pds/variance-covariance.html#def-sd) in the [training set](https://morrison-lab.github.io/lds/chapters/big-ideas.html#def-training-set).

::: {.callout-tip}

#### Hint

[Broadcasting](https://morrison-lab.github.io/lds/chapters/programming-primer.html#exm-prog-matrices) subtracts a [mean](https://morrison-lab.github.io/sds/exploratory-descriptive.html#def-sample-mean) and divides by a [standard deviation](https://morrison-lab.github.io/sds/exploratory-descriptive.html#def-sample-sd) for every column at once,
and both numbers come from the [training set](https://morrison-lab.github.io/lds/chapters/big-ideas.html#def-training-set) alone.

:::

```{python}
# your code here
```

#### Lab: fit a linear model to the Hitters training set

Fit a [linear regression](https://morrison-lab.github.io/lds/chapters/linear-regression.html#def-linear-model) of `Salary` on the standardized [predictors](https://morrison-lab.github.io/lds/chapters/anatomy.html#def-feature) in the [training set](https://morrison-lab.github.io/lds/chapters/big-ideas.html#def-training-set) from Exercise 7,
and report its [mean](https://morrison-lab.github.io/sds/exploratory-descriptive.html#def-sample-mean) absolute error on the [test set](https://morrison-lab.github.io/lds/chapters/big-ideas.html#def-test-set).

::: {.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 the error to report is the [mean](https://morrison-lab.github.io/sds/exploratory-descriptive.html#def-sample-mean) of the absolute differences between the [predictions](https://morrison-lab.github.io/lds/chapters/course-overview.html#def-prediction) and the actual salaries.
In R, the `linear_reg()` specification of [tidymodels](https://morrison-lab.github.io/lds/chapters/programming-primer.html#exm-prog-tidymodels) is fit with `fit()`.

:::

```{python}
# your code here
```

#### Lab: fit a single-hidden-layer network to the Hitters training set

Fit a [neural network](https://morrison-lab.github.io/lds/chapters/neural-nets.html#def-fully-connected) with one hidden layer of $10$ units and a [weight decay](https://morrison-lab.github.io/lds/chapters/regularization.html#def-weight-decay) penalty of $0.1$ to the [training set](https://morrison-lab.github.io/lds/chapters/big-ideas.html#def-training-set) from Exercise 7,
and report its [mean](https://morrison-lab.github.io/sds/exploratory-descriptive.html#def-sample-mean) absolute error on the [test set](https://morrison-lab.github.io/lds/chapters/big-ideas.html#def-test-set).

::: {.callout-tip}

#### Hint

The [neural network estimator](https://morrison-lab.github.io/lds/chapters/programming-primer.html#exm-prog-sklearn) of scikit-learn has settings for the sizes of its hidden layers and for its [weight decay](https://morrison-lab.github.io/lds/chapters/regularization.html#def-weight-decay) penalty.

:::

```{python}
# your code here
```
