0% found this document useful (0 votes)
29 views

Example - 1

The document provides examples of using a Naive Bayes classifier to perform classification tasks. In the first example, it loads car evaluation data, preprocesses the data by encoding categorical features, splits the data into training and test sets, fits a Gaussian Naive Bayes model to the training data and evaluates the model's accuracy on the test set. The second example demonstrates additional preprocessing steps like imputing missing values, one-hot encoding, scaling and evaluating a Naive Bayes model trained on preprocessed data. Overall, the document shows how to apply a Naive Bayes approach to classification problems with common preprocessing steps.

Uploaded by

Ishani Mehta
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views

Example - 1

The document provides examples of using a Naive Bayes classifier to perform classification tasks. In the first example, it loads car evaluation data, preprocesses the data by encoding categorical features, splits the data into training and test sets, fits a Gaussian Naive Bayes model to the training data and evaluates the model's accuracy on the test set. The second example demonstrates additional preprocessing steps like imputing missing values, one-hot encoding, scaling and evaluating a Naive Bayes model trained on preprocessed data. Overall, the document shows how to apply a Naive Bayes approach to classification problems with common preprocessing steps.

Uploaded by

Ishani Mehta
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

https://acadgild.

com/blog/naive-bayesian-model

Example – 1

import numpy as np

import pandas as pd

import matplotlib.pyplot as plt

from sklearn import metrics , model_selection

## Import the Classifier.

from sklearn.naive_bayes import GaussianNB

data = pd.read_csv('J:\\Machine
Learning\\Class\Practical\\Algorithms\\car_evaluation.csv',names=['buying','maint','doors','perso
ns','leg_boot','safety','class'])

data.head()

data.info()

#Identify target variable

# Convert string categorical values into an integer code using factorize method of pandas library

data['class'],class_names = pd.factorize(data['class'])

print(class_names)

print(data['class'].unique())

# Identify the predictor variables and encode any string variables to equivalent integer codes

data['buying'],_ = pd.factorize(data['buying'])

data['maint'],_ = pd.factorize(data['maint'])

data['doors'],_ = pd.factorize(data['doors'])

data['persons'],_ = pd.factorize(data['persons'])

data['lug_boot'],_ = pd.factorize(data['leg_boot'])
data['safety'],_ = pd.factorize(data['safety'])

data.head()

data.info()

#Select the predictor feature and the target variable

X = data.iloc[:,:-1]

y = data.iloc[:,-1]

#Split training and testing

# split data randomly into 70% training and 30% test

X_train, X_test, y_train, y_test = model_selection.train_test_split(X, y, test_size=0.3,


random_state=123)

#Training/model fitting

model = GaussianNB()

## Fit the model on the training data.

model.fit(X_train, y_train)

#Model parameters study

# use the model to make predictions with the test data

y_pred = model.predict(X_test)

# how did our model perform

count_misclassified = (y_test != y_pred).sum()

print('Misclassified samples: {}'.format(count_misclassified))

accuracy = metrics.accuracy_score(y_test, y_pred)

print('Accuracy: {:.2f}'.format(accuracy))
Example: 2

import pandas as pd

dataset = pd.read_csv('J:\\Machine Learning\\Class\Practical\\Preprocessing\\Data1.csv')

print(dataset.columns)

dataset

X = dataset.iloc[:, :-1].values

Y = dataset.iloc[:, 3].values

from sklearn.preprocessing import Imputer

imputer = Imputer(missing_values = 'NaN', strategy = 'mean', axis=0)

imputer = imputer.fit(X[:, 1:3])

X[:, 1:3] = imputer.transform(X[:, 1:3])

from sklearn.preprocessing import Imputer

imputer = Imputer(missing_values = 'NaN', strategy = 'median', axis=0)

imputer = imputer.fit(X[:, 1:3])

X[:, 1:3] = imputer.transform(X[:, 1:3])

from sklearn.preprocessing import Imputer

imputer = Imputer(missing_values = 'NaN', strategy = 'most_frequent', axis=0)

imputer = imputer.fit(X[:, 1:3])

X[:, 1:3] = imputer.transform(X[:, 1:3])


X

# Encode Categorical Data

from sklearn.preprocessing import LabelEncoder, OneHotEncoder

labelencoder_X = LabelEncoder()

X[:, 0] = labelencoder_X.fit_transform(X[:, 0])

onehotencoder = OneHotEncoder(categorical_features = [0])

X = onehotencoder.fit_transform(X).toarray()

labelencoder_Y = LabelEncoder()

Y = labelencoder_Y.fit_transform(Y)

# Split the data between the Training Data and Test Data

from sklearn.model_selection import train_test_split

X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size = 0.2

,random_state = 0)

X_train

X_test

#Feature Scaling

from sklearn.preprocessing import StandardScaler

sc_X = StandardScaler()

X_train = sc_X.fit_transform(X_train)

X_test = sc_X.transform(X_test)

X_train

X_test
# training the model on training set

from sklearn.naive_bayes import GaussianNB

gnb = GaussianNB()

gnb.fit(X_train, Y_train)

# making predictions on the testing set

y_pred = gnb.predict(X_test)

# comparing actual response values (y_test) with predicted response values (y_pred)

from sklearn import metrics

print("Gaussian Naive Bayes model accuracy(in %):", metrics.accuracy_score(Y_test,


y_pred)*100)

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy