sample_comments_diverse

sample_comments_diverse(
    data=None,
    *columns,
    n=SAMPLE_SIZE,
    min_chars=MIN_CHARS,
    max_chars=MAX_CHARS,
    exclude=None,
    lambda_=DIVERSITY_LAMBDA,
    method='mmr',
    score='entropy',
    stopwords=None,
    max_candidates=MAX_CANDIDATES,
    seed=None,
)

Pick a diverse, information-rich sample of comments.

A smarter alternative to sample_comments(): instead of sampling uniformly, it scores each comment and selects a set that is both informative and varied, so a quote slide isn’t five ways of saying the same thing. Scoring is lexical: comments are turned into TF-IDF vectors, an information score is computed (Shannon entropy of the word distribution, or TF-IDF mass), and a randomised maximal-marginal-relevance pass trades off informativeness against similarity to already-picked comments. The same sentence is never picked twice.

The trade-off argument is lambda_ because lambda is a Python keyword; R names it lambda. Python draws different random numbers from R, so the same seed picks a different (equally varied) set.

Parameters

Name Type Description Default
data pandas.DataFrame A data frame. If omitted, the session default is used. None
*columns str or selector One or more comment columns. ()
n As in sample_comments(). SAMPLE_SIZE
min_chars As in sample_comments(). SAMPLE_SIZE
max_chars As in sample_comments(). SAMPLE_SIZE
exclude As in sample_comments(). SAMPLE_SIZE
lambda_ float Diversity/informativeness trade-off in [0, 1] for method="mmr": higher favours informative comments, lower favours dissimilar ones. Default 0.6. DIVERSITY_LAMBDA
method str “mmr” (default) for the diversity-aware greedy selection, or “entropy” to weight a random sample by each comment’s information score. 'mmr'
score str How to measure information: “entropy” (default, Shannon bits) or “tfidf” (summed TF-IDF weight). 'entropy'
stopwords list of str or bool None (default) uses the English Snowball list, as R’s stopwords package supplies it; a list supplies your own; False disables removal. None
max_candidates int Largest number of comments compared with one another (default 500). A bigger corpus is narrowed to a random shortlist of this size first, because the comparison grows with the square of the number kept. MAX_CANDIDATES
seed int Seed for reproducible sampling. None

Returns

Name Type Description
pandas.DataFrame source, comment, length and the info score, ordered by selection.

See Also

sample_comments, plot_quotes_tree

Examples

>>> sample_comments_diverse(podracing_survey, "nps_com", "show_com", n=5, seed=1)