hanelo / knn-traditional-knn-bayesian Goto Github PK
View Code? Open in Web Editor NEWComparison btw KNN traditional and KNN bayesian while using the bayesian approach
License: MIT License
Comparison btw KNN traditional and KNN bayesian while using the bayesian approach
License: MIT License
the knn traditional file could be decluttered:
import numpy as np
from sklearn.datasets import load_iris
from sklearn.neighbors import KNeighborsClassifier
from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score
def traditional_knn(x_train, y_train, x_test, k):
knn = KNeighborsClassifier(n_neighbors=k)
knn.fit(x_train, y_train)
test_predictions = knn.predict(x_test)
return test_predictions
def evaluate_classification(y_true, y_pred):
accuracy = accuracy_score(y_true, y_pred)
precision = precision_score(y_true, y_pred, average=None)
recall = recall_score(y_true, y_pred, average=None)
f1 = f1_score(y_true, y_pred, average=None)
return accuracy, precision, recall, f1
# Load the Iris dataset
iris = load_iris()
X = iris.data
y = iris.target
# Set the value of k
k = 3
# Split the dataset into training and test sets
np.random.seed(42) # Set seed for reproducibility
indices = np.random.permutation(len(X))
train_size = int(0.8 * len(X))
X_train, X_test = X[indices[:train_size]], X[indices[train_size:]]
y_train, y_test = y[indices[:train_size]], y[indices[train_size:]]
# Run traditional k-NN with MAP estimation
traditional_test_predictions = traditional_knn(X_train, y_train, X_test, k)
# Compute evaluation metrics for traditional k-NN
traditional_test_accuracy, traditional_test_precision, traditional_test_recall, traditional_test_f1_score = evaluate_classification(y_test, traditional_test_predictions)
print("Traditional k-NN Results:")
print(f"Test Accuracy: {traditional_test_accuracy}")
print(f"Test Precision: {traditional_test_precision}")
print(f"Test Recall: {traditional_test_recall}")
print(f"Test F1-score: {traditional_test_f1_score}")
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.