Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I resolve "Use scipy.optimize.linear_sum_assignment instead"

Tags:

python

I am using python script for people detection. I have the following line in my script:

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

When I run my script I have got the following lines:

/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)

Please, advice me how to solve it.

like image 605
Sergiy Tupikin Avatar asked Aug 06 '19 05:08

Sergiy Tupikin


2 Answers

You need to replace the sklearn.utils.linear_assignment_.linear_assignment function by the scipy.optimize.linear_sum_assignment function.

The difference is in the return format: linear_assignment() is returning a numpy array and linear_sum_assignment() a tuple of numpy arrays. You obtain the same output by converting the output of linear_sum_assignment() in array and transpose it.

Your script should look like this:

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)
like image 131
Arthur knn Avatar answered Jan 04 '23 23:01

Arthur knn


Replace the linear_assignment for 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

like image 22
Samuel Luz Gomes Avatar answered Jan 05 '23 00:01

Samuel Luz Gomes