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.
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.
# 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=(5, 2.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) (None, 32) 1184
dense (Dense) (None, 8) 264
dense_1 (Dense) (None, 1) 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.
# 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=(5, 2.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]))
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 another 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.
# 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) (None, 32) 1184
dense (Dense) (None, 8) 264
dense_1 (Dense) (None, 1) 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
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.
Visit SH Fintech Modeling for additional insight on this topic: https://kiandlee.blogspot.com/2023/01/python-importing-ipynb-file-jupyter.html.
Disclosure: Interactive Brokers
Information posted on IBKR Campus that is provided by third-parties does NOT constitute a recommendation that you should contract for the services of that third party. Third-party participants who contribute to IBKR Campus are independent of Interactive Brokers and Interactive Brokers does not make any representations or warranties concerning the services offered, their past or future performance, or the accuracy of the information provided by the third party. Past performance is no guarantee of future results.
This material is from SHLee AI Financial Model and is being posted with its permission. The views expressed in this material are solely those of the author and/or SHLee AI Financial Model and Interactive Brokers is not endorsing or recommending any investment or trading discussed in the material. This material is not and should not be construed as an offer to buy or sell any security. It should not be construed as research or investment advice or a recommendation to buy, sell or hold any security or commodity. This material does not and is not intended to take into account the particular financial conditions, investment objectives or requirements of individual customers. Before acting on this material, you should consider whether it is suitable for your particular circumstances and, as necessary, seek professional advice.