sapientml / core

A SapientML plugin of SapientMLGenerator
Apache License 2.0
10 stars 12 forks source link

Exclude Incompatible Models for Sparse Data in Skeleton Predictor #47

Closed ihkao closed 7 months ago

ihkao commented 8 months ago

Issue:

This PR addresses a TypeError encountered in the GaussianNB model when processing sparse matrix data within our machine learning pipeline. The GaussianNB algorithm does not support sparse matrices, leading to a TypeError as illustrated below:

raise TypeError(
TypeError: A sparse matrix was passed, but dense data is required. Use X.toarray() to convert to a dense numpy array.

Modifications:

Impact:

Conclusion:

This change addresses a crucial issue in data compatibility, allowing for more robust handling of diverse data types while also acknowledging the implications on memory and performance.

ihkao commented 8 months ago

Thank you for your PR. We already recognize this issue. So, this pattern is not considered in test cases now. https://github.com/sapientml/sapientml/blob/467a2acb000a3dea5c8bd94b390d782ebc85dd32/tests/sapientml/test_generatedcode.py#L251-L253

We've not added to_dense() because of the reason you mentioned:

However, converting large sparse matrices to dense format can significantly increase memory usage, potentially impacting the model's training and prediction performance. This trade-off needs to be considered, especially when dealing with large datasets. It's important to note that very sparse data may not align well with the assumptions of the GaussianNB algorithm. When most values are zero, a simple Gaussian may not fit the data effectively, potentially leading to less useful or inaccurate classifications. This limitation should be considered when applying the GaussianNB model to highly sparse datasets.

In my opinion, we need another way substituting this PR to solve the issue. For example, when the data would be sparse by transformers such as TfidfVectorizer, GuassianNB (and other models handling no sparse data) are not proposed.

PR Update:

Thank you for your insights and the link to the relevant test cases. Based on your feedback and the challenges associated with converting large sparse matrices to dense formats, I have revised my PR and change the title to "Exclude Incompatible Models for Sparse Data in Skeleton Predictor." The updated approach now involves strategically excluding GaussianNB and SVC models in scenarios where TfidfVectorizer is used, thus avoiding the need for to_dense() and its associated memory concerns.

Modification:

The primary modification in this update involves the exclusion of GaussianNB and SVC models when TfidfVectorizer is employed. This adaptation stems from the understanding that TfidfVectorizer typically results in data being transformed into a sparse matrix format. Given that GaussianNB and SVC models are known to unsupport with sparse data, their exclusion is a strategic decision aimed at refining the model selection process within our system.

Impact:

This change has a two-fold impact on the system's operation. Firstly, it upholds the core functionalities such as fetching preprocessing and model labels, resolving label conflicts, and systematically ordering labels. This ensures that the integration of this update is seamless and does not disrupt the existing workflow. Secondly, and more significantly, it enhances the prediction performance. By deliberately avoiding models that does not support to sparse matrices, the function is now more aligned with flexibility. This is particularly beneficial for datasets processed using TfidfVectorizer, allowing for a more precise and effective model selection strategy.