{"id":23899,"library":"ipfn","title":"ipfn: Iterative Proportional Fitting","description":"ipfn is a Python library for iterative proportional fitting (IPF) with N dimensions. It is used to adjust contingency tables to match known marginal totals. The current version is 1.4.4. Release cadence is low; updates are infrequent.","status":"active","version":"1.4.4","language":"python","source_language":"en","source_url":"https://github.com/Dirguis/ipfn","tags":["iterative proportional fitting","IPF","matrix adjustment","contingency tables"],"install":[{"cmd":"pip install ipfn","lang":"bash","label":"Install via pip"}],"dependencies":[{"reason":"Numerical computations and array manipulations","package":"numpy","optional":false},{"reason":"DataFrame support for input and output","package":"pandas","optional":true}],"imports":[{"note":"Common alias is not standard; use as ipfn or alias explicitly","wrong":"from ipfn import ipfn as ipf","symbol":"ipfn","correct":"from ipfn import ipfn"}],"quickstart":{"code":"import numpy as np\nimport pandas as pd\nfrom ipfn import ipfn\n\n# Seed marginal totals\nmarginals = [np.array([100, 200]), np.array([150, 150])]\n# Original matrix\nZ = np.array([[50, 50], [150, 50]])\n\n# Create IPF object and fit\nipf = ipfn(Z, marginals, convergence_rate=1e-5, max_iteration=100)\nipf.iterate()\nprint(ipf.Z)","lang":"python","description":"Basic example of iterative proportional fitting with two dimensions."},"warnings":[{"fix":"Z_original = Z.copy() before calling ipfn.","message":"The `ipfn` function modifies the input array in-place. If you need to preserve the original matrix, pass a copy.","severity":"gotcha","affected_versions":"all"},{"fix":"Ensure marginals = [array1, array2] with matching dimensions.","message":"Marginal totals must be provided as a list of numpy arrays, not as a single array or pandas Series.","severity":"gotcha","affected_versions":"all"},{"fix":"Check that sum of each marginal equals sum of Z before fitting.","message":"The library does not validate that marginal sums match the total sum of the input matrix. Discrepancies may cause divergence.","severity":"gotcha","affected_versions":"all"},{"fix":"Set max_iteration to a higher value (e.g., 1000) and convergence_rate to a looser tolerance (e.g., 1e-4).","message":"Convergence is not guaranteed; increase max_iteration or adjust convergence_rate if needed.","severity":"gotcha","affected_versions":"all"}],"env_vars":null,"last_verified":"2026-05-01T00:00:00.000Z","next_check":"2026-07-30T00:00:00.000Z","problems":[{"fix":"Wrap the integer in a numpy array: np.array([100])","cause":"Passing a single integer as a marginal instead of an array.","error":"TypeError: cannot unpack non-iterable int object"},{"fix":"Check that each marginal array length matches the corresponding axis size of Z.","cause":"Marginal dimensions do not match the original matrix dimensions.","error":"ValueError: operands could not be broadcast together with shapes"},{"fix":"Upgrade to latest version: pip install --upgrade ipfn","cause":"Old version of ipfn (before 1.0) used a different API.","error":"ImportError: cannot import name 'ipfn' from 'ipfn'"}],"ecosystem":"pypi","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}