I am trying to merge the two <class 'numpy.ndarray'> into a single array.
Code that I tried:
for emb in embs:
print(np.concatenate( emb['name'], axis=0 ))
Input:
[[-0.05646843 -0.00381141 -0.05721215 0.06029008]
[-0.02690301 0.04218878 0.00404305 0.05841358]]
[[-0.02675129 0.03649405 -0.04379312 -0.04826404]
[ 0.01131434 0.0581139 0.00785002 -0.02065904]]
Output:
[[-0.05646843 -0.00381141 -0.05721215 0.06029008]
[-0.02690301 0.04218878 0.00404305 0.05841358]
[-0.02675129 0.03649405 -0.04379312 -0.04826404]
[ 0.01131434 0.0581139 0.00785002 -0.02065904]]
Any leads would be appreciated.
CodePudding user response:
Your question has several problems.
What is embs
; we can guess, such as a list of dict
, or maybe pandas dataframe?
What is the block labeled input
? The result of this loop:
for emb in embs:
print(emb['name'])
What is output
? That actual output of your code, or the desired output? If the latter, what did your code do? What was wrong with it?
Often we see naive attempts to replicate this list iteration:
alist = []
for x in another_list:
alist.append(x)
But I'm not sure that's what you have in mind. You don't define any array to 'collect' the loop values in. And your loop just has a print()
, which doesn't return anything (or rather returns None
). And concatenate
doesn't work in in-place.
You've read enough of the concatenate
docs to use the axis
parameter, but your single first argument looks nothing like the documented tuple of arrays, (a1, a2, ...)
Since embs
is apparently a list of 2 somethings with a name
value, and the values are matching 2d arrays, this concatenate
should produce your desired output
:
np.concatenate( (embs[0]['name'], embs[1]['name']), axis=0 )
We could make that argument tuple with a list comprehension, or my earlier list append, but I think you need to see this concatenation spelled out in detail. Your understanding of python loops is still weak.
CodePudding user response:
Try this:
print(np.concatenate([emb['name'] for emb in embs]))
CodePudding user response:
input :
data1 = [[-0.05646843 ,-0.00381141, -0.05721215, 0.06029008],
[-0.02690301 , 0.04218878 , 0.00404305, 0.05841358]]
data2 = [[-0.02675129 , 0.03649405, -0.04379312, -0.04826404],
[ 0.01131434 , 0.0581139 , 0.00785002 , -0.02065904]]
output:
concatenated_data = np.concatenate((np.array(data1),np.array(data2)))
print(concatenated_data)