Commit a423bd27 authored by Leodegario Lorenzo II's avatar Leodegario Lorenzo II
Browse files

Add Voting Models

parent 51ff87af
......@@ -62,6 +62,7 @@ from sklearn.ensemble import HistGradientBoostingRegressor
from sklearn.ensemble import AdaBoostClassifier, AdaBoostRegressor
from sklearn.ensemble import ExtraTreesClassifier, ExtraTreesRegressor
from sklearn.neural_network import MLPClassifier, MLPRegressor
from sklearn.ensemble import VotingClassifier, VotingRegressor
# XGBoost, Catboost, and LightGBM requires library installation
......@@ -668,7 +669,10 @@ class MLModels:
start_time = time.time()
# Initialize and train the model
clf = self.model(**{self._setting_name: s})
if self._setting_name is not None:
clf = self.model(**{self._setting_name: s})
clf = self.model, y_train)
# Get train time
......@@ -1850,4 +1854,72 @@ class CNB(NaiveBayesClassifier):
model = ComplementNB
class BNB(NaiveBayesClassifier):
model = BernoulliNB
\ No newline at end of file
model = BernoulliNB
# Voting Models
class VotingModel(MLModels):
# Intitialize settings
_setting = [None]
_setting_name = None
def __init__(self, models, params, task, voting='hard', weights=None):
Initialize Voting model using selected models and parameters
self : MLModels object
Voting model object
models : list of str
List of str to be used in creating the ensemble.
See MLModels.list_all_methods() for possible options.
params : list of dict
List of dictionary of hyperparameters of the selected model.
task : str
Set as 'Classification' or 'Regression' depending on the task
voting : 'hard' or 'soft'
Set voting condition
weights : array like, default=None
Set pre-defined weights of voting power of each classifier
# Initialize classifier container
ests = []
# Iterate through all selected models and corresponding parameters
for m, args in zip(models, params):
ests.append((m, MLModels.all_methods()[task][m].model()
# Set as model estimator
self.ests = ests
# Initialize model
if task == 'Classification':
self.model = VotingClassifier(ests, voting=voting,
self.task = task
elif task == 'Regression':
self.model = VotingRegressor(ests, voting=voting, weights=weights)
self.task = task
def fit(self, X, y):
"""Fit the estimators using the input `X` and target `y` data""", y)
def predict(self, X):
"""Return model predictions given `X` data"""
return self.model.predict(X)
def score(self, X, y):
"""Return default accuracy setting from the given data"""
return self.model.score(X, y)
def summary(self):
"""Print a summary of train and test result of the voting model"""
print("Voting Model Performance Summary")
print(f"Task: {self.task}\n")
print("Training Accuracy: {:.2f}%".format(
print("Test Accuracy: {:.2f}%".format(self.test_accuracy[0]*100))
print("Training Time: {:.4f} secs".format(self.train_time[0]))
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment