Last active
December 4, 2019 02:46
-
-
Save armancohan/43f9290ad99c0c0d24643d6ce2777330 to your computer and use it in GitHub Desktop.
Return the structure of a possibly nested dictionary object without all the values
This file contains hidden or 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
import typing | |
def return_dict_structure(obj): | |
"""Return the structure of a possibly nested dictionary object.""" | |
new_obj = {} | |
if isinstance(obj, typing.List) or isinstance(obj, typing.Tuple): | |
if obj: | |
return [return_dict_structure(obj[0]), '...'] | |
else: | |
return [] | |
elif isinstance(obj, typing.Dict): | |
for k, v in obj.items(): | |
new_obj[k] = return_dict_structure(v) | |
else: | |
return type(obj) | |
return new_obj | |
# Train test validation stratified split | |
from sklearn.model_selection import train_test_split | |
def split_ds(X, y, val_test_size=0.3): | |
X_train, X_test_val, y_train, y_test_val = train_test_split(X, y, test_size=val_test_size, random_state=32, stratify=y) | |
X_test, X_val, y_test, y_val = train_test_split(X_test_val, y_test_val, test_size=0.5, random_state=32, stratify=y_test_val) | |
return X_train, X_val, X_test, y_train, y_val, y_test |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment