cqcn1991 cqcn1991 - 1 month ago 4x
Python Question

Ipython Notebook: Elegant way of extracting method out?

As my notebook gets longer, I want to extract some code out, so the notebook would be easier to read.

For example, this is a

that I want to extract from the notebook

def R_square_of(MSE, kde_result):
# R square measure:
# https://en.wikipedia.org/wiki/Coefficient_of_determination
y_mean = np.mean(kde_result)
SS_tot = np.power(kde_result - y_mean,2)
SS_tot_avg = np.average(SS_tot)

SS_res_avg = MSE
R_square = 1 - SS_res_avg/SS_tot_avg

return R_square

How can I do it effectively?

My thoughts:

  1. It's pretty easy to create a
    and put the code above there, then
    from my_helper import *

  2. The problem is that, I may use other package in the method (in this case,
    , i.e.
    ), then I need to re-import numpy in
    . Can it re-use the environment created in
    ipython notebook
    , hence no need for re-importing?

  3. If I change the code in
    , I need to restart the kernel to load the change(NameError: global name 'numpy' is not defined), this makes it difficult to change code in that file.

Kos Kos

Instead of importing your other file, you could instead run it with the %run magic command:

In [1]: %run -i my_helper.py

-i: run the file in IPython’s namespace instead of an empty one. This is useful if you are experimenting with code written in a text editor which depends on variables defined interactively.

I'd still take the opportunity to recommend writing the file as a proper python module and importing it. This way you actually develop a codebase usable outside of the notebook environment. You could write tests for it or publish it somewhere.