English 中文(简体)
• 只用Numpy和Azza训练最不发达国家广场[封闭]
原标题:Constrained Least Squares with Numpy and Python only [closed]
Closed. This question needs debugging details. It is not currently accepting answers.

如何只使用<代码>numpy限制最不发达国家广场。 是否有任何办法将制约因素纳入<代码>numpy.linalg.lstsq(。 或者有<代码>numpy+ 围起来,限制最不发达国家广场? 我知道,我可以很容易地利用<代码>cvxpy和scipy.optimize,但这种优化将成为numba的一部分。 JITed功能,这两个功能没有numba的支持。

EDIT: Here is a dummy example of a problem I want to solve

arr_true = np.vstack([np.random.normal(0.3, 2, size=20),
                      np.random.normal(1.4, 2, size=20),
                      np.random.normal(4.2, 2, size=20)]).transpose()
x_true = np.array([0.3, 0.35, 0.35])
y = arr_true @ x_true

def problem():
    noisy_arr = np.vstack([np.random.normal(0.3, 2, size=20),
                        np.random.normal(1.4, 2, size=20),
                        np.random.normal(4.2, 2, size=20)]).transpose()

    x = cvxpy.Variable(3)
    objective = cvxpy.Minimize(cvxpy.sum_squares(noisy_arr @ x - y))
    constraints = [0 <= x, x <= 1, cvxpy.sum(x) == 1]
    prob = cvxpy.Problem(objective, constraints)
    result = prob.solve()
    output = prob.value
    args = x.value

    return output, args

从根本上说,我的问题是,将1美元+x_2+x_3 = 1美元和4美元x_i、0 q x_i leq 1美元等值的Ax-y$降到最低。

问题回答

Is it linear? Based on your numpy.linalg.lstsq suggestion I ll assume that for now. Also, I assume (to solve the simple case first) that you talk about equality constraints. Then you can easily solve it yourself by using Lagrange multipliers, and the solution can be found by solving a linear equation system.

If the problem is to minimize A * x - y over the constraints B * x = b, then solve

As an example:

from numpy import array, zeros
from numpy.linalg import solve

A = array([[1, 2], [3, 4], [5, 6], [7, 8]])
y = array([3.2, 6.8, 11.1, 15.2])

B = array([[1, -1]])
b = array([0.1])

S = zeros((3, 3))
r = zeros(3)

S[:2, :2] = A.T @ A
S[2:, :2] = B
S[:2, 2:] = B.T
r[:2] = A.T @ y
r[2] = b

x = solve(S, r)[:2]
print(x)

记者: 也许可以列入插图,但简短的测试使我得以进入<条码>x

[1.06262376 0.96262376]

This seems reasonable, and my constraint x1 - x2 = 0.1 is also fulfilled.





相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签