0% found this document useful (0 votes)
23 views9 pages

Naive Bayes

naive bayes exercise

Uploaded by

Dev V
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
23 views9 pages

Naive Bayes

naive bayes exercise

Uploaded by

Dev V
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 9
LAB EXERCISE - 14 Naive Bayes lassifier 1. Aim of the Experiment: Implement and demonstrate the working of Naive Bayesian classifier using a sample data set. Build the model to classify a test sample. 2. Reference to Text book for Algorithms: Refer to Section 8.3.1 in Chapter 8 Bayesian Learning to understand the working of the algorithm, Listing 1: Sample Dataset Used: Table 8.1 Table 8.1 Trai 2 [28 [No Good “Moderate Yes 3. [29 [No ‘Average Poor ‘No 4 [8 [No ‘Average Good ‘No 5. [58 Yes Good Moderate Yes 6 |e ‘Yes Good Moderate Yes 7 [e8 [Yes Good Poor ‘No 8 [29 [No Very good | Good Yes 9 [B8 [Yes Good Good Yes 10. [28 [Yes ‘Average Good Yes 3. Python Program with Explanation: 1, Import LabelEncoder to normalize labels. from sklearn.preprocessing import LabelEncoder 2. Import train_test_split function, Copyright @ Oxford University Press, India 2021 from sklearn.model_selection import train_test_spli 3. Import Gaussian Classifier from sklearn.naive_bayes. from sklearn.naive_bayes import GaussianNB 4, Import preprocessing package to use transformer classes. from sklearn import preprocessing 5. Import classification_report and confusion_matrix from. sklearn.mettics to measure the quality of predictions, from sklearn.metrics import classification_report, confusion_matrix 6. Create lists, CGPA, Inter, PK, CS and Job. CGPA = ['g9','g8','29','18','g8'/'g9',18''e9''e8'/e8'] Inter = ['Y','N','N4'N' "YY “Y PK = [4H Fat 254 4444'S] CS =['G)'M''P'6)'M'M!/'P)'G''G''G'] Job = [V4.4 /NY'NI YY YUINE SEY V5] 7. Create labelEncoder le to encode labels with value between 0 and no_of_classes-1 le = preprocessing.LabelEncoder() 8. Convert non-numeric labels of all features into numbers. Then print and sce the encoded labels. CGPA_encoded = le fit_transform(CGPA) print("CGPA:", CGPA_encoded) Inter_encoded = le-fit_transform(Inter) PK_encoded = le.fit_transform(PK) CS_encoded = le.fit_transform(CS) label = le.fit_transform(Job) print("Inter:",Inter_encoded) print ("PK:",PK_encoded) Copyright @ Oxford University Press, India 2021 print ("CS:",CS_encoded) print("Job:" label) 9. Create a list/dynamic array called “features features =] 10. Append all the encoded features to the list created. for iin range(len(CGPA_encoded)): features.append{[CGPA_encoded!i, Inter_encodedfi], PK_encodedfi, 5_encoded{ill) 11, Split the dataset into training dataset and test dataset by using the function train_test_splitQ, X train,X_testyy_train,y_test=train_test_split(features,label,test_size=0.30,random_s tate=2) 12, Create a Gaussian Classifier, model = GaussianNB() 13, Train the model using the training sets. model fit(features, label) 14, After training, use the fitted model to predict a new instance. y_pred = model.predict(X_test) 15. Generate classification report & confusion matrix to measure the quality of predictions. print(classification_report(y_test, y_pred)) print(confusion_matrixty_test, y_pred)) 16. Predict Output. Copyright @ Oxford University Press, India 2021 # The non-numerical equivalent of the new instance [2, 0, 2, 0] given is [2:'18", 0:°N 2: “G'] print({2,0,2,0]) if model,predict{{{2,0,2,0]) print("Predicted Value:Got JOB",predicted ) else: print("Didnt get JOB") # The non-numerical equivalent of the new instance [0, 1, 0, 1] given is (0:"g8 1,08", 1M] print({0,1,0,1]) if model.predict({{0,1,0,11) print(""Predicted Value:Got JOB" predicted ) print("Didnt get 108") Complete Program: from sklearn preprocessing import LabelEncoder from sklearn.model_selection import train_test_split from sklearn.nalve_bayes import GaussianNB {rom sklearn import preprocessing from sklearn.metrics import classification_report, confusion_mateix CPA = [9 Inter = [¥ creating labelEncoder reprocessing.LabelEncoder() +# Converting string labels into numbers. Copyright @ Oxford University Press, India 2021 left transform(coPa) print("CGPA:", CGPA_encoded) Inter_encoded = e.it_transform(inter) PK_encoded = lesit_transform(PK) CS_encoded = le.fit_transform(CS) label le.ft_transformUlob) print("Inter:",Inter_encoded)} print ("PK:",PK_encoded) print ("CS:",CS_encoded} print("Job:" label) features = (} for iin range(len(CGPA_encoded)}: features.append([CGPA_encodedlil, Inter_encoded[i], PK_encoded{i], CS_encoded{il}) X train,X_testy_train,y_test=train_test_split{features,label,test_size=0.30,random_state=2) Create a Gaussian Classifier ‘model = GaussianNB() #4 Train the model using the training sets model it(features,label) Predict Output y_pred = model.predict(X_test) print(classification_reportly_test, y_pred)) print(confusion_matrix(y_test, y_predl) print([2,0,2,0}) if model predict({l2,0,2,0) print("Predicted Value:Got JOB") print("Predicted Value:Didn't get JOB") Copyright @ Oxford University Press, India 2021 print({0,2,0,2]) if model.predict({(0,1,0,1) print("Predicted Value:Got JOB") print("Predicted Value:Didn't get JO Outpu Python 3.8.3 (tags/v3.8.3:6f8¢832, May 13 2020, 22:37:02) [MSC v.1924 64 bit (AMDS4)] on win32 ‘Type "help", "copyright", “credits” or “license()" for more information. RESTART: C:\Users\ADMIN\pythonpgms\inal\jnf naive bayes sklearn test. py capa: (1012012100) Inter: [1000111011] Pk: [1022000102] ¢5:[0120112000) Job: [1100110113] precision recall f1-score support 1 100 100 100 3 accuracy 1003 macroavg 1.00 1.00 100 3 weighted avg 1.00 1.00 100 3 (0) (2,0, 2,0] Predicted Value:Didn't get JOB (0,1, 0,1) Predicted Value:Got JOB Copyright @ Oxford University Press, India 2021 Screenshot of the Output: Listing 2: Program Code: from sklearn import datasets from sklearn import metrics from sklearn.naive_bayes import GaussianNB from sklearn.metrics import classification_report, confusion_matrix from skiearn.model_selection import train_test_split, ff Load the Iris dataset dataset = datasets.load_iris() 4 Fita Naive Bayes model to the data model = GaussianNa() X_train,X_testy_train,y_test=train_test_split(dataset data dataset target,test_s e=2) 0.30,random_stat model fit(X_train, y_train) Copyright @ Oxford University Press, India 2021 print(model) 4## Make predictions vy.expected = _test y_predicted = model.predict(X_test) ## Evaluate the model and print the classification report, Confusion Matrix print(metrics.classification_report(y_expected, y_predicted)) print(metrics.confusion_matrix(y_expected, y_predicted)) Sereen Shot of the Progra Output: Python 3.8.3 (tags/v3.8.3:6f8cB32, May 13 2020, 22:37:02) [MSC v.1924 64 bit (AMD64)] on win32 ‘Type "help", "copyright", "credits" or "license()" for more information. RESTART: C:\Users\ADMIN\pythonpgms\Review inf naive bayes iris.py Copyright @ Oxford University Press, India 2021 GaussianNB() precision recall fl-score support 0 100 100 100 17 1 100 093 097 15 2 093 100 096 13 accuracy 0.9845 macroavg 0.98 0.98 0.98 45 weighted avg 0.98 0.98 0.98 45 (127 0 9} [014 4) [0 013) Screen Shot of the Output: Copyright @ Oxford University Press, India 2021

You might also like