Furkan Eraslan
Furkan Eraslan

Reputation: 431

No module named 'sklearn.utils.linear_assignment_'

I am trying to run a project from github , every object counter applications using sort algorithm. I can't run any of them because of a specific error, attaching errors screenshot. Can anyone help me about fixing this issue? enter image description here

Upvotes: 26

Views: 56357

Answers (5)

InputBlackBoxOutput
InputBlackBoxOutput

Reputation: 353

  1. Use pip to install lap and optionally scipy;
  2. Uncomment the import and use the following function:
def linear_assignment(cost_matrix):
  try:
    import lap
    _, x, y = lap.lapjv(cost_matrix, extend_cost=True)
    return np.array([[y[i], i] for i in x if i >= 0])
  except ImportError:
    from scipy.optimize import linear_sum_assignment
    x, y = linear_sum_assignment(cost_matrix)
    return np.array(list(zip(x, y)))

Upvotes: 2

Santle Camilus
Santle Camilus

Reputation: 985

pip install scikit-learn==0.22.2

Upvotes: 19

Mix
Mix

Reputation: 178

As yiakwy points out in a github comment the scipy.optimize.linear_sum_assignment is not the perfect replacement:

I am concerned that linear_sum_assignment is not equivalent to linear_assignment which later implements "maximum values" matching strategy not "complete matching" strategy, i.e. in tracking problem maybe an old landmark lost and a new detection coming in. We don't have to make a complete assignment, just match as more as possible.

I have found this out while trying to use it inside SORT-based yolo tracking code which that replacement broke (I was lucky that it did otherwise, I would get wrong results from the experiments without realising it...)

Instead, I suggest copying the module itself to the last version of sklearn and include as module in your code.

https://github.com/scikit-learn/scikit-learn/blob/0.22.X/sklearn/utils/linear_assignment_.py

For instance if you copy this file into an utils directory import with from utils.linear_assignment_ import linear_assignment

Upvotes: 14

enthusiastdev
enthusiastdev

Reputation: 995

The linear_assignment function is deprecated in 0.21 and will be removed from 0.23, but sklearn.utils.linear_assignment_ can be replaced by scipy.optimize.linear_sum_assignment.

You can use:

from scipy.optimize import linear_sum_assignment as linear_assignment

then you can run the file and don't need to change the code.

Upvotes: 77

enthusiastdev
enthusiastdev

Reputation: 995

You are getting this error because you haven't install scikit module yet. Install scikit-learn module from https://pypi.org/project/scikit-learn/

Upvotes: -4

Related Questions