Python : %run to import another folder's ipynb files

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.


%run to Import another folder's ipynb files



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.ipynb, a1_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.

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
# 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=(52.5))
    plt.plot(index,df); plt.plot(index,predicted)
    plt.show()
    return plt
_________________________________________________________________
 
cs



2) Reading data : a2_read_data.ipynb


a2_read_data.ipynb is used to read data.

1
2
3
4
5
6
7
8
9
10
11
12
13
# 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]))
_________________________________________________________________
cs



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.

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
# 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)      (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



No comments:

Post a Comment