Asset Classes

Free investment financial education

Language

Multilingual content from IBKR

Close Navigation
Learn more about IBKR accounts
Python: %run to Import Another Folder’s ipynb Files

Python: %run to Import Another Folder’s ipynb Files

Posted January 31, 2023 at 3:01 pm

Sang-Heon Lee
SHLee AI Financial Model

This post shows how to use %run command for importing Jupyter Notebook (ipynb) files from another folders. It is essentially to run the imported ipynb files before running a main file. It can be used to split code blocks such as importing library, declaring user-defined functions, data and its preprocessing.

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

This post is similar to the previous post but easily extended to ipynb files from other folders. So I introduce it to you. It is so easy.

To import ipynb files in another folders, we can use %run command.

As an example, I split one file into three files: a0_load_lib_func.ipynba1_read_data.ipynb, and a2_run_rnn.ipynb. The first two files are saved at D:/blog_temp/ for an illustration.

1) Common code block : a1_load_lib_func.ipynb

a1_load_lib_func.ipynb contains package libraries, some user-defined functions.

# Import 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

2) Reading data : a2_read_data.ipynb

a2_read_data.ipynb is used to read data.

# Read 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]))

3) Main file : a3_run_rnn.ipynb

The following main file (a3_run_rnn.ipynb) imports two above files and run a SimpleRNN tensorflow function.

# Import other folder's ipynb files
# In[1]:
_________________________________________________________________
# the same folder
#%run "a1_load_lib_func.ipynb"
#%run "a2_read_data.ipynb"
 
# different folder
%run "D:/blog_temp/a1_load_lib_func.ipynb"
%run "D:/blog_temp/a2_read_data.ipynb"
_________________________________________________________________
 
# ## 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

Visit SH Fintech Modeling for additional insight on this topic: https://kiandlee.blogspot.com/2023/01/python-run-to-import-another-folders.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.

IBKR Campus Newsletters

This website uses cookies to collect usage information in order to offer a better browsing experience. By browsing this site or by clicking on the "ACCEPT COOKIES" button you accept our Cookie Policy.