私は次の方法を持っています:
def unify_np_array(A):
'''Set every nonzero element in a numpy array to 1'''
nonzero = A > 0
A[nonzero] /= A[nonzero]
return A
次のテストコードで呼び出します:
def test_small_np(self):
r = np.array([[3, 0], [0, 2]])
DataConverter.unify_np_array(r)
e = np.array([[1, 0], [0, 1]])
np.testing.assert_array_equal(r, e)
単体テストは成功しますが、次の警告が表示されます。
DeprecationWarning: Implicitly casting between incompatible kinds.
In a future numpy release, this will raise an error.
Use casting="unsafe" if this is intentional.
A[nonzero] /= A[nonzero]
ただし、キャストしている互換性のない種類はわかりません。numpy 配列のサブセクションをそれ自体で分割します。私が暗黙的に作成しているキャストを特定できる人はいますか?