Answered step by step
Verified Expert Solution
Link Copied!

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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Beginning C# 5.0 Databases

Authors: Vidya Vrat Agarwal

2nd Edition

1430242604, 978-1430242604

Students also viewed these Databases questions

Question

2. How were various roles filled?

Answered: 1 week ago