Question: Similarly, write the function `train_nb_eval_acc` that trains Naive Bayes models and takes following the arguments: * `train_images`: A numpy array with the shape `(N,height,width)`, where
Similarly, write the function `train_nb_eval_acc` that trains Naive Bayes models and takes following the arguments: * `train_images`: A numpy array with the shape `(N,height,width)`, where * `N` is the number of samples and could be anything, * `height` is each individual image's height in pixels (i.e., number of rows in each image), * and `width` is each individual image's width in pixels (i.e., number of columns in each image). Do not assume anything about `images`'s dtype or number of samples. * `train_labels`: A numpy array with the shape `(N,)`, where `N` is the number of samples and has the `int64` dtype. * `eval_images`: The evaluation images with similar characteristics to `train_images`. * `eval_labels`: The evaluation labels with similar characteristics to `train_labels`. * `density_model`: A string that is either `'Gaussian'` or `'Bernoulli'`. In the former (resp. latter) case, you should train a Naive Bayes with the Gaussian (resp. Bernoulli) density model. and returns the following: * `eval_acc`: a floating number scalar between 0 and 1 that represents the accuracy of the trained model on the evaluation data. We have provided a template function that uses the previous functions and only requires you to fill in the missing parts. It also handles the input shapes in an agnostic way. **Note**: You do not need to implement the Naive Bayes classifier from scratch in this assignment; Make sure you use `scikit-learn`'s Naive Bayes module for training and prediction in this task. We have already imported these two functions in the first code cell: * `from sklearn.naive_bayes import GaussianNB, BernoulliNB`
PLEASE SUBMIT TEXT, NOT A SCREENSHOT. IT MUST ALSO PASS THE ASSERTION CHECK
def train_nb_eval_acc(train_images, train_labels, eval_images, eval_labels, density_model='Gaussian'): """ Trains Naive Bayes models, apply the model, and return an accuracy.
Parameters: train_images (np.array): A numpy array with the shape (N,height,width) train_labels (np.array): A numpy array with the shape (N,), where N is the number of samples and has the int64 dtype. eval_images (np.array): The evaluation images with similar characteristics to train_images. eval_labels (np.array): The evaluation labels with similar characteristics to train_labels. density_model (string): A string that is either 'Gaussian' or 'Bernoulli'. Returns: eval_acc (np.float): a floating number scalar between 0 and 1 that represents the accuracy of the trained model on the evaluation data. """ assert density_model in ('Gaussian', 'Bernoulli') # YOUR CODE HERE
raise NotImplementedError return eval_acc
# Don't mind the following lines and do not change them train_nb_eval_acc_gauss = lambda *args, **kwargs: train_nb_eval_acc(*args, density_model='Gaussian', **kwargs) train_nb_eval_acc_bern = lambda *args, **kwargs: train_nb_eval_acc(*args, density_model='Bernoulli', **kwargs)
#ASSERTION:
# Checking against the pre-computed test database test_results = test_case_checker(train_nb_eval_acc_gauss, task_id='8_G') assert test_results['passed'], test_results['message'] # Gaussian Model Test Results
test_results = test_case_checker(train_nb_eval_acc_bern, task_id='8_B') assert test_results['passed'], test_results['message'] # Bernoulli Model Test Results
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
