Python 混合数据的Numpy savetxt

Python 混合数据的Numpy savetxt,python,numpy,formatting,typeerror,Python,Numpy,Formatting,Typeerror,我必须保存一个数组,其中字符串作为第一列,一些整数/浮点数作为剩余列。我试过了 rows = ['a', 'b', 'c'] value = np.random.rand(3,3) np.savetxt('out.csv', np.c_[rows, value], fmt='%s %.2f %.2f %.2f') 但结果是错误的 TypeError: Mismatch between array dtype ('|S32') and format specifier ('%s %.2f %.2

我必须保存一个数组,其中字符串作为第一列,一些整数/浮点数作为剩余列。我试过了

rows = ['a', 'b', 'c']
value = np.random.rand(3,3)
np.savetxt('out.csv', np.c_[rows, value], fmt='%s %.2f %.2f %.2f')
但结果是错误的

TypeError: Mismatch between array dtype ('|S32') and format specifier ('%s %.2f %.2f %.2f')
我可以用
numpy.savetxt
来做这件事吗

PS:下面的代码可以工作,但我不能限制位数

np.savetxt('out.csv', np.c_[rows, value], fmt='%s')
上述命令的输出为

a 0.20196028482097483 0.5926321104002011 0.3249535106614311
b 0.061901131792619135 0.2124539226474711 0.7246679538084769
c 0.8459228604109359 0.1808180141813832 0.6723417117192844
我需要的输出是

a 0.20 0.59 0.32
b 0.06 0.21 0.72
c 0.85 0.18 0.67

numpy数组只能有1个数据类型。因为第一列是字符串,所以整个数组将转换为字符串。因此,您不能使用
%.2f
,而可以使用
%.4s
如下:

np.savetxt('out.csv', np.c_[rows, value], fmt='%s %.4s %.4s %.4s')

numpy数组只能有1个数据类型。因为第一列是字符串,所以整个数组将转换为字符串。因此,您不能使用
%.2f
,而可以使用
%.4s
如下:

np.savetxt('out.csv', np.c_[rows, value], fmt='%s %.4s %.4s %.4s')

查看您试图保存的内容:

In [457]: arr = np.c_[rows, value]
In [458]: arr
Out[458]: 
array([['a', '0.5798052037530684', '0.340056048668929',
        '0.9826015148933265'],
       ['b', '0.686642341561269', '0.22840250256173122',
        '0.874930037338561'],
       ['c', '0.38991473280876576', '0.1744123512308029',
        '0.7399608481535285']], dtype='<U32')
相反,每个浮点值需要单独的字段:

In [462]: arr = np.array(list(zip(rows, *value)), 'U3,f,f,f')
In [463]: arr
Out[463]: 
array([('a', 0.5798052 , 0.68664235, 0.38991472),
       ('b', 0.34005606, 0.2284025 , 0.17441235),
       ('c', 0.9826015 , 0.87493   , 0.73996085)],
      dtype=[('f0', '<U3'), ('f1', '<f4'), ('f2', '<f4'), ('f3', '<f4')])
oops-这已经转换了
数组-我应该使用:

arr = np.array(list(zip(rows, *value.T)), 'U3,f,f,f')
另一个选项是创建对象数据类型数组:

In [466]: M = np.zeros((3,4),object)
In [467]: M[:,0] = rows
In [468]: M[:,1:] = value
In [469]: M
Out[469]: 
array([['a', 0.5798052037530684, 0.340056048668929, 0.9826015148933265],
       ['b', 0.686642341561269, 0.22840250256173122, 0.874930037338561],
       ['c', 0.38991473280876576, 0.1744123512308029, 0.7399608481535285]],
      dtype=object)
In [470]: np.savetxt('test.txt', M, fmt='%s %.2f %.2f %.2f')
In [471]: cat test.txt
a 0.58 0.34 0.98
b 0.69 0.23 0.87
c 0.39 0.17 0.74

查看您试图保存的内容:

In [457]: arr = np.c_[rows, value]
In [458]: arr
Out[458]: 
array([['a', '0.5798052037530684', '0.340056048668929',
        '0.9826015148933265'],
       ['b', '0.686642341561269', '0.22840250256173122',
        '0.874930037338561'],
       ['c', '0.38991473280876576', '0.1744123512308029',
        '0.7399608481535285']], dtype='<U32')
相反,每个浮点值需要单独的字段:

In [462]: arr = np.array(list(zip(rows, *value)), 'U3,f,f,f')
In [463]: arr
Out[463]: 
array([('a', 0.5798052 , 0.68664235, 0.38991472),
       ('b', 0.34005606, 0.2284025 , 0.17441235),
       ('c', 0.9826015 , 0.87493   , 0.73996085)],
      dtype=[('f0', '<U3'), ('f1', '<f4'), ('f2', '<f4'), ('f3', '<f4')])
oops-这已经转换了
数组-我应该使用:

arr = np.array(list(zip(rows, *value.T)), 'U3,f,f,f')
另一个选项是创建对象数据类型数组:

In [466]: M = np.zeros((3,4),object)
In [467]: M[:,0] = rows
In [468]: M[:,1:] = value
In [469]: M
Out[469]: 
array([['a', 0.5798052037530684, 0.340056048668929, 0.9826015148933265],
       ['b', 0.686642341561269, 0.22840250256173122, 0.874930037338561],
       ['c', 0.38991473280876576, 0.1744123512308029, 0.7399608481535285]],
      dtype=object)
In [470]: np.savetxt('test.txt', M, fmt='%s %.2f %.2f %.2f')
In [471]: cat test.txt
a 0.58 0.34 0.98
b 0.69 0.23 0.87
c 0.39 0.17 0.74