【问题标题】:numpy fromstring deprecated use frombuffer insteadnumpy fromstring 不推荐使用 frombuffer 代替
【发布时间】:2020-06-04 19:58:12
【问题描述】:

在 Python 代码中使用 numpy 1.18.1

` def printBoard(self): 当前 = self.player 其他 = self.player % 2 + 1

    currBin   = '{:049b}'.format(self.current_position)
    currRev = currBin[::-1]

    cArr = (np.fromstring(currRev,'u1') - ord('0'))*current

    other_position = self.current_position^self.mask
    othBin   = '{:049b}'.format(other_position)
    othRev = othBin[::-1]

    oArr = (np.fromstring(othRev,'u1') - ord('0'))*other

    tArr =  oArr+cArr

    brd = np.reshape(tArr,(7,7),order = 'F')
    for y in range(bitBoard.HEIGHT,-1,-1):
        for x in range(bitBoard.WIDTH):
            print(brd[y,x],end = ' ')
        print()
    print()
    `

行:

cArr = (np.fromstring(currRev,'u1') - ord('0'))*current

给出以下警告:

DeprecationWarning: The binary mode of fromstring is deprecated, as it behaves surprisingly on    unicode inputs. Use frombuffer instead
  cArr = (np.fromstring(currRev,'u1') - ord('0'))*current

将 'fromstring' 替换为 'frombuffer' 会出现以下错误:

cArr = (np.frombuffer(currRev,'u1') - ord('0'))*current

TypeError: a bytes-like object is required, not 'str'

尽管进行了一些谷歌搜索,但我找不到应该改用的东西。有人可以帮忙吗?

谢谢。

艾伦

【问题讨论】:

  • 从概念上解释这些行的作用。您的示例不是最小的,您发布的代码中有 90% 与问题无关。
  • 我知道我给的太多了,但是当我问一个问题时,我经常被要求提供整个代码!这次我给出了希望就足够的功能。这些行采用以二进制形式编码的 connect4 列中的一列,并将其转换为 0 和 1 或 0 和 2,具体取决于播放器。它将结果数组加在一起并输出该列中片段的位置

标签: python string numpy typeerror deprecation-warning


【解决方案1】:

代码的相关部分是生成currRev 的部分。由此我可以构建这个例子:

In [751]: astr = '{:049b}'.format(123)[::-1]                                                   
In [752]: astr                                                                                 
Out[752]: '1101111000000000000000000000000000000000000000000'

您的警告:

In [753]: np.fromstring(astr, 'u1')                                                            
/usr/local/bin/ipython3:1: DeprecationWarning: The binary mode of fromstring is deprecated, as it behaves surprisingly on unicode inputs. Use frombuffer instead
  #!/usr/bin/python3
Out[753]: 
array([49, 49, 48, 49, 49, 49, 49, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48,
       48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48,
       48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48],
      dtype=uint8)

frombuffer 想要一个字节串,所以让我们创建一个:

In [754]: astr.encode()                                                                        
Out[754]: b'1101111000000000000000000000000000000000000000000'
In [755]: np.frombuffer(astr.encode(),'u1')                                                    
Out[755]: 
array([49, 49, 48, 49, 49, 49, 49, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48,
       48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48,
       48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48],
      dtype=uint8)

以及该行的其余部分:

In [756]: _-ord('0')                                                                           
Out[756]: 
array([1, 1, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
       0, 0, 0, 0, 0], dtype=uint8)

获取相同数组的另一种方法:

In [758]: np.array(list(astr),'uint8')                                                         
Out[758]: 
array([1, 1, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
       0, 0, 0, 0, 0], dtype=uint8)

【讨论】:

  • 非常感谢。那行得通。我不太喜欢玩比特,所以我不确定该怎么做。
  • 在 py2 中,默认字符串是字节大小。在py3中,unicode是默认的,字节串用b'...'标记
  • 感谢您的解释
猜你喜欢
  • 1970-01-01
  • 2021-04-26
  • 2021-10-29
  • 2022-08-08
  • 1970-01-01
  • 2021-10-23
  • 2021-03-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多