Python : Importing ipynb files (Jupyter Notebook) from an ipynb file

This post shows how to import a Jupyter Notebook (ipynb) file from another Jupyter Notebook file. It will avoid occasional mistakes and save time to write redundant common codes such as importing library, declaring user-defined functions, data and its preprocessing, to name a few.


Importing an Jupyter Notebook from another Jupyter Notebook



To import an ipynb file in another ipynb file, we need to install import-ipynb python package which is apapted exactly to our purpose.

Install import-ipynb library from the command prompt
!pip install import-ipynb

Import it from your notebook
import import_ipynb

Import your BBB.ipynb notebook as if it was BBB.py file
from BBB import *



Sample code as a whole : a_simple_rnn.ipynb


A sample code is a deep learning model using the SimpleRNN model which consists of including package libraries, loading and preprocessing data, setting up model, fitting and prediction. This is a whole file which will be divided into two files in the next.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# Import and Install Library
 
# In[1]:
_________________________________________________________________
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from keras.models import Sequential
from keras.layers import Dense, SimpleRNN
get_ipython().run_line_magic('matplotlib''inline')
from IPython.core.interactiveshell import InteractiveShell
InteractiveShell.ast_node_interactivity = "all"
_________________________________________________________________
 
# ## Functions
# In[2]:
_________________________________________________________________
# convert into dataset matrix
def convertToMatrix(data, step):
    X, Y =[], []
    for i in range(len(data)-step):
        d=i+step; X.append(data[i:d,]); Y.append(data[d,])
    return np.array(X), np.array(Y)
 
def draw_plot1(df,predicted):
    index = df.index.values
    plt.figure(figsize=(52.5))
    plt.plot(index,df); plt.plot(index,predicted)
    plt.show()
    return plt
_________________________________________________________________
 
# ## Load Dataset
# In[3]:
_________________________________________________________________
step = 4; N = 1000; Tp = 800    
t=np.arange(0,N)
x=np.sin(0.02*t)+2*np.random.rand(N)
df = pd.DataFrame(x)
# df.head(); plt.plot(df); plt.show()
train=df.values
train = np.append(train,np.repeat(train[-1,],step))
trainX,trainY = convertToMatrix(train,step)
trainX = np.reshape(trainX, (trainX.shape[0], 1, trainX.shape[1]))
_________________________________________________________________
 
# ## Building Model
# In[4]:
_________________________________________________________________
model = Sequential()
model.add(SimpleRNN(units=32, input_shape=(1,step), activation="relu"))
model.add(Dense(8, activation="relu")) 
model.add(Dense(1))
model.compile(loss='mean_squared_error', optimizer='rmsprop')
model.summary()
_________________________________________________________________
Model: "sequential"
_________________________________________________________________
 Layer (type)                Output Shape              Param #   
=================================================================
 simple_rnn (SimpleRNN)      (None32)                1184      
                                                                 
 dense (Dense)               (None8)                 264       
                                                                 
 dense_1 (Dense)             (None1)                 9         
                                                                 
=================================================================
Total params: 1,457
Trainable params: 1,457
Non-trainable params: 0
_________________________________________________________________
 
# ## Training Model
# In[5]:
_________________________________________________________________
model.fit(trainX, trainY, epochs=100, batch_size=16, verbose=0)
_________________________________________________________________
 
# In[6]:
_________________________________________________________________
trainPredict = model.predict(trainX)
trainScore = model.evaluate(trainX, trainY, verbose=0)
print(trainScore)
draw_plot1(df,trainPredict)
_________________________________________________________________
 



1) Common code block : a0_load_lib_data_func.ipynb


The following python code in the Jupyter notebook (a0_load_lib_data_func.ipynb) contains package libraries, some user-defined functions, and data. This file will be imported in each mode files.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# Import and Install Library
 
# In[1]:
_________________________________________________________________
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from keras.models import Sequential
from keras.layers import Dense, SimpleRNN
get_ipython().run_line_magic('matplotlib''inline')
from IPython.core.interactiveshell import InteractiveShell
InteractiveShell.ast_node_interactivity = "all"
_________________________________________________________________
 
# ## Functions
# In[2]:
_________________________________________________________________
# convert into dataset matrix
def convertToMatrix(data, step):
    X, Y =[], []
    for i in range(len(data)-step):
        d=i+step; X.append(data[i:d,]); Y.append(data[d,])
    return np.array(X), np.array(Y)
 
def draw_plot1(df,predicted):
    index = df.index.values
    plt.figure(figsize=(52.5))
    plt.plot(index,df); plt.plot(index,predicted)
    plt.show()
    return plt
_________________________________________________________________
 
# ## Load Dataset
# In[3]:
_________________________________________________________________
step = 4; N = 1000; Tp = 800    
t=np.arange(0,N)
x=np.sin(0.02*t)+2*np.random.rand(N)
df = pd.DataFrame(x)
# df.head(); plt.plot(df); plt.show()
train=df.values
train = np.append(train,np.repeat(train[-1,],step))
trainX,trainY = convertToMatrix(train,step)
trainX = np.reshape(trainX, (trainX.shape[0], 1, trainX.shape[1]))
_________________________________________________________________
 
cs



2) Each model-specific file : a1_simple_rnn_wo_lib_data_func.ipynb


The following python code (a1_simple_rnn_wo_lib_data_func.ipynb) imports the above Jupyter Notebook file (a0_load_lib_data_func.ipynb). As the imported file contains common code blocks, this file does not contain these redundant information but has the content of each specific model and routines for forecasting performance comparisons.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# Import a0_read_lib_data_func.ipynb file
# In[1]:
_________________________________________________________________
# install if not installed
#!pip install import-ipynb
 
# import it from your notebook
import import_ipynb
 
# import a0_load_lib_data_func notebook
from a0_load_lib_data_func import *
_________________________________________________________________
 
# ## Building Model
# In[4]:
_________________________________________________________________
model = Sequential()
model.add(SimpleRNN(units=32, input_shape=(1,step), activation="relu"))
model.add(Dense(8, activation="relu")) 
model.add(Dense(1))
model.compile(loss='mean_squared_error', optimizer='rmsprop')
model.summary()
_________________________________________________________________
Model: "sequential"
-----------------------------------------------------------------
 Layer (type)                Output Shape              Param #   
=================================================================
 simple_rnn (SimpleRNN)      (None32)                1184      
                                                                 
 dense (Dense)               (None8)                 264       
                                                                 
 dense_1 (Dense)             (None1)                 9         
                                                                 
=================================================================
Total params: 1,457
Trainable params: 1,457
Non-trainable params: 0
-----------------------------------------------------------------
# ## Training Model
# In[5]:
_________________________________________________________________
model.fit(trainX, trainY, epochs=100, batch_size=16, verbose=0)
_________________________________________________________________
<keras.callbacks.History at 0x22e182f8c70>
# In[6]:
_________________________________________________________________
trainPredict = model.predict(trainX)
trainScore = model.evaluate(trainX, trainY, verbose=0)
print(trainScore)
draw_plot1(df,trainPredict)
_________________________________________________________________
0.3513454794883728
 
cs


Concluding Remarks


This post shows how to use the common code block file which is imported in another model-specific file in Jupyter notebook. Running one file as a whole and running two sparate files deliver the same output. But the latter will be useful since it can avoid redundant copy-and-paste works. \(\blacksquare\)


No comments:

Post a Comment