Machine Learning
Budget: $10 – $30 USD
what you aim to accomplish in this assignment:
1. Import the Mushroom data set.
2. Use the KNN algorithm to impute missing values in the dataset. Note: You are not
permitted to use the KNNImputer class from Scikit-learn. Instead, you must
explicitly write the code to perform the imputation using the KNeighborsClassifier
algorithm .
The first step is to think through what will be your feature data and what will be your
response data for this imputation step. You will want to one-hot-encode your feature
data and ordinal encode your response data. Next, you should train your KNN model on
those instances for which the feature of interest is valued, then have the model make
predictions for those instances that have missing values. This is how you will have the
KNN model impute missing data.
When you have computed the missing values, create a data structure (i.e. a list) called
missing_values that contains all of the imputed values (in terms of the original data
set) in order of increasing index from the original data set. You must then print the first
10 instances of missing_values to the screen. Finally, you will impute the missing
values back into the original data set before continuing, so that the next step starts fresh
with a complete data set in terms of the raw data values.
Graded Concept Question (include a section in your notebook): Would it still be
possible to train the KNN model if you one-hot encoded the response data instead?
Why or why not?
3. Train a RandomForestClassifier as well as a LogisticRegression model to predict
whether a mushroom is edible or poisonous given this data set of nominally-valued
characteristics. Train the model on the feature data supplied to you after you’ve one-hot
encoded it. You should ordinally encode the response data. (This means that you
should one-hot encode the feature data, but instead use a binary value for the response
data.)
Graded Concept Question (include a section in your notebook): Would it still be
possible to train these two models if you one-hot encoded the response data instead,
being careful to specify that the drop parameter of the OneHotEncoder class is set to
‘first’? Why or why not?
You do not need to perform a grid search to find the optimal hyperparameter
values . Accordingly, you should use the default hyperparameter settings for
both models, along with a random_state of 42 for the RandomForestClassifier.
4. When you train both the RandomForestClassifier and LogisticRegression models,
use the magic command %%time to time how long it takes to complete training. See the
Python Data Science Handbook for more information about this magic command.
5. Compute the accuracy, precision, and recall scores for a test set. Briefly discuss the
performance of your models in terms of these values.
6. Perform dimensionality reduction using PCA and keep 95% of the variance. By what
percentage were you able to reduce the number of dimensions of the training set? How
many features (i.e. dimensions) are you left with after reducing dimensionality?
7. Train two new models, a Random Forest Classifier and a Logistic Regression model,
on this reduced dataset to predict whether a mushroom is edible or not. Again, time the
training of these two models on the reduced dataset. You will again use the default
hyperparameter values for both models, with a random state parameter of 42 for the
Random Forest Classifier.
8. Compute the accuracy, precision, and recall scores for the model trained on the
reduced data set using the same test set as before. What conclusions can you make
about these models and the PCA process? How do the models compare? Discuss the
trends observed for model training time and performance for the full and reduced data
sets. Tabulate the information in your final analysis.
1. Import the Mushroom data set.
2. Use the KNN algorithm to impute missing values in the dataset. Note: You are not
permitted to use the KNNImputer class from Scikit-learn. Instead, you must
explicitly write the code to perform the imputation using the KNeighborsClassifier
algorithm .
The first step is to think through what will be your feature data and what will be your
response data for this imputation step. You will want to one-hot-encode your feature
data and ordinal encode your response data. Next, you should train your KNN model on
those instances for which the feature of interest is valued, then have the model make
predictions for those instances that have missing values. This is how you will have the
KNN model impute missing data.
When you have computed the missing values, create a data structure (i.e. a list) called
missing_values that contains all of the imputed values (in terms of the original data
set) in order of increasing index from the original data set. You must then print the first
10 instances of missing_values to the screen. Finally, you will impute the missing
values back into the original data set before continuing, so that the next step starts fresh
with a complete data set in terms of the raw data values.
Graded Concept Question (include a section in your notebook): Would it still be
possible to train the KNN model if you one-hot encoded the response data instead?
Why or why not?
3. Train a RandomForestClassifier as well as a LogisticRegression model to predict
whether a mushroom is edible or poisonous given this data set of nominally-valued
characteristics. Train the model on the feature data supplied to you after you’ve one-hot
encoded it. You should ordinally encode the response data. (This means that you
should one-hot encode the feature data, but instead use a binary value for the response
data.)
Graded Concept Question (include a section in your notebook): Would it still be
possible to train these two models if you one-hot encoded the response data instead,
being careful to specify that the drop parameter of the OneHotEncoder class is set to
‘first’? Why or why not?
You do not need to perform a grid search to find the optimal hyperparameter
values . Accordingly, you should use the default hyperparameter settings for
both models, along with a random_state of 42 for the RandomForestClassifier.
4. When you train both the RandomForestClassifier and LogisticRegression models,
use the magic command %%time to time how long it takes to complete training. See the
Python Data Science Handbook for more information about this magic command.
5. Compute the accuracy, precision, and recall scores for a test set. Briefly discuss the
performance of your models in terms of these values.
6. Perform dimensionality reduction using PCA and keep 95% of the variance. By what
percentage were you able to reduce the number of dimensions of the training set? How
many features (i.e. dimensions) are you left with after reducing dimensionality?
7. Train two new models, a Random Forest Classifier and a Logistic Regression model,
on this reduced dataset to predict whether a mushroom is edible or not. Again, time the
training of these two models on the reduced dataset. You will again use the default
hyperparameter values for both models, with a random state parameter of 42 for the
Random Forest Classifier.
8. Compute the accuracy, precision, and recall scores for the model trained on the
reduced data set using the same test set as before. What conclusions can you make
about these models and the PCA process? How do the models compare? Discuss the
trends observed for model training time and performance for the full and reduced data
sets. Tabulate the information in your final analysis.