Python bottleneck.argpartsort() Examples

The following are 19 code examples of bottleneck.argpartsort(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module bottleneck , or try the search function .
Example #1
Source File: evaluation.py    From content_wmf with MIT License 6 votes vote down vote up
def recall_at_multiple_ks_batch(train_data, heldout_data, Et, Eb, user_idx,
                                topks, vad_data):
    batch_users = user_idx.stop - user_idx.start

    X_pred = rec_eval._make_prediction(train_data, Et, Eb, user_idx,
                                       batch_users, vad_data=vad_data)
    recalls = np.empty((len(topks), batch_users))
    for i, k in enumerate(topks):
        idx = bn.argpartsort(-X_pred, k, axis=1)
        X_pred_binary = np.zeros_like(X_pred, dtype=bool)
        X_pred_binary[np.arange(batch_users)[:, np.newaxis], idx[:, :k]] = True

        X_true_binary = (heldout_data[user_idx] > 0).toarray()
        tmp = (np.logical_and(X_true_binary,
                              X_pred_binary).sum(axis=1)).astype(np.float32)
        recalls[i] = tmp / X_true_binary.sum(axis=1)
    return recalls 
Example #2
Source File: rec_eval.py    From content_wmf with MIT License 6 votes vote down vote up
def precision_at_k_batch(train_data, heldout_data, Et, Eb, user_idx,
                         k=20, normalize=True, vad_data=None):
    batch_users = user_idx.stop - user_idx.start

    X_pred = _make_prediction(train_data, Et, Eb, user_idx,
                              batch_users, vad_data=vad_data)
    idx = bn.argpartsort(-X_pred, k, axis=1)
    X_pred_binary = np.zeros_like(X_pred, dtype=bool)
    X_pred_binary[np.tile(np.arange(batch_users), (k, 1)).T, idx[:, :k]] = True

    X_true_binary = (heldout_data[user_idx] > 0).toarray()
    tmp = (np.logical_and(X_true_binary, X_pred_binary).sum(axis=1)).astype(
        np.float32)

    if normalize:
        precision = tmp / np.minimum(k, X_true_binary.sum(axis=1))
    else:
        precision = tmp / k
    return precision 
Example #3
Source File: rec_eval.py    From content_wmf with MIT License 6 votes vote down vote up
def recall_at_k_batch(train_data, heldout_data, Et, Eb, user_idx,
                      k=20, vad_data=None):
    batch_users = user_idx.stop - user_idx.start

    X_pred = _make_prediction(train_data, Et, Eb, user_idx,
                              batch_users, vad_data=vad_data)
    idx = bn.argpartsort(-X_pred, k, axis=1)
    X_pred_binary = np.zeros_like(X_pred, dtype=bool)
    X_pred_binary[np.tile(np.arange(batch_users), (k, 1)).T, idx[:, :k]] = True

    X_true_binary = (heldout_data[user_idx] > 0).toarray()
    tmp = (np.logical_and(X_true_binary, X_pred_binary).sum(axis=1)).astype(
        np.float32)

    recall = tmp / X_true_binary.sum(axis=1)
    return recall 
Example #4
Source File: Util_basic.py    From DualLearning with MIT License 5 votes vote down vote up
def batch_gensample_model(in_queue, out_queue, f_init, f_next, k, gpulock, n_resample, minlen, maxlen, bleulowerbound = 0.25):
    while True:
        req = in_queue.get()
        if req is None:
            break
        x, x_mask, tosample_seqs_x, tosample_seqs_y, fixed_seqs_x, fixed_seqs_y = preprocess_data(req[1], req[2], minlen, maxlen)
        XX, YY = fixed_seqs_x, fixed_seqs_y
        assert len(XX) == len(YY), 'len(XX) NE len(YY)'
        WW = [1.] * len(XX)
        resample_counter = 0
        if x is not None:
            samples, scores = beam_search_sample(f_init, f_next, x, x_mask, gpulock, k, maxlen)
            resample_size = len(tosample_seqs_x)
            for idx in xrange(resample_size):
                bleu_scores = []
                str_tt = [str(y) for y in tosample_seqs_y]
                for ii in xrange(len(samples[idx])):
                    str_ss = [str(x) for x in samples[idx][ii]]
                    bleu_scores.append( nt.bleu([str_tt], str_ss, [0.25] * 4) )
                sorted_id = bottleneck.argpartsort(-numpy.array(bleu_scores, dtype = 'float32'), n = n_resample)[:n_resample]

                start_idx = 0

                if bleu_scores[sorted_id[0]] < 0.999:
                    XX.append(tosample_seqs_x[idx])
                    YY.append(tosample_seqs_y[idx])
                    WW.append(1.)
                    start_idx = 1

                for ii in xrange(start_idx, n_resample):
                    if bleu_scores[sorted_id[ii]] < bleulowerbound:
                        break
                    XX.append(tosample_seqs_x[idx])
                    YY.append(samples[idx][sorted_id[ii]])
                    WW.append(bleu_scores[sorted_id[ii]])
                    resample_counter += 1

        out_queue.put((req[0], XX, YY, WW, resample_counter))
    out_queue.put(None)
    print 'Process %d Finish One' % os.getpid()
    return 
Example #5
Source File: numpy_compat.py    From hed-dlg-truncated with GNU General Public License v3.0 5 votes vote down vote up
def argpartition(a, kth, axis=-1):
            return bottleneck.argpartsort(a, kth, axis) 
Example #6
Source File: matutils.py    From xlinkBook with MIT License 5 votes vote down vote up
def argsort(x, topn=None):
        """Return indices of the `topn` greatest elements in numpy array `x`, in order."""
        if topn is None:
            topn = x.size
        if topn <= 0:
            return []
        if topn >= x.size:
            return numpy.argsort(x)[::-1]
        biggest = bottleneck.argpartsort(x, x.size - topn)[-topn:]
        # the indices in `biggest` are not sorted by magnitude => sort & return
        return biggest.take(numpy.argsort(x.take(biggest))[::-1]) 
Example #7
Source File: matutils.py    From topical_word_embeddings with MIT License 5 votes vote down vote up
def argsort(x, topn=None):
        """Return indices of the `topn` greatest elements in numpy array `x`, in order."""
        if topn is None:
            topn = x.size
        if topn <= 0:
            return []
        if topn >= x.size:
            return numpy.argsort(x)[::-1]
        biggest = bottleneck.argpartsort(x, x.size - topn)[-topn:]
        # the indices in `biggest` are not sorted by magnitude => sort & return
        return biggest.take(numpy.argsort(x.take(biggest))[::-1]) 
Example #8
Source File: matutils.py    From topical_word_embeddings with MIT License 5 votes vote down vote up
def argsort(x, topn=None):
        """Return indices of the `topn` greatest elements in numpy array `x`, in order."""
        if topn is None:
            topn = x.size
        if topn <= 0:
            return []
        if topn >= x.size:
            return numpy.argsort(x)[::-1]
        biggest = bottleneck.argpartsort(x, x.size - topn)[-topn:]
        # the indices in `biggest` are not sorted by magnitude => sort & return
        return biggest.take(numpy.argsort(x.take(biggest))[::-1]) 
Example #9
Source File: matutils.py    From topical_word_embeddings with MIT License 5 votes vote down vote up
def argsort(x, topn=None):
        """Return indices of the `topn` greatest elements in numpy array `x`, in order."""
        if topn is None:
            topn = x.size
        if topn <= 0:
            return []
        if topn >= x.size:
            return numpy.argsort(x)[::-1]
        biggest = bottleneck.argpartsort(x, x.size - topn)[-topn:]
        # the indices in `biggest` are not sorted by magnitude => sort & return
        return biggest.take(numpy.argsort(x.take(biggest))[::-1]) 
Example #10
Source File: matutils.py    From pynlpini with GNU General Public License v2.0 5 votes vote down vote up
def argsort(x, topn=None):
        """Return indices of the `topn` greatest elements in numpy array `x`, in order."""
        if topn is None:
            topn = x.size
        if topn <= 0:
            return []
        if topn >= x.size:
            return numpy.argsort(x)[::-1]
        biggest = bottleneck.argpartsort(x, x.size - topn)[-topn:]
        # the indices in `biggest` are not sorted by magnitude => sort & return
        return biggest.take(numpy.argsort(x.take(biggest))[::-1]) 
Example #11
Source File: matutils.py    From category2vec with GNU Lesser General Public License v3.0 5 votes vote down vote up
def argsort(x, topn=None):
        """Return indices of the `topn` greatest elements in numpy array `x`, in order."""
        if topn is None:
            topn = x.size
        if topn <= 0:
            return []
        if topn >= x.size:
            return numpy.argsort(x)[::-1]
        biggest = bottleneck.argpartsort(x, x.size - topn)[-topn:]
        # the indices in `biggest` are not sorted by magnitude => sort & return
        return biggest.take(numpy.argsort(x.take(biggest))[::-1]) 
Example #12
Source File: numpy_compat.py    From NMT-Coverage with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def argpartition(a, kth, axis=-1):
            return bottleneck.argpartsort(a, kth, axis) 
Example #13
Source File: numpy_compat.py    From NMT-Coverage with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def argpartition(a, kth, axis=-1):
            return bottleneck.argpartsort(a, kth, axis) 
Example #14
Source File: matutils.py    From topical_word_embeddings with MIT License 5 votes vote down vote up
def argsort(x, topn=None):
        """Return indices of the `topn` greatest elements in numpy array `x`, in order."""
        if topn is None:
            topn = x.size
        if topn <= 0:
            return []
        if topn >= x.size:
            return numpy.argsort(x)[::-1]
        biggest = bottleneck.argpartsort(x, x.size - topn)[-topn:]
        # the indices in `biggest` are not sorted by magnitude => sort & return
        return biggest.take(numpy.argsort(x.take(biggest))[::-1]) 
Example #15
Source File: numpy_compat.py    From LV_groundhog with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def argpartition(a, kth, axis=-1):
            return bottleneck.argpartsort(a, kth, axis) 
Example #16
Source File: numpy_compat.py    From hred-qs with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def argpartition(a, kth, axis=-1):
            return bottleneck.argpartsort(a, kth, axis) 
Example #17
Source File: numpy_compat.py    From hred-latent-piecewise with GNU General Public License v3.0 5 votes vote down vote up
def argpartition(a, kth, axis=-1):
            return bottleneck.argpartsort(a, kth, axis) 
Example #18
Source File: matutils.py    From topical_word_embeddings with MIT License 5 votes vote down vote up
def argsort(x, topn=None):
        """Return indices of the `topn` greatest elements in numpy array `x`, in order."""
        if topn is None:
            topn = x.size
        if topn <= 0:
            return []
        if topn >= x.size:
            return numpy.argsort(x)[::-1]
        biggest = bottleneck.argpartsort(x, x.size - topn)[-topn:]
        # the indices in `biggest` are not sorted by magnitude => sort & return
        return biggest.take(numpy.argsort(x.take(biggest))[::-1]) 
Example #19
Source File: matutils.py    From topical_word_embeddings with MIT License 5 votes vote down vote up
def argsort(x, topn=None):
        """Return indices of the `topn` greatest elements in numpy array `x`, in order."""
        if topn is None:
            topn = x.size
        if topn <= 0:
            return []
        if topn >= x.size:
            return numpy.argsort(x)[::-1]
        biggest = bottleneck.argpartsort(x, x.size - topn)[-topn:]
        # the indices in `biggest` are not sorted by magnitude => sort & return
        return biggest.take(numpy.argsort(x.take(biggest))[::-1])