quinngroup / dr1dl-pyspark

Dictionary Learning in PySpark
Apache License 2.0
1 stars 1 forks source link

copying vector to matrix #11

Closed MOJTABAFA closed 8 years ago

MOJTABAFA commented 8 years ago

for copying a vector into a different matrix actually I couldn't find a numpy function , numpy.tile can not give a solution to me so I had to convert line by line their code and creating a function which deals with normall arrays in python. please let me know if there is any numpy solution for that .

magsol commented 8 years ago

I am not exactly sure what you mean by this; do you mean inserting a numpy array into a certain row or column in a matrix? If that is the case, this is the syntax:

import numpy as np
matrix = np.random.random((10, 10))

# Inserts a row in-place in a matrix
row = np.random.random(10)
matrix[5, :] = row

# Inserts a column in-place in a matrix
col = np.random.random(10)
matrix[:, 5] = col
MOJTABAFA commented 8 years ago

yeah , but the problem here is we dont know about column number ( you consideredo it as 5). and in case of using a loop , I think the performance would not be changed. here is the line by line converted code for VcTtoMtx , for the first function using numpy is definitely possible but for the 2nd one it seems .... :

def op_vctCopy2MTX( vct_input, mtx_input, N, idx_copy):

for n in range (N):
    mtx_input[[idx_copy],[n]] = vct_input[n]

def op_vctCopy2MTX2( vct_input, mtx_input, N, idx_copy, idxs_n, R):

for r in range (R):
    n = idxs_n[r]
    mtx_input[[idx_copy],[n]] = vct_input[n]
magsol commented 8 years ago

For line 53, that's one line of Python as I gave you above:

mtx_input[idx_copy, :] = vct_input

For line 61, that's a little trickier but still one line:

mtx_input[idx_copy, :] = vct_input[idxs_n]

In Python, using an explicit loop is often a severe performance hit. Using array slicing (as in my examples here) calls very efficient precompiled C++ plus in the NumPy/SciPy libraries that are much, much faster than looping for n in range(N).

MOJTABAFA commented 8 years ago

Thanks, so instead of calling function VctbyMtx and VctbyMtx2 we should drop them and in continue in main function do the following substitution :

    #op_vctCopy2MTX2( v, Z, P, m, idxs_n, R )
    mtx_input[idx_copy, :] = vct_input
    #op_vctCopy2MTX( u_new, D, T, m)
    mtx_input[idx_copy, :] = vct_input[idxs_n]
MOJTABAFA commented 8 years ago

Am I right ?

magsol commented 8 years ago

LGTM.

MOJTABAFA commented 8 years ago

I applied the changes on code, Thanks