简体   繁体   English

如何使用 Numpy 将矩阵中的值替换为颜色 eg[0,255,0]

[英]How to use Numpy to replace values in a matrix with a color e.g.[0,255,0]

I have a matrix which only contains 0 or 1. I also have a list of colors eg the below我有一个只包含 0 或 1 的矩阵。我还有一个 colors 的列表,例如下面

class_colors = [[0,0,0], [0,255,0]]
m = np.random.random_integers(0,1,(5,5))

eg the m looks like:例如m看起来像:

0,1,0,1
0,1,0,1
0,1,0,1

How can I replace the 1-values in m with the class_colors[1] and 0-values in m with class_colors[0] , so the m will look something like:我如何用class_colors[1]替换m中的 1 值,用class_colors[0]替换m中的 0 值,这样m看起来像:

[0,0,0], [0,255,0],[0,0,0], [0,255,0]
[0,0,0], [0,255,0],[0,0,0], [0,255,0]
[0,0,0], [0,255,0],[0,0,0], [0,255,0]

I used to be able to do so with np.argmax() and np.take() but it requires the m looks like [class_num,w,h] and then I can do argmax with axis=0 .我曾经能够使用np.argmax()np.take()这样做,但它需要m看起来像[class_num,w,h]然后我可以用axis=0做 argmax 。

I know I could do it with for loop, but is there any better and faster approach to do this?我知道我可以用 for 循环来完成,但是有没有更好更快的方法来做到这一点?

Not surprisingly the intuitive way works (using advanced indexing):毫不奇怪,直观的方式有效(使用高级索引):

class_colors = np.array([[0,0,0], [0,255,0]])
m = np.array([0,1,0,1,0,1,0,1,0,1,0,1]).reshape((3,4))
class_colors[m]

Is it what you expect:是不是如你所愿:

>>> c[m]
array([[[  0,   0,   0],
        [  0, 255,   0],
        [  0,   0,   0],
        [  0, 255,   0]],

       [[  0,   0,   0],
        [  0, 255,   0],
        [  0,   0,   0],
        [  0, 255,   0]]])

>>> c  # c = np.array(class_colors)
array([[  0,   0,   0],
       [  0, 255,   0]])

>>> m  # m = np.random.randint(0, 2, (2, 4))
array([[0, 1, 0, 1],
       [0, 1, 0, 1]])

By extension:通过扩展:

class_colors = [[0,0,0], [0,255,0], [255,0,0], [0,0,255]]
c = np.array(class_colors)
m = np.random.randint(0, 4, (2, 4))
# m <- array([[2, 1, 1, 3], [2, 3, 1, 0]])
>>> c[m]
array([[[255,   0,   0],
        [  0, 255,   0],
        [  0, 255,   0],
        [  0,   0, 255]],

       [[255,   0,   0],
        [  0,   0, 255],
        [  0, 255,   0],
        [  0,   0,   0]]])

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 通过向量的差异对矩阵创建进行向量化(例如,对于 numpy) - Vectorization of matrix creation by difference of vectors (e.g. for numpy) numpy如何处理具有不确定值(例如0.6499(6))的数据文件? - How does numpy handle data files with uncertainty values, e.g., 0.6499(6)? 如何为ARM交叉编译python包(例如Numpy) - how to cross compile python packages (e.g. Numpy) for ARM 列表到 Numpy 数组:如何在不添加额外值(例如 Null 或 0)的情况下将不同大小元素的列表转换为 numpy 数组? - List to Numpy Array: How to convert a list of different size elements to a numpy array without adding extra values (e.g., Null or 0)? 如何为 wxPython(例如 wxMSW)使用特定于平台的“端口”? - How to use platform specific "ports" for wxPython (e.g. wxMSW)? Numpy:如何快速替换矩阵中的相等值? - Numpy: How to quickly replace equal values in an matrix? 如何将 numpy 布尔值数组转换为 python 布尔值以进行序列化(例如用于 mongodb)? - How do I convert an array of numpy booleans to python booleans for serialization (e.g. for mongodb)? 如何将浮点数numpy数组保存到具有较少存储空间的二进制文件中(例如:不使用科学计数法)? - How to save a float numpy array to a binary file with less storage (e.g.: not in scientific notation)? 如何使用NumPy / SciPy计算运动/运行/滚动任意函数(例如峰度和偏度) - How to calculate moving / running / rolling arbitrary function (e.g. kurtosis & skewness) using NumPy / SciPy numpy 的就地操作(例如`+=`)如何工作? - How do numpy's in-place operations (e.g. `+=`) work?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM