From ecd57b3cb731779e7fc8375a58ecb20e50d634c9 Mon Sep 17 00:00:00 2001 From: Sheng-Wei Chen Date: Thu, 26 Dec 2024 13:32:31 +0800 Subject: [PATCH 1/2] 1. np.full(..., 0) return an np.array with int64, but we need float64. 2. np.matrix ** 2 will be represented as the square of a ``square matrix'' in the latest NumPy version. For the element-wise square of an np.matrix, we should use np.square( np.matrix ). --- libmultilabel/linear/tree.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/libmultilabel/linear/tree.py b/libmultilabel/linear/tree.py index fd93b5f..0988dc7 100644 --- a/libmultilabel/linear/tree.py +++ b/libmultilabel/linear/tree.py @@ -95,18 +95,18 @@ def _beam_search(self, instance_preds: np.ndarray, beam_width: int) -> np.ndarra continue slice = np.s_[self.weight_map[node.index] : self.weight_map[node.index + 1]] pred = instance_preds[slice] - children_score = score - np.maximum(0, 1 - pred) ** 2 + children_score = score - np.square( np.maximum(0, 1 - pred) ) next_level.extend(zip(node.children, children_score.tolist())) cur_level = sorted(next_level, key=lambda pair: -pair[1])[:beam_width] next_level = [] num_labels = len(self.root.label_map) - scores = np.full(num_labels, 0) + scores = np.full(num_labels, 0.0) for node, score in cur_level: slice = np.s_[self.weight_map[node.index] : self.weight_map[node.index + 1]] pred = instance_preds[slice] - scores[node.label_map] = np.exp(score - np.maximum(0, 1 - pred) ** 2) + scores[node.label_map] = np.exp( score - np.square( np.maximum(0, 1 - pred) ) ) return scores From a51891f4622e221859a7a63794f40c8273dcd1f1 Mon Sep 17 00:00:00 2001 From: Sheng-Wei Chen Date: Thu, 26 Dec 2024 14:20:20 +0800 Subject: [PATCH 2/2] reformat by black formatter --- libmultilabel/linear/tree.py | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/libmultilabel/linear/tree.py b/libmultilabel/linear/tree.py index 0988dc7..64070b2 100644 --- a/libmultilabel/linear/tree.py +++ b/libmultilabel/linear/tree.py @@ -95,7 +95,7 @@ def _beam_search(self, instance_preds: np.ndarray, beam_width: int) -> np.ndarra continue slice = np.s_[self.weight_map[node.index] : self.weight_map[node.index + 1]] pred = instance_preds[slice] - children_score = score - np.square( np.maximum(0, 1 - pred) ) + children_score = score - np.square(np.maximum(0, 1 - pred)) next_level.extend(zip(node.children, children_score.tolist())) cur_level = sorted(next_level, key=lambda pair: -pair[1])[:beam_width] @@ -106,7 +106,7 @@ def _beam_search(self, instance_preds: np.ndarray, beam_width: int) -> np.ndarra for node, score in cur_level: slice = np.s_[self.weight_map[node.index] : self.weight_map[node.index + 1]] pred = instance_preds[slice] - scores[node.label_map] = np.exp( score - np.square( np.maximum(0, 1 - pred) ) ) + scores[node.label_map] = np.exp(score - np.square(np.maximum(0, 1 - pred))) return scores @@ -151,14 +151,14 @@ def count(node): root.dfs(count) model_size = get_estimated_model_size(root) - print(f'The estimated tree model size is: {model_size / (1024**3):.3f} GB') + print(f"The estimated tree model size is: {model_size / (1024**3):.3f} GB") # Calculate the total memory (excluding swap) on the local machine - total_memory = psutil.virtual_memory().total - print(f'Your system memory is: {total_memory / (1024**3):.3f} GB') + total_memory = psutil.virtual_memory().total + print(f"Your system memory is: {total_memory / (1024**3):.3f} GB") - if (total_memory <= model_size): - raise MemoryError(f'Not enough memory to train the model.') + if total_memory <= model_size: + raise MemoryError(f"Not enough memory to train the model.") pbar = tqdm(total=num_nodes, disable=not verbose) @@ -221,7 +221,7 @@ def get_estimated_model_size(root): def collect_stat(node: Node): nonlocal total_num_weights - + if node.isLeaf(): total_num_weights += len(node.label_map) * node.num_features_used else: @@ -231,7 +231,7 @@ def collect_stat(node: Node): # 16 is because when storing sparse matrices, indices (int64) require 8 bytes and floats require 8 bytes # Our study showed that among the used features of every binary classification problem, on average no more than 2/3 of weights obtained by the dual coordinate descent method are non-zeros. - return total_num_weights * 16 * 2/3 + return total_num_weights * 16 * 2 / 3 def _train_node(y: sparse.csr_matrix, x: sparse.csr_matrix, options: str, node: Node):