2值矩阵,将 -1,1矩阵变为0,1矩阵,或者将0,1矩阵变为其他2值矩阵

当遇到矩阵为0-1类型,或者是-1,1类型的时候,可以选择利用numpy中的where 进行修改矩阵内部值

若矩阵为:

import numpy as np
#生成一个3*3的矩阵数字1-9的矩阵
a=np.arange(9).reshape(3,3)
print(a)
#将a 中,小于1的数变为-1,其余的,变为1
b=np.where(a<1,-1,1)
print(b)

结果为 :

a
[[0 1 2]
 [3 4 5]
 [6 7 8]]
 b
[[-1  1  1]
 [ 1  1  1]
 [ 1  1  1]]
原文地址:https://www.cnblogs.com/DeepRunning/p/9205885.html