python - numpy append to an indexed array (after np.where) -
i want append values selection of array without having go through loop.
i.e. if want add 0 values locations of array:
a=np.array([[1,2,3,4,5],[1,2,3,4,5]]) condition=np.where(a>2) a[condition]=np.append(a[condition],np.array([0]*len(condition[0]))) -> valueerror: shape mismatch: value array of shape (12,) not broadcast indexing result of shape (6,)
edit clarification:
i need add values (and dimension if needed) selected array location. loop looks that:
for t in range(len(ind)): c = cols[t] r = rows[t] if data1[r, c] > 2: data2[r,c]=np.append(data2[r,c],t)
is there way remove loop (~100 000 iterations)? thank
let's @ pieces:
in [92]: a=np.array([[1,2,3,4,5],[1,2,3,4,5]]) ...: condition=np.where(a>2) ...: in [93]: out[93]: array([[1, 2, 3, 4, 5], [1, 2, 3, 4, 5]]) in [94]: condition out[94]: (array([0, 0, 0, 1, 1, 1], dtype=int32), array([2, 3, 4, 2, 3, 4], dtype=int32)) in [95]: a[condition] out[95]: array([3, 4, 5, 3, 4, 5]) in [96]: np.append(a[condition],np.array([0]*len(condition[0]))) out[96]: array([3, 4, 5, 3, 4, 5, 0, 0, 0, 0, 0, 0])
you trying put 12 values 6 slots. no can do!
what expecting? don't think should speculate. go ahead show loop.
Comments
Post a Comment