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 cell/function
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:
- It's pretty easy to create a
my_helper.py
and put the code above there, thenfrom my_helper import *
- The problem is that, I may use other package in the method (in this case,
np
, i.e.numpy
), then I need to re-import numpy inmy_helper.py
. Can it re-use the environment created inipython notebook
, hence no need for re-importing? - If I change the code in
my_helper.py
, 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.