Skip to content
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

fix: Create Visualizer from a copy of the regressor to prevent updates to original regressor #30

Merged
merged 3 commits into from
Jul 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.3.0
rev: v4.6.0
hooks:
- id: check-yaml
- id: end-of-file-fixer
- id: trailing-whitespace
- repo: https://github.com/pycqa/isort
rev: 5.12.0
rev: 5.13.2
hooks:
- id: isort
args: [--settings-path, pyproject.toml]
- repo: https://github.com/psf/black
rev: 22.6.0
rev: 24.4.2
hooks:
- id: black
args: [--config, pyproject.toml]
Expand Down
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## neptune-sklearn 2.1.4

### Fixes
- `create_feature_importance_chart()` now does not update the original coefficients of the regressor ([#30](https://github.com/neptune-ai/neptune-sklearn/pull/30)

## neptune-sklearn 2.1.3

### Fixes
Expand Down
3 changes: 2 additions & 1 deletion src/neptune_sklearn/impl/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@
)
from neptune.new.utils import stringify_unsupported

from copy import deepcopy
from warnings import warn


Expand Down Expand Up @@ -628,7 +629,7 @@ def create_feature_importance_chart(regressor, X_train, y_train):

try:
fig, ax = plt.subplots()
visualizer = FeatureImportances(regressor, is_fitted=True, ax=ax)
visualizer = FeatureImportances(deepcopy(regressor), is_fitted=True, ax=ax)
visualizer.fit(X_train, y_train)
visualizer.finalize()

Expand Down
15 changes: 12 additions & 3 deletions tests/test_e2e.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@

import matplotlib as mpl
import pytest
from numpy import array_equal
from sklearn.cluster import KMeans
from sklearn.dummy import (
DummyClassifier,
DummyRegressor,
)
from sklearn.linear_model import LinearRegression
from sklearn.model_selection import GridSearchCV

import neptune_sklearn as npt_utils
Expand All @@ -35,20 +37,23 @@ def test_classifier_summary(iris):

def test_regressor_summary(diabetes):
with init_run() as run:
model = DummyRegressor()
model = LinearRegression()
model.fit(diabetes.x_train, diabetes.y_train)

original_coef = model.coef_

run["summary"] = npt_utils.create_regressor_summary(
model, diabetes.x_train, diabetes.x_test, diabetes.y_train, diabetes.y_test
)

assert array_equal(model.coef_, original_coef), "Original model coefficients modified."

run.wait()
validate_run(run, log_charts=True)


def test_kmeans_summary(iris):
with init_run() as run:

model = KMeans()
model.fit(iris.x)

Expand Down Expand Up @@ -77,7 +82,11 @@ def test_unsupported_object(diabetes):
)

run["regressor_summary"] = npt_utils.create_regressor_summary(
grid_cv, diabetes.x_train, diabetes.x_test, diabetes.y_train, diabetes.y_test
grid_cv,
diabetes.x_train,
diabetes.x_test,
diabetes.y_train,
diabetes.y_test,
)

run.wait()
Expand Down
Loading