def assignment_dlib(cost_mat):
    # Let's imagine you need to assign N people to N jobs.  Additionally, each
    # person will make your company a certain amount of money at each job, but each
    # person has different skills so they are better at some jobs and worse at
    # others.  You would like to find the best way to assign people to these jobs.
    # In particular, you would like to maximize the amount of money the group makes
    # as a whole.  This is an example of an assignment problem and is what is solved
    # by the dlib.max_cost_assignment() routine.

    # So in this example, let's imagine we have 3 people and 3 jobs. We represent
    # the amount of money each person will produce at each job with a cost matrix.
    # Each row corresponds to a person and each column corresponds to a job. So for
    # example, below we are saying that person 0 will make $1 at job 0, $2 at job 1,
    # and $6 at job 2.
    #cost = dlib.matrix([[1, 2, 6], [5, 3, 6], [4, 5, 0]])
    cost = dlib.matrix(cost_mat)

    # To find out the best assignment of people to jobs we just need to call this
    # function.
    assignment = dlib.max_cost_assignment(cost)

    # This prints optimal assignments:  [2, 0, 1]
    # which indicates that we should assign the person from the first row of the
    # cost matrix to job 2, the middle row person to job 0, and the bottom row
    # person to job 1.
    #print("Optimal assignments: {}".format(assignment))

    # This prints optimal cost:  16.0
    # which is correct since our optimal assignment is 6+5+5.
    #print("Optimal cost: {}".format(dlib.assignment_cost(cost, assignment)))

    return assignment
import dlib

# Let's imagine you need to assign N people to N jobs.  Additionally, each
# person will make your company a certain amount of money at each job, but each
# person has different skills so they are better at some jobs and worse at
# others.  You would like to find the best way to assign people to these jobs.
# In particular, you would like to maximize the amount of money the group makes
# as a whole.  This is an example of an assignment problem and is what is solved
# by the dlib.max_cost_assignment() routine.

# So in this example, let's imagine we have 3 people and 3 jobs. We represent
# the amount of money each person will produce at each job with a cost matrix.
# Each row corresponds to a person and each column corresponds to a job. So for
# example, below we are saying that person 0 will make $1 at job 0, $2 at job 1,
# and $6 at job 2.
cost = dlib.matrix([[1, 2, 6], [5, 3, 6], [4, 5, 0]])

# To find out the best assignment of people to jobs we just need to call this
# function.
assignment = dlib.max_cost_assignment(cost)

# This prints optimal assignments:  [2, 0, 1]
# which indicates that we should assign the person from the first row of the
# cost matrix to job 2, the middle row person to job 0, and the bottom row
# person to job 1.
print("Optimal assignments: {}".format(assignment))

# This prints optimal cost:  16.0
# which is correct since our optimal assignment is 6+5+5.
print("Optimal cost: {}".format(dlib.assignment_cost(cost, assignment)))
示例#3
0
# Let's imagine you need to assign N people to N jobs.  Additionally, each
# person will make your company a certain amount of money at each job, but each
# person has different skills so they are better at some jobs and worse at
# others.  You would like to find the best way to assign people to these jobs.
# In particular, you would like to maximize the amount of money the group makes
# as a whole.  This is an example of an assignment problem and is what is solved
# by the dlib.max_cost_assignment() routine.

# So in this example, let's imagine we have 3 people and 3 jobs. We represent
# the amount of money each person will produce at each job with a cost matrix.
# Each row corresponds to a person and each column corresponds to a job. So for
# example, below we are saying that person 0 will make $1 at job 0, $2 at job 1,
# and $6 at job 2.
cost = dlib.matrix([[1, 2, 6],
                    [5, 3, 6],
                    [4, 5, 0]])

# To find out the best assignment of people to jobs we just need to call this
# function.
assignment = dlib.max_cost_assignment(cost)

# This prints optimal assignments:  [2, 0, 1]
# which indicates that we should assign the person from the first row of the
# cost matrix to job 2, the middle row person to job 0, and the bottom row
# person to job 1.
print("Optimal assignments: {}".format(assignment))

# This prints optimal cost:  16.0
# which is correct since our optimal assignment is 6+5+5.
print("Optimal cost: {}".format(dlib.assignment_cost(cost, assignment)))
示例#4
0
    def match(input_matrix, allow_zero_scores=False):
        """
        Builds match list
        :param input_matrix: 2 dimensional array of scores
        :param allow_zero_scores: Should items with a score of 0 be considered to be matched?  Default is False
        :return:
        """
        out_dict = dict()
        out_dict["Matches"] = []
        out_dict["Details"] = {"Quality Scores": []}
        out_dict["Quality Score"] = 0
        out_dict["Precision"] = 0
        out_dict["Recall"] = 0
        out_dict["F1"] = 0
        warn_id_list = list(input_matrix.index)
        event_id_list = list(input_matrix.columns)
        score_matrix = np.array(input_matrix)

        if min(score_matrix.shape) == 0:
            pass
        else:
            if np.max(score_matrix) <= 0:
                pass
            else:
                score_matrix = score_matrix.copy(order="C")
                row_count, col_count = score_matrix.shape
                zero_matrix = np.zeros(score_matrix.shape)
                score_matrix = np.maximum(score_matrix, zero_matrix)

                k_max = max(score_matrix.shape[0], score_matrix.shape[1])
                score_matrix_square = np.zeros((k_max, k_max))
                score_matrix_square[:score_matrix.shape[0], :score_matrix.
                                    shape[1]] = score_matrix

                assign = dlib.max_cost_assignment(
                    dlib.matrix(score_matrix_square))
                assign = [(i, assign[i]) for i in range(k_max)]

                assign_scores = np.array(
                    [score_matrix_square[x[0], x[1]] for x in assign])
                assign = np.array(assign)

                #assign = assign[assign_scores > 0]

                if not allow_zero_scores:
                    assign_scores = np.array(
                        [score_matrix_square[x[0], x[1]] for x in assign])
                    assign = np.array(assign)
                    assign = assign[assign_scores > 0]
                assign = list([tuple(x) for x in assign])
                assign = [(int(x[0]), int(x[1])) for x in assign]
                assign = [(warn_id_list[a[0]], event_id_list[a[1]])
                          for a in assign]
                out_dict["Matches"] = assign
                scores_ = assign_scores[assign_scores > 0]
                out_dict["Quality Score"] = np.mean(scores_)
                out_dict["Details"] = {"Quality Scores": list(scores_)}
                prec = len(assign) / row_count
                rec = len(assign) / col_count
                out_dict["Precision"] = prec
                out_dict["Recall"] = rec
                out_dict["F1"] = Scorer.f1(prec, rec)

        return out_dict