Python 在这种情况下如何使用np.stack?

Python 在这种情况下如何使用np.stack?,python,numpy,Python,Numpy,我有两个np.ndarray: predictions = np.array([[0.2, 0.9], [0.01, 0.0], [0.3, 0.8], ...]) filenames = np.array(["file1", "file2", "file3", ...]) 文件名中的每个文件对应于预测中的每个数组: 文件1==>[0.2,0.9] 文件2==>[0.01,0.0] 文件3==>[0.3,0.8] 我想将这两个数组中的值打印到csv文件中,如下所示: fileName

我有两个np.ndarray:

predictions = np.array([[0.2, 0.9], [0.01, 0.0], [0.3, 0.8], ...])
filenames = np.array(["file1", "file2", "file3", ...])
文件名中的每个文件对应于预测中的每个数组:

文件1==>[0.2,0.9]

文件2==>[0.01,0.0]

文件3==>[0.3,0.8]

我想将这两个数组中的值打印到csv文件中,如下所示:

fileName        label1      label2
file1           0.2         0.9 
file2           0.1         0.0
file3           0.3         0.8
我希望使用np.stack将这两个np.array合并成一个数据结构,然后使用np.savetext(路径、数组)输出到csv文件


但是np.stack(array,axis=1)似乎只接受两个形状相同的数组。有没有办法让stack在这种情况下工作?

这里有一种使用
zip的方法:

>>> np.array(zip(filenames, *zip(*predictions)))
array([['file1', '0.2', '0.9'],
       ['file2', '0.01', '0.0'],
       ['file3', '0.3', '0.8']], 
      dtype='|S5')
另一个是:


您可以向文件名添加另一个维度,然后使用
hstack()
将其与预测堆叠:

该解决方案使用以下程序和例程:

import numpy as np
result = np.hstack((np.expand_dims(filenames, axis=1), predictions))

# saving to csv file using `np.savetxt`:
with open('./text_files/predictions.csv', 'wb') as fh:
    np.savetxt(fh, X= result, header='fileName\tlabel1\tlabel2', delimiter='\t', fmt='%-8s\t%-6s\t%-6s')
predictions.csv
(测试文件)内容:

# fileName  label1  label2
file1       0.2     0.9   
file2       0.01    0.0   
file3       0.3     0.8   

有两个数组,一个是带数字的2d数组,另一个是带字符串的1d数组

In [53]: predictions = np.array([[0.2, 0.9], [0.01, 0.0], [0.3, 0.8]])
    ...: filenames = np.array(["file1", "file2", "file3"])

In [54]: predictions
Out[54]: 
array([[ 0.2 ,  0.9 ],
       [ 0.01,  0.  ],
       [ 0.3 ,  0.8 ]])
In [55]: filenames
Out[55]: 
array(['file1', 'file2', 'file3'], 
      dtype='<U5')
请注意,结果是字符串类型。这可能没问题
column\u stack
vstack
也可以使用,但它们最终会调整尺寸,并使用concatenate,就像我所做的那样

np.stack
在新维度上连接数组。我认为你不想要3d阵列

In [58]: np.savetxt('test', arr, fmt='%10s')
In [59]: cat test
     file1        0.2        0.9
     file2       0.01        0.0
     file3        0.3        0.8
您可以调整
fmt
,但使用字符串时,您会在
%s
上遇到一些变化
savetxt
还允许使用页眉和页脚

为了更好地控制
fmt
,如小数位数等,我们必须构造一个结构化数组,一个将字符串字段与两个浮点字段混合的数组。如果需要的话,我可以对此进行扩展

另一种选择是只
zip
数组和写行
savetxt
在编写文本文件时没有任何神奇的功能

In [65]: for f, n in zip(filenames, predictions):
    ...:     print('%s  %s'%(f, '%10.2f %10.2f'%tuple(n)))
    ...:     
file1        0.20       0.90
file2        0.01       0.00
file3        0.30       0.80
考虑到从1列字符串和2列浮点数组创建结构化数组的复杂性,最后一种
zip
方法可能是最简单的

结构化阵列 构造此阵列的更简单方法是:

arr = np.rec.fromarrays((filenames, predictions[:,0], predictions[:,1]))
我更喜欢这样的结构化数组:

In [123]: dt=np.dtype([('files', 'U10'), ('pred', 'float64', (2,))])
In [124]: dt
Out[124]: dtype([('files', '<U10'), ('pred', '<f8', (2,))])
In [125]: arr = np.zeros((3,),dtype=dt)
In [126]: arr['files']=filenames
In [127]: arr['pred']=predictions
In [128]: arr
Out[128]: 
array([('file1', [0.2, 0.9]), ('file2', [0.01, 0.0]), ('file3', [0.3, 0.8])], 
      dtype=[('files', '<U10'), ('pred', '<f8', (2,))])
[123]中的
:dt=np.dtype([('files','U10'),('pred','float64',(2,)]))
In[124]:dt

Out[124]:数据类型([('files','preds=np.hstack([filename[:,None],predictions])np.savetxt('my_submit.csv',preds,fmt='%d,%.5f,%.5f',header='image,ALB,BET',comments='')为什么'np.savetxt'会产生这个错误:“类型错误:数组数据类型之间不匹配。”(
dtype
将字符串数组连接到数字数组时是字符串。它只能用
%s
格式化。不能使用数字格式化程序.np.savetxt('test',arr,fmt='%10s'):这将概率放在一列中,我想要3个不同的列。为什么不起作用:“np.savetxt('test.csv',arr,fmt='%s,%.5f,%.5f',header='image,xx,yy')“?您的
arr
形状是什么?我的是(3,3)(请参见
Out[57]
)。您解决方案的问题是,它将所有值放在一列中。例如,我希望每个“0.20.9”都放在两列中。我不明白。我的
Out[59]
显示3列-标签和2列数字。除列标题外,它与接受的答案相同。由于“fmt=“%10s”,您的第59行实际上在一列中,因为您只有一个格式变量。我认为它需要“%s,%s,%s”。顺便问一句,“%10s”中的“10”是什么?
In [65]: for f, n in zip(filenames, predictions):
    ...:     print('%s  %s'%(f, '%10.2f %10.2f'%tuple(n)))
    ...:     
file1        0.20       0.90
file2        0.01       0.00
file3        0.30       0.80
In [114]: arr = np.zeros((3,),np.dtype('U10,f,f'))
In [115]: arr['f0']=filenames
In [116]: arr['f1']=predictions[:,0]
In [117]: arr['f2']=predictions[:,1]
In [118]: np.savetxt('test',arr, fmt='%10s %10.2f %10.1f')
In [119]: cat test
     file1       0.20        0.9
     file2       0.01        0.0
     file3       0.30        0.8
arr = np.rec.fromarrays((filenames, predictions[:,0], predictions[:,1]))
In [123]: dt=np.dtype([('files', 'U10'), ('pred', 'float64', (2,))])
In [124]: dt
Out[124]: dtype([('files', '<U10'), ('pred', '<f8', (2,))])
In [125]: arr = np.zeros((3,),dtype=dt)
In [126]: arr['files']=filenames
In [127]: arr['pred']=predictions
In [128]: arr
Out[128]: 
array([('file1', [0.2, 0.9]), ('file2', [0.01, 0.0]), ('file3', [0.3, 0.8])], 
      dtype=[('files', '<U10'), ('pred', '<f8', (2,))])