我正在尝试预处理python中的数据以用于深度学习keras函数。
我categorical crossentropy
在模型拟合中用作损失函数。它需要分类变量作为目标。
我的目标数据样本:
y_train = y_train.astype('category')
y_train.head()
truth
0 0
1 0
2 1
3 0
4 0
当我尝试将数据框列转换为分类列时:
num_classes=2
y_train = keras.utils.to_categorical(y_train, num_classes)
产生错误:IndexError: index 1 is out of bounds for axis 1 with size 1
。
如何正确转换数据?
顺便说一下,如果我有3800个观测值的样本,每个样本都具有2300个数字(float32)特征,那么哪种keras模型更适合于二进制分类?这些功能主要描述图形对象。
不幸的是,我没有设法重现您的错误。运行:
a=pd.DataFrame(np.concatenate([np.zeros(3),np.ones(3)]) ).astype('int').astype('category')
from keras.utils import to_categorical
to_categorical(a, 2)
我得到一个输出:
array([[1., 0.],
[1., 0.],
[1., 0.],
[0., 1.],
[0., 1.],
[0., 1.]], dtype=float32)
也许是版本问题!
好消息是您不必使用categorical_crossentropy
二进制分类问题。您可以使用binary_crossentropy
损失并以y_train作为目标来进行建模。
关于您最后一个关于哪种keras模型更适合于二进制分类的请求,Keras预训练模型正在引用图像。您似乎拥有表格数据,尽管您将无法使用经过预先训练的模型,但是您将必须自己运行自定义模型。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句