numpy数组保存到csv

时间:2018-01-30 12:27:49

标签: python-3.x csv numpy

我试图将numpy数组保存到csv文件但是有问题,

我使用两种不同的解决方案,但它们不起作用

我的numpy数组看起来像,

In[39]: arr[0]
Out[39]: 
array([ array([[ 30,  29, 198, ..., 149, 149, 149],
   [ 29,  29, 197, ..., 149, 149, 149],
   [ 29,  29, 197, ..., 149, 149, 149],
   ..., 
   [ 63,  63,  96, ..., 105, 104, 104],
   [ 63,  63,  96, ..., 106, 105, 105],
   [ 77,  77, 217, ..., 217, 217, 217]], dtype=uint8),
   list([0, 0, 0, 0, 0, 0, 0, 0, 0])], dtype=object)

它的形状是(1200,2)numpy数组,我想把它保存到csv文件,

  

使用np.savetxt函数

In[40]: np.savetxt("numpy_array.csv", arr, delimiter=',')
Traceback (most recent call last):
  File "D:\Program files\Anaconda3\lib\site-packages\numpy\lib\npyio.py", line 1254, in savetxt
    fh.write(asbytes(format % tuple(row) + newline))
TypeError: only length-1 arrays can be converted to Python scalars
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
  File "D:\Program files\Anaconda3\lib\site-packages\IPython\core\interactiveshell.py", line 2862, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-41-673bcc1d77a6>", line 1, in <module>
    np.savetxt("numpy_array.csv", arr, delimiter=',')
  File "D:\Program files\Anaconda3\lib\site-packages\numpy\lib\npyio.py", line 1258, in savetxt
    % (str(X.dtype), format))
TypeError: Mismatch between array dtype ('object') and format specifier ('%.18e,%.18e')
  

使用pandas

In[42]: df = pd.DataFrame(arr)
In[43]: df[:5]
Out[43]: 
                                                   0  \
0  [[30, 29, 198, 198, 197, 197, 197, 197, 197, 1...   
1  [[29, 29, 197, 197, 196, 196, 197, 197, 197, 1...   
2  [[29, 29, 196, 196, 196, 196, 196, 196, 196, 1...   
3  [[29, 29, 196, 196, 196, 196, 196, 196, 196, 1...   
4  [[29, 29, 196, 196, 196, 196, 196, 196, 197, 1...   
                             1  
0  [0, 0, 0, 0, 0, 0, 0, 0, 0]  
1  [1, 0, 0, 0, 0, 0, 0, 0, 0]  
2  [1, 0, 0, 0, 0, 0, 0, 0, 0]  
3  [1, 0, 0, 0, 0, 0, 0, 0, 0]  
4  [1, 0, 0, 0, 0, 0, 0, 0, 0]  
In[44]: df.to_csv("h.csv", index=False)
In[45]: a = pd.read_csv("h.csv", header=None,names =['input', 'output'])
In[46]: a[:5]
Out[46]: 
                                               input  \
0                                                  0   
1  [[ 30  29 198 ..., 149 149 149]\r\n [ 29  29 1...   
2  [[ 29  29 197 ..., 149 149 149]\r\n [ 29  29 1...   
3  [[ 29  29 196 ..., 149 149 149]\r\n [ 29  29 1...   
4  [[ 29  29 196 ..., 149 149 149]\r\n [ 29  29 1...   
                        output  
0                            1  
1  [0, 0, 0, 0, 0, 0, 0, 0, 0]  
2  [1, 0, 0, 0, 0, 0, 0, 0, 0]  
3  [1, 0, 0, 0, 0, 0, 0, 0, 0]  
4  [1, 0, 0, 0, 0, 0, 0, 0, 0]  

当我打印&#34; df [:5]&#34;时,一切看起来都很棒,但是在我将它保存到csv然后从csv中读取它看起来很糟糕,数字之间没有逗号而且有&#39; \ r \ n&#39;列表之间。

我希望看到&#34; df [:5]&#34;读取csv文件后的输出,我该怎么办,有什么问题?

2 个答案:

答案 0 :(得分:0)

Nuumpy本身没有'另存为csv'功能。通常你通过另一个包(比如pandas或pickle)保存它。

你看到的'它看起来很糟糕'是熊猫的格式。添加arr = np.array(a) 你再次拥有numpy格式。

答案 1 :(得分:0)

你的数组是2d,(1200,2),对象为dtype。显然,第一列包含2d数组,第2列包含。

arr[0,0]是一个二维数组

array([[ 30,  29, 198, ..., 149, 149, 149],
   [ 29,  29, 197, ..., 149, 149, 149],
   [ 29,  29, 197, ..., 149, 149, 149],
   ..., 
   [ 63,  63,  96, ..., 105, 104, 104],
   [ 63,  63,  96, ..., 106, 105, 105],
   [ 77,  77, 217, ..., 217, 217, 217]], dtype=uint8)

您可以轻松地以csv格式书写。例如:

In [342]: arr = np.array([[ 30,  29, 198, 149, 149, 149],
     ...:    [ 29,  29, 197, 149, 149, 149],
     ...:    [ 29,  29, 197, 149, 149, 149],
     ...:    [ 63,  63,  96, 105, 104, 104],
     ...:    [ 63,  63,  96, 106, 105, 105],
     ...:    [ 77,  77, 217, 217, 217, 217]], dtype=np.uint8)
     ...:    
     ...:    
In [343]: np.savetxt('arr.txt', arr, delimiter=',', fmt='%4d')

生成一个类似于:

的文件
In [344]: cat arr.txt
  30,  29, 198, 149, 149, 149
  29,  29, 197, 149, 149, 149
  29,  29, 197, 149, 149, 149
  63,  63,  96, 105, 104, 104
  63,  63,  96, 106, 105, 105
  77,  77, 217, 217, 217, 217

有关savetxt的更多详情,请阅读fmt

但是完整数组与csv文件的简单2d布局不兼容。当然你可以写一些更复杂的东西,但你无法用csv读者加载np.genfromtxtnp.loadtxt。那些期望整齐的行和列布局具有明确定义的分隔符。

In [346]: data = np.genfromtxt('arr.txt',delimiter=',',dtype=None)
In [347]: data
Out[347]: 
array([[ 30,  29, 198, 149, 149, 149],
       [ 29,  29, 197, 149, 149, 149],
       [ 29,  29, 197, 149, 149, 149],
       [ 63,  63,  96, 105, 104, 104],
       [ 63,  63,  96, 106, 105, 105],
       [ 77,  77, 217, 217, 217, 217]])

pandas df显示两列,一列是数组,另一列是列。但是在a列中,0似乎包含2d数组的字符串表示,如换行符所示。你看过h.csv文件了吗?使用csv的部分原因是人们可以阅读它,其他程序(如excel)可以阅读它。

制作一个像你的大号阵列

In [349]: barr = np.empty((3,2), object)
In [350]: barr[:,0]=[arr,arr,arr]
In [351]: barr[:,1]=[[0,0,0] for _ in range(3)]
In [352]: barr
Out[352]: 
array([[array([[ 30,  29, 198, 149, 149, 149],
       [ 29,  29, 197, 149, 149, 149],
       [ 29,  29, 197, 149, 149, 149],
       [ 63,  63,  96, 105, 104, 104],
       [ 63,  63,  96, 106, 105, 105],
       [ 77,  77, 217, 217, 217, 217]], dtype=uint8),
        list([0, 0, 0])],
       [array([[ 30,  29, 198, 149, 149, 149],
   ...
       [ 77,  77, 217, 217, 217, 217]], dtype=uint8),
        list([0, 0, 0])]], dtype=object)

编写%s格式,这是唯一可以使用这样的对象的格式:

In [354]: np.savetxt('barr.txt',barr, delimiter=',',fmt='%s')
In [355]: cat barr.txt
[[ 30  29 198 149 149 149]
 [ 29  29 197 149 149 149]
 [ 29  29 197 149 149 149]
 [ 63  63  96 105 104 104]
 [ 63  63  96 106 105 105]
 [ 77  77 217 217 217 217]],[0, 0, 0]
[[ 30  29 198 149 149 149]
 [ 29  29 197 149 149 149]
 [ 29  29 197 149 149 149]
 [ 63  63  96 105 104 104]
 [ 63  63  96 106 105 105]
 [ 77  77 217 217 217 217]],[0, 0, 0]
[[ 30  29 198 149 149 149]
 [ 29  29 197 149 149 149]
 [ 29  29 197 149 149 149]
 [ 63  63  96 105 104 104]
 [ 63  63  96 106 105 105]
 [ 77  77 217 217 217 217]],[0, 0, 0]

这不是有效的csv文件。它是文本,但是[]和不同的行长度,标准的csv文件读取器都不能处理它。

像对待pandas一样保存该数组,我得到:

In [364]: cat pdbarr.txt
0,1
"[[ 30  29 198 149 149 149]
 [ 29  29 197 149 149 149]
 [ 29  29 197 149 149 149]
 [ 63  63  96 105 104 104]
 [ 63  63  96 106 105 105]
 [ 77  77 217 217 217 217]]","[0, 0, 0]"
"[[ 30  29 198 149 149 149]
 [ 29  29 197 149 149 149]
 [ 29  29 197 149 149 149]
 [ 63  63  96 105 104 104]
 [ 63  63  96 106 105 105]
 [ 77  77 217 217 217 217]]","[0, 0, 0]"
"[[ 30  29 198 149 149 149]
 [ 29  29 197 149 149 149]
 [ 29  29 197 149 149 149]
 [ 63  63  96 105 104 104]
 [ 63  63  96 106 105 105]
 [ 77  77 217 217 217 217]]","[0, 0, 0]"

注意所有引号 - 它将这些组件数组和列表写为字符串。同样,不是有效的csv