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

LAB # 08 Naive Bayes.ipynb - Colab

Uploaded by

Abdullah khan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

LAB # 08 Naive Bayes.ipynb - Colab

Uploaded by

Abdullah khan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

09/12/2024, 22:49 LAB # 08 Naive Bayes.

ipynb - Colab

from sklearn.datasets import load_breast_cancer


import pandas as pd

# Load the Breast Cancer dataset


cancer = load_breast_cancer()

# Convert dataset to a pandas DataFrame for easy exploration


data = pd.DataFrame(data=cancer.data, columns=cancer.feature_names)

# Add the target column to the DataFrame


data['target'] = cancer.target

# Display the first 5 rows (head) of the DataFrame


print("Head of the dataset:")
print(data.head())

# Show the shape of the DataFrame (rows and columns)


print("\nNumber of rows and columns in the dataset:")
print(data.shape)

# Display the feature names


print("\nFeature Names:")
print(cancer.feature_names)

# Display the target names (labels)


print("\nTarget Names:")
print(cancer.target_names)

Head of the dataset:


mean radius mean texture mean perimeter mean area mean smoothness \
0 17.99 10.38 122.80 1001.0 0.11840
1 20.57 17.77 132.90 1326.0 0.08474
2 19.69 21.25 130.00 1203.0 0.10960
3 11.42 20.38 77.58 386.1 0.14250
4 20.29 14.34 135.10 1297.0 0.10030

mean compactness mean concavity mean concave points mean symmetry \


0 0.27760 0.3001 0.14710 0.2419
1 0.07864 0.0869 0.07017 0.1812
2 0.15990 0.1974 0.12790 0.2069
3 0.28390 0.2414 0.10520 0.2597
4 0.13280 0.1980 0.10430 0.1809

mean fractal dimension ... worst texture worst perimeter worst area \
0 0.07871 ... 17.33 184.60 2019.0
1 0.05667 ... 23.41 158.80 1956.0
2 0.05999 ... 25.53 152.50 1709.0
3 0.09744 ... 26.50 98.87 567.7
4 0.05883 ... 16.67 152.20 1575.0

worst smoothness worst compactness worst concavity worst concave points \


0 0.1622 0.6656 0.7119 0.2654
1 0.1238 0.1866 0.2416 0.1860
2 0.1444 0.4245 0.4504 0.2430

https://colab.research.google.com/drive/1HTf1EePlrv_g08JjKjPHXlT6lwLjMvAN#printMode=true 1/3
09/12/2024, 22:49 LAB # 08 Naive Bayes.ipynb - Colab
3 0.2098 0.8663 0.6869 0.2575
4 0.1374 0.2050 0.4000 0.1625

worst symmetry worst fractal dimension target


0 0.4601 0.11890 0
1 0.2750 0.08902 0
2 0.3613 0.08758 0
3 0.6638 0.17300 0
4 0.2364 0.07678 0

[5 rows x 31 columns]

Number of rows and columns in the dataset:


(569, 31)

Feature Names:
['mean radius' 'mean texture' 'mean perimeter' 'mean area'
'mean smoothness' 'mean compactness' 'mean concavity'
'mean concave points' 'mean symmetry' 'mean fractal dimension'
'radius error' 'texture error' 'perimeter error' 'area error'
'smoothness error' 'compactness error' 'concavity error'
'concave points error' 'symmetry error' 'fractal dimension error'
'worst radius' 'worst texture' 'worst perimeter' 'worst area'
'worst smoothness' 'worst compactness' 'worst concavity'
'worst concave points' 'worst symmetry' 'worst fractal dimension']

Target Names:
['malignant' 'benign']

from sklearn.datasets import load_breast_cancer


from sklearn.model_selection import train_test_split
from sklearn.naive_bayes import GaussianNB
from sklearn.metrics import accuracy_score, classification_report, confusion_matrix

# Load Breast Cancer dataset


cancer = load_breast_cancer()
X = cancer.data # Features
y = cancer.target # Labels (malignant or benign)

# Split data into training and testing sets


X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)

# Initialize Gaussian Naive Bayes model


gnb = GaussianNB()

# Train the model


gnb.fit(X_train, y_train)

# Make predictions
y_pred = gnb.predict(X_test)

# Evaluate the model


print("Accuracy:", accuracy_score(y_test, y_pred))
# print("Classification Report:\n", classification_report(y_test, y_pred))
print("Confusion Matrix:\n", confusion_matrix(y_test, y_pred))

https://colab.research.google.com/drive/1HTf1EePlrv_g08JjKjPHXlT6lwLjMvAN#printMode=true 2/3
09/12/2024, 22:49 LAB # 08 Naive Bayes.ipynb - Colab
Accuracy: 0.9415204678362573
Confusion Matrix:
[[ 57 6]
[ 4 104]]

Start coding or generate with AI.

Start coding or generate with AI.

https://colab.research.google.com/drive/1HTf1EePlrv_g08JjKjPHXlT6lwLjMvAN#printMode=true 3/3

You might also like