ValueError:传递的项目数错误 1,位置暗示 2
Posted
技术标签:
【中文标题】ValueError:传递的项目数错误 1,位置暗示 2【英文标题】:ValueError: Wrong number of items passed 1, placement implies 2 【发布时间】:2021-04-08 08:51:31 【问题描述】:我通过多处理计算出了错误 ROC 曲线和运行大型数据集。此过程运行正常,但 30 分钟后,我突然收到错误消息。
多处理代码已更改:
for i in range(0, len(values) - 1):
for j in range(i + 1, len(values)):
# print(values[i], " and ", values[j])
positive = []
positive.append(values[i])
positive.append(values[j])
print(positive)
proc = multiprocessing.Process(args=(positive,))
print(proc)
positives.append(proc)
proc.start()
for proc in positives:
proc.join()
positives = pd.DataFrame(positives, columns=["file_x", "file_y"])
追溯
['1013_StephenHawking_67_m.jpg', '1023_StephenHawking_45_m.jpg']
<Process(Process-236167, initial)>
Traceback (most recent call last):
File "/home/khawar/.local/lib/python3.6/site-packages/pandas/core/internals/managers.py", line 1671, in create_block_manager_from_blocks
make_block(values=blocks[0], placement=slice(0, len(axes[0])))
File "/home/khawar/.local/lib/python3.6/site-packages/pandas/core/internals/blocks.py", line 2744, in make_block
return klass(values, ndim=ndim, placement=placement)
File "/home/khawar/.local/lib/python3.6/site-packages/pandas/core/internals/blocks.py", line 2400, in __init__
super().__init__(values, ndim=ndim, placement=placement)
File "/home/khawar/.local/lib/python3.6/site-packages/pandas/core/internals/blocks.py", line 131, in __init__
f"Wrong number of items passed len(self.values), "
ValueError: Wrong number of items passed 1, placement implies 2
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/khawar/deepface/tests/Ensemble-Face-Recognition.py", line 53, in <module>
positives = pd.DataFrame(positives, columns=["file_x", "file_y"])
File "/home/khawar/.local/lib/python3.6/site-packages/pandas/core/frame.py", line 523, in __init__
mgr = init_ndarray(data, index, columns, dtype=dtype, copy=copy)
File "/home/khawar/.local/lib/python3.6/site-packages/pandas/core/internals/construction.py", line 234, in init_ndarray
return create_block_manager_from_blocks(block_values, [columns, index])
File "/home/khawar/.local/lib/python3.6/site-packages/pandas/core/internals/managers.py", line 1681, in create_block_manager_from_blocks
raise construction_error(tot_items, blocks[0].shape[1:], axes, e)
ValueError: Shape of passed values is (236167, 1), indices imply (236167, 2)
【问题讨论】:
【参考方案1】:这可能是索引混乱的情况。
尝试使用.loc[df.index.drop_duplicates()]
删除任何潜在的重复索引:
positives = pd.DataFrame(positives, columns=["file_x", "file_y"]).loc[positives.index.drop_duplicates()]
【讨论】:
这里的df是什么? @KhawarIslam 错字,抱歉 :) positives = pd.DataFrame(positives, columns=["file_x", "file_y"]).loc[positives.index.drop_duplicates()] AttributeError: 'builtin_function_or_method' 对象没有属性 'drop_duplicates '【参考方案2】:已编辑
我想再次强调您的问题是关于ValueError: Wrong number of items passed 1, placement implies 2
。
ValueError
发生是因为您的变量 positives
存储的是 multiporcessing.Process
的对象而不是 (str, str),这是有问题的。
由于您只是配对字符串,我会改为执行以下操作:
from itertools import combinations
data = idendities # using your json data
# list comprehensive, create pairwise combination
positives = [ list(combinations(v, 2) for key, value in data.items() ]
# unset nested lists
positives = [ pair for l in positives for pair in l ]
# len(positives) = 3300
positives = pd.DataFrame(positives, columns=["file_x", "file_y"])
输出
# positives before doing pd.DataFrame
[('1020_StephenHawking_65_m.jpg', '1004_StephenHawking_43_m.jpg'),
('1020_StephenHawking_65_m.jpg', '1017_StephenHawking_65_m.jpg'),
('1020_StephenHawking_65_m.jpg', '1014_StephenHawking_67_m.jpg'),
('1020_StephenHawking_65_m.jpg', '1006_StephenHawking_36_m.jpg'),
('1020_StephenHawking_65_m.jpg', '1000_StephenHawking_1_m.jpg'),
('1020_StephenHawking_65_m.jpg', '1018_StephenHawking_66_m.jpg'),
... ... ]
# positives after pd.DataFrame
file_x file_y
0 1020_StephenHawking_65_m.jpg 1004_StephenHawking_43_m.jpg
1 1020_StephenHawking_65_m.jpg 1017_StephenHawking_65_m.jpg
2 1020_StephenHawking_65_m.jpg 1014_StephenHawking_67_m.jpg
3 1020_StephenHawking_65_m.jpg 1006_StephenHawking_36_m.jpg
4 1020_StephenHawking_65_m.jpg 1000_StephenHawking_1_m.jpg
而且,内存问题肯定不是来自这部分代码。
【讨论】:
我必须应用多处理,否则系统会冻结。 认为使用with Pool() as p: result = p.map(func, values)
可能会更好;它更清楚。 @KhawarIslam
可以发表你的答案吗?这样我才能更好地理解它。我觉得有一些图像问题。你有什么意见?
我不确定,因为您在这里没有太多上下文;比如你的目标是什么?问题显然来自最后一行(这是您的程序错误消息显示的内容)
根据您上面的答案。此行不起作用 proc = multiprocessing.Process(args=(positive,))。我对吗?实际上我有 20000 张图像,我正在生成对来相互比较并找到彼此之间的相似性以上是关于ValueError:传递的项目数错误 1,位置暗示 2的主要内容,如果未能解决你的问题,请参考以下文章
Pandas DataFrame ValueError:传递的项目数错误 2,位置暗示 1
ValueError:错误的项目数通过 47,位置意味着 1 和 KeyError:'size'
ValueError:错误的项目数通过 500,位置暗示 1,Python 和 Pandas