Skip to content

Commit 2ff7ea7

Browse files
Merge pull request #301 from shyamcody/dev
added provide_models method.
2 parents 27ec3ca + 6e55d6e commit 2ff7ea7

File tree

1 file changed

+66
-1
lines changed

1 file changed

+66
-1
lines changed

lazypredict/Supervised.py

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@ class LazyClassifier:
174174
>>> X_train, X_test, y_train, y_test = train_test_split(X, y,test_size=.5,random_state =123)
175175
>>> clf = LazyClassifier(verbose=0,ignore_warnings=True, custom_metric=None)
176176
>>> models,predictions = clf.fit(X_train, X_test, y_train, y_test)
177+
>>> model_dictionary = clf.provide_models(X_train,X_test,y_train,y_test)
177178
>>> models
178179
| Model | Accuracy | Balanced Accuracy | ROC AUC | F1 Score | Time Taken |
179180
|:-------------------------------|-----------:|--------------------:|----------:|-----------:|-------------:|
@@ -221,6 +222,7 @@ def __init__(
221222
self.ignore_warnings = ignore_warnings
222223
self.custom_metric = custom_metric
223224
self.predictions = predictions
225+
self.models = {}
224226
self.random_state = random_state
225227

226228
def fit(self, X_train, X_test, y_train, y_test):
@@ -294,6 +296,7 @@ def fit(self, X_train, X_test, y_train, y_test):
294296
)
295297

296298
pipe.fit(X_train, y_train)
299+
self.models[name] = pipe
297300
y_pred = pipe.predict(X_test)
298301
accuracy = accuracy_score(y_test, y_pred, normalize=True)
299302
b_accuracy = balanced_accuracy_score(y_test, y_pred)
@@ -375,6 +378,35 @@ def fit(self, X_train, X_test, y_train, y_test):
375378
predictions_df = pd.DataFrame.from_dict(predictions)
376379
return scores, predictions_df if self.predictions is True else scores
377380

381+
def provide_models(self, X_train, X_test, y_train, y_test):
382+
"""
383+
This function returns all the model objects trained in fit function.
384+
If fit is not called already, then we call fit and then return the models.
385+
Parameters
386+
----------
387+
X_train : array-like,
388+
Training vectors, where rows is the number of samples
389+
and columns is the number of features.
390+
X_test : array-like,
391+
Testing vectors, where rows is the number of samples
392+
and columns is the number of features.
393+
y_train : array-like,
394+
Training vectors, where rows is the number of samples
395+
and columns is the number of features.
396+
y_test : array-like,
397+
Testing vectors, where rows is the number of samples
398+
and columns is the number of features.
399+
Returns
400+
-------
401+
models: dict-object,
402+
Returns a dictionary with each model pipeline as value
403+
with key as name of models.
404+
"""
405+
if len(self.models.keys()) == 0:
406+
self.fit(X_train,X_test,y_train,y_test)
407+
408+
return self.models
409+
378410

379411
# Helper class for performing classification
380412

@@ -406,8 +438,10 @@ class LazyRegressor:
406438
>>> offset = int(X.shape[0] * 0.9)
407439
>>> X_train, y_train = X[:offset], y[:offset]
408440
>>> X_test, y_test = X[offset:], y[offset:]
409-
>>> reg = LazyRegressor(verbose=0,ignore_warnings=False, custom_metric=None )
441+
>>> reg = LazyRegressor(verbose=0,ignore_warnings=False, custom_metric=None )
410442
>>> models,predictions = reg.fit(X_train, X_test, y_train, y_test)
443+
>>> model_dictionary = clf.provide_models(X_train,X_test,y_train,y_test)
444+
>>> models
411445
| Model | R-Squared | RMSE | Time Taken |
412446
|:------------------------------|------------:|---------:|-------------:|
413447
| SVR | 0.877199 | 2.62054 | 0.0330021 |
@@ -460,6 +494,7 @@ def __init__(
460494
self.ignore_warnings = ignore_warnings
461495
self.custom_metric = custom_metric
462496
self.predictions = predictions
497+
self.models = {}
463498
self.random_state = random_state
464499

465500
def fit(self, X_train, X_test, y_train, y_test):
@@ -531,6 +566,7 @@ def fit(self, X_train, X_test, y_train, y_test):
531566
steps=[("preprocessor", preprocessor), ("regressor", model())]
532567
)
533568
pipe.fit(X_train, y_train)
569+
self.models[name] = pipe
534570
y_pred = pipe.predict(X_test)
535571
r_squared = r2_score(y_test, y_pred)
536572
rmse = np.sqrt(mean_squared_error(y_test, y_pred))
@@ -589,6 +625,35 @@ def fit(self, X_train, X_test, y_train, y_test):
589625
predictions_df = pd.DataFrame.from_dict(predictions)
590626
return scores, predictions_df if self.predictions is True else scores
591627

628+
def provide_models(self, X_train, X_test, y_train, y_test):
629+
"""
630+
This function returns all the model objects trained in fit function.
631+
If fit is not called already, then we call fit and then return the models.
632+
Parameters
633+
----------
634+
X_train : array-like,
635+
Training vectors, where rows is the number of samples
636+
and columns is the number of features.
637+
X_test : array-like,
638+
Testing vectors, where rows is the number of samples
639+
and columns is the number of features.
640+
y_train : array-like,
641+
Training vectors, where rows is the number of samples
642+
and columns is the number of features.
643+
y_test : array-like,
644+
Testing vectors, where rows is the number of samples
645+
and columns is the number of features.
646+
Returns
647+
-------
648+
models: dict-object,
649+
Returns a dictionary with each model pipeline as value
650+
with key as name of models.
651+
"""
652+
if len(self.models.keys()) == 0:
653+
self.fit(X_train,X_test,y_train,y_test)
654+
655+
return self.models
656+
592657

593658
Regression = LazyRegressor
594659
Classification = LazyClassifier

0 commit comments

Comments
 (0)