Join Our 5-Week ML/AI Engineer Interview Bootcamp 🚀 led by ML Tech Leads at FAANGs
Implement a difference transformation for a time series, which converts raw values into changes between consecutive time steps. The first-order difference is defined as:
Implement the function
Rules:
order times (e.g., order=2 means difference the series, then difference the result)..diff() or np.diff). Use array slicing.Output:
| Argument | Type |
|---|---|
| order | int |
| series | np.ndarray |
| Return Name | Type |
|---|---|
| value | np.ndarray |
Return NumPy array.
No pandas/.diff or np.diff; use array slicing.
Each order reduces length by 1.
Use current = series.astype(float) to ensure you can perform subtraction on float arrays.
Use array slicing for efficiency: current[1:] - current[:-1] computes differences for the whole array at once.
Loop order times, updating current with the sliced difference result each time.