python-3.x - 我正在尝试生成数据集,但出现值错误“ValueError:'a' 不能为空,除非没有采样”
问题描述
我正在使用包含超过 3000 张图像的数据集进行迁移学习。这是代码的一部分:
import glob
import numpy as np
import os
import shutil
np.random.seed(42)
files = glob.glob('train/*')
cat_files = [fn for fn in files if 'cat' in fn]
dog_files = [fn for fn in files if 'dog' in fn]
len(cat_files), len(dog_files)
cat_train = np.random.choice(cat_files, size=1500, replace=False)
解决方案
如果没有来自 的一些示例数据,很难确切地知道发生了什么train/
,但是谷歌搜索您的错误消息会从源代码中找到np.random.choice()
:
def choice(self, a, size=None, replace=True, p=None):
...
Raises
-------
ValueError
If a is an int and less than zero, if a or p are not 1-dimensional,
if a is an array-like of size 0, if p is not a vector of
probabilities, if a and p have different lengths, or if
replace=False and the sample size is greater than the population
size
...
# Format and Verify input
a = np.array(a, copy=False)
if a.ndim == 0:
try:
# __index__ must return an integer by python rules.
pop_size = operator.index(a.item())
except TypeError:
raise ValueError("'a' must be 1-dimensional or an integer")
if pop_size <= 0 and np.prod(size) != 0:
raise ValueError("'a' must be greater than 0 unless no samples are taken")
看起来可能cat_files
是空的,或者不是正确的类型。在将其传递给 之前,您是否验证了其内容np.random.choice()
?
推荐阅读
- validation - 输入验证 MIPS
- git - Flutter 的 pubspec.yaml 版本更改自动提交标记
- r - MLE 错误:“vmmin”中的初始值不是有限的
- php - InvalidArgumentException 未找到视图 [admin\layouts\adminpanel]
- python - 在 Deepnote 上安装 Conda
- typescript - 如何将函数的参数转换为具有从函数参数名称派生的命名属性的接口?
- python - 在 Django DetailView 中引用 ForeignKey
- ios - 如何检查 Swift URL 是否是目录
- kofax - 如何使用 KTA SDK 创建图像导入作业?
- javascript - 如何在 html img 标签中使用二进制图像?