Untitled

mail@pastecode.io avatar
unknown
plain_text
18 days ago
1.3 kB
3
Indexable
Never
import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.naive_bayes import GaussianNB
from sklearn.metrics import accuracy_score, classification_report

# Step 1: Generate synthetic data
np.random.seed(42)
num_samples = 1660
mean_class1 = [1, 2]
cov_class1 = [[1, 0.5], [0.5, 1]]
mean_class2 = [4, 5]
cov_class2 = [[1, -0.5], [-0.5, 1]]

class1_data = np.random.multivariate_normal(mean_class1, cov_class1, num_samples // 2)
class2_data = np.random.multivariate_normal(mean_class2, cov_class2, num_samples // 2)

X = np.concatenate((class1_data, class2_data), axis=0)
y = np.array([0] * (num_samples // 2) + [1] * (num_samples // 2))

# Step 2: Perform data preprocessing (not needed for this synthetic data)

# Step 3: Split data 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)

# Step 4: Train the Naive Bayes classifier
model = GaussianNB()
model.fit(X_train, y_train)

# Step 5: Test the classifier with new data and analyze the results
y_pred = model.predict(X_test)

# Calculate accuracy
accuracy = accuracy_score(y_test, y_pred)
print("Accuracy:", accuracy)

# Generate classification report
print("Classification Report:")
print(classification_report(y_test, y_pred))
Leave a Comment