Skip to content
DeepForgeby svx
All problems
EasyML Fundamentalsml-108

Laplace Smoothing

Problem

Apply Laplace (add-alpha) smoothing to a list of counts: (c_i + alpha) / (sum(c) + alpha * K).

Empty counts returns []; if the smoothed denominator is 0, return all zeros.

Hint

Add alpha to every count and alpha times K to the denominator.

Test cases

case 1
in: [[0,0,0],1]
expected: [0.3333333333333333,0.3333333333333333,0.3333333333333333]
case 2
in: [[10,0],1]
expected: [0.9166666666666666,0.08333333333333333]
case 3
in: [[3,1,1],0.5]
expected: [0.5384615384615384,0.23076923076923078,0.23076923076923078]
case 4
in: [[],1]
expected: []
Starter codePython
def laplace_smoothing(counts, alpha):
    # Your code here
    pass

4 test cases · run your code against them in the editor