build matrix from blocks

2024/10/12 11:18:58

I have an object which is described by two quantities, A and B (in real case they can be more than two). Objects are correlated depending on the value of A and B. In particular I know the correlation matrix for A and for B. Just as example:

a = np.array([[1, 1, 0, 0],[1, 1, 0, 0],[0, 0, 1, 1],[0, 0, 1, 1]])
b = np.array([[1, 1, 0],[1, 1, 1],[0, 1, 1]])
na = a.shape[0]
nb = b.shape[0]

correlation for A:

correlation A

so if an element has A == 0.5 and the other equal to A == 1.5 they are fully correlated (red). Otherwise if an element has A == 0.5 and the second item has A == 3.5 they are uncorrelated (blue).

Similarly for B:

correlation B

Now I want multiply the two correlation matrixes, but I want to obtain as final matrix a matrix with two axis, where the new axes are a folded version of the original axes:

def get_folded_bin(ia, ib):return ia * nb + ib

here what I am doing:

result = np.swapaxes(np.tensordot(a, b, axes=0), 1, 2).reshape(na* nb, na * nb)

visually:

enter image description here

and in particular this must hold:

for ia1 in xrange(na):for ia2 in xrange(na):for ib1 in xrange(nb):for ib2 in xrange(nb):assert(a[ia1, ia2] * b[ib1, ib2] == result[get_folded_bin(ia1, ib1), get_folded_bin(ia2, ib2)])

actually my problem is to do it with more quantities (A, B, C, ...) in a general way. Maybe there is also a simpler function within numpy to do that.

Answer

np.einsum lets you simplify the tensordot expression a bit:

result = np.einsum('ij,kl->ikjl',a,b).reshape(-1, na * nb)

I don't think there's a way of eliminating the reshape.

It may also be easier to generalize to more arrays, though I wouldn't get carried away with too many iteration variables in one einsum expression.

https://en.xdnf.cn/q/118204.html

Related Q&A

How to convert string with UTC offset

I have date as In [1]: a = "Sun 10 May 2015 13:34:36 -0700"When I try to convert it using strptime, its giving error.In [3]: datetime.strptime(a, "%a %d %b %Y %H:%M:%S %Z"...: ) ---…

regex multiline not working on repeated patterns

I am trying to get a regex expression to match multiple patterns with multilines but it keeps matching everything. For instance I want to match two times this code:STDMETHOD(MyFunc)( D2D1_SIZE_U size, …

Django Rest Framework slug_field error

I have this serializer and model. I want to show in my API the field username of User model, but I receive this error.AttributeError at /api/v1/client_share_requests/1/Profile object has no attribute u…

Django - 500 internal server error no module named django

django return 500 internal server error (apache 2.4.10, ubuntu 15.04, django 1.9.6)apache log:[wsgi:warn] mod_wsgi: Compiled for Python/3.4.2. [wsgi:warn] mod_wsgi: Runtime using Python/3.4.3. [mpm_eve…

Unable to connect to Google Bigtable using HBase REST api

Following this example, running the test script "python put_get_with_client.py" results in a 400 error (Bad Request).Bad requestjava.lang.ClassCastException: org.apache.hadoop.hbase.client.Bi…

HTML form button to run PHP to execute Python script

I am building an HTML document that is meant to run locally. On it is a button that I would like to have run a Python script when clicked. Im trying to use a PHP-generated button. Theres no input or ou…

Python - finding time slots

I am writing a small Python script to find time available slots based off calendar appointments. I was able to reuse the code on the post here: (Python - Algorithm find time slots).It does seem to wor…

ReportLab - error when creating a table

This is the first time Ive used ReportLab, I have tried to edit an existing script that does exactly what I want to do, but I get the following error, when I try and run the script.Script - import os, …

Secure login with Python credentials from user database

I like to create a secure login with Python but need to check the user table from a database, so that multiple users can log in with their own password. Mainly like this, works like a charm but not sec…

count number of names in list in python [duplicate]

This question already has answers here:How to count the frequency of the elements in an unordered list? [duplicate](33 answers)Closed 6 years ago.i have one list wich has names in it:names = [test,hal…