Function' object has no attribute 'shape

I have got error “function’ object has no attribute 'shape” in Naive Forecast
it is related to print(f"validation series has shape: {series_valid.shape}\n")

Does anyone know how can I define shape?
Here is what I have set


You’ve defined naive_forecast to be a function instead of a numpy array. On a related note, don’t rename train_val_split to a custom function.

Notice that you have used naive_forecast twice. One is the defined function as noted by @balaji.ambresh. The second is inside that function, as one of the slices from series. This is allowable Python. However, due to scope rules, only the function name is visible at the level of your print statements…the scope of the second use is limited to within the defined function it occurs in.

There is something else odd going on in this code and error stack. Notice that you are returning time_valid, series_valid, and naive_forecast from the function. 1) that return statement is outside the ###YOUR CODE HERE### tags. In almost 6 years of supporting several DeepLearning specializations on Coursera, I have never seen graded code where the function definition is inside the tags but the return statement is outside. Even if it were executing properly within the notebook it will likely fail the grader. 2) I don’t see where the defined function is being called. Which means those values are never being assigned to anything. Which means the first print statement should have failed, too, because series_valid is being used before it is defined. It looks like the code has been run previously, then restructured, then partially run again and using a stale, cached, object for series_valid.

Whenever you restructure code in a Jupyter notebook, it is a good idea to restart and run all cells from the top. Otherwise you are debugging against an incoherent state…the names and scope in the notebook namespace aren’t identical to what you are seeing in the modified code.

Whoa, actually I only got it partially right. You actually define naive_forecast 3 times…once as a function, once as a slice, and once as an integer. series_valid is similarly defined once as a slice, and once as an integer. time_series is also defined twice.

I’m pretty confused about the objective of this code :man_shrugging:

Thank you @balaji.ambresh and @ai_curious for the review

I encountered “Naive_forecast is not defined” although I have defined it
Kindly provide a pointer to overcome this issue


A review of some basic Python concepts may be in order…excerpts below from

4.2. Naming and binding

4.2.1. Binding of names

Names refer to objects. Names are introduced by name binding operations.

Each assignment or import statement occurs within a block defined by a class or function definition or at the module level (the top-level code block).

If a name is bound in a block, it is a local variable of that block

my emphasis added at the end there. You define a function, then define some local variables, or names in official Pythonese, within that block. You have a return statement that includes the local variables. So far so good. But the function isn’t ever called, at least in the fragment in the screen capture, so those variables aren’t assigned to objects that occur in the same scope as the print statements, which in this specific case are at what this documentation calls the module level. If this doesn’t make sense, you might consider a basic Python tutorial before trying to complete these courses.

Please follow these steps to refresh the workspace and fetch the starter code. Use it as a reference to fix undefined references in your notebook.

Thank you for the pointer @balaji.ambresh and @ai_curious
After updating step 4 variable with “time_step” I am able to generate naive_forecast chart


1 Like