Код: Выделить всё
Xb = y
Я хотел бы найти оптимальный вектор параметров b, который минимизирует 2-норму |y - X b|, в то же время соблюдая ограничение на Xb, так что c1 < Xb < c2, с c1 > 0 и c2 > 0< /code> для известных c1, c2. Другими словами, Xb должен быть ограничен положительным диапазоном.
У меня уже есть решение для неограниченного Xb, используя scipy.linalg.lstsq:
Код: Выделить всё
max_degree = sum(self.degree)
scale = False
x = self.e.to_numpy()
y = self.f.to_numpy()
z = self.cd.to_numpy()
# Flatten input
x = np.asarray(x).ravel()
y = np.asarray(y).ravel()
z = np.asarray(z).ravel()
# Remove masked values
mask = ~(np.ma.getmask(z) | np.ma.getmask(x) | np.ma.getmask(y))
x, y, z = x[mask].ravel(), y[mask].ravel(), z[mask].ravel()
# Scale coordinates to smaller values to avoid numerical problems at larger degrees
if scale:
x, y, norm, offset = self._scale()
coeff = np.zeros((self.degree[0] + 1, self.degree[1] + 1))
idx = EF_Poly2D_CD_Model._get_coeff_idx(coeff)
# Calculate elements 1, x, y, x*y, x**2, y**2, ...
# np.vander will only create powers of 1 variable, it takes 1-D arrays
A = self.polyvander2d(self.degree)
# masking: We only want the combinations with maximum order COMBINED power
if max_degree is not None:
mask = (idx[:, 0] + idx[:, 1])
Подробнее здесь: [url]https://stackoverflow.com/questions/78529229/least-squares-fitting-with-bounded-response-variable-y-in-python[/url]