Is there any way to run a full python script ? I am not looking to just call a function written in python.

 채택된 답변

Yongjian Feng
Yongjian Feng 2021년 8월 7일

1 개 추천

What is the difference between a full python script and a function written in python? They are the same in my opinion.

댓글 수: 8

Abhishek Sharma
Abhishek Sharma 2021년 8월 7일
import pandas as pd
import random
import numpy as np
import matplotlib.pyplot as plt
from scipy import io
import pickle as pkl
import keras
from keras.models import Sequential, Model, load_model
from keras.layers import LSTM, Dense, RepeatVector, TimeDistributed, Input, BatchNormalization, \
multiply, concatenate, Flatten, Activation, dot
from keras.optimizers import Adam
from keras.utils import plot_model
# from keras.callbacks import EarlyStopping
import pydot as pyd
from keras.utils.vis_utils import plot_model, model_to_dot
keras.utils.vis_utils.pydot = pyd
import numpy as np
import math
import tensorflow as tf
from pandas import DataFrame
from pandas import concat
from pandas import read_csv
from keras.models import Sequential
from keras.layers import Dense, TimeDistributed
from keras.layers import LSTM
from sklearn.preprocessing import MinMaxScaler
from sklearn.metrics import mean_squared_error
from tensorflow.python.keras.callbacks import TensorBoard
import datetime
keras=tf.keras
import scipy.io
from scipy import io
# Get x values of the sine wave
time= np.arange(0, 200, 0.01);
mat = scipy.io.loadmat('y.mat')
amplitude=mat['Y']
# Amplitude of the sine wave is sine of a variable like time
# amplitude= time*0.002+0.02
# amplitude=np.sin(2*math.pi*time)+np.sin(3*2*math.pi*time)+np.sin(5* 2*math.pi*time)
# noise = np. random. normal(0, .2, amplitude_. shape)
# amplitude=amplitude_+noise
# amplitude = scipy.io.loadmat('data.mat')
# amplitude=amplitude['data']
amplitude=amplitude/np.max(amplitude)
df=pd.DataFrame(amplitude)
df_train=amplitude[0:12000]
df_val=amplitude[12000:15000]
df_test=amplitude[15000:20000]
df_train=pd.DataFrame(df_train)
df_val=pd.DataFrame(df_val)
df_test=pd.DataFrame(df_test)
def series_to_supervised(data, n_in=1, n_out=1, dropnan=True):
n_vars = 1 if type(data) is list else data.shape[1]
df = DataFrame(data)
cols, names = list(), list()
# input sequence (t-n, ... t-1)
for i in range(n_in, 0, -1):
cols.append(df.shift(i))
names += [('var%d(t-%d)' % (j+1, i)) for j in range(n_vars)]
# forecast sequence (t, t+1, ... t+n)
for i in range(0, n_out):
cols.append(df.shift(-i))
if i == 0:
names += [('var%d(t)' % (j+1)) for j in range(n_vars)]
else:
names += [('var%d(t+%d)' % (j+1, i)) for j in range(n_vars)]
# put it all together
agg = concat(cols, axis=1)
agg.columns = names
# drop rows with NaN values
if dropnan:
agg.dropna(inplace=True)
return agg
n_in=100
gap=100 # number of steps after which forecast is needed
n_out=100+gap # horizon of forecast
reframed_train=series_to_supervised(df_train,n_in,n_out)
reframed_val=series_to_supervised(df_val,n_in,n_out)
reframed_test=series_to_supervised(df_test,n_in,n_out)
# reframed_train=reframed_train.drop(['var(t)])
for i in range(1,gap):
reframed_train=reframed_train.drop(['var1(t+%s)' %(i)],axis=1)
reframed_train=reframed_train.drop(['var1(t)'],axis=1)
# for i in range(1,n_out):
# reframed_train=reframed_train.drop(['var2(t+%s)' %(i)],axis=1)
# reframed_train=reframed_train.drop(['var2(t)'],axis=1)
for i in range(1,gap):
reframed_val=reframed_val.drop(['var1(t+%s)' %(i)],axis=1)
reframed_val=reframed_val.drop(['var1(t)'],axis=1)
# for i in range(1,n_out):
# reframed_val=reframed_val.drop(['var2(t+%s)' %(i)],axis=1)
# reframed_val=reframed_val.drop(['var2(t)'],axis=1)
for i in range(1,gap):
reframed_test=reframed_test.drop(['var1(t+%s)' %(i)],axis=1)
reframed_test=reframed_test.drop(['var1(t)'],axis=1)
# for i in range(1,n_out):
# reframed_test=reframed_test.drop(['var2(t+%s)' %(i)],axis=1)
# reframed_test=reframed_test.drop(['var2(t)'],axis=1)
reframed_train=reframed_train.values
reframed_val=reframed_val.values
reframed_test=reframed_test.values
train_X=reframed_train[:,0:(df.shape[1]*n_in)]
train_Y=reframed_train[:,df.shape[1]*n_in:(df.shape[1]*n_in)+df.shape[1]*n_out]
val_X=reframed_val[:,0:(df.shape[1]*n_in)]
val_Y=reframed_val[:,df.shape[1]*n_in:(df.shape[1]*n_in)+df.shape[1]*n_out]
test_X=reframed_test[:,0:(df.shape[1]*n_in)]
test_Y=reframed_test[:,df.shape[1]*n_in:(df.shape[1]*n_in)+df.shape[1]*n_out]
train_X = train_X.reshape((train_X.shape[0],n_in, df.shape[1]))
train_Y = train_Y.reshape((train_Y.shape[0],n_out-gap,1))
val_X = val_X.reshape((val_X.shape[0],n_in, df.shape[1]))
val_Y = val_Y.reshape((val_Y.shape[0],n_out-gap,1))
test_X=test_X.reshape((test_X.shape[0],n_in, df.shape[1]))
test_Y = test_Y.reshape((test_Y.shape[0],n_out-gap,1))
# def create_model():
# return tf.keras.models.Sequential([
# tf.keras.layers.Bidirectional(LSTM(12 8,return_sequences=False,stateful=False,batch_input_shape=(None,n_in,1))),
# # tf.keras.layers.Bidirectional(LSTM(50,return_sequences=True)),
# tf.keras.layers.Dense(n_out-gap)])
# #model.add(Dense(1))
# model = create_model()
# model.compile(loss=keras.losses.Huber(), optimizer='adam',metrics=["mae"])
# early_stopping = keras.callbacks.EarlyStopping(patience=10)
# history=model.fit(train_X, train_Y,validation_data=(val_X, val_Y),batch_size=32,epochs=1,callbacks=[early_stopping],shuffle=True)
# history=model.fit(train_X, train_Y,validation_data=(val_X, val_Y),batch_size=1,epochs=50,shuffle=False)
# # model.save('sinus.h5')
# m=model.predict(test_X)
# m = m.reshape((m.shape[0],n_out-gap,1))
# io.savemat("forecast_lin.mat",{"forecast_lin": m})
# io.savemat("true.mat",{"true": test_Y})
# error=np.abs(test_Y-m)
# mape=np.mean(np.abs((test_Y - m) / m)) * 100
# print('MAPE is: %.2f percent' % (mape))
Abhishek Sharma
Abhishek Sharma 2021년 8월 7일
I am looking to run this in matlab
Yongjian Feng
Yongjian Feng 2021년 8월 8일
It seems to me that you actually want to run a ML script that calls other ML packages.
This can be done. You might need to play with the env var PYTHONPATH from matlab to point to the installed python packages.
According to experience of the other users who did this successfully, python virtualenv could cause some problems. If you don't have to use virtualenv, avoid it.
Abhishek Sharma
Abhishek Sharma 2021년 8월 8일
Can you please share the method or any link to it.
Yongjian Feng
Yongjian Feng 2021년 8월 8일
편집: Yongjian Feng 2021년 8월 8일
There are several MATLAB Answers related to this:
There is another one regarding python virtualenv on Mac. I have trouble to find.
My suggestion, give it a try. If there is any error message, post it and we can look at it together.
Abhishek Sharma
Abhishek Sharma 2021년 8월 9일
I tried i but it gave following error:
Traceback (most recent call last):
File "ez_train_stlf.py", line 9, in <module>
import pandas as pd
ModuleNotFoundError: No module named 'pandas'
Abhishek Sharma
Abhishek Sharma 2021년 8월 9일
편집: Abhishek Sharma 2021년 8월 9일
I think it's not able to import the modules.
Yongjian Feng
Yongjian Feng 2021년 8월 9일
  1. Is pandas installed properly in python? Can you use python to import it without problems?
  2. Figure out where from it is imported, then inside matlab, call setenv (https://www.mathworks.com/help/matlab/ref/setenv.html) to set PYTHONPATH to add that location to it.
If you don't use virtualenv, this can be done easily.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Call Python from MATLAB에 대해 자세히 알아보기

태그

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by