python - Simpler code to combine multiple lists into one array? I actually wish "lists" are still "lists" in that array -
i have question similar this , that, solutions lists "united" lack of "differentiation".
my python code following:
y = np.empty([1,len(test)]) count = -1 feature in test : n = engine.neighbours(np.asarray(feature)) if len(n) != 4096: print "error" continue count = count + 1 y [count] = engine.neighbours(np.asarray(feature))
i wondering if there simplified code job?
the linked questions have flattening list of lists. in code processing list of lists (i'm guessing), filtering out some, , collecting rest in 2d array.
oops! y
initialized 1 row, yet try place values in y[count]
, count
increase size of test
.
y = np.empty([1,len(test)]) count = -1 # prefer start 0, thats style issue feature in test : n = engine.neighbours(np.asarray(feature)) # engine.neighbors requires array; , returns array? if len(n) != 4096: print "error" continue count = count + 1 y [count] = engine.neighbours(np.asarray(feature)) # why call neighbors again? why not reuse n?
a common way of creating array incrementally is:
alist = [] feature in tests: n = engine.neighbors(np.array(feature) # test n length alist.append(n) y = np.array(alist)
since length test, n
have same length, resulting array 2d, shape (n,4096)
, n
number of tests correct length.
initializing y
np.empty((length(tests),4096)]
, , inserting y[count,:] = n
may faster. may end unfilled rows if fail length test, can remove those.
initializing y
np.empty((1,4096)]
, , inserting y=np.append(y,n)
should work. note append
different list append. , slower. prefer people use concatenate
directly:
y = np.concatenate([y, n[none,:]], axis=0)
the concatenation explicit, , required dimension manipulation clear.
to make 1d array of arrays, have this:
y=np.empty((4,),dtype=object) in range(len(y)): y[i]=np.arange(i,2*i)
producing:
array([array([], dtype=int32), array([1]), array([2, 3]), array([3, 4, 5])], dtype=object)
which arguably little more y
list produced by
y=[] in range(4): y.append(np.arange(i,2*i))
in i'm assuming engine.neighbors()
takes 1d array, , returns 1d array. if if took/returned multiple feature
'vectorize' things. long can give 1 feature
@ time stuck form of iteration.
Comments
Post a Comment