Skip to content

Commit

Permalink
[python] use the same zip function in 2 and 3 Python (#1681)
Browse files Browse the repository at this point in the history
  • Loading branch information
StrikerRUS authored and guolinke committed Sep 20, 2018
1 parent 2c5409b commit ed92296
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 8 deletions.
2 changes: 1 addition & 1 deletion python-package/lightgbm/basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ def _data_from_pandas(data, feature_name, categorical_feature, pandas_categorica
else:
if len(cat_cols) != len(pandas_categorical):
raise ValueError('train and valid dataset categorical_feature do not match.')
for col, category in zip(cat_cols, pandas_categorical):
for col, category in zip_(cat_cols, pandas_categorical):
if list(data[col].cat.categories) != list(category):
data[col] = data[col].cat.set_categories(category)
if len(cat_cols): # cat_cols is pandas Index object
Expand Down
6 changes: 3 additions & 3 deletions python-package/lightgbm/engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from . import callback
from .basic import Booster, Dataset, LightGBMError, _InnerPredictor
from .compat import (SKLEARN_INSTALLED, _LGBMGroupKFold, _LGBMStratifiedKFold,
integer_types, range_, string_type)
integer_types, range_, zip_, string_type)


def train(params, train_set, num_boost_round=100,
Expand Down Expand Up @@ -187,7 +187,7 @@ def train(params, train_set, num_boost_round=100,
booster = Booster(params=params, train_set=train_set)
if is_valid_contain_train:
booster.set_train_data_name(train_data_name)
for valid_set, name_valid_set in zip(reduced_valid_sets, name_valid_sets):
for valid_set, name_valid_set in zip_(reduced_valid_sets, name_valid_sets):
booster.add_valid(valid_set, name_valid_set)
finally:
train_set._reverse_update_params()
Expand Down Expand Up @@ -285,7 +285,7 @@ def _make_n_folds(full_data, folds, nfold, params, seed, fpreproc=None, stratifi
kstep = int(num_data / nfold)
test_id = [randidx[i: i + kstep] for i in range_(0, num_data, kstep)]
train_id = [np.concatenate([test_id[i] for i in range_(nfold) if k != i]) for k in range_(nfold)]
folds = zip(train_id, test_id)
folds = zip_(train_id, test_id)

ret = CVBooster()
for train_idx, test_idx in folds:
Expand Down
9 changes: 5 additions & 4 deletions python-package/lightgbm/plotting.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
import numpy as np

from .basic import Booster
from .compat import MATPLOTLIB_INSTALLED, GRAPHVIZ_INSTALLED, LGBMDeprecationWarning, range_, string_type
from .compat import (MATPLOTLIB_INSTALLED, GRAPHVIZ_INSTALLED, LGBMDeprecationWarning,
range_, zip_, string_type)
from .sklearn import LGBMModel


Expand Down Expand Up @@ -86,12 +87,12 @@ def plot_importance(booster, ax=None, height=0.2,
if not len(importance):
raise ValueError("Booster's feature_importance is empty.")

tuples = sorted(zip(feature_name, importance), key=lambda x: x[1])
tuples = sorted(zip_(feature_name, importance), key=lambda x: x[1])
if ignore_zero:
tuples = [x for x in tuples if x[1] > 0]
if max_num_features is not None and max_num_features > 0:
tuples = tuples[-max_num_features:]
labels, values = zip(*tuples)
labels, values = zip_(*tuples)

if ax is None:
if figsize is not None:
Expand All @@ -101,7 +102,7 @@ def plot_importance(booster, ax=None, height=0.2,
ylocs = np.arange(len(values))
ax.barh(ylocs, values, align='center', height=height, **kwargs)

for x, y in zip(values, ylocs):
for x, y in zip_(values, ylocs):
ax.text(x + 1, y, x, va='center')

ax.set_yticks(ylocs)
Expand Down

0 comments on commit ed92296

Please sign in to comment.