PY-95
Sample Identified Records
Task
Write sample_identified_records(record_ids, fields, rng, sample_size, replace).
record_ids is a non-empty one-dimensional sequence of unique, non-empty
strings. Position i names one complete source record. fields is a non-empty
dictionary of aligned one-dimensional arrays. Every field array must have the
same length as record_ids; a field value at position i belongs to the ID at
position i. rng is one already-created NumPy Generator.
sample_size is a non-negative integer and replace is a boolean:
- With
replace=False, selectsample_sizedistinct source positions. A request larger than the population is invalid. - With
replace=True, every draw can select any source position again. The sample may therefore be larger than the population, and repeated positions are allowed.
Use one call to rng.choice over the integer positions 0 through
len(record_ids) - 1. Keep the returned positions in their generated order.
Apply that one index array to record_ids and to every field. Do not perform a
separate random selection for any field.
Return a new dictionary with exactly these keys:
indexes, ids, fields, coverage
indexes is a new one-dimensional integer array. ids and every array in
fields are new arrays in sampled order. coverage is a new dictionary with
exactly these keys:
source_size, sample_size, unique_selected, duplicate_count,
unselected_count, replacement
duplicate_count is sample_size - unique_selected, and
unselected_count is source_size - unique_selected. These are observations
of this sample, not claims about any larger population. An empty sample is
valid and has zero selected and duplicate counts.
Do not change record_ids, fields, their arrays, rng, sample_size, or
replace. A valid call advances the supplied generator once. Invalid input
must raise ValueError before selecting positions.
Example
If the generated indexes are [2, 0], the sampled pairs are
("M-103", 21.5) and ("M-101", 18.5). The exact indexes depend on the
generator state, but the alignment rule does not.
Your implementation
You may import NumPy as np. Do not print, create another generator, sort the
sample, or ask for input.