arrays - fastest way to arrange data in python numpy based on list -


I have a problem in managing data in a number of data:

  Numpy.array ([1,3,5,4,6]) and I have the data:  
  numpy.array ([1, 2], 3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19])   

and more I need to manage data

  numpy.array ([[1,9999,9999,9999,9999,9999,9999] [2,3,4,9999,9999,9999] [ 5,6,7,8,9,99,99] [10,11,12,13,99,99 99] [14,15,16,17,18,19]])   

I thought it is the same with diagonal / diagonal / trace functionality.

I have usually worked using basic repetition ... Is this functionality in MMPI so that it can perform faster?

Here are some ways to organize the data:

  Numpy import arange, array, rank (j, n): a = randint (1, n, m) b, c = array (aima), ones (m, n), dtype = Int) * 999 return A, B, CDEF Basic (A, B, C) Basic number: # Some basic repetition is assumed on the basis of n = 0 for the xrange (lane (a)): m = A [C] of [K, M:], N = B [N: N + m], n + m def advanced_1 (a, b, c): # Svens based on answers cum_a = r_ [0, a cumsum ()] I = arange (len (a)) Repeat (a) j = Arange (cum_a [-1]) - cum_a [: - 1] .repeat (a) c [i, j] = b def advance_2 (a , B, c): # other loopless version c [arange (c.shape [1]) + zero ((lane (a), 1), dtype = int) & lt; A [:, none]] = b   

and some time:

  in []: m, n = 10, in 100 []: A, B, C = GN_TEST (M, N) in []: 1. * a.sum () / (M * N) Out []: 0.531 in []:% timeit advanced_1 (a, b, c) 10000 Loop, Best 3: 99.2 in the loop []:% timeit advanced_2 (a, b, c) 10000 loops, best 3: 68 per loop per []:% timeit basic_1 (a, b, c) 10000 loops, best : 3: 47.1 in the loop []: m, n = 50, in 500 []: a, b, c = gen_tst (m, n) []: 1. * a.sum () / (M * N ) Out []: 0.455 in []:% timeit advanced_1 (a, b, c) 1000 loops, 3: 1.03 ms per loop []:% timeit advanced_2 (a, B, c) 1000 loops, 3: 1.06 ms per loop in best []:% timeit basic_1 (a, b, c) 1000 loops, best 3: 227 per loop per []: m, n = 250, in 2500 [ ]: A, b, c = gn_tist (m, n) in []: 1. * a.sum () / (m * n) out []: 0.486 []:% timeit advanced_1 (a, b, c ) 10 loops, maximum 3: 30.4 ms per loop []:% timeit advanced_2 (a, b, c) 10 loops, best 3: 32.4 ms per loop []:% timeit basic_1 (a, b, c) 1000 loops , Best 3: 2 Loops per MS   

So basic repetition seems quite efficient.

Update : Certainly the performance of basic repetition-based implementation can still be improved as a starting point suggestion; For example, consider this (basic repetition based on less extra):

  def for basic 2 (a, b, c): n = 0 k, in mum (a) : Nm = n + mc [k, m], n = b [n: nm], nm    

Comments