-
-
Notifications
You must be signed in to change notification settings - Fork 198
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
Sourcery refactored main branch #6
base: main
Are you sure you want to change the base?
Conversation
c1dcbfb
to
745afab
Compare
# classify features into 3 types: binary, float and int | ||
binary_features = [] | ||
for col in X.columns.values: | ||
if list(sorted(np.unique(X[col].values))) == [0, 1]: | ||
binary_features.append(col) | ||
|
||
int_features = [] | ||
for col in list(set(X.columns.values) - set(binary_features)): | ||
if list(X[col].map(int).values) == list(X[col].values): | ||
int_features.append(col) | ||
|
||
binary_features = [ | ||
col | ||
for col in X.columns.values | ||
if list(sorted(np.unique(X[col].values))) == [0, 1] | ||
] | ||
int_features = [ | ||
col | ||
for col in list(set(X.columns.values) - set(binary_features)) | ||
if list(X[col].map(int).values) == list(X[col].values) | ||
] |
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.
Function _get_tree_info
refactored with the following changes:
- Convert for loop into list comprehension [×3] (
list-comprehension
) - Inline variable that is immediately returned (
inline-immediately-returned-variable
) - Swap if/else branches (
swap-if-else-branches
) - Replace if statement with if expression (
assign-if-exp
)
This removes the following comments ( why? ):
# classify features into 3 types: binary, float and int
features = tree_info['features'] | ||
binary_features = tree_info['binary_features'] | ||
int_features = tree_info['int_features'] | ||
target_names = tree_info['target_names'] | ||
|
||
node = {} | ||
if parent == 'null': | ||
node['name'] = "HEAD" | ||
else: | ||
features = tree_info['features'] | ||
feature = features[parent] | ||
binary_features = tree_info['binary_features'] | ||
int_features = tree_info['int_features'] | ||
if pos == 'left': | ||
if feature in binary_features: | ||
node['name'] = feature + ': 0' | ||
node['name'] = f'{feature}: 0' | ||
elif feature in int_features: | ||
node['name'] = feature + " <= " + str(int(tree_model.tree_.threshold[parent])) | ||
node['name'] = f"{feature} <= {int(tree_model.tree_.threshold[parent])}" | ||
else: | ||
node['name'] = feature + " <= " + str(round(tree_model.tree_.threshold[parent], 3)) | ||
node[ | ||
'name' | ||
] = f"{feature} <= {str(round(tree_model.tree_.threshold[parent], 3))}" | ||
elif feature in binary_features: | ||
node['name'] = f'{feature}: 1' | ||
elif feature in int_features: | ||
node['name'] = f"{feature} > {int(tree_model.tree_.threshold[parent])}" | ||
else: | ||
if feature in binary_features: | ||
node['name'] = feature + ': 1' | ||
elif feature in int_features: | ||
node['name'] = feature + " > " + str(int(tree_model.tree_.threshold[parent])) | ||
else: | ||
node['name'] = feature + " > " + str(round(tree_model.tree_.threshold[parent], 3)) | ||
node[ | ||
'name' | ||
] = f"{feature} > {str(round(tree_model.tree_.threshold[parent], 3))}" |
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.
Function _parse_tree
refactored with the following changes:
- Move assignments closer to their usage (
move-assign
) - Merge else clause's nested if statement into elif (
merge-else-if-into-elif
) - Use f-string instead of string concatenation [×10] (
use-fstring-for-concatenation
) - Hoist conditional out of nested conditional [×2] (
hoist-if-from-if
) - Remove unnecessary calls to
str()
from formatted values in f-strings [×2] (remove-str-from-fstring
)
tree_rules[node_id] = {} | ||
tree_rules[node_id]['features'] = {} | ||
|
||
tree_rules[node_id] = {'features': {}} |
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.
Function _extract_rules
refactored with the following changes:
- Merge dictionary assignment with declaration (
merge-dict-assign
)
if feat[0] == -sys.maxsize: | ||
rule = k + ': 0' | ||
else: | ||
rule = k + ': 1' | ||
rule = f'{k}: 0' if feat[0] == -sys.maxsize else f'{k}: 1' | ||
elif k in tree_info['int_features']: | ||
if feat[0] == -sys.maxsize: | ||
rule = k + ' <= ' + str(int(feat[1])) | ||
rule = f'{k} <= {int(feat[1])}' | ||
elif feat[1] == sys.maxsize: | ||
rule = k + ' > ' + str(int(feat[0])) | ||
rule = f'{k} > {int(feat[0])}' | ||
else: | ||
rule = str(int(feat[0])) + ' < ' + k + ' <= ' + str(int(feat[1])) | ||
rule = f'{int(feat[0])} < {k} <= {int(feat[1])}' | ||
elif feat[0] == -sys.maxsize: | ||
rule = f'{k} <= {str(round(feat[1], 3))}' | ||
elif feat[1] == sys.maxsize: | ||
rule = f'{k} > {str(round(feat[0], 3))}' | ||
else: | ||
if feat[0] == -sys.maxsize: | ||
rule = k + ' <= ' + str(round(feat[1], 3)) | ||
elif feat[1] == sys.maxsize: | ||
rule = k + ' > ' + str(round(feat[0], 3)) | ||
else: | ||
rule = str(round(feat[0], 3)) + ' < ' + k + ' <= ' + str(round(feat[1], 3)) | ||
rule = f'{str(round(feat[0], 3))} < {k} <= {str(round(feat[1], 3))}' |
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.
Function _clean_rules
refactored with the following changes:
- Merge else clause's nested if statement into elif (
merge-else-if-into-elif
) - Replace if statement with if expression (
assign-if-exp
) - Use f-string instead of string concatenation [×18] (
use-fstring-for-concatenation
) - Remove unnecessary calls to
str()
from formatted values in f-strings [×4] (remove-str-from-fstring
)
if n<=1: | ||
return 1 | ||
return fib(n-1) + fib(n-2) | ||
return 1 if n<=1 else fib(n-1) + fib(n-2) |
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.
Function fib
refactored with the following changes:
- Lift code into else after jump in control flow (
reintroduce-else
) - Replace if statement with if expression (
assign-if-exp
)
if n<=1: | ||
return 1 | ||
return fib(n-1) + fib(n-2) | ||
return 1 if n<=1 else fib(n-1) + fib(n-2) |
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.
Function fib
refactored with the following changes:
- Lift code into else after jump in control flow (
reintroduce-else
) - Replace if statement with if expression (
assign-if-exp
)
Branch
main
refactored by Sourcery.If you're happy with these changes, merge this Pull Request using the Squash and merge strategy.
See our documentation here.
Run Sourcery locally
Reduce the feedback loop during development by using the Sourcery editor plugin:
Review changes via command line
To manually merge these changes, make sure you're on the
main
branch, then run:Help us improve this pull request!