Пример #1
0
Файл: ris.py Проект: rjos/ris
def slow_ris3(dataset, targets, threshold, n_jobs=1):
    scores, radius = _scores_radius(dataset, targets, n_jobs)
    _norm_by_clas(scores, targets)
    # Remove from idx the ones that have bad scores and recompute radius

    idx = scores.argsort(None)[::-1]
    for i, v in enumerate(scores[idx]):
        if v < threshold:
            idx = idx[:i]
            break

    # Now recompute radius

    for id in idx:
        instance = dataset[id, :]
        target = targets[id]
        max_dist = inf
        for o_id in idx:
            o_instance = dataset[o_id, :]
            o_target = targets[o_id]
            if o_target != target:
                dist = euclidean(instance, o_instance)
                if dist < max_dist:
                    max_dist = dist
        radius[id] = max_dist

    return slow_relevants(idx, scores, radius, threshold, dataset, targets)
Пример #2
0
Файл: ris.py Проект: rjos/ris
def slow_relevants(idx, scores, radius, threshold, dataset, targets):
    selected_set = []
    selected_radius = []
    for id in idx:
        score = scores[id]
        if score < threshold:
            break
        instance = dataset[id, :]
        target = targets[id]

        for s_id in selected_set:

            another_target = targets[s_id]
            if another_target == target:
                dist = euclidean(instance, dataset[s_id, :])
                if dist < radius[s_id]:
                    break
        else:
            selected_set.append(id)
            selected_radius.append(radius[id])

    return selected_set, selected_radius
Пример #3
0
Файл: ris.py Проект: rjos/ris
def _scores_radius(dataset, targets, n_jobs=1):
    scores = np.zeros((targets.size, 1), dtype=np.float_)
    radius = np.zeros_like(scores)
    # Calculate score
    for i, (instance, target) in enumerate(zip(dataset, targets)):
        denom = 0
        num = 0
        radius[i] = np.inf
        for another_instance, another_target in zip(dataset, targets):
            dist = euclidean(instance, another_instance)
            val = exp(-dist)
            denom += val
            if target == another_target:
                num += val
            else:
                num -= val
                if dist < radius[i]:
                    radius[i] = dist
        num -= 1
        denom -= 1
        scores[i] = num / denom
    return scores, radius