Answered step by step
Verified Expert Solution
Question
1 Approved Answer
write a function called lag() which accepts two arguments: x, a numpy array. num_places, the number of places by which to lag x. The result
write a function called lag() which accepts two arguments:
- x, a numpy array.
- num_places, the number of places by which to lag x.
The result of lag(x, num_places) should be a new array y which is a copy of x that has been "shifted over" by num_places. You may assume that the input x is a 1d numpy array of floats. The first num_places elements of y should be nan.
A bit more technically, y[i] = x[i - num_places] if i >= num_places, and y[i] = np.nan if i < num_places.
Here's an example:
x = np.array([1.0, 2.0, 3.0, 4.0, 5.0]) lag(x, 2)
# output array([nan, nan, 1., 2., 3.])
- For full credit, please do not use for loops.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started