How to add an extra column to a NumPy array
Date : March 29 2020, 07:55 AM
I wish did fix the issue. I think a more straightforward solution and faster to boot is to do the following: import numpy as np
N = 10
a = np.random.rand(N,N)
b = np.zeros((N,N+1))
b[:,:1] = a
In [23]: N = 10
In [24]: a = np.random.rand(N,N)
In [25]: %timeit b = np.hstack((a,np.zeros((a.shape[0],1))))
10000 loops, best of 3: 19.6 us per loop
In [27]: %timeit b = np.zeros((a.shape[0],a.shape[1]+1)); b[:,:1] = a
100000 loops, best of 3: 5.62 us per loop

determining repeated values in numpy array and adding them in another column python
Date : March 29 2020, 07:55 AM
I think the issue was by ths following , If I understand correctly, you want the first column in your output to be the full sequence of values in w (including repetitions), and the second column to be the counts for each value (also repeated for repeated values in w)? You can do this using np.unique by returning both the item counts and the set of 'inverse' indices that reconstruct the original array from the unique values (in the example below, uvals[idx] would give you back w). You can use the inverse indices to index into the count values according to wherever the corresponding unique items occur within w: w = np.array([1, 3, 4, 5, 6, 2, 9, 2, 4, 2, 1, 3, 3, 6])
uvals, idx, counts = np.unique(w, return_counts=True, return_inverse=True)
out = np.vstack((w, counts[idx])).T
print(out)
# [[1 2]
# [3 3]
# [4 2]
# [5 1]
# [6 2]
# [2 3]
# [9 1]
# [2 3]
# [4 2]
# [2 3]
# [1 2]
# [3 3]
# [3 3]
# [6 2]]

Python: adding values to a numpy bool array columnwise
Tag : arrays , By : user161314
Date : March 29 2020, 07:55 AM
may help you . I have a list of values and a numpy bool array that looks like the following: , Use transpose method: import numpy as np
boo = np.array([[False, True, True, False, False, True],
[True, True, True, False, False, True],
[True, True, True, True, True, True]])
x = np.zeros(boo.shape, dtype=int)
y = np.array([1, 7, 2, 2, 3, 7, 1, 1, 4, 2, 9, 1, 2])
x.T[boo.T] = y
print(x)
[[0 2 7 0 0 9]
[1 2 1 0 0 1]
[7 3 1 4 2 2]]

Date : March 29 2020, 07:55 AM
hope this fix your issue I wrote the following code that does: (1) Generate root matrix of shape (3, 16), and (2) Generate 1000 binary vectors C such that each vector is added at the end of the root matrix iteratively one at a time. , If you can swap this: batch = root[i:(i + (len(root)))]
batch = np.append(batch, C[i])
batch = np.append(batch, [C[i]], axis=0)

numpy array concatenate with extra column to each array
Date : March 29 2020, 07:55 AM
To fix the issue you can do I try to split a numPy array in roughly equal parts and merge them together with an extra value but end up being confused how I could do this. I have a list : [0., 2.25, 4., 4., 4., 4., 4., 4., 4., 2.25], which after an np.array_split and concatenate with an extra column should end up like: [0. , 2.25, 4., 8., 4., 4., 4., 8., 4., 4., 8., 4. , 2.25] In [71]: alist = [0., 2.25, 4., 4., 4., 4., 4., 4., 4., 2.25]
In [72]: x = np.array(alist)
In [73]: xs = np.array_split(x, 4)
In [75]: xs
Out[75]:
[array([0. , 2.25, 4. ]),
array([4., 4., 4.]),
array([4., 4.]),
array([4. , 2.25])]
In [76]: np.concatenate(xs)
Out[76]: array([0. , 2.25, 4. , 4. , 4. , 4. , 4. , 4. , 4. , 2.25])
In [77]: np.array(xs)
Out[77]:
array([array([0. , 2.25, 4. ]), array([4., 4., 4.]), array([4., 4.]),
array([4. , 2.25])], dtype=object)
In [79]: np.array_split(x,5)
Out[79]:
[array([0. , 2.25]),
array([4., 4.]),
array([4., 4.]),
array([4., 4.]),
array([4. , 2.25])]
In [80]: np.array(np.array_split(x,5))
Out[80]:
array([[0. , 2.25],
[4. , 4. ],
[4. , 4. ],
[4. , 4. ],
[4. , 2.25]])
In [84]: for i,v in enumerate(xs[:1]):
...: xs[i] = np.concatenate([v,[8]])
...:
In [85]: xs
Out[85]:
[array([0. , 2.25, 4. , 8. ]),
array([4., 4., 4., 8.]),
array([4., 4., 8.]),
array([4. , 2.25])]
In [86]: np.concatenate(xs)
Out[86]:
array([0. , 2.25, 4. , 8. , 4. , 4. , 4. , 8. , 4. , 4. , 8. ,
4. , 2.25])

