首页 > 解决方案 > 函数的参数为​​ False 和 True

问题描述

我想了解以下代码。在面向对象的部分中,论点之一是one_hot= one_hot. 但是如果我extract_labelsone_hot=one_hot它调用函数是行不通的。我必须使用TrueFalse声明。有人可以指导我为什么会这样吗?以及这些TrueFalse指示。one=True当我在调用extract_label函数时使用它打印一个热向量时,我打印了结果。

#Functions
def dense_to_one_hot(labels_dense, num_classes=2):

    num_labels = labels_dense.shape[0]
    index_offset = numpy.arange(num_labels) * num_classes
    labels_one_hot = numpy.zeros((num_labels, num_classes))
    labels_one_hot.flat[index_offset + labels_dense.ravel()] = 1
    return labels_one_hot
def extract_labels(labels, one_hot=False):
    if one_hot :
        return dense_to_one_hot(labels)
    return labels

#Object oriented part
def read_data_sets(all_width_dir,width_dir, one_hot=False):
    class DataSets(object):
    pass
    data_sets = DataSets()
    train_labels = extract_labels(TRAIN_LABELS, one_hot=one_hot)
    test_labels = extract_labels(TEST_LABELS, one_hot=one_hot)

return data_sets

train_label = np.load("./Input/1.5/train_label.npy")
labels = extract_labels(train_label, one_hot=one_hot)
print(labels)

标签: pythonnumpy

解决方案


函数 extract_labels 有 2 个参数:

  • 标签 - 必需,没有默认值
  • one_hot - 可选,默认值为 False

要调用像 one_hot=one_hot 这样的 extract_labells,您需要在之前指定 one_hot 变量


推荐阅读