python - 函数的参数为 False 和 True
问题描述
我想了解以下代码。在面向对象的部分中,论点之一是one_hot= one_hot
. 但是如果我extract_labels
用one_hot=one_hot
它调用函数是行不通的。我必须使用True
和False
声明。有人可以指导我为什么会这样吗?以及这些True
和False
指示。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)
解决方案
函数 extract_labels 有 2 个参数:
- 标签 - 必需,没有默认值
- one_hot - 可选,默认值为 False
要调用像 one_hot=one_hot 这样的 extract_labells,您需要在之前指定 one_hot 变量
推荐阅读
- php - 如何在 Magento 中获取电子邮件已读回执?
- java - PHP文件中的JSON返回垃圾
- python - 如何在烧瓶中的 app.run() 之前运行代码?
- sql - 查询返回“单行子查询返回多行”
- mysql - 选择需要这么长时间才能完成
- r - 在 R 中实现梯度下降时的问题
- javascript - 需要什么才能让 javascript 从具有身份验证控制的 Google Cloud Storage 中提取文件?
- linux - 使用 ffmpeg 将音频与图像和视频连接起来
- linux - && 的目的是什么:; 在 bash 脚本中
- scala - Spark Structured Streaming - 带有窗口时间事件的自定义聚合