← 返回 netflix 的题目列表Calculate Kullback-Leibler Divergence Between Two Distributions
类型:online_judge
netflix
Given two probability distributions, write a Python function to compute their Kullback-Leibler divergence. Requirements:
The input is two numpy arrays p and q, representing two probability distributions, with the same length.
Ensure the distributions are normalized, i.e., the sum of the elements in the arrays is 1.
Assume all elements are positive.
Return the Kullback-Leibler divergence value, rounded to five decimal places.
Input Format:
p: Numpy array of the first probability distribution.
q: Numpy array of the second probability distribution.
Output Format:
Return the value of the divergence.
Example:
Input:
p = np.array([0.1, 0.4, 0.5])
q = np.array([0.2, 0.3, 0.5])
Output:
0.07877
Example
Input
0.1 0.4 0.5
0.2 0.3 0.5