如何在python中将二进制矩阵转换为双极性矩阵 [英] How to convert a binary matrix to a bipolar one in python

查看:150
本文介绍了如何在python中将二进制矩阵转换为双极性矩阵的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Keras 中有一个函数可以为标签数组生成二进制矩阵:

There is a function in Keras to generate a binary matrix for an array of labels:

# Consider an array of 5 labels out of a set of 3 classes {0, 1, 2}:
> labels
array([0, 2, 1, 2, 0])
# `to_categorical` converts this into a matrix with as many
# columns as there are classes. The number of rows
# stays the same.
> to_categorical(labels)
array([[ 1.,  0.,  0.],
       [ 0.,  0.,  1.],
       [ 0.,  1.,  0.],
       [ 0.,  0.,  1.],
       [ 1.,  0.,  0.]], dtype=float32)

我需要上述功能,但要用-1而不是零.我没有找到任何选择或其他功能来做到这一点.有没有简单的方法可以做到这一点?

I need the above functionality, but having -1 instead of zeros. I didn't find any option or other functions to do it. Is there any easy way to do that?

推荐答案

只需重新缩放数据:

2*to_categorical(labels)-1

这篇关于如何在python中将二进制矩阵转换为双极性矩阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆