Beispiel #1
0
def iqr_model_test(svm_model, matrix_kernel_test, clipids_test, sv_labels,
                   target_class=1):
    """
    Apply an SVM model on test data

    @param svm_model: Loaded SVM model to run with.

    @param matrix_kernel_test: n-by-m kernel matrix where n (row) is
        |SVs| & m (col) is |test data|
    @type matrix_kernel_test: numpy.core.multiarray.ndarray

    @param clipids_test: list of clipids ordered

    @param target_class: positive class id. Default = 1.

    @return: dictionary with 'probs','clipids'
    @rtype: dictionary with 'probs' (np.array), 'clipids' (int list)

    """
    log = logging.getLogger("SMQTK.iqr_model_test")

    weights = svmtools.get_SV_weights_nonlinear_svm(svm_model,
                                                    target_class=target_class)
    # case when the label of positive data was 2nd in SVM model symbol list
    # since platt scaling was parameterized for negative data, swap probs
    idx_target = svmtools.get_column_idx_for_class(svm_model, target_class)
    if idx_target == 1:
        weights *= -1.0

    # compute margins
    margins = weights[0] * matrix_kernel_test[0]
    for i in range(1, matrix_kernel_test.shape[0]):
        margins += weights[i] * matrix_kernel_test[i]
    if matrix_kernel_test.ndim == 1:   # case where there was single test data
        margins = np.array([margins])  # make a single number margin into an np array

    # compute probs, using platt scaling
    log.debug("Computing probabilities via Platt Scaling")
    rho = svm_model.rho[0]
    probA = svm_model.probA[0]
    probB = svm_model.probB[0]
    probs = 1.0 / (1.0 + np.exp((margins - rho) * probA + probB))
    del margins

    # # case when the label of positive data was 2nd in SVM model symbol list
    # # since platt scaling was parameterized for negative data, swap probs
    # idx_target = svmtools.get_column_idx_for_class(svm_model, target_class)
    # if idx_target == 1:
    #     probs = 1.0 - probs

    output = dict()
    output['probs'] = probs
    output['clipids'] = clipids_test
    return output
Beispiel #2
0
def iqr_model_test(filepath_model,
                   matrix_kernel_test,
                   clipids_test,
                   target_class=1):
    """
    Apply an SVM model on test data

    @param filepath_model: a full path to load the learned SVM model
    @param matrix_kernel_test: n-by-m kernel maxtrix where n (row) is |SVs| & m (col) is |test data|
    @type matrix_kernel_test: 2D numpy.array
    @param clipids_test: list of clipids ordered
    @param target_class: positive class id. Default = 1.
    @return: dictionary with 'probs','clipids'
    @rtype: dictionary with 'probs' (np.array), 'clipids' (int list)
    """

    model = svmutil.svm_load_model(filepath_model)
    weights = svmtools.get_SV_weights_nonlinear_svm(model,
                                                    target_class=target_class)

    # compute margins
    margins = weights[0] * matrix_kernel_test[0]
    for i in range(1, matrix_kernel_test.shape[0]):
        margins += weights[i] * matrix_kernel_test[i]
    if matrix_kernel_test.ndim == 1:  # case where there was single test data
        margins = np.array([margins
                            ])  # make a single number margin into an np array

    # compute probs, using platt scaling
    rho = model.rho[0]
    probA = model.probA[0]
    probB = model.probB[0]
    probs = 1.0 / (1.0 + np.exp((margins - rho) * probA + probB))
    del margins

    # case when the label of positive data was 2nd in SVM model symbol list
    # since platt scaling was parameterized for negative data, swap probs
    idx_target = svmtools.get_column_idx_for_class(model, target_class)
    if idx_target == 1:
        probs = 1.0 - probs

    output = dict()
    output['probs'] = probs
    output['clipids'] = clipids_test
    return output
Beispiel #3
0
def iqr_model_test(svm_model, matrix_kernel_test, clipids_test, target_class=1):
    """
    Apply an SVM model on test data

    @param svm_model: Loaded SVM model to run with
    @param matrix_kernel_test: n-by-m kernel maxtrix where n (row) is |SVs| & m
        (col) is |test data|
    @type matrix_kernel_test: 2D numpy.array
    @param clipids_test: list of clipids ordered
    @param target_class: positive class id. Default = 1.

    @return: dictionary with 'probs','clipids'
    @rtype: dictionary with 'probs' (np.array), 'clipids' (int list)

    """
    weights = svmtools.get_SV_weights_nonlinear_svm(svm_model,
                                                    target_class=target_class)

    # compute margins
    margins = weights[0] * matrix_kernel_test[0]
    for i in range(1, matrix_kernel_test.shape[0]):
        margins += weights[i] * matrix_kernel_test[i]
    if matrix_kernel_test.ndim == 1:   # case where there was single test data
        margins = np.array([margins])  # make a single number margin into an np array

    # compute probs, using platt scaling
    rho = svm_model.rho[0]
    probA = svm_model.probA[0]
    probB = svm_model.probB[0]
    probs = 1.0 / (1.0 + np.exp((margins - rho) * probA + probB))
    del margins

    # case when the label of positive data was 2nd in SVM model symbol list
    # since platt scaling was parameterized for negative data, swap probs
    idx_target = svmtools.get_column_idx_for_class(svm_model, target_class)
    if idx_target == 1:
        probs   = 1.0 - probs
    elif idx_target > 1:
        raise RuntimeError("Unexpected column index for target class.")

    output = dict()
    output['probs'] = probs
    output['clipids'] = clipids_test
    return output