NumPy ArraysIntro
dtypes and Converting Types
~9 mincode completion
Write to_probabilities(counts) that converts an array of integer counts into fractions of the total, as floats.
[1, 3] returns [0.25, 0.75]. Assume the counts sum to more than zero.
Examples
Counts of 1 and 3 become a quarter and three quarters
- Input
- to_probabilities([1, 3])
- Output
- [0.25, 0.75]
Equal counts split evenly
- Input
- to_probabilities([2, 2, 2, 2])
- Output
- [0.25, 0.25, 0.25, 0.25]
Hints
Hint 1
Work directly with the arguments counts and return the result rather than printing it.
Hint 2
Watch for this: integer division truncated to zero.
Requirements
counts: an integer array of counts, e.g. np.array([1, 3])Return a float array of the same shape, summing to 1.0.
Constraints
Allowed library: NumPy only
Time limit: 200 ms, Memory: 64 MB
Where this shows up
~9 min
••••••••••••••••
8 employers weight this skill
3 frontier labs, 2 quant funds, 2 big tech firms, 1 autonomy company. Top match scores 68.
Python
import numpy as np
def to_probabilities(counts):
"""
Convert counts into fractions of the total.
Args:
counts: an integer array of counts, e.g. np.array([1, 3])
Returns:
A float array of the same shape, summing to 1.0.
"""
# YOUR CODE HERE
pass