@@ -1441,32 +1441,23 @@ def _write_sparse_block(self, row_index, stream, target=None):
14411441 assert target in self .target_column , "'target' must be in the target column"
14421442 stream .write (f"{ target } \t " )
14431443 row = self .matrix .getrow (row_index )
1444- # Empty row in the sparse matrix: use the first variable as missing data
1445- # TODO: remove this part once Khiops bug
1446- # https://github.com/KhiopsML/khiops/issues/235 is solved
1447- if row .size == 0 :
1448- for variable_index in self .column_ids :
1449- stream .write (f"{ variable_index + 1 } : " )
1450- break
1451- # Non-empty row in the sparse matrix: get non-missing data
1452- else :
1453- # Variable indices are not always sorted in `row.indices`
1454- # Khiops needs variable indices to be sorted
1455- sorted_indices = np .sort (row .nonzero ()[1 ], axis = - 1 , kind = "mergesort" )
1456-
1457- # Flatten row for Python < 3.9 scipy.sparse.lil_matrix whose API
1458- # is not homogeneous with other sparse matrices: it stores
1459- # opaque Python lists as elements
1460- # Thus:
1461- # - if isinstance(self.matrix, sp.lil_matrix) and Python 3.8, then
1462- # row.data is np.array([list([...])])
1463- # - else, row.data is np.array([...])
1464- # TODO: remove this flattening once Python 3.8 support is dropped
1465- sorted_data = np .fromiter (self ._flatten (row .data ), row .data .dtype )[
1466- sorted_indices .argsort ()
1467- ]
1468- for variable_index , variable_value in zip (sorted_indices , sorted_data ):
1469- stream .write (f"{ variable_index + 1 } :{ variable_value } " )
1444+ # Variable indices are not always sorted in `row.indices`
1445+ # Khiops needs variable indices to be sorted
1446+ sorted_indices = np .sort (row .nonzero ()[1 ], axis = - 1 , kind = "mergesort" )
1447+
1448+ # Flatten row for Python < 3.9 scipy.sparse.lil_matrix whose API
1449+ # is not homogeneous with other sparse matrices: it stores
1450+ # opaque Python lists as elements
1451+ # Thus:
1452+ # - if isinstance(self.matrix, sp.lil_matrix) and Python 3.8, then
1453+ # row.data is np.array([list([...])])
1454+ # - else, row.data is np.array([...])
1455+ # TODO: remove this flattening once Python 3.8 support is dropped
1456+ sorted_data = np .fromiter (self ._flatten (row .data ), row .data .dtype )[
1457+ sorted_indices .argsort ()
1458+ ]
1459+ for variable_index , variable_value in zip (sorted_indices , sorted_data ):
1460+ stream .write (f"{ variable_index + 1 } :{ variable_value } " )
14701461 stream .write ("\n " )
14711462
14721463 def create_table_file_for_khiops (self , output_dir , sort = True ):
0 commit comments