Skip to content
DeepForgeby svx
All problems
MediumTime Seriests-259

Fourier Features

Problem

Build Fourier features at time t: for harmonics h = 1..harmonics append sin(2*pi*h*t/period) and cos(2*pi*h*t/period).

Return an empty list when period is not positive or harmonics < 1. Fourier terms let linear models fit smooth seasonality.

Hint

Each harmonic contributes a sine-cosine pair, so the vector length is 2 * harmonics.

Test cases

case 1
in: [0,4,2]
expected: [0,1,0,1]
case 2
in: [1,4,1]
expected: [1,6.123233995736766e-17]
case 3
in: [1,4,2]
expected: [1,6.123233995736766e-17,1.2246467991473532e-16,-1]
case 4
in: [0,0,2]
expected: []
Starter codePython
import math
def fourier_features(t, period, harmonics):
    # Your code here
    pass

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