Answered step by step
Verified Expert Solution
Link Copied!
Question
1 Approved Answer

Need help with fixing error in code in 5.A to habe 5.B run Import Statements Run the cell below to import the NumPy, Matplotlib, and

image text in transcribedimage text in transcribedimage text in transcribed

Need help with fixing error in code in 5.A to habe 5.B run

Import Statements Run the cell below to import the NumPy, Matplotlib, and math packages. [ ] import numpy as np import matplotilib.pyplot as plt import math Four arrays have been created to store the datasets that you will be working with in this lab. Running the cell below will import those arrays into your workspace. We will describe the imported arrays later in this notebook. The cell will also import some functions that will be used to test your code in Problem 7. Run that cell now. fron MATH_599.1ab_08 import x1,y1,x2,y2 fron MATH_599.1ab_68 import unit_test_1, unit_test_2, unit_test_3 Problem 5 - Naive Bayes Classifier In this problem, you will build a binary naive Bayes classifier. You will then apply the classifier to perform spam detection. The training data for this problem is stored in the arrays x1 and y1 that were imported near the beginning of this notebook. The array x1 is a 2D feature array with 10 features and 10,000 observations. Each observation is intended to represent a single email. Each feature is associated with a specific word that might be associated with a spam email. The features are binary (0/1) variables indicating the presence (1) or absence (0) of the word in the email. The array y1 contains the training labels. The target variable Y is a binary variable indicating whether the email is spam (1) or not spam (0). The shapes of these arrays are printed below. [15] print(X1.shape) print(y1.shape) (10000,10) (10000, Part 5.A Complete the definition of the function naive_bayes () in the cell below. The role of the three parameters are as follows: - x is a 2D array containing the training features. - y is a 1D array containing the training labels. - x is a 1D array containing features for an observation to be classified. The function should calculate a score for both classes (class 0 and class 1), and then calculate and return the likelihood ratio. These calculations are explained in the "Naive Bayes Classifier" lesson. def naive_bayes (x,y,x) : \# Calculate probability of class 0 prob_class_ =(1y)mean() \# Calculate probability of class 1 prob_class_1 =ymean() \# Calculate likelihood for class likelihood_ =(1x)8(1x) mean (axis =0) \# Calculate likelihood for class 1 likelihood_1 =xX. mean (axis =0 ) \# Calculate the product of likelihoods for class \# Calculate the product of likelihoods for class 1 product_1 = np.prod(1ikelihood_1) \# Calculate the likelihood ratio likelihood_ratio =( prob_class_1 product_1 )/( prob_class_0 product_e ) return likelihood_ratid TypeError Traceback (most recent call last) 3 email_ 3=[0,1,8,8,8,1,0,2,1,0] 4 ..--> 5 result_1 = naive_bayes (X1,y1, email_1) 6 result_ 2 = naive_bayes (X1,y1, email_2) 7 result_3 = naive_bayes (X1,y1, email_3) Sipython-input-63-eeeb9a5ffd26> in naive_bayes (x,y,x) 4 \# Calculate likelihood for class likelihood_ = np.mean ((1x)(1x)) \# Calculate likelihood for class 1 likelihood_1 =x np.mean (x) TypeError: unsupported operand type(s) for -: 'int' and 'list

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image
Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Oracle Solaris 11.2 System Administration (oracle Press)

Authors: Harry Foxwell

1st Edition

007184421X, 9780071844215

More Books

Students explore these related Databases questions