如何解决“改用scipy.optimize.linear_sum_assignment”

时间:2019-08-06 05:53:03

标签: python

我正在使用python脚本进行人员检测。 我的脚本中包含以下行:

import time
import cv2 as cv
import glob
import argparse
import sys
import numpy as np
import os.path
from imutils.video import FPS
from collections import deque
from sklearn.utils.linear_assignment_ import linear_assignment

运行脚本时,我有以下几行:

/home/user/.local/lib/python3.6/site-packages/sklearn/utils/linear_assignment_.py:127:
     DeprecationWarning: The linear_assignment function is deprecated in 0.21 and will be removed from 0.23. Use scipy.optimize.linear_sum_assignment instead.
     DeprecationWarning)

请给我建议如何解决。

2 个答案:

答案 0 :(得分:1)

您需要将sklearn.utils.linear_assignment_.linear_assignment函数替换为scipy.optimize.linear_sum_assignment function

区别在于返回格式:linear_assignment()返回一个 numpy数组,而linear_sum_assignment()返回一个 numpy数组的元组。您可以通过将linear_sum_assignment()的输出转换为数组并进行转置来获得相同的输出。

您的脚本应如下所示:

import time
import cv2 as cv
import glob
import argparse
import sys
import numpy as np
import os.path
from imutils.video import FPS
from collections import deque
from scipy.optimize import linear_sum_assignment

#compute your cost matrix
indices = linear_sum_assignment(cost_matrix)  
indices = np.asarray(indices)
indices = np.transpose(indices)

答案 1 :(得分:0)

linear_assignment 替换为 linear_sum_assignment

# from sklearn.utils.linear_assignment_ import linear_assignment
from scipy.optimize import linear_sum_assignment
cost = np.array([[4, 1, 3], [2, 0, 5], [3, 2, 2]])
# result = linear_assignment(cost)
result = linear_sum_assignment(cost)
result = np.array(list(zip(*result)))

https://docs.scipy.org/doc/scipy/reference/generated/scipy.optimize.linear_sum_assignment.html