You are on page 1of 2

#Lab Examination

#Name: Syed Waqar Hussain


#ID: BSE-20S-037

import numpy as np
from sklearn.datasets import make_classification

# Set seed for reproducibility


np.random.seed(42)

# Define the parameters for the synthetic dataset


num_samples = 50000
num_features = 15
num_classes = 4

# Create synthetic dataset


X, y = make_classification(
n_samples=num_samples,
n_features=num_features,
n_classes=num_classes,
n_clusters_per_class=1,
n_informative=4,
n_redundant=1,
random_state=42
)

# Display the shape of the dataset


print(f"Shape of X: {X.shape}, Shape of y: {y.shape}")

Shape of X: (50000, 5), Shape of y: (50000,)

from sklearn.model_selection import train_test_split


from sklearn.neighbors import KNeighborsClassifier
from sklearn.naive_bayes import GaussianNB
from sklearn.metrics import accuracy_score

# Split the dataset into training and testing sets


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

# K-nearest neighbors algorithm


knn_model = KNeighborsClassifier(n_neighbors=5)
knn_model.fit(X_train, y_train)
knn_predictions = knn_model.predict(X_test)
knn_accuracy = accuracy_score(y_test, knn_predictions)
print(f"Accuracy of KNN: {knn_accuracy:.4f}")

Accuracy of KNN: 0.9181

nb_model = GaussianNB()
nb_model.fit(X_train, y_train)
nb_predictions = nb_model.predict(X_test)
nb_accuracy = accuracy_score(y_test, nb_predictions)
print(f"Accuracy of Naive Bayes: {nb_accuracy:.4f}")

Accuracy of Naive Bayes: 0.8449

You might also like