【问题标题】:'NoneType' object is not subscriptable -- using `np.fromregex` [closed]“NoneType”对象不可下标——使用“np.fromregex”[关闭]
【发布时间】:2016-06-01 20:30:57
【问题描述】:

这个问题有很多答案(请参阅Python Math - TypeError: 'NoneType' object is not subscriptable)。我的问题不同,因为我正确地期望 np.genfromtxt(...) 返回一个数组(即 np.genfromtxt(...) 不是一个就地函数)。

我正在尝试将以下内容解析并存储到一维数组中:

http://pastie.org/10860707#2-3

为此,我尝试了:

pattern = re.compile(b'[\s,]')
theta = np.fromregex("RegLogTheta", regexp = pattern, dtype = float)

这是回溯(应该如何格式化?):

Traceback (most recent call last):
File "/Users/ahanagrawal/Documents/Java/MachL/Chap3/ExamScoreVisual2.py", line    36, in <module>
theta = np.fromregex("RegLogTheta", regexp = pattern, dtype = float)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/numpy/lib/npyio.py", line 1240, in fromregex
newdtype = np.dtype(dtype[dtype.names[0]])
TypeError: 'NoneType' object is not subscriptable

如果您想运行它,请从http://pastie.org/10860707#2-3 下载文本文件并运行上面的代码。

【问题讨论】:

  • 请发布完整的回溯。
  • 您甚至没有在发布的代码中使用np.genfromtxt
  • 请不要在外部网站上发布数据。将其复制到您的问题中。
  • 寻求调试帮助的问题(“为什么这段代码不起作用?”)必须包括所需的行为、特定问题或错误在问题本身中重现它所需的最短代码。没有明确的问题陈述的问题对其他读者没有用处。请参阅:How to create a Minimal, Complete, and Verifiable Example
  • 实际上,使用此设置从外部网站下载数据比从问题中复制粘贴更容易。

标签: python numpy text-parsing


【解决方案1】:

文件多行,逗号分隔,每行3个数字,最后一个只有2个

In [182]: fname='../Downloads/pastie-10860707.txt'

In [183]: np.fromregex(fname,regexp=pattern,dtype=float)
... 
np.fromregex(fname,regexp=pattern,dtype=float)

/usr/lib/python3/dist-packages/numpy/lib/npyio.py in fromregex(file, regexp, dtype)
   1240             # Create the new array as a single data-type and then
   1241             #   re-interpret as a single-field structured array.
-> 1242             newdtype = np.dtype(dtype[dtype.names[0]])
   1243             output = np.array(seq, dtype=newdtype)
   1244             output.dtype = dtype

TypeError: 'NoneType' object is not subscriptable

通过简单的“br”读取加载,文件如下所示:

In [184]: txt
Out[184]: b'2.75386225e+00,1.80508078e+00,2.95729122e+00,\n-4.21413726e+00,  -3.38139076e+00,  -4.22751379e+00,\n ...      4.23010784e-01,  -1.14839331e+00,  -9.56098910e-01,\n        -1.15019836e+00,   1.13845303e-06'

最后一行缺少的数字会给genfromtxt 带来问题。

您选择的模式是错误的。它看起来像一个分隔符模式。但是fromregex docs 中的模式会产生组:

regexp = r"(\\d+)\\s+(...)"

fromregex

seq = regexp.findall(file.read())  # read whole file and group it
output = np.array(seq, dtype=dtype)  # make array from seq

如果你想使用fromregex,你需要想出一个模式来生成一个可以直接转换成数组的元组列表。

=================

虽然再次查看错误消息,但我发现当前的问题在于dtypedtype=float 不是此函数的有效 dtype 规范。它需要一个复合 dtype(结构化)。

此操作会产生错误,其中float 是您的dtype 参数:

In [189]: np.dtype(float).names[0]
 ...
TypeError: 'NoneType' object is not subscriptable

但它正在尝试这样做,因为模式已经产生了

In [194]: pattern.findall(txt)
Out[194]: 
[b',',
 b',',
 b',',
 b'\n',
 b',',
 b' ',
 b' ',
 ....]

不是它预期的元组列表。

===================

我可以加载文件

In [213]: np.genfromtxt(txt.splitlines(),delimiter=',',usecols=[0,1])
Out[213]: 
array([[  2.75386225e+00,   1.80508078e+00],
       [ -4.21413726e+00,  -3.38139076e+00],
       [  7.46991792e-01,  -1.08010066e+00],
        ...
       [  4.23010784e-01,  -1.14839331e+00],
       [ -1.15019836e+00,   1.13845303e-06]])

我正在使用usecols 暂时解决最后一行只有 2 个数字的问题。

如果我删除 \n 并将其拆分为逗号,我可以直接使用 np.array 解析生成的文本字段。

In [231]: txt1=txt.replace(b'\n',b'').split(b',')

In [232]: np.array(txt1,float)
Out[232]: 
array([  2.75386225e+00,   1.80508078e+00,   2.95729122e+00,
        -4.21413726e+00,  -3.38139076e+00,  -4.22751379e+00,
          ...
         4.23010784e-01,  -1.14839331e+00,  -9.56098910e-01,
        -1.15019836e+00,   1.13845303e-06])

此模式包括十进制和科学记数法:

In [266]: pattern=re.compile(br"(\d+\.\d+e[\+\-]\d+)")

In [267]: np.fromregex(fname,regexp=pattern,dtype=np.dtype([('f0',float)]))['f0']
Out[267]: 
array([  2.75386225e+00,   1.80508078e+00,   2.95729122e+00,
         4.21413726e+00,   3.38139076e+00,   4.22751379e+00,
      ...
         4.23010784e-01,   1.14839331e+00,   9.56098910e-01,
         1.15019836e+00,   1.13845303e-06])

现在我正在创建一个结构化数组并提取该字段。可能有办法解决这个问题。但fromregex 似乎更倾向于使用结构化数据类型。

【讨论】:

  • 这应该是一个答案吗?
  • 评论太长了!另外,您在我完成编辑之前就提出了您的问题。我还没有完成编辑。
  • @hpaulj 我认为以下应该可行:theta = np.fromregex("RegLogTheta", regexp = r"\s+,(\d+)\s+,", dtype = [(np.float128)])。唯一的问题是 [(np.float128)] 不是公认的 dtype,我不明白为什么。
  • @hpaulj 哦,我评论后你也编辑了。让我先检查一下那个编辑。
  • 我概括了你的模式来处理科学记数法。
猜你喜欢
  • 1970-01-01
  • 2013-09-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-11-10
  • 2015-01-23
相关资源
最近更新 更多