【问题标题】:Python: How to read line by line in a numpy array?Python:如何在 numpy 数组中逐行读取?
【发布时间】:2016-06-05 08:11:13
【问题描述】:

我想知道我们可以在数组中逐行读取。例如:

array([[ 0.28,  0.22,  0.23,  0.27],
       [ 0.12,  0.29,  0.34,  0.21],
       [ 0.44,  0.56,  0.51,  0.65]])

以数组形式读取第一行以执行一些操作,然后继续第二行数组:

array([0.28,0.22,0.23,0.27])

产生上述数组的原因是这两行代码:

from numpy import genfromtxt
single=genfromtxt('single.csv',delimiter=',')

single.csv

0.28,  0.22,  0.23,  0.27
0.12,  0.29,  0.34,  0.21
0.44,  0.56,  0.51,  0.65

使用readlines() 似乎生成列表而不是数组。就我而言,我使用的是 csv 文件。我试图逐行使用值行,而不是一起使用它们以避免内存错误。谁能帮帮我?

with open('single.csv') as single:
    single=single.readlines()

【问题讨论】:

  • 你的csv 里面有array([0.28,0.22,0.23,0.27]) 吗?这不是csv 格式。
  • readlines 产生一个字符串列表。解析每一行以获取数字

标签: python arrays numpy


【解决方案1】:

你可以使用np.fromstring

import numpy as np
with open('single.csv') as f:
    lines=f.readlines()
    for line in lines:
        myarray = np.fromstring(line, dtype=float, sep=',')
        print(myarray)

http://docs.scipy.org/doc/numpy-1.10.1/reference/generated/numpy.fromstring.htmlHow to read csv into record array in numpy?

【讨论】:

  • 对不起,我不熟悉 np.fromstring。它返回了这个错误:NameError: name 'double' is not defined
【解决方案2】:

您似乎没有在 Python 中读取文件的经验。让我在 Ipython 迭代会话中详细介绍一个示例

创建多行文本来模拟您的文件

In [23]: txt="""0.28,  0.22,  0.23,  0.27
0.12,  0.29,  0.34,  0.21
0.44,  0.56,  0.51,  0.65"""

将其分成几行来模拟readlines的结果

In [24]: txt=txt.splitlines(True)

In [25]: txt
Out[25]: 
['0.28,  0.22,  0.23,  0.27\n',
 '0.12,  0.29,  0.34,  0.21\n',
 '0.44,  0.56,  0.51,  0.65']

我可以使用genfromtxt 将其转换为数组(您可以像这样将结果传递给readlinesgenfromtxt

In [26]: np.genfromtxt(txt, delimiter=',')
Out[26]: 
array([[ 0.28,  0.22,  0.23,  0.27],
       [ 0.12,  0.29,  0.34,  0.21],
       [ 0.44,  0.56,  0.51,  0.65]])

我可以遍历这些行,去掉 \n 并在 ',' 上拆分

In [27]: for line in txt:
    print line.strip().split(',')
   ....:     
['0.28', '  0.22', '  0.23', '  0.27']
['0.12', '  0.29', '  0.34', '  0.21']
['0.44', '  0.56', '  0.51', '  0.65']

我可以通过列表理解将每个字符串转换为浮点数:

In [28]: for line in txt:                                  
    print [float(x) for x in line.strip().split(',')]
   ....:     
[0.28, 0.22, 0.23, 0.27]
[0.12, 0.29, 0.34, 0.21]
[0.44, 0.56, 0.51, 0.65]

或者通过将迭代放在另一个列表推导中,我可以获得一个数字列表列表:

In [29]: data=[[float(x) for x in line.strip().split(',')] for line in  txt]

In [30]: data
Out[30]: [[0.28, 0.22, 0.23, 0.27], [0.12, 0.29, 0.34, 0.21], [0.44, 0.56, 0.51, 0.65]]

我可以把它变成一个数组

In [31]: np.array(data)
Out[31]: 
array([[ 0.28,  0.22,  0.23,  0.27],
       [ 0.12,  0.29,  0.34,  0.21],
       [ 0.44,  0.56,  0.51,  0.65]])

genfromtxt 本质上是在经历这个序列——读取行,拆分它们,将字符串转换为值,最后从列表中创建一个数组。

有捷径,但我认为你会从详细完成这些步骤中受益。它既是关于基本 Python 字符串和列表操作的练习,也是关于数组的练习。

【讨论】:

  • 谢谢 :) 很有帮助。
【解决方案3】:
for list in array:
    print(list)
    for item in list:
        print(item)

给出输出:

[0.28, 0.22, 0.23, 0.27]
0.28
0.22
0.23
0.27
[0.12, 0.29, 0.34, 0.21]
0.12
0.29
0.34
0.21
[0.44, 0.56, 0.51, 0.65]
0.44
0.56
0.51
0.65

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-02-04
    • 1970-01-01
    • 1970-01-01
    • 2014-01-15
    • 1970-01-01
    • 1970-01-01
    • 2015-02-05
    • 2019-04-16
    相关资源
    最近更新 更多