Ниже у меня очень неэффективный код.Код идентичен в 6 раундах, за исключением [0:x,1:2]
, где x имеет изменение x-1 в каждом раунде, в конечном итоге останавливаясь на x = 8 (для переменных y, x) и x = 9 (для переменной t).Я сохраняю результаты прогнозирования в переменных x1, x2...x6
.См. Ниже для ясного понимания.
Какой самый простой и краткий способ написать этот код в сжатой форме, чтобы не повторять каждый шаг избыточного кода?Я подумал о введении цикла, который просматривает список переменных для x - но есть ли что-то более простое или более надежное?
import pandas as pd
import numpy as np
#round 1
y = Macro.iloc[0:13,1:2]
x = Macro.iloc[0:13,2:21]
t = Macro.iloc[13:14,2:21]
boost = GradientBoostingRegressor(n_estimators=100, learning_rate=0.1, max_depth=1, random_state=0, loss='ls').fit(x, y)
x6 = boost.predict(t)
#round 2
y = Macro.iloc[0:12,1:2]
x = Macro.iloc[0:12,2:21]
t = Macro.iloc[12:13,2:21]
boost = GradientBoostingRegressor(n_estimators=100, learning_rate=0.1, max_depth=1, random_state=0, loss='ls').fit(x, y)
x5 = boost.predict(t)
#round 3
y = Macro.iloc[0:11,1:2]
x = Macro.iloc[0:11,2:21]
t = Macro.iloc[11:12,2:21]
boost = GradientBoostingRegressor(n_estimators=100, learning_rate=0.1, max_depth=1, random_state=0, loss='ls').fit(x, y)
x4 = boost.predict(t)
# round 4
y = Macro.iloc[0:10,1:2]
x = Macro.iloc[0:10,2:21]
t = Macro.iloc[10:11,2:21]
boost = GradientBoostingRegressor(n_estimators=100, learning_rate=0.1, max_depth=1, random_state=0, loss='ls').fit(x, y)
x3 = boost.predict(t)
# round 5
y = Macro.iloc[0:9,1:2]
x = Macro.iloc[0:9,2:21]
t = Macro.iloc[9:10,2:21]
boost = GradientBoostingRegressor(n_estimators=100, learning_rate=0.1, max_depth=1, random_state=0, loss='ls').fit(x, y)
x2 = boost.predict(t)
# round 6
y = Macro.iloc[0:8,1:2]
x = Macro.iloc[0:8,2:21]
t = Macro.iloc[8:9,2:21]
boost = GradientBoostingRegressor(n_estimators=100, learning_rate=0.1, max_depth=1, random_state=0, loss='ls').fit(x, y)
x1 = boost.predict(t)