Confused with Hidden State

Confused with Hidden State.

  1. Hidden State I’m lost with the instructions too.

Will I get this over time?

L.

Hello Kindly post the link to the mission you are referring to.

Also it would be better if you follow the Community Guidelines when asking a question

To bide by the guidelines, here’s the URL to the Hidden State:

https://app.dataquest.io/m/427/project%3A-learn-and-install-jupyter-notebook/6/hidden-state

Oh sorry, that content is locked for me.

Hi @friendl74,

The following are the instructions in that screen:

  1. Run the %history -p command to get an understanding of the current state of your program.
    • The output of %history -p should be pretty verbose since we ran quite a lot of code so far — you should also see that it’s not very easy to understand the state of the program at this point.
  2. Restart the state of your program and clear the output of every cell by clicking the Restart & Clear Output action from the Kernel menu.
  3. In a separate cell, run the %history -p command again to confirm that no code has been run (except for %history -p ).
  4. Run the code we wrote in the previous exercise again.
  5. Modify the first cell (the one where you defined only the function welcome() and the variables dq , jn , and py ):
    • The function should not print the string 'Welcome to ' + a_string + '!' anymore. Instead, the string 'Welcome to ' + a_string + '!' is saved to a variable and the variable is returned (using a return statement).
    • Delete the line of code where you assigned the string 'Python' to the py variable.
  6. Run the modified code, and then try to think about the state of the program just by using the code and the output you see.
    • You should notice that the state you deduced is contradictory to the output printed in the second cell — for instance, welcome(dq) is not supposed to print anything (because we didn’t use print() this time), and welcome(py) should raise an error because py is not defined anymore.
  7. Pretend you don’t know what happened exactly and run the %history -p command again to find out.
  8. Fix the issue either by running the second code cell again or by clicking Restart & Run All in the Kernel menu.

Can you please let us know, what specific part are you confused about? :slightly_smiling_face:

Best,
Sahil

I’m confused by the whole thing. Hidden State.

@info.victoromondi Don’t worry Victor. Another techie is helping me now on this - see thread. Best wishes.

Hi @friendl74,

Perhaps a better way to understand that screen would be to forget about the term called hidden state. In a typical python code, what we see is what we get. For example, suppose if we run the following script:

value = 10

def add_to_value(num):
    return num + value

result = add_to_value(10)
print(result)

We can clearly expect it to print 20 because we can see the number we are adding and the number stored in the value variable. However, in a Jupyter notebook, we run codes in separate cells, and each cell uses the same global scope.

For example, let’s split the above code into three separate cells:

If we modify the first cell and run the second and third cells, we will not get the updated value.

This is because each cell has to be run separately. Unless we run it, the changes will not be made. While in this case, it is obvious that the unexpected output is because we haven’t run that first cell. However, as we create more and more cells, it is hard to know which cell we forgot to run, which cell we run more than once, etc.

This is what the hidden state was referring to. The ability to run cells in any order we want is an advantage; however, it also has a drawback of creating unintentional results. So as a fix for it, whenever we see unexpected results, we should try running all cells in sequential order. And the easiest way to do that is by clicking Restart & Run All in the Kernel menu.

The purpose of the instruction on this screen is to demonstrate this issue and teach you what to do to fix it.

As you continue to work with guided projects, you will experience this issue at least once. So it will be more clear at that time. Let me know if you have any questions about it. :slightly_smiling_face:

Best,
Sahil

2 Likes

Hi again,

Thank you for that information.

A question, in your last code example, code is run sequentially i.e. [5][6][7][8]. So if you run all, it won’t run top to bottom, but by those numbers sequentially?
That’s why you get the answer 50?

In the last example, code was not run sequentially, [5] [6] [8] [7]. I ran last cell [7] before running second last cell [8]. That’s why the value of 50 was printed, otherwise (if we run it sequentially), 30 would have been printed.

1 Like

Why do we have the numbers? [5][6] etc?

This was the 5th ran cell, 6th ran cell…

@Sahil I just ran you last example in notebook, and I think I understand what it all means now. I now understand the numbers, i.e. [5] too.

I think the more I use J. Notebook, the more things will make more sense.

L.

Why? If you use an editor like Visual Studio, it doesn’t have that ability to do that.

1 Like

Hi @friendl74,

Each tool is designed for a specific audience. While, visual studio is meant for programming in general, Jupyter notebook is created for the purpose of data exploration and sharing. If you are creating a software, it doesn’t make sense to split parts of codes into different cells because all codes need to be run at once for the software to function properly. On the other hand, for data exploration, it doesn’t make sense to run all code at once. For example, let’s say, in one cell, we are exploring the columns available in a dataset, and in another cell, we are exploring the statistical summary of a dataset. Since we are only exploring one thing at a time, splitting code into cells will reduce unnecessary computation makes it easier for the reader to understand what we are doing in each cell. We only have to run all the codes at once if we see an unexpected output due to running codes in non-sequential order.

Here is a great read:
https://www.nature.com/articles/d41586-018-07196-1

Best,
Sahil

I see. I wish this module on DQ explained that in detail what you just written about Jupyter Notebook.

I’ll read the link.

Thanks for your help.

L.

1 Like