Question: 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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!