Skip to content

Fix GMM.condition() ValueError with single conditioning value#61

Merged
AlexanderFabisch merged 1 commit into
AlexanderFabisch:masterfrom
jkitchin:fix-condition-scalar-exponent
Jan 12, 2026
Merged

Fix GMM.condition() ValueError with single conditioning value#61
AlexanderFabisch merged 1 commit into
AlexanderFabisch:masterfrom
jkitchin:fix-condition-scalar-exponent

Conversation

@jkitchin

Copy link
Copy Markdown
Contributor

Summary

GMM.condition() raises ValueError: setting an array element with a sequence when conditioning on a single value. This affects all input formats ([[val]], [val], np.array(...), scalar).

Root Cause

The to_norm_factor_and_exponents() method returns exponents as an array of shape (n_samples,). When conditioning on a single value, this is a 1-element array, not a scalar. The original code attempted to assign this array directly to marginal_prior_exponents[k] (a scalar slot), causing the error.

Fix

Extract the scalar value with exponents[0] since condition() is designed to handle a single conditioning value:

# Before (broken):
marginal_norm_factors[k], marginal_prior_exponents[k] =     mvn.marginalize(indices).to_norm_factor_and_exponents(x)

# After (fixed):
norm_factor, exponents =     mvn.marginalize(indices).to_norm_factor_and_exponents(x)
marginal_norm_factors[k] = norm_factor
marginal_prior_exponents[k] = exponents[0]

Minimal Reproducer

import numpy as np
from gmr import GMM

np.random.seed(42)
data = np.random.randn(100, 2)

gmm = GMM(n_components=2, random_state=42)
gmm.from_samples(data)

# This fails with ValueError before the fix
conditional = gmm.condition([1], [[0.75]])

Testing

  • Added test_conditional_distribution_input_formats() to verify various input formats work
  • All 61 existing tests pass

The to_norm_factor_and_exponents() method returns exponents as an array
of shape (n_samples,). When conditioning on a single value, this is a
1-element array, not a scalar. Attempting to assign this array to
marginal_prior_exponents[k] (a scalar slot) raised:

    ValueError: setting an array element with a sequence

The fix extracts the scalar value with exponents[0] since condition()
is designed to handle a single conditioning value.

Added test to verify various input formats work correctly:
- 1D array: np.array([val])
- list: [val]
- nested list: [[val]]
- 2D array: np.array([[val]])
jkitchin added a commit to KitchinHUB/generative-optimization that referenced this pull request Jan 11, 2026
The gmr library GMM.condition() method has a bug that causes ValueError
when conditioning on a single value. Added sample_gmm_conditional() helper
function that conditions each MVN component separately as a workaround.

See upstream fix: AlexanderFabisch/gmr#61

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
@AlexanderFabisch AlexanderFabisch merged commit dab6fb4 into AlexanderFabisch:master Jan 12, 2026
3 checks passed
@AlexanderFabisch

Copy link
Copy Markdown
Owner

Thanks for the contribution!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants