The image as attached is my question, I already have the answer with red text. Also I implement a python code for this question as follow: And all you need to do is help me check the python code. If there is any error, please help me correct it, thank you. # import the libraries import numpy as np import matplotlib.pyplot as plt import pandas as pd import sklearn from sklearn import preprocessing from sklearn.preprocessing import LabelEncoder from sklearn.naive_bayes import GaussianNB #0.2 Assigning features and label variables ID = ['1234', '6754', '2345','2457','3567','7532','1456','1468','8989','9876'] Temp = ['H','H','N','H','N','N','N','H','H','H'] BP = ['H','N','H','H','H','H','N','N','N','N'] Test1 = ['P','N','P','P','P','N','N','N','N','N'] Test2 = ['N','P','P','N','P','N','N','N','P','N'] Health = ['D','D','D','D','D','H','H','H','H','H'] #0.3 Creating labelEncoder and Converting string labels into numbers. le = preprocessing.LabelEncoder() Temp_encoded=le.fit_transform(Temp) print("The encoded temp values are: {}".format(Temp_encoded)) BP_encoded=le.fit_transform(BP) print("The encoded BP values are: {}".format(BP_encoded)) Test1_encoded=le.fit_transform(Test1) print("The encoded Test1 values are: {}".format(Test1_encoded)) Test2_encoded=le.fit_transform(Test2) print("The encoded Test2 values are: {}".format(Test2_encoded)) Health_encoded=le.fit_transform(Health) print("The encoded Health values are: {}".format(Health_encoded)) #0.3.1 Now combine all the features in a single variable (list of tuples). features=zip(Temp_encoded,BP_encoded,Test1_encoded,Test2_encoded) features = list(features) print(features) #0.4 Create a Naive Bayesian Classifier and train the model using the training sets model = GaussianNB() model.fit(features,Health_encoded) #0.5 Predict the output for the new patient: 0: Diseased and 1: Healthy # a) giving value of each term in our NB classifier predicted= model.predict([[0,0,0,1]]) # b) Our Prediction print("Predicted Value:", predicted) #0.6 So our prediction shows the new patient’s health as Healthy (H).
The image as attached is my question, I already have the answer with red text.
Also I implement a python code for this question as follow:
And all you need to do is help me check the python code.
If there is any error, please help me correct it, thank you.
# import the libraries
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import sklearn
from sklearn import preprocessing
from sklearn.preprocessing import LabelEncoder
from sklearn.naive_bayes import GaussianNB
#0.2 Assigning features and label variables
ID = ['1234', '6754', '2345','2457','3567','7532','1456','1468','8989','9876']
Temp = ['H','H','N','H','N','N','N','H','H','H']
BP = ['H','N','H','H','H','H','N','N','N','N']
Test1 = ['P','N','P','P','P','N','N','N','N','N']
Test2 = ['N','P','P','N','P','N','N','N','P','N']
Health = ['D','D','D','D','D','H','H','H','H','H']
#0.3 Creating labelEncoder and Converting string labels into numbers.
le = preprocessing.LabelEncoder()
Temp_encoded=le.fit_transform(Temp)
print("The encoded temp values are: {}".format(Temp_encoded))
BP_encoded=le.fit_transform(BP)
print("The encoded BP values are: {}".format(BP_encoded))
Test1_encoded=le.fit_transform(Test1)
print("The encoded Test1 values are: {}".format(Test1_encoded))
Test2_encoded=le.fit_transform(Test2)
print("The encoded Test2 values are: {}".format(Test2_encoded))
Health_encoded=le.fit_transform(Health)
print("The encoded Health values are: {}".format(Health_encoded))
#0.3.1 Now combine all the features in a single variable (list of tuples).
features=zip(Temp_encoded,BP_encoded,Test1_encoded,Test2_encoded)
features = list(features)
print(features)
#0.4 Create a Naive Bayesian Classifier and train the model using the training sets
model = GaussianNB()
model.fit(features,Health_encoded)
#0.5 Predict the output for the new patient: 0: Diseased and 1: Healthy
# a) giving value of each term in our NB classifier
predicted= model.predict([[0,0,0,1]])
# b) Our Prediction
print("Predicted Value:", predicted)
#0.6 So our prediction shows the new patient’s health as Healthy (H).
![2. (15 pts) Given the following data set, we apply a naïve Bayesian (NB) classifier to predict the Health status.
Each patient is described by Temp={H,N}, BP={H,N}, Test1={P,N} and Test2={P,N}. The class is Health={D,H}.
ID
Temp
ВР
Test1
Test2
Health
1234
H
H.
N
6754
H
N
N
P
D
2345
N
H
P
P
2457
H
H.
P
N
D
3567
N
H
P
7532
N
H.
N
N
H
1456
N
N
N
H
1468
H
N
N
N
H
8989
H
N
N
P
H
9876
H
N
N
N
H
For a new patient, ID 9999, with Temp=H, BP=H, Test1=N, Test2=P, is he/she Healthy (H) or Diseased (D)?
Show (a) the value of each term in your NB classifier, and (b) your prediction.
Answer:
P_temp(H/D)=3/5, P_temp(N/D)=2/5
P_temp(H/H)=3/5, P_temp(N/H)=2/5
P_BP(H/D)=4/5, P_BP(N/D)=1/5
P_BP(H/H)=1/5, P_BP(N/H)=4/5
P_Test1(P/D)=4/5, P_Test1(N/D)=1/5
P_Test1(P/H)=0/5, P_Test1(N/H)=5/5
P_Test2(P/D)=3/5, P_Test2(N/D)=2/5
P_Test2(P/H)=1/5, P_Test2(N/H)=4/5
new patient A: Temp=H, BP=H, Test1=N, Test2=P
S((H,H,N,P),D) = 3/5 * 4/5 * 1/5 * 3/5 = 0.0576
S((H,H,N,P),H) = 3/5 * 1/5 * 5/5 * 1/5 = 0.024
S((H,H,N,P),D) > S((H,H,N,P),H)
So predict A as class D.](/v2/_next/image?url=https%3A%2F%2Fcontent.bartleby.com%2Fqna-images%2Fquestion%2Ff0f10baf-c814-4112-826a-b133a2e73caf%2F52dea9ff-2b29-4e30-944d-d91081581f15%2Fk2n249o_processed.png&w=3840&q=75)
![](/static/compass_v2/shared-icons/check-mark.png)
Step by step
Solved in 2 steps with 1 images
![Blurred answer](/static/compass_v2/solution-images/blurred-answer.jpg)
![Computer Networking: A Top-Down Approach (7th Edi…](https://www.bartleby.com/isbn_cover_images/9780133594140/9780133594140_smallCoverImage.gif)
![Computer Organization and Design MIPS Edition, Fi…](https://www.bartleby.com/isbn_cover_images/9780124077263/9780124077263_smallCoverImage.gif)
![Network+ Guide to Networks (MindTap Course List)](https://www.bartleby.com/isbn_cover_images/9781337569330/9781337569330_smallCoverImage.gif)
![Computer Networking: A Top-Down Approach (7th Edi…](https://www.bartleby.com/isbn_cover_images/9780133594140/9780133594140_smallCoverImage.gif)
![Computer Organization and Design MIPS Edition, Fi…](https://www.bartleby.com/isbn_cover_images/9780124077263/9780124077263_smallCoverImage.gif)
![Network+ Guide to Networks (MindTap Course List)](https://www.bartleby.com/isbn_cover_images/9781337569330/9781337569330_smallCoverImage.gif)
![Concepts of Database Management](https://www.bartleby.com/isbn_cover_images/9781337093422/9781337093422_smallCoverImage.gif)
![Prelude to Programming](https://www.bartleby.com/isbn_cover_images/9780133750423/9780133750423_smallCoverImage.jpg)
![Sc Business Data Communications and Networking, T…](https://www.bartleby.com/isbn_cover_images/9781119368830/9781119368830_smallCoverImage.gif)