-
Notifications
You must be signed in to change notification settings - Fork 3.8k
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] avoid data copy where possible #2383
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
8071146
avoid copy where possible
StrikerRUS 8d0d302
fixed conflicts
StrikerRUS 2f1bd57
use precise type for importance type
StrikerRUS ce37741
removed pointless code
StrikerRUS 7c82f58
simplify sparse pandas Series conversion
StrikerRUS ed7e3af
more memory savings
StrikerRUS 14721e2
always force type conversion for 1-D arrays
StrikerRUS 93897f3
one more copy=False
StrikerRUS b90da65
Merge remote-tracking branch 'origin/master' into numpy_copy
StrikerRUS 5a13092
Merge remote-tracking branch 'origin/master' into numpy_copy
StrikerRUS 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 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 |
---|---|---|
|
@@ -80,10 +80,7 @@ def list_to_1d_numpy(data, dtype=np.float32, name='list'): | |
elif isinstance(data, Series): | ||
if _get_bad_pandas_dtypes([data.dtypes]): | ||
raise ValueError('Series.dtypes must be int, float or bool') | ||
if hasattr(data.values, 'values'): # SparseArray | ||
return data.values.values.astype(dtype) | ||
else: | ||
return data.values.astype(dtype) | ||
return np.array(data, dtype=dtype, copy=False) # SparseArray should be supported as well | ||
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. Dense:
Sparse:
|
||
else: | ||
raise TypeError("Wrong type({0}) for {1}.\n" | ||
"It should be list, numpy 1-D array or pandas Series".format(type(data).__name__, name)) | ||
|
@@ -296,7 +293,9 @@ def _data_from_pandas(data, feature_name, categorical_feature, pandas_categorica | |
raise ValueError("DataFrame.dtypes for data must be int, float or bool.\n" | ||
"Did not expect the data types in the following fields: " | ||
+ ', '.join(data.columns[bad_indices])) | ||
data = data.values.astype('float') | ||
data = data.values | ||
if data.dtype != np.float32 and data.dtype != np.float64: | ||
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. Why not simple
|
||
data = data.astype(np.float32) | ||
else: | ||
if feature_name == 'auto': | ||
feature_name = None | ||
|
@@ -311,7 +310,7 @@ def _label_from_pandas(label): | |
raise ValueError('DataFrame for label cannot have multiple columns') | ||
if _get_bad_pandas_dtypes(label.dtypes): | ||
raise ValueError('DataFrame.dtypes for label must be int, float or bool') | ||
label = label.values.astype('float').flatten() | ||
label = np.ravel(label.values.astype(np.float32, copy=False)) | ||
return label | ||
|
||
|
||
|
@@ -534,8 +533,7 @@ def __pred_for_np2d(self, mat, num_iteration, predict_type): | |
def inner_predict(mat, num_iteration, predict_type, preds=None): | ||
if mat.dtype == np.float32 or mat.dtype == np.float64: | ||
data = np.array(mat.reshape(mat.size), dtype=mat.dtype, copy=False) | ||
else: | ||
"""change non-float data to float data, need to copy""" | ||
else: # change non-float data to float data, need to copy | ||
data = np.array(mat.reshape(mat.size), dtype=np.float32) | ||
ptr_data, type_ptr_data, _ = c_float_array(data) | ||
n_preds = self.__get_num_preds(num_iteration, mat.shape[0], predict_type) | ||
|
@@ -876,8 +874,7 @@ def __init_from_np2d(self, mat, params_str, ref_dataset): | |
self.handle = ctypes.c_void_p() | ||
if mat.dtype == np.float32 or mat.dtype == np.float64: | ||
data = np.array(mat.reshape(mat.size), dtype=mat.dtype, copy=False) | ||
else: | ||
# change non-float data to float data, need to copy | ||
else: # change non-float data to float data, need to copy | ||
data = np.array(mat.reshape(mat.size), dtype=np.float32) | ||
|
||
ptr_data, type_ptr_data, _ = c_float_array(data) | ||
|
@@ -915,8 +912,7 @@ def __init_from_list_np2d(self, mats, params_str, ref_dataset): | |
|
||
if mat.dtype == np.float32 or mat.dtype == np.float64: | ||
mats[i] = np.array(mat.reshape(mat.size), dtype=mat.dtype, copy=False) | ||
else: | ||
# change non-float data to float data, need to copy | ||
else: # change non-float data to float data, need to copy | ||
mats[i] = np.array(mat.reshape(mat.size), dtype=np.float32) | ||
|
||
chunk_ptr_data, chunk_type_ptr_data, holder = c_float_array(mats[i]) | ||
|
@@ -1012,7 +1008,7 @@ def construct(self): | |
used_indices = list_to_1d_numpy(self.used_indices, np.int32, name='used_indices') | ||
assert used_indices.flags.c_contiguous | ||
if self.reference.group is not None: | ||
group_info = np.array(self.reference.group).astype(int) | ||
group_info = np.array(self.reference.group).astype(np.int32, copy=False) | ||
_, self.group = np.unique(np.repeat(range_(len(group_info)), repeats=group_info)[self.used_indices], | ||
return_counts=True) | ||
self.handle = ctypes.c_void_p() | ||
|
@@ -2512,7 +2508,7 @@ def feature_importance(self, importance_type='split', iteration=None): | |
ctypes.c_int(importance_type_int), | ||
result.ctypes.data_as(ctypes.POINTER(ctypes.c_double)))) | ||
if importance_type_int == 0: | ||
return result.astype(int) | ||
return result.astype(np.int32) | ||
else: | ||
return result | ||
|
||
|
This file contains 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.
Fix
FutureWarning
: