\(~\)
Directions:
- Homework must be completed individually. Any guidance or help
received from mentors, classmates, online resources other than course
materials (including AI/LLMs) must be acknowledged.
- Clearly organize your responses so that each question (1, 2, 3, 4)
and sub-question (A, B, C, etc.) are clearly evident.
- Please submit a single Jupyter notebook displaying all output and
recording textual answers using neatly formatted markdown chunks.
- Your submission should be made via Canvas no later than 11:59pm on
the assigned due-date.
Question #1 (Weighted Averages and Python Skills)
Consider the weighted average: \[
\bar{x}_w = \frac{\sum_{i=1}^{n} w_i
x_i}{\sum_{i=1}^{n}w_i}\]
where \(w_i = e^{\theta x_i}\)
- Part A: Create a Python function that accepts,
\(\mathbf{x}\), a vector of numerical
values and \(\theta\), a user-specified
constant, and returns the weighted average using the definitions given
above.
- Part B: Show empirically that as \(\theta\) becomes large the weighted average
calculation is dominated by the maximum value in the data.
- Part C: For
x = [1,2,3,4,5,6], use an
iterative approach to find a value of \(\theta\) where the weighted average is
within 0.001 of the data’s maximum value.
\(~\)
Question #2 (Data preparation and Python skills)
In this question you will use data from an experiment where subjects
received various combinations of alcohol (coded P for placebo or M for
active alcohol) and cannabis (coded X for placebo, Y for low THC, and Z
for high THC) before performing a simulated drive in an advanced
simulator. You will need to work with two data sources:
- The lane departures key, a CSV file found at the URL :
https://remiller1450.github.io/data/lane_departures_key.csv
that records each instance of a lane departure (any part of the
subject’s vehicle going outside the boundaries of their lane).
- The time-series data for each subject, which is stored in this
zipped folder.
- Part A: Create a single
pandas
dataFrame containing every lane departure from the zipped folder. This
data frame should have 27 columns and a large number of rows.
- Part B: Using the lane departures key file, attach
dosing information to the dataFrame you created in Part A by noting that
each run of the driving simulator is given a unique DAQ name. Your
approach should preserve the number of rows in the dataFrame from Part
A.
- Part C - Find the maximum absolute lateral distance
(the variable
lat_dist) of each lane departure using
grouped summarization to remove the time component. Hint: each
DAQ is a unique identifier of a drive, but not an individual lane
departure. The variable instance must be combined with the
DAQ to provide a unique identifier.
- Part D - Create a plot displaying side-by-side
boxplots of the distribution of maximum absolute lateral distances for
each experimental condition (combination of the alcohol and cannabis
conditions).
\(~\)
Question #3 (KNN concepts and practice)
The table below provides a training data set consisting of 6
observations, 3 predictors, and a categorical outcome:
|
Observation
|
X1
|
X2
|
X3
|
Y
|
|
1
|
0
|
3
|
0
|
Red
|
|
2
|
2
|
0
|
0
|
Red
|
|
3
|
0
|
1
|
3
|
Red
|
|
4
|
0
|
1
|
2
|
Green
|
|
5
|
-1
|
0
|
1
|
Green
|
|
6
|
1
|
1
|
1
|
Red
|
Suppose we’re interested in using \(k\)-nearest neighbors to predict the
outcome of a test data-point at \(\{X_1=0,
X_2=0, X_3=0\}\).
You should answer the following questions using a calculator or basic
Python functions. You should not use any functions in
sklearn. Additionally, you do not need to perform any
standardization/scaling.
- Part A: Calculate the euclidean distance
between each observation and the test data-point of \(\{X_1=0, X_2=0, X_3=0\}\).
- Part B: What is the predicted class of
\(Y\) for the test data-point if
uniform weighting and \(k=1\) (one
neighbor) are used? Why?
- Part C: What is the predicted probability
that \(Y=\text{Green}\) if uniform
weighting and \(k=3\) (three
neighbors) are used? Why?
- Part D: Using \(k=3\), will the predicted
probability of \(Y=\text{Green}\)
be higher if distance weighting is used instead of uniform
weighting? Briefly explain (you do not need to perform the
calculation).
- Part E: Re-scale each data-point by subtracting
that feature’s mean and dividing by its standard deviation (ie: what
StandardScaler() does) and re-calculate the distance
between the test data-point and each observation. What is the predicted
probability of this data-point being “Green” using uniform weighting and
three neighbors? How does this compare to Part C?
\(~\)
Question #4 (Application using sklearn)
For this question you should use the dataset available here:
https://remiller1450.github.io/data/Ozone.csv
These data document daily Ozone concentrations in New York City in
1973. Ozone is a pollutant that is has been linked to numerous health
problems. The goal of this application is develop methods for accurately
predicting the Ozone concentration on future dates using that date’s
expected solar radiation, wind speed, and temperature.
- Part A: Separate the outcome from the predictors
(dropping the “Day” column), then perform an 80-20 train-test split
using
random_state=3. Next, create a pre-processing
pipeline that performs standardization before applying a KNN regressor
model.
- Part B: Create a sequence of values of \(k\) from 5 to 35 by increments of 5.
Calculate the RMSE of the model from Part A on the training
data for each of these values of \(k\) and display the results on a line graph
showing \(k\) on the x-axis and the
training RMSE on the y-axis.
- Part C: Now consider using a decision tree model
for these data. Create a sequence of maximum depths from 1 to 10, then
calculate the training RMSE of each model and display the results on a
line graph showing the tree depth on the x-axis and the training RMSE on
the y-axis.
- Part D: Using the graphs from Parts B and C, which
model do you think might be most effective on the test set? Or
is it difficult to tell? Briefly explain your reasoning.
- Part E: Report the RMSE on the test set
for each model considered in Parts B and C. How do these results compare
with the model you identified in Part D?
- Part F: Suppose you are asked to recommend one of
the two models evaluated in Part E to a city planner who cares about
both interpretability of the model’s decisions and its overall
performance. Provide a recommendation for one of the two models you
evaluated accompanied by a 3-5 sentence justification of the model
written in a way that the city planner (who is not familiar with machine
learning or algorithms) would understand.