在字典中连接多个 Numpy 数组
concatenating multiple Numpy arrays in dictionaries
我有四本词典。字典中每个键的每个值都是一维 numpy 数组。我想将所有这些 numpy 数组合并为一个。例如:
first dictionary = {'feature1': array([0., 0., 1., 0.]),
'feature2': array([0., 1., 0., 0.]),
'feature3': array([1., 0., 0.,0.,0.,0.])}
second dictionary = {'feature4': array([0.]),
'feature5': array([0., 0.]),
'feature6': array([0.023]),
'feature7': array([0.009]),
'feature8': array([0.])}
third dictionary = {'feature9': array([ 0., 0., 0., 912., 0., 0., 0.]),
'feature10': array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.]),}
生成的最终 numpy 应如下所示:
array([0., 0., 1., 0.,0., 1., 0., 0.,1., 0., 0.,0.,0.,0.,
0.,0., 0.,0.023,0.009,0.,0.,
0., 0., 912., 0., 0., 0.,0., 0., 0., 0.,
0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.]
举个例子,我把这本词典变小了,但我在每本词典中都有 50 个键。所以基本上我想加入我字典中的所有 numpy 数组。我怎样才能做到这一点?将不胜感激。
你可以这样做:
output = []
for dictionary in [firstdictionary,seconddictionary,thirddictionary]:
for key, value in dictionary.items():
output += list(value)
output_array = np.array(output)
我有四本词典。字典中每个键的每个值都是一维 numpy 数组。我想将所有这些 numpy 数组合并为一个。例如:
first dictionary = {'feature1': array([0., 0., 1., 0.]),
'feature2': array([0., 1., 0., 0.]),
'feature3': array([1., 0., 0.,0.,0.,0.])}
second dictionary = {'feature4': array([0.]),
'feature5': array([0., 0.]),
'feature6': array([0.023]),
'feature7': array([0.009]),
'feature8': array([0.])}
third dictionary = {'feature9': array([ 0., 0., 0., 912., 0., 0., 0.]),
'feature10': array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.]),}
生成的最终 numpy 应如下所示:
array([0., 0., 1., 0.,0., 1., 0., 0.,1., 0., 0.,0.,0.,0.,
0.,0., 0.,0.023,0.009,0.,0.,
0., 0., 912., 0., 0., 0.,0., 0., 0., 0.,
0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.]
举个例子,我把这本词典变小了,但我在每本词典中都有 50 个键。所以基本上我想加入我字典中的所有 numpy 数组。我怎样才能做到这一点?将不胜感激。
你可以这样做:
output = []
for dictionary in [firstdictionary,seconddictionary,thirddictionary]:
for key, value in dictionary.items():
output += list(value)
output_array = np.array(output)