-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(Metalearner): Implemented Metalearner Logistic Regression
- Loading branch information
Showing
3 changed files
with
96 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
#==============================================================================# | ||
# Author: Dominik Müller # | ||
# Copyright: 2022 IT-Infrastructure for Translational Medical Research, # | ||
# University of Augsburg # | ||
# # | ||
# This program is free software: you can redistribute it and/or modify # | ||
# it under the terms of the GNU General Public License as published by # | ||
# the Free Software Foundation, either version 3 of the License, or # | ||
# (at your option) any later version. # | ||
# # | ||
# This program is distributed in the hope that it will be useful, # | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of # | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # | ||
# GNU General Public License for more details. # | ||
# # | ||
# You should have received a copy of the GNU General Public License # | ||
# along with this program. If not, see <http://www.gnu.org/licenses/>. # | ||
#==============================================================================# | ||
#-----------------------------------------------------# | ||
# Library imports # | ||
#-----------------------------------------------------# | ||
# External libraries | ||
import pickle | ||
from sklearn.linear_model import LogisticRegression | ||
import numpy as np | ||
# Internal libraries/scripts | ||
from aucmedi.ensemble.metalearner.ml_base import Metalearner_Base | ||
|
||
#-----------------------------------------------------# | ||
# Metalearner: Logistic Regression # | ||
#-----------------------------------------------------# | ||
class Logistic_Regression(Metalearner_Base): | ||
""" A Logistic Regression based Metalearner. | ||
This class should be passed to a Ensemble function like Stacking for combining predictions. | ||
!!! warning | ||
Can only be utilized for binary and multi-class tasks. | ||
Does not work on multi-label annotations! | ||
""" | ||
#---------------------------------------------# | ||
# Initialization # | ||
#---------------------------------------------# | ||
def __init__(self): | ||
self.model = LogisticRegression(random_state=0, | ||
solver="newton-cg", | ||
multi_class="multinomial") | ||
|
||
#---------------------------------------------# | ||
# Training # | ||
#---------------------------------------------# | ||
def training(self, x, y): | ||
# Preprocess to sparse encoding | ||
y = np.argmax(y, axis=-1) | ||
# Train model | ||
self.model = self.model.fit(x, y) | ||
|
||
#---------------------------------------------# | ||
# Prediction # | ||
#---------------------------------------------# | ||
def prediction(self, data): | ||
# Compute prediction probabilities via fitted model | ||
pred = self.model.predict_proba(data) | ||
# Return results as NumPy array | ||
return pred | ||
|
||
#---------------------------------------------# | ||
# Dump Model to Disk # | ||
#---------------------------------------------# | ||
def dump(self, path): | ||
# Dump model to disk via pickle | ||
with open(path, "wb") as pickle_writer: | ||
pickle.dump(self.model, pickle_writer) | ||
|
||
#---------------------------------------------# | ||
# Load Model from Disk # | ||
#---------------------------------------------# | ||
def load(self, path): | ||
# Load model from disk via pickle | ||
with open(path, "rb") as pickle_reader: | ||
self.model = pickle.load(pickle_reader) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters