Multiple Matrix Multiplications with Numpy
Question: I have about 650 csv-based matrices. I plan on loading each one using Numpy as
in the following example:

    
    
    m1 = numpy.loadtext(open("matrix1.txt", "rb"), delimiter=",", skiprows=1)
    

There are matrix2.txt, matrix3.txt, ..., matrix650.txt files that I need to
process.

My end goal is to multiply each matrix by each other, meaning I don't
necessarily have to maintain 650 matrices but rather just 2 (1 ongoing and 1
that I am currently multiplying my ongoing by.)

Here is an example of what I mean with matrices defined from 1 to n: M1, M2,
M3, .., Mn.

M1*M2*M3*...*Mn

The dimensions on all the matrices are the same. **The matrices are not
square. There are 197 rows and 11 columns.** None of the matrices are sparse
and every cell comes into play.

What is the best/most efficient way to do this in python?

EDIT: I took what was suggested and got it to work by taking the transpose
since it isn't a square matrix. As an addendum to the question, i**s there a
way in Numpy to do element by element multiplication**?


Answer: A Python3 solution, if "each matrix by each other" actually means just
multiplying them in a row and _the matrices have compatible dimensions_ ( (n,
m) · (m, o) · (o, p) · ... ), which you hint at with "(1 ongoing and 1
that...)", then use (if available):

    
    
    from functools import partial
    fnames = map("matrix{}.txt".format, range(1, 651))
    np.linalg.multi_dot(map(partial(np.loadtxt, delimiter=',', skiprows=1), fnames))
    

or:

    
    
    from functools import reduce, partial
    fnames = map("matrix{}.txt".format, range(1, 651))
    matrices = map(partial(np.loadtxt, delimiter=',', skiprows=1), fnames)
    res = reduce(np.dot, matrices)
    

Maps etc. are lazy in python3, so files are read as needed. Loadtxt doesn't
require a pre-opened file, a filename will do.

Doing all the combinations lazily, given that the matrices have the same shape
(will do a lot of rereading of data):

    
    
    from functools import partial
    from itertools import starmap, combinations
    map_loadtxt = partial(map, partial(np.loadtxt, delimiter=',', skiprows=1))
    fname_combs = combinations(map("matrix{}.txt".format, range(1, 651)), 2)
    res = list(starmap(np.dot, map(map_loadtxt, fname_combs)))
    

Using a bit of grouping to reduce reloading of files:

    
    
    from itertools import groupby, combinations, chain
    from functools import partial
    from operator import itemgetter
    
    loader = partial(np.loadtxt, delimiter=',', skiprows=1)
    fname_pairs = combinations(map("matrix{}.txt".format, range(1, 651)), 2)
    groups = groupby(fname_pairs, itemgetter(0))
    res = list(chain.from_iterable(
        map(loader(k).dot, map(loader, map(itemgetter(1), g)))
        for k, g in groups
    ))
    

Since the matrices are not square, but have the same dimensions, you would
have to add transposes before multiplication to match the dimensions. For
example either `loader(k).T.dot` or `map(np.transpose, map(loader, ...))`.

If on the other hand the question actually was meant to address element wise
multiplication, replace `np.dot` with `np.multiply`.

