Повышение эффективности python скрипта, содержащего scipy оптимизации - PullRequest
0 голосов
/ 04 августа 2020

Я пишу фрагмент кода, который выполняется, но выполнение одной части занимает очень много времени и заканчивается замедлением общего выполнения сценария. Я не очень продвинут в программировании, поэтому я (пока) не знаю о стратегиях, которые бы ускорили этот процесс.

Вот как выглядит проблемно медленный код:

### forecasting function to be optimized, (example of functions to show the structure of the script)###
def winter_holts_high_low(x):
   pass

def winter_holts_low_high(x):
   pass

def winter_high_low(x):
   pass

def winter_low_high(x):
   pass 

def holts(x):
   pass

def exponential_smoothing(x):
   pass


data2 = data2.set_index('Id')
for i in data2.index:
    x0 = np.array([0.1, 0.1, 0.1])

    solver = data2['solver'].loc[i]

    if data2['seasonality_status'].loc[i] == "seasonal" and data2['trend_status'].loc[i] == "trending" and data2['demand_level'].loc[i] == 'high' and data2['variability'].loc[i] == 'low':
        result = minimize(winter_holts_high_low, x0, bounds=[(0, 1), (0, 1), (0, 1)], method=solver)


    if data2['seasonality_status'].loc[i] == "seasonal" and data2['trend_status'].loc[i] == "trending" and (data2['demand_level'].loc[i] == 'low' or data2['variability'].loc[i] == 'high'):
            result = minimize(winter_holts_low_high, x0, bounds=[(0, 1), (0, 1), (0, 1)], method=solver)


    if data2['seasonality_status'].loc[i] == "seasonal" and data2['trend_status'].loc[i] == "Not_trending" and data2['demand_level'].loc[i] == 'high' and data2['variability'].loc[i] == 'low':
            result = minimize(winter_high_low, x0, bounds=[(0, 1), (0, 1), (0, 1)], method=solver)

    if data2['seasonality_status'].loc[i] == "seasonal" and data2['trend_status'].loc[i] == "Not_trending" and (
            data2['demand_level'].loc[i] == 'low' or data2['variability'].loc[i] == 'high'):
        result = minimize(winter_low_high, x0, bounds=[(0, 1), (0, 1), (0, 1)], method=solver)

    if data2['seasonality_status'].loc[i] == "not_seasonal" and data2['trend_status'].loc[i] == "trending":
        result = minimize(holts, x0, bounds=[(0, 1), (0, 1), (0, 1)], method=solver)

    if data2['seasonality_status'].loc[i] == "not_seasonal" and data2['trend_status'].loc[i] == "Not_trending":
        result = minimize(exponential_smoothing, x0, bounds=[(0, 1), (0, 1), (0, 1)], method=solver)

и вот набор данных игрушки:

        deseasonalize_d-1 demand_level variability seasonality_status  \
Id                                                                      
100121         123.031501         high       small       not_seasonal   
100122          74.364646         high       small       not_seasonal   
100131         595.439056         high       small       not_seasonal   
100135         263.901198         high       small       not_seasonal   
100150         179.421932         high       small       not_seasonal   

        trend_status          
Id                                                                     
100121  Not_trending    
100122  Not_trending    
100131  Not_trending  
100135  Not_trending   
100150  Not_trending  
        

Я знаю, что в большинстве случаев разумнее использовать np.where вместо операторов if, которые я пробовал следующим образом:

data2 = data2.set_index('Id')
for i in data2.index:
    x0 = np.array([0.1, 0.1, 0.1])


    solver = data2['solver'].loc[i]

    np.where(((data2['seasonality_status'].loc[i] == "seasonal") & (data2['trend_status'].loc[i] == "trending") & (data2['demand_level'].loc[i] == 'high') & (data2['variability'].loc[i] == 'low')),
        minimize(winter_holts_high_low, x0, bounds=[(0, 1), (0, 1), (0, 1)], method=solver),"")


    np.where(((data2['seasonality_status'].loc[i] =='seasonal') &(data2['trend_status'].loc[i] == "trending") & ((data2['demand_level'].loc[i] == 'low') | (data2['variability'].loc[i] == 'high'))), minimize(winter_holts_low_high, x0, bounds=[(0, 1), (0, 1), (0, 1)], method=solver),"")


    np.where(((data2['seasonality_status'].loc[i] == "seasonal") & (data2['trend_status'].loc[i] == "Not_trending") & ( data2['demand_level'].loc[i] == 'high') & (data2['variability'].loc[i] == 'low')), minimize(winter_high_low, x0, bounds=[(0, 1), (0, 1), (0, 1)], method=solver), "")

    np.where(((data2['seasonality_status'].loc[i] == 'seasonal') & (data2['trend_status'].loc[i] == "Not_trending") & ((data2['demand_level'].loc[i] == 'low') | (data2['variability'].loc[i] == 'high'))), minimize(winter_low_high, x0, bounds=[(0, 1), (0, 1), (0, 1)], method=solver), "")

Я не уверен, что происходит, но скрипту требуется 1 minute and 09 secondes для завершения с операторами if и более 5 minutes с np.where. Мне любопытно посмотреть, как это можно оптимизировать, чтобы работать быстрее.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...