v-crn Code Log

主に備忘録

ValueError: Integers to negative integer powers are not allowed.[Python][Numpy]

エラー内容

以下のように、負の数を指数とした累乗の計算でエラーが出た。

for num in np.arange(-5, 5):
  print(10**num)

""" ValueError: Integers to negative integer powers are not allowed.
"""

「負の整数による整数のべき乗は許されない」とのこと。

解決策

Python標準のrangeメソッドを使う。

for num in range(-5, 5):
  print(10**num)

numpyでは負の整数による整数のべき乗はできない。

逆に言えば、整数でなければnumpyでも期待通りの動作をする:

for num in np.arange(-5, 5):
  print(10.**num)

numpyは整数型の癖の強さに定評がある。

Python 3.x - python エラー|teratail