-
Notifications
You must be signed in to change notification settings - Fork 2
Adding Logistic Regression class implementation & utils #41
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
54f7065
utility and testing scripts needed for implementing ML and SSML
b335056
adding Logistic Regression class implementation
bc8cbfc
Merge branch 'utils' into logreg since tests require it
c661099
removing pytests for future ML implementations
a7d4bfe
commenting cross validation test for ssml since it will be included i…
738720e
removing leftovers from migration
b6a2c36
condensing bool return
de820fa
addressing comments from PR review
cfeb32b
Merge branch 'logreg' of github.com:stompsjo/RadClass into logreg
49ed491
splitting utils test between cross_validation and pca
785e424
implementing more meaningful test data for testing cross validation
fd3cf53
changing print statements to logging
56da530
clarifying the testing logic for ML classes
533bbb2
moving default init params to support kwargs
f731a56
adding pragmas for coveralls to ignore PCA functions
c9cb249
pep8
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,161 @@ | ||
| # For hyperopt (parameter optimization) | ||
| from hyperopt import STATUS_OK | ||
| # sklearn models | ||
| from sklearn import linear_model | ||
| # diagnostics | ||
| from sklearn.metrics import balanced_accuracy_score | ||
| from scripts.utils import run_hyperopt | ||
| import joblib | ||
|
|
||
|
|
||
| class LogReg: | ||
| ''' | ||
| Methods for deploying sklearn's logistic regression | ||
| implementation with hyperparameter optimization. | ||
| Data agnostic (i.e. user supplied data inputs). | ||
| TODO: Currently only supports binary classification. | ||
| Add multinomial functions and unit tests. | ||
| Add functionality for regression(?) | ||
| Inputs: | ||
| params: dictionary of logistic regression input functions. | ||
| keys max_iter, tol, and C supported. | ||
| random_state: int/float for reproducible intiailization. | ||
| ''' | ||
|
|
||
| # only binary so far | ||
| def __init__(self, **kwargs): | ||
| # supported keys = ['max_iter', 'tol', 'C'] | ||
| # defaults to a fixed value for reproducibility | ||
| self.random_state = kwargs.pop('random_state', 0) | ||
| # parameters for logistic regression model: | ||
| # defaults to sklearn.linear_model.LogisticRegression default vals | ||
| self.max_iter = kwargs.pop('max_iter', 100) | ||
| self.tol = kwargs.pop('tol', 0.0001) | ||
| self.C = kwargs.pop('C', 1.0) | ||
| self.model = linear_model.LogisticRegression( | ||
| random_state=self.random_state, | ||
| max_iter=self.max_iter, | ||
| tol=self.tol, | ||
| C=self.C | ||
| ) | ||
|
|
||
| def fresh_start(self, params, data_dict): | ||
| ''' | ||
| Required method for hyperopt optimization. | ||
| Trains and tests a fresh logistic regression model | ||
| with given input parameters. | ||
| This method does not overwrite self.model (self.optimize() does). | ||
| Inputs: | ||
| params: dictionary of logistic regression input functions. | ||
| keys max_iter, tol, and C supported. | ||
| data_dict: compact data representation with the four requisite | ||
| data structures used for training and testing a model. | ||
| keys trainx, trainy, testx, and testy required. | ||
| ''' | ||
|
|
||
| # unpack data | ||
| trainx = data_dict['trainx'] | ||
| trainy = data_dict['trainy'] | ||
| testx = data_dict['testx'] | ||
| testy = data_dict['testy'] | ||
|
|
||
| # supervised logistic regression | ||
| clf = LogReg(params=params, random_state=self.random_state) | ||
| # train and test model | ||
| clf.train(trainx, trainy) | ||
| # uses balanced_accuracy accounts for class imbalanced data | ||
| clf_pred, acc = clf.predict(testx, testy) | ||
|
|
||
| # loss function minimizes misclassification | ||
| return {'loss': 1-acc, | ||
| 'status': STATUS_OK, | ||
| 'model': clf.model, | ||
| 'params': params, | ||
| 'accuracy': acc} | ||
|
|
||
| def optimize(self, space, data_dict, max_evals=50, verbose=True): | ||
| ''' | ||
| Wrapper method for using hyperopt (see utils.run_hyperopt | ||
| for more details). After hyperparameter optimization, results | ||
| are stored, the best model -overwrites- self.model, and the | ||
| best params -overwrite- self.params. | ||
| Inputs: | ||
| space: a hyperopt compliant dictionary with defined optimization | ||
| spaces. For example: | ||
| # quniform returns float, some parameters require int; | ||
| # use this to force int | ||
| space = {'max_iter': scope.int(hp.quniform('max_iter', | ||
| 10, | ||
| 10000, | ||
| 10)), | ||
| 'tol' : hp.loguniform('tol', 1e-5, 1e-1), | ||
| 'C' : hp.uniform('C', 0.001,1000.0) | ||
| } | ||
| See hyperopt docs for more information. | ||
| data_dict: compact data representation with the four requisite | ||
| data structures used for training and testing a model. | ||
| keys trainx, trainy, testx, testy required. | ||
| max_evals: the number of epochs for hyperparameter optimization. | ||
| Each iteration is one set of hyperparameters trained | ||
| and tested on a fresh model. Convergence for simpler | ||
| models like logistic regression typically happens well | ||
| before 50 epochs, but can increase as more complex models, | ||
| more hyperparameters, and a larger hyperparameter space is tested. | ||
| verbose: boolean. If true, print results of hyperopt. | ||
| If false, print only the progress bar for optimization. | ||
| ''' | ||
|
|
||
| best, worst = run_hyperopt(space=space, | ||
| model=self.fresh_start, | ||
| data_dict=data_dict, | ||
| max_evals=max_evals, | ||
| verbose=verbose) | ||
|
|
||
| # save the results of hyperparameter optimization | ||
| self.best = best | ||
| self.model = best['model'] | ||
| self.params = best['params'] | ||
| self.worst = worst | ||
|
|
||
| def train(self, trainx, trainy): | ||
| ''' | ||
| Wrapper method for sklearn's logisitic regression training method. | ||
| Inputs: | ||
| trainx: nxm feature vector/matrix for training model. | ||
| trainy: nxk class label vector/matrix for training model. | ||
| ''' | ||
|
|
||
| # supervised logistic regression | ||
| self.model.fit(trainx, trainy) | ||
|
|
||
| def predict(self, testx, testy=None): | ||
| ''' | ||
| Wrapper method for sklearn's logistic regression predict method. | ||
| Inputs: | ||
| testx: nxm feature vector/matrix for testing model. | ||
| testy: nxk class label vector/matrix for training model. | ||
| optional: if included, the predicted classes -and- | ||
| the resulting classification accuracy will be returned. | ||
| ''' | ||
|
|
||
| pred = self.model.predict(testx) | ||
|
|
||
| acc = None | ||
| if testy is not None: | ||
| # uses balanced_accuracy_score to account for class imbalance | ||
| acc = balanced_accuracy_score(testy, pred) | ||
|
|
||
| return pred, acc | ||
|
|
||
| def save(self, filename): | ||
| ''' | ||
| Save class instance to file using joblib. | ||
| Inputs: | ||
| filename: string filename to save object to file under. | ||
| The file must be saved with extension .joblib. | ||
| Added to filename if not included as input. | ||
| ''' | ||
|
|
||
| if filename[-7:] != '.joblib': | ||
| filename += '.joblib' | ||
| joblib.dump(self, filename) | ||
Empty file.
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -2,3 +2,10 @@ numpy | |
| h5py | ||
| progressbar2 | ||
| scipy>=1.7.0 | ||
| scikit-learn | ||
| hyperopt | ||
| matplotlib | ||
| seaborn | ||
| joblib | ||
| torch | ||
| shadow-ssml | ||
Empty file.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.