[ create a new paste ] login | about

Link: http://codepad.org/AiAZoioo    [ raw code | fork ]

Python, pasted on Nov 25:
import scipy
import scipy.sparse
import time

if __name__ == '__main__':

    zero_frac = 0.95
    
    C = (scipy.rand(100000,400) > zero_frac).astype(scipy.int16)
    T = scipy.rand(400) # does not sum to one but whatever

    C_sparse = scipy.sparse.coo_matrix(C)
    sp_coords = C_sparse.nonzero()
    sp_data = C_sparse.data

    start_time = time.time()
    lik_dense = scipy.prod(T ** C, 1)
    print('Time dense: %f,' % (time.time() - start_time))

    start_time = time.time()
    log_results = scipy.log(T[sp_coords[1]] ** sp_data);
    lik_sparse = scipy.exp(scipy.sparse.csr_matrix((log_results, sp_coords), shape=C_sparse.shape).sum(1))
    print('Time sparse: %f,' % (time.time() - start_time))

    print lik_dense[:10]
    print lik_sparse[:10]


Create a new paste based on this one


Comments: