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

[python] error on nan in y for DataFrames #2352

Closed
wants to merge 1 commit into from
Closed
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
2 changes: 1 addition & 1 deletion python-package/lightgbm/sklearn.py
Original file line number Diff line number Diff line change
Expand Up @@ -525,7 +525,7 @@ def fit(self, X, y,
eval_metric = [eval_metric] if isinstance(eval_metric, (string_type, type(None))) else eval_metric
params['metric'] = set(original_metric + eval_metric)

if not isinstance(X, (DataFrame, DataTable)):
if not isinstance(X, DataTable):
_X, _y = _LGBMCheckXY(X, y, accept_sparse=True, force_all_finite=False, ensure_min_samples=2)
_LGBMCheckConsistentLength(_X, _y, sample_weight)
else:
Expand Down
40 changes: 40 additions & 0 deletions tests/python_package_test/test_sklearn.py
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,35 @@ def test_pandas_sparse(self):
pred_dense = gbm.predict(X_test.to_dense(), raw_score=True)
np.testing.assert_allclose(pred_sparse, pred_dense)

@unittest.skipIf(not lgb.compat.PANDAS_INSTALLED, 'pandas is not installed')
def test_nan_y_pd(self):
import pandas as pd

nrows = 1000
ncols = 10
X = pd.DataFrame(np.random.randn(nrows, ncols))
y = pd.Series(np.full(nrows, np.nan))
weight = np.zeros(nrows)
params = {'n_estimators': 20, 'verbose': -1}
params_fit = {'X': X, 'y': y}
gbm = lgb.LGBMRegressor(**params)
self.assertRaises(ValueError, gbm.fit, **params_fit)

@unittest.skipIf(not lgb.compat.PANDAS_INSTALLED, 'pandas is not installed')
def test_nan_handle_pd(self):
import pandas as pd

nrows = 1000
ncols = 10
X = pd.DataFrame(np.random.randn(nrows, ncols))
y = pd.Series(np.random.randn(nrows) + np.full(nrows, 1e30))
weight = np.zeros(nrows)
params = {'n_estimators': 20, 'verbose': -1}
params_fit = {'X': X, 'y': y, 'sample_weight': weight, 'eval_set': (X, y),
'verbose': False, 'early_stopping_rounds': 5}
gbm = lgb.LGBMRegressor(**params).fit(**params_fit)
np.testing.assert_allclose(gbm.evals_result_['training']['l2'], np.nan)

def test_predict(self):
iris = load_iris()
X_train, X_test, y_train, y_test = train_test_split(iris.data, iris.target,
Expand Down Expand Up @@ -638,6 +667,17 @@ def test_nan_handle(self):
gbm = lgb.LGBMRegressor(**params).fit(**params_fit)
np.testing.assert_allclose(gbm.evals_result_['training']['l2'], np.nan)

def test_nan_y_np(self):
nrows = 1000
ncols = 10
X = np.random.randn(nrows, ncols)
y = np.full(nrows, np.nan)
weight = np.zeros(nrows)
params = {'n_estimators': 20, 'verbose': -1}
params_fit = {'X': X, 'y': y}
gbm = lgb.LGBMRegressor(**params)
self.assertRaises(ValueError, gbm.fit, **params_fit)

def test_class_weight(self):
X, y = load_digits(10, True)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
Expand Down