Подразделение массива - переводящий от MATLAB до Python

Использование $resource позволит вам достичь того, что вы хотите, а также даст вам гораздо больший контроль по сравнению с $http

(Не забудьте включить ngResrouce в качестве зависимость от вашего приложения.)

myDemo.factory('photosFactory', function($resource) {
    var factory = {};

    factory.getPhotos = $resource('content/test_data.json', {}, {
        'query': {method: 'GET', isArray: true}
    });
    return factory;
});

controllers.AppCtrl = function($scope, $location, $http, photosFactory) {
    $scope.photos = [];
    init();
    function init() {
        $scope.photos = photosFactory.getPhotos.query();
    }
};

7
задан Vebjorn Ljosa 18 June 2009 в 18:39
поделиться

4 ответа

Строка

c = a.' / b

вычисляет решение уравнения cb = a T для c . В Numpy нет оператора, который бы делал это напрямую. Вместо этого вы должны решить b T c T = a для c T и транспонировать результат:

c = numpy.linalg.lstsq(b.T, a.T)[0].T
8
ответ дан 6 December 2019 в 08:16
поделиться

The symbol / is the matrix right division operator in MATLAB, which calls the mrdivide function. From the documentation, matrix right division is related to matrix left division in the following way:

B/A = (A'\B')'

If A is a square matrix, B/A is roughly equal to B*inv(A) (although it's computed in a different, more robust way). Otherwise, x = B/A is the solution in the least squares sense to the under- or over-determined system of equations x*A = B. More detail about the algorithms used for solving the system of equations is given here. Typically packages like LAPACK or BLAS are used under the hood.

The NumPy package for Python contains a routine lstsq for computing the least-squares solution to a system of equations. This routine will likely give you comparable results to using the mrdivide function in MATLAB, but it is unlikely to be exact. Any differences in the underlying algorithms used by each function will likely result in answers that differ slightly from one another (i.e. one may return a value of 1.0, whereas the other may return a value of 0.999). The relative size of this error could end up being larger, depending heavily on the specific system of equations you are solving.

To use lstsq, you may have to adjust your problem slightly. It appears that you want to solve an equation of the form cB = a, where B is 25-by-18, a is 1-by-18, and c is 1-by-25. Applying a transpose to both sides gives you the equation BTcT = aT, which is a more standard form (i.e. Ax = b). The arguments to lstsq should be (in this order) BT (an 18-by-25 array) and aT (an 18-element array). lstsq should return a 25-element array (cT).

Note: while NumPy doesn't make any distinction between a 1-by-N or N-by-1 array, MATLAB certainly does, and will yell at you if you don't use the proper one.

7
ответ дан 6 December 2019 в 08:16
поделиться

In Matlab, A.' means transposing the A matrix. So mathematically, what is achieved in the code is AT/B.


How to go about implementing matrix division in Python (or any language) (Note: Let's go over a simple division of the form A/B; for your example you would need to do AT first and then AT/B next, and it's pretty easy to do the transpose operation in Python |left-as-an-exercise :)|)

You have a matrix equation C*B=A (You want to find C as A/B)

RIGHT DIVISION (/) is as follows:

C*(B*BT)=A*BT

You then isolate C by inverting (B*BT)

i.e.,

C = A*BT*(B*BT)' ----- [1]

Therefore, to implement matrix division in Python (or any language), get the following three methods.

  • Matrix multiplication
  • Matrix transpose
  • Matrix inverse

Then apply them iteratively to achieve division as in [1].

Only, you need to do AT/B, therefore your final operation after implementing the three basic methods should be:

AT*BT*(B*BT)'

Note: Don't forget the basic rules of operator precedence :)

5
ответ дан 6 December 2019 в 08:16
поделиться

[отредактировано] Как указал Сувеш, я был полностью неправильно раньше. однако numpy все еще может легко выполнять процедуру, которую он дает в своем сообщении:

A = numpy.matrix(numpy.random.random((18, 1))) # as noted by others, your dimensions are off
B = numpy.matrix(numpy.random.random((25, 18)))
C = A.T * B.T * (B * B.T).I
1
ответ дан 6 December 2019 в 08:16
поделиться
Другие вопросы по тегам:

Похожие вопросы: