OptiMask: extracting the largest (non-contiguous) submatrix without NaN
Project description
OptiMask: Efficient NaN Data Removal in Python
OptiMask
is a Python package designed for efficiently handling NaN values in matrices, specifically focusing on computing the largest non-contiguous submatrix without NaN. OptiMask employs a heuristic method, relying solely on Numpy for speed and efficiency. In machine learning applications, OptiMask surpasses traditional methods like pandas dropna
by maximizing the amount of valid data available for model fitting. It strategically identifies the optimal set of columns (features) and rows (samples) to retain or remove, ensuring that the largest (non-contiguous) submatrix without NaN is utilized for training models.
The problem differs from the computation of the largest rectangles of 1s in a binary matrix (which can be tackled with dynamic programming) and requires a novel approach.
Key Features
- Largest Submatrix without NaN: OptiMask calculates the largest submatrix without NaN, enhancing data analysis accuracy.
- Efficient Computation: With optimized computation, OptiMask provides rapid results without undue delays.
- Numpy and Pandas Compatibility: OptiMask seamlessly adapts to both Numpy and Pandas data structures.
Utilization
To employ OptiMask, install the optimask
package via pip:
pip install optimask
OptiMask is also available on the conda-forge channel:
conda install -c conda-forge optimask
mamba install optimask
Usage Example
Import the OptiMask
class from the optimask
package and utilize its methods for efficient data masking:
from optimask import OptiMask
import numpy as np
# Create a matrix with NaN values
m = 120
n = 7
data = np.zeros(shape=(m, n))
data[24:72, 3] = np.nan
data[95, :5] = np.nan
# Solve for the largest submatrix without NaN values
rows, cols = OptiMask().solve(data)
# Calculate the ratio of non-NaN values in the result
coverage_ratio = len(rows) * len(cols) / data.size
# Check if there are any NaN values in the selected submatrix
has_nan_values = np.isnan(data[rows][:, cols]).any()
# Print or display the results
print(f"Coverage Ratio: {coverage_ratio:.2f}, Has NaN Values: {has_nan_values}")
# Output: Coverage Ratio: 0.85, Has NaN Values: False
The grey cells represent the NaN locations, the blue ones represent the valid data, and the red ones represent the rows and columns removed by the algorithm:
OptiMask’s algorithm is useful for handling unstructured NaN patterns, as shown in the following example:
Performances
OptiMask
efficiently handles large matrices, delivering results within reasonable computation times:
from optimask import OptiMask
import numpy as np
def generate_random(m, n, ratio):
"""Missing at random arrays"""
arr = np.zeros((m, n))
nan_count = int(ratio * m * n)
indices = np.random.choice(m * n, nan_count, replace=False)
arr.flat[indices] = np.nan
return arr
x = generate_random(m=100_000, n=1_000, ratio=0.02)
%time rows, cols = OptiMask(verbose=True).solve(x)
>>> Trial 1 : submatrix of size 37094x49 (1817606 elements) found.
>>> Trial 2 : submatrix of size 35667x51 (1819017 elements) found.
>>> Trial 3 : submatrix of size 37908x48 (1819584 elements) found.
>>> Trial 4 : submatrix of size 37047x49 (1815303 elements) found.
>>> Trial 5 : submatrix of size 37895x48 (1818960 elements) found.
>>> Result: the largest submatrix found is of size 37908x48 (1819584 elements) found.
>>> CPU times: total: 172 ms
>>> Wall time: 435 ms
Documentation
For detailed documentation, including installation instructions, API usage, and examples, visit OptiMask Documentation.
Repository Link
Find more about OptiMask on GitHub.
Citation
If you use OptiMask in your research or work, please cite it:
@software{optimask2024,
author = {Cyril Joly},
title = {OptiMask: NaN Removal and Largest Submatrix Computation},
year = {2024},
url = {https://github.com/CyrilJl/OptiMask},
}
Or:
OptiMask (2024). NaN Removal and Largest Submatrix Computation. Developed by Cyril Joly: https://github.com/CyrilJl/OptiMask
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
File details
Details for the file optimask-1.3.3.tar.gz
.
File metadata
- Download URL: optimask-1.3.3.tar.gz
- Upload date:
- Size: 8.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | b4c9cf5a78b095500966c20f8b5f2035402dfb1463b555a09d11425e91acf111 |
|
MD5 | b40c73f9daf4efecb832108d1f44e470 |
|
BLAKE2b-256 | 717a694b2ba87a05ccb020337f317f96b87453cffc9cd00ff66eaa99ae78106e |