diff --git a/HISTORY.md b/HISTORY.md index 34b3ead87c..0a750a968e 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -12,6 +12,9 @@ _????-??-??_ * Bundle STB with mlpack and add `ResizeImages()` functionality (#3823). + * Fix conversion of empty Armadillo objects to numpy in Python bindings + (#3896). + ## mlpack 4.5.1 _2024-12-02_ diff --git a/src/mlpack/bindings/python/mlpack/preprocess_json_params.py b/src/mlpack/bindings/python/mlpack/preprocess_json_params.py index cd87ae4bc7..9eb94a4435 100644 --- a/src/mlpack/bindings/python/mlpack/preprocess_json_params.py +++ b/src/mlpack/bindings/python/mlpack/preprocess_json_params.py @@ -95,7 +95,7 @@ def np_to_arma(obj): n_rows, n_cols = obj[key].shape dic = OrderedDict() - + dic["n_rows"] = str(n_cols) # implicit transpose dic["n_cols"] = str(n_rows) # implicit transpose @@ -127,14 +127,18 @@ def arma_to_np(obj): if isinstance(obj, OrderedDict): for key in obj.keys(): if isinstance(obj[key], OrderedDict): - # if "vec_state" is present in dictionary, then - # it must be armadillo vector. + # if "vec_state" is present in dictionary, then it must be an Armadillo + # vector. if "vec_state" in obj[key].keys(): n_rows = int(obj[key]["n_rows"]) n_cols = int(obj[key]["n_cols"]) - # implicit transpose - obj[key] = np.array(obj[key]["elem"])\ - .reshape(n_cols, n_rows).astype(type(obj[key]["elem"][0])) + + # Perform an implicit transpose, if there are any elements. + if n_rows > 0 and n_cols > 0: + obj[key] = np.array(obj[key]["elem"]).reshape(n_cols, + n_rows).astype(type(obj[key]["elem"][0])) + else: + obj[key] = np.zeros((n_rows, n_cols)) else: arma_to_np(obj[key]) else: