This repository was archived by the owner on Jan 8, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 59
Cross Validation Added #407
Open
prakrutisingh24
wants to merge
13
commits into
gramener:master
Choose a base branch
from
prakrutisingh24:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 8 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
cdc0e5a
new scoring metrics added
e737117
new changes
64e6c22
made the requested changes
babc4bf
removed unnecessary files and print statements + fixed _predict()
b1683b8
removed empty lines and spaces + fixed if..else indentation
ddd5d73
new changes
4b672cd
added Cross Validation results
1063207
cross validation
09b49f1
requested changes made
5f24173
single input for CV
eaa58dc
final suggested changes done
e175cfe
Merge branch 'master' of https://github.com/gramener/gramex
5f631c1
resolved conflicts
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,6 +20,9 @@ | |
| from slugify import slugify | ||
| from tornado.gen import coroutine | ||
| from tornado.web import HTTPError | ||
| from sklearn.metrics import get_scorer | ||
| from sklearn.model_selection import cross_val_predict, cross_val_score | ||
| from sklearn.model_selection import cross_val_predict, cross_val_score | ||
|
|
||
| op = os.path | ||
| MLCLASS_MODULES = [ | ||
|
|
@@ -39,7 +42,7 @@ | |
| 'pipeline': True, | ||
| 'nums': [], | ||
| 'cats': [], | ||
| 'target_col': None, | ||
| 'target_col': None | ||
| } | ||
| ACTIONS = ['predict', 'score', 'append', 'train', 'retrain'] | ||
| DEFAULT_TEMPLATE = op.join(op.dirname(__file__), '..', 'apps', 'mlhandler', 'template.html') | ||
|
|
@@ -103,7 +106,6 @@ def setup(cls, data=None, model={}, config_dir='', **kwargs): | |
|
|
||
| cls.set_opt('class', model.get('class')) | ||
| cls.set_opt('params', model.get('params', {})) | ||
|
|
||
| if op.exists(cls.model_path): # If the pkl exists, load it | ||
| cls.model = joblib.load(cls.model_path) | ||
| elif data is not None: | ||
|
|
@@ -112,14 +114,26 @@ def setup(cls, data=None, model={}, config_dir='', **kwargs): | |
| data = cls._filtercols(data) | ||
| data = cls._filterrows(data) | ||
| cls.model = cls._assemble_pipeline(data, mclass=mclass, params=params) | ||
|
|
||
| # train the model | ||
| target = data[target_col] | ||
| train = data[[c for c in data if c != target_col]] | ||
| # cross validation | ||
| mod = cls.modelFunction() | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is not required. The model is already present as |
||
| CVscore = cross_val_score(mod, train, target) | ||
| CV = sum(CVscore)/len(CVscore) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| print('CV score: ', CV) | ||
| gramex.service.threadpool.submit( | ||
| _fit, cls.model, train, target, cls.model_path, cls.name) | ||
| cls.config_store.flush() | ||
|
|
||
| @classmethod | ||
| def modelFunction(cls, mclass = ''): | ||
| model_kwargs = cls.config_store.load('model', {}) | ||
| mclass = model_kwargs.get('class', False) | ||
| if mclass: | ||
| model = search_modelclass(mclass)(**model_kwargs.get('params', {})) | ||
| return model | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This function is not required. |
||
|
|
||
| @classmethod | ||
| def load_data(cls, default=pd.DataFrame()): | ||
| try: | ||
|
|
@@ -268,6 +282,10 @@ def _predict(self, data=None, score_col=''): | |
| self.model = cache.open(self.model_path, joblib.load) | ||
| try: | ||
| target = data.pop(score_col) | ||
| metric = self.get_argument('_metric', False) | ||
| if metric: | ||
| scorer = get_scorer(metric) | ||
| return scorer(self.model, data, target) | ||
| return self.model.score(data, target) | ||
| except KeyError: | ||
| # Set data in the same order as the transformer requests | ||
|
|
||
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This line appears twice.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This extra line is unnecessary.