Exp 3 Bi
Exp 3 Bi
03
Aim: Implement and evaluate using Python
a) Classification Algorithm – Naïve Bayes
THEORY
Naive Bayes Classifier Algorithm
Naive Bayes algorithm is a supervised learning algorithm, which is based on Bayes theorem
and used for solving classification problems.It is mainly used in text classification that includes
a high-dimensional training dataset.Naïve Bayes Classifier is one of the simple and most
effective Classification algorithms which helps in building the fast machine learning models
that can make quick predictions.It is a probabilistic classifier, which means it predicts on the
basis of the probability of an object.Some popular examples of Naïve Bayes Algorithm are
spam filtration, Sentimental analysis, and classifying articles.
Bayes' Theorem
Bayes' theorem is also known as Bayes' Rule or Bayes' law, which is used to determine the
probability of a hypothesis with prior knowledge. It depends on the conditional probability.
The formula for Bayes' theorem is given as:
Where,
P(A|B) is Posterior probability: Probability of hypothesis A on the observed event B. P(B|
A) is Likelihood probability: Probability of the evidence given that the probability of a
hypothesis is true.
# Load the
dataset try:
user_data = pd.read_csv("userdata.csv") # Change the file path accordingly
except FileNotFoundError:
print("Error: File not found.")
exit()
# Check if the 'target' column
exists if 'target' not in
user_data.columns:
print("Error: 'target' column not found in the dataset.")
exit()
# Split dataset into features and labels
X = user_data.drop(columns=['target']) #
Features y = user_data['target'] # Labels
# Encode categorical labels
label_encoder =
LabelEncoder()
y = label_encoder.fit_transform(y)
# Split the dataset into training and test sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
Alam Umar|Roll no.16|A1|BI Lab|TE-IT Pg.2
In the above code, we have loaded the dataset into our program using "dataset =
pd.read_csv('user_data.csv'). The loaded dataset is divided into training and test set, and
then we have scaled the feature variable.
Output:
In the above output we can see that the Naïve Bayes classifier has segregated the data points
with the fine boundary. It is Gaussian curve as we have used GaussianNB classifier in our code.
Alam Umar|Roll no.16|A1|BI Lab|TE-IT Pg.6
CONCLUSION
Output:
In the above output we can see that the Naïve Bayes classifier has segregated the data points
with the fine boundary. It is Gaussian curve as we have used GaussianNB classifier in our code.
Alam Umar|Roll no.16|A1|BI Lab|TE-IT
Pg.11
CONCLUSION