Skip to main content

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 and np.nan for 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): If True, forces all filled values to be >= 0. Default is True

Returns

  • filled_grid (np.ndarray): Copy of grid with all np.nan values filled
  • result (scipy.optimize.OptimizeResult): SciPy optimization result containing:
    • x: Array of solved unknowns
    • success: Boolean indicating if optimization succeeded
    • message: String describing the optimization outcome
    • Additional optimization metrics

How It Works

  1. Identify unknowns: Locates all np.nan positions in the input matrix
  2. Build linear system: Constructs a system of linear equations Ax = b where:
    • Each row constraint contributes one equation
    • Each column constraint contributes one equation
    • Variables are the unknown matrix entries
  3. Apply bounds: Sets lower bounds (0 or -∞) and upper bounds (+∞) for variables
  4. Solve optimization: Uses scipy.optimize.lsq_linear to find the best least-squares solution
  5. 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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

matrix_filler-0.1.0.tar.gz (4.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

matrix_filler-0.1.0-py3-none-any.whl (4.5 kB view details)

Uploaded Python 3

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

Hashes for matrix_filler-0.1.0.tar.gz
Algorithm Hash digest
SHA256 5a4b049bf2b377f8028738bbc7d58aaaa6d209bfb6dd15da65d322e46c914f1c
MD5 0788018b7e35df070c25c4e9f7d2359e
BLAKE2b-256 1cf856865d7ef9155e7086b0c4c38f17b43e45967fc9dc4ce98b97742dcadab7

See more details on using hashes here.

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

Hashes for matrix_filler-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 20139462d15bc260976b902310236bbfc51fdc4b99174e53036b5118b7ed20d4
MD5 3dfb36f705cb63ba65ea7b9779a5f785
BLAKE2b-256 9a7c7bb9dc05f31aa44759fa4215f19f71ca3b0480269e386d9d6b72d5e7941c

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page