Skip to content

Commit

Permalink
add exception if no continous features in golden features transformat…
Browse files Browse the repository at this point in the history
…ion (#187)
  • Loading branch information
pplonski committed Sep 15, 2020
1 parent 20c645a commit 38fdb65
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
22 changes: 21 additions & 1 deletion supervised/preprocessing/goldenfeatures_transformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
MULTICLASS_CLASSIFICATION,
REGRESSION,
)
from supervised.exceptions import AutoMLException


def get_binary_score(X_train, y_train, X_test, y_test):
Expand Down Expand Up @@ -54,6 +55,7 @@ def get_score(item):
diff_score = scorer(x_train, y_train, x_test, y_test)
except Exception as e:
diff_score = None
print(str(e))

try:
a, b = (
Expand All @@ -78,6 +80,7 @@ def get_score(item):
x_test = np.divide(a, b, out=np.zeros_like(a), where=b != 0).reshape(-1, 1)
ratio_2_score = scorer(x_train, y_train, x_test, y_test)
except Exception as e:
print(str(e))
ratio_2_score = None

return (diff_score, ratio_1_score, ratio_2_score)
Expand All @@ -96,13 +99,25 @@ def __init__(self, results_path=None, ml_task=None):
else:
self._scorer = get_regression_score

self._error = None

if results_path is not None:
self._result_file = os.path.join(results_path, "golden_features.json")
self.try_load()

def fit(self, X, y):
if self._new_features:
return
if self._error is not None and self._error:
raise AutoMLException(
"Golden Features not created due to error (please check errors.md)."
)
return
if X.shape[1] == 0:
self._error = f"Golden Features not created. No continous features. Input data shape: {X.shape}, {y.shape}"
self.save(self._result_file)
raise AutoMLException("Golden Features not created. No continous features.")

start_time = time.time()
combinations = itertools.combinations(X.columns, r=2)
items = [i for i in combinations]
Expand Down Expand Up @@ -132,7 +147,9 @@ def fit(self, X, y):
# scores += [get_score(item)]

if not scores:
return
self._error = f"Golden Features not created. Empty scores. Input data shape: {X.shape}, {y.shape}"
self.save(self._result_file)
raise AutoMLException("Golden Features not created. Empty scores.")

result = []
for i in range(len(items)):
Expand Down Expand Up @@ -189,13 +206,16 @@ def to_json(self):
"result_file": self._result_file,
"ml_task": self._ml_task,
}
if self._error is not None and self._error:
data_json["error"] = self._error
return data_json

def from_json(self, data_json):
self._new_features = json.loads(data_json.get("new_features", []))
self._new_columns = json.loads(data_json.get("new_columns", []))
self._result_file = data_json.get("result_file")
self._ml_task = data_json.get("ml_task")
self._error = data_json.get("error")

def save(self, destination_file):
with open(destination_file, "w") as fout:
Expand Down
5 changes: 3 additions & 2 deletions supervised/utils/importance.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ def compute_and_plot(
}
)
df_imp.to_csv(
os.path.join(model_file_path, f"{learner_name}_importance.csv"), index=False
os.path.join(model_file_path, f"{learner_name}_importance.csv"),
index=False,
)
except Exception as e:
print("Problem during computing permutation importance. Skipping ...")
print("Problem during computing permutation importance. Skipping ...")

0 comments on commit 38fdb65

Please sign in to comment.