Fill missing matrix values (np.nan) to satisfy row/column sum constraints using bounded least squares.
Project description
Matrix Filler
Fill missing matrix values (np.nan) to satisfy row/column sum constraints using bounded least squares optimization.
Overview
Matrix Filler is a Python package that solves the problem of filling unknown values in a 2D matrix when you know the desired row and column sums. It uses scipy's bounded least squares optimization to find the best values that satisfy the constraints.
Features
- Constraint-based filling: Automatically fills missing values to match specified row and column sums
- Optimization-based approach: Uses bounded least squares for robust solutions
- Flexible bounds: Optional non-negative constraint for filled values
- Detailed results: Returns both the filled matrix and optimization diagnostics
Installation
From PyPI (when published)
pip install matrix-filler
From Source
git clone https://github.com/AreedAdmin/matrix-filler.git
cd matrix-filler
pip install -e .
Requirements
- Python >= 3.10
- numpy
- scipy
Usage
Basic Example
import numpy as np
from matrix_filler import fill_matrix_with_constraints
# Create a matrix with missing values (np.nan)
grid = np.array([
[1.0, np.nan, 3.0],
[np.nan, 2.0, np.nan],
[4.0, np.nan, 5.0]
])
# Define desired row and column sums
row_targets = np.array([10.0, 15.0, 20.0]) # Sum for each row
col_targets = np.array([12.0, 8.0, 25.0]) # Sum for each column
# Fill the matrix
filled_grid, result = fill_matrix_with_constraints(
grid,
row_targets,
col_targets,
non_negative=True
)
print("Filled Matrix:")
print(filled_grid)
print(f"\nOptimization Success: {result.success}")
print(f"Message: {result.message}")
Advanced Example with Validation
import numpy as np
from matrix_filler import fill_matrix_with_constraints
# Create a partially filled matrix
grid = np.array([
[10.0, np.nan, np.nan],
[np.nan, 20.0, np.nan],
[np.nan, np.nan, 30.0]
])
row_targets = np.array([50.0, 60.0, 70.0])
col_targets = np.array([40.0, 80.0, 60.0])
# Fill the matrix (allow negative values)
filled_grid, result = fill_matrix_with_constraints(
grid,
row_targets,
col_targets,
non_negative=False
)
# Verify the constraints
print("Row sums:", filled_grid.sum(axis=1))
print("Column sums:", filled_grid.sum(axis=0))
print("\nExpected row sums:", row_targets)
print("Expected column sums:", col_targets)
API Reference
fill_matrix_with_constraints(grid, row_targets, col_targets, non_negative=True)
Fill np.nan entries in a 2D numpy array so that row and column sums match given targets.
Parameters
- grid (
np.ndarray): 2D array with known values andnp.nanfor unknowns - row_targets (
np.ndarray): Desired sum of each row (length = number of rows) - col_targets (
np.ndarray): Desired sum of each column (length = number of columns) - non_negative (
bool, optional): IfTrue, forces all filled values to be >= 0. Default isTrue
Returns
- filled_grid (
np.ndarray): Copy of grid with allnp.nanvalues filled - result (
scipy.optimize.OptimizeResult): SciPy optimization result containing:x: Array of solved unknownssuccess: Boolean indicating if optimization succeededmessage: String describing the optimization outcome- Additional optimization metrics
How It Works
- Identify unknowns: Locates all
np.nanpositions in the input matrix - Build linear system: Constructs a system of linear equations
Ax = bwhere:- Each row constraint contributes one equation
- Each column constraint contributes one equation
- Variables are the unknown matrix entries
- Apply bounds: Sets lower bounds (0 or -∞) and upper bounds (+∞) for variables
- Solve optimization: Uses
scipy.optimize.lsq_linearto find the best least-squares solution - Fill matrix: Inserts the solved values back into the matrix
Use Cases
- Data imputation: Fill missing values in datasets with known marginal totals
- Matrix completion: Complete partially observed matrices with constraints
- Statistical modeling: Generate synthetic data matching specific row/column distributions
- Operations research: Solve transportation and allocation problems
- Survey data: Reconstruct contingency tables from partial observations
Limitations
- The problem may not have an exact solution if constraints are inconsistent
- The algorithm finds the best least-squares approximation when exact solutions don't exist
- Performance may degrade for very large matrices with many unknowns
Contributing
Contributions are welcome! Please feel free to submit issues, feature requests, or pull requests.
License
This project is licensed under the MIT License.
Author
Shehab Hassani
Email: shhehab.hassani@areednow.com
Links
- Homepage: https://github.com/AreedAdmin/matrix-filler.git
- Source Code: https://github.com/AreedAdmin/matrix-filler.git
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file matrix_filler-0.1.0.tar.gz.
File metadata
- Download URL: matrix_filler-0.1.0.tar.gz
- Upload date:
- Size: 4.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.10.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5a4b049bf2b377f8028738bbc7d58aaaa6d209bfb6dd15da65d322e46c914f1c
|
|
| MD5 |
0788018b7e35df070c25c4e9f7d2359e
|
|
| BLAKE2b-256 |
1cf856865d7ef9155e7086b0c4c38f17b43e45967fc9dc4ce98b97742dcadab7
|
File details
Details for the file matrix_filler-0.1.0-py3-none-any.whl.
File metadata
- Download URL: matrix_filler-0.1.0-py3-none-any.whl
- Upload date:
- Size: 4.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.10.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
20139462d15bc260976b902310236bbfc51fdc4b99174e53036b5118b7ed20d4
|
|
| MD5 |
3dfb36f705cb63ba65ea7b9779a5f785
|
|
| BLAKE2b-256 |
9a7c7bb9dc05f31aa44759fa4215f19f71ca3b0480269e386d9d6b72d5e7941c
|