Skip to content
Learn Netverks

Lesson

Step 17/36 47% through track

constraints-bounds

Constraints and bounds

Last reviewed May 28, 2026 Content v20260528
Track mode
server_script
Means
Server runner
Reading
~1 min
Level
intermediate

This lesson

This lesson teaches Constraints and bounds: SciPy scientific routines on NumPy arrays—statistics, optimization, linear algebra, and numerical methods.

Teams apply Constraints and bounds in every serious SciPy project—skipping it leaves blind spots in analysis and reviews.

You will apply Constraints and bounds in contexts like: Research code, engineering simulations, and specialized analytics.

Read the narrative, run NumPy + SciPy snippets in the playground (install scipy and numpy with pip if needed), inspect outputs and convergence, and complete MCQs.

When you can explain the previous lesson's ideas in your own words.

Real optimizers enforce box bounds (each variable in [low, high]) and sometimes linear/nonlinear constraints—essential for physically plausible parameters.

Bounds format

For minimize with method='L-BFGS-B', pass bounds=[(0, None), (-5, 5)] per variable. Use None for open ends.

Constraints dict

  • type='ineq' — c(x) ≥ 0
  • type='eq' — c(x) = 0
  • Methods: SLSQP, trust-constr

Bounded example

import numpy as np
from scipy import optimize

def f(x):
    return (x[0] - 1) ** 2 + (x[1] - 2) ** 2

bounds = [(0, 1), (0, 1)]  # box [0,1] x [0,1]
res = optimize.minimize(f, [0.5, 0.5], method='L-BFGS-B', bounds=bounds)
print(res.x)

Important interview questions and answers

  1. Q: Why bounds in ML?
    A: Keep weights or probabilities in valid ranges—e.g. non-negative concentrations.
  2. Q: L-BFGS-B?
    A: Limited-memory BFGS with box constraints—workhorse for bounded ML-style problems.

Self-check

  1. How do you pass bounds to minimize?
  2. What does an inequality constraint mean in SciPy's convention?

Tip: Use L-BFGS-B when parameters must stay in a box (probabilities, physical lengths).

Interview prep

L-BFGS-B?

Limited-memory BFGS with box bounds per variable.

ineq constraint?

c(x) >= 0 in SciPy constraint dict.

Interview tip Lesson completion confidence

Can you explain this lesson in 30 seconds without reading notes?

Not saved yet.

Playground

Runs on the configured server runner (dev: npm run runner with LEARNING_RUNNER_ENABLED=true). Output appears below the editor.

Check yourself

Multiple choice — immediate feedback.

Discussion

Past discussion is visible to everyone. Only logged-in users can post comments and replies.

Starter discussion topics

  • Bounds in minimize?
  • Inequality cons?

Sign up or log in to post comments and sync lesson progress across devices.

No discussion yet. Be the first to ask a question.

Jump