mirror of
https://github.com/titanscouting/tra-analysis.git
synced 2024-11-10 06:54:44 +00:00
5d5d6c4c5e
fixed headers Signed-off-by: Arthur Lu <learthurgo@gmail.com>
65 lines
2.4 KiB
Python
65 lines
2.4 KiB
Python
# Titan Robotics Team 2022: NaiveBayes submodule
|
|
# Written by Arthur Lu
|
|
# Notes:
|
|
# this should be imported as a python module using 'from tra_analysis import NaiveBayes'
|
|
# setup:
|
|
|
|
__version__ = "1.0.1"
|
|
|
|
__changelog__ = """changelog:
|
|
1.0.1:
|
|
- optimized imports
|
|
1.0.0:
|
|
- ported analysis.NaiveBayes() here
|
|
- removed classness
|
|
"""
|
|
|
|
__author__ = (
|
|
"Arthur Lu <learthurgo@gmail.com>",
|
|
)
|
|
|
|
__all__ = [
|
|
'gaussian',
|
|
'multinomial'
|
|
'bernoulli',
|
|
'complement'
|
|
]
|
|
|
|
import sklearn
|
|
from . import ClassificationMetric
|
|
|
|
def gaussian(data, labels, test_size = 0.3, priors = None, var_smoothing = 1e-09):
|
|
|
|
data_train, data_test, labels_train, labels_test = sklearn.model_selection.train_test_split(data, labels, test_size=test_size, random_state=1)
|
|
model = sklearn.naive_bayes.GaussianNB(priors = priors, var_smoothing = var_smoothing)
|
|
model.fit(data_train, labels_train)
|
|
predictions = model.predict(data_test)
|
|
|
|
return model, ClassificationMetric(predictions, labels_test)
|
|
|
|
def multinomial(data, labels, test_size = 0.3, alpha=1.0, fit_prior=True, class_prior=None):
|
|
|
|
data_train, data_test, labels_train, labels_test = sklearn.model_selection.train_test_split(data, labels, test_size=test_size, random_state=1)
|
|
model = sklearn.naive_bayes.MultinomialNB(alpha = alpha, fit_prior = fit_prior, class_prior = class_prior)
|
|
model.fit(data_train, labels_train)
|
|
predictions = model.predict(data_test)
|
|
|
|
return model, ClassificationMetric(predictions, labels_test)
|
|
|
|
def bernoulli(data, labels, test_size = 0.3, alpha=1.0, binarize=0.0, fit_prior=True, class_prior=None):
|
|
|
|
data_train, data_test, labels_train, labels_test = sklearn.model_selection.train_test_split(data, labels, test_size=test_size, random_state=1)
|
|
model = sklearn.naive_bayes.BernoulliNB(alpha = alpha, binarize = binarize, fit_prior = fit_prior, class_prior = class_prior)
|
|
model.fit(data_train, labels_train)
|
|
predictions = model.predict(data_test)
|
|
|
|
return model, ClassificationMetric(predictions, labels_test)
|
|
|
|
def complement(data, labels, test_size = 0.3, alpha=1.0, fit_prior=True, class_prior=None, norm=False):
|
|
|
|
data_train, data_test, labels_train, labels_test = sklearn.model_selection.train_test_split(data, labels, test_size=test_size, random_state=1)
|
|
model = sklearn.naive_bayes.ComplementNB(alpha = alpha, fit_prior = fit_prior, class_prior = class_prior, norm = norm)
|
|
model.fit(data_train, labels_train)
|
|
predictions = model.predict(data_test)
|
|
|
|
return model, ClassificationMetric(predictions, labels_test) |