{"id":465772,"date":"2025-07-03T09:12:21","date_gmt":"2025-07-03T09:12:21","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=465772"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=465772","title":{"rendered":"<span>\u041a\u0440\u043e\u0441\u0441-\u0432\u0430\u043b\u0438\u0434\u0430\u0446\u0438\u044f \u043d\u0430 \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0445 \u0440\u044f\u0434\u0430\u0445: \u043a\u0430\u043a \u043d\u0435 \u043f\u0435\u0440\u0435\u043c\u0435\u0448\u0430\u0442\u044c \u0432\u0440\u0435\u043c\u044f<\/span>"},"content":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p>\u041f\u0440\u0438\u0432\u0435\u0442, \u0425\u0430\u0431\u0440!<\/p>\n<p>\u0421\u0435\u0433\u043e\u0434\u043d\u044f \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u0442\u043e, \u0447\u0442\u043e \u0447\u0430\u0449\u0435 \u0432\u0441\u0435\u0433\u043e \u043b\u043e\u043c\u0430\u0435\u0442 \u0434\u0430\u0436\u0435 \u043a\u0440\u0443\u0442\u043e \u0432\u044b\u0433\u043b\u044f\u0434\u044f\u0449\u0438\u0435 \u043c\u043e\u0434\u0435\u043b\u0438 \u043f\u0440\u0438 \u0440\u0430\u0431\u043e\u0442\u0435 \u0441 \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u043c\u0438 \u0440\u044f\u0434\u0430\u043c\u0438 \u2014 <strong>\u043d\u0435\u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u0430\u044f \u043a\u0440\u043e\u0441\u0441-\u0432\u0430\u043b\u0438\u0434\u0430\u0446\u0438\u044f<\/strong>. \u0420\u0430\u0437\u0431\u0435\u0440\u0435\u043c, \u043f\u043e\u0447\u0435\u043c\u0443 <code>KFold<\/code> \u0442\u0443\u0442 \u043d\u0435 \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442, \u043a\u0430\u043a \u043b\u0435\u0433\u043a\u043e \u0441\u043b\u043e\u0432\u0438\u0442\u044c \u0443\u0442\u0435\u0447\u043a\u0443 \u0431\u0443\u0434\u0443\u0449\u0435\u0433\u043e, \u043a\u0430\u043a\u0438\u0435 \u0441\u043f\u043b\u0438\u0442\u0442\u0435\u0440\u044b \u0440\u0435\u0430\u043b\u044c\u043d\u043e \u0447\u0435\u0441\u0442\u043d\u044b \u043f\u043e \u043e\u0442\u043d\u043e\u0448\u0435\u043d\u0438\u044e \u043a\u043e \u0432\u0440\u0435\u043c\u0435\u043d\u0438, \u043a\u0430\u043a \u0432\u0430\u043b\u0438\u0434\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0444\u0438\u0447\u0438 \u0441 \u043b\u0430\u0433\u0430\u043c\u0438 \u0438 \u0430\u0433\u0440\u0435\u0433\u0430\u0442\u0430\u043c\u0438.<\/p>\n<h3>\u041f\u043e\u0447\u0435\u043c\u0443 KFold \u2014 \u043f\u043b\u043e\u0445\u0430\u044f \u0438\u0434\u0435\u044f \u0434\u043b\u044f time-series<\/h3>\n<p><code>KFold<\/code> \u2014 \u0448\u0442\u0443\u043a\u0430 \u043a\u043b\u0430\u0441\u0441\u043d\u0430\u044f\u2026 <strong>\u043d\u043e \u0442\u043e\u043b\u044c\u043a\u043e \u0435\u0441\u043b\u0438 \u0442\u0432\u043e\u0438 \u0434\u0430\u043d\u043d\u044b\u0435 \u043d\u0435 \u0437\u0430\u0432\u0438\u0441\u044f\u0442 \u043e\u0442 \u0432\u0440\u0435\u043c\u0435\u043d\u0438<\/strong>. \u041e\u043d \u0431\u044b\u043b \u0441\u043e\u0437\u0434\u0430\u043d \u0434\u043b\u044f \u043c\u0438\u0440\u0430, \u0433\u0434\u0435 \u043a\u0430\u0436\u0434\u044b\u0439 \u043e\u0431\u044a\u0435\u043a\u0442 \u043d\u0435\u0437\u0430\u0432\u0438\u0441\u0438\u043c. \u0414\u043b\u044f \u0437\u0430\u0434\u0430\u0447 \u043a\u043b\u0430\u0441\u0441\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u0438 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0439 \u0438\u043b\u0438 \u0430\u043d\u0430\u043b\u0438\u0437\u0430 \u0442\u0430\u0431\u043b\u0438\u0447\u043d\u044b\u0445 \u0434\u0430\u043d\u043d\u044b\u0445 <code>KFold<\/code> \u2014 \u043f\u043e\u0447\u0442\u0438 \u0440\u0435\u0444\u043b\u0435\u043a\u0441. \u041d\u043e \u0432\u043e\u0442 \u0438\u0434\u0435\u0442 \u0440\u0435\u0447\u044c \u043f\u0440\u043e \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0435 \u0440\u044f\u0434\u044b \u2014 \u0441\u043f\u0440\u043e\u0441 \u043d\u0430 \u0437\u0434\u0440\u0430\u0432\u044b\u0439 \u0441\u043c\u044b\u0441\u043b \u0440\u0435\u0437\u043a\u043e \u0432\u044b\u0440\u0430\u0441\u0442\u0430\u0435\u0442.<\/p>\n<p>\u0412\u0440\u0435\u043c\u044f \u0432 \u0442\u0430\u043a\u0438\u0445 \u0437\u0430\u0434\u0430\u0447\u0430\u0445 \u2014 \u043d\u0435 \u043f\u0440\u043e\u0441\u0442\u043e \u043a\u043e\u043b\u043e\u043d\u043a\u0430 <code>date<\/code>, \u043a\u043e\u0442\u043e\u0440\u0443\u044e \u043c\u043e\u0436\u043d\u043e \u0431\u0440\u043e\u0441\u0438\u0442\u044c \u0432 <code>dropna()<\/code> \u0438 \u0437\u0430\u0431\u044b\u0442\u044c. \u042d\u0442\u043e <strong>\u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442, \u043f\u0440\u0438\u0447\u0438\u043d\u043d\u043e-\u0441\u043b\u0435\u0434\u0441\u0442\u0432\u0435\u043d\u043d\u0430\u044f \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430<\/strong>.  \u0410 <code>KFold<\/code> \u0441 \u0435\u0433\u043e <code>shuffle=True<\/code> \u043e\u0442\u043d\u043e\u0441\u0438\u0442\u0441\u044f \u043a\u043e \u0432\u0440\u0435\u043c\u0435\u043d\u0438, \u043a\u0430\u043a \u043a \u0441\u043a\u0443\u0447\u043d\u043e\u0439 \u043f\u043e\u0434\u043f\u0438\u0441\u0438 \u043d\u0430 \u0433\u0440\u0430\u0444\u0438\u043a\u0435. \u041e\u043d \u043f\u0435\u0440\u0435\u043c\u0435\u0448\u0438\u0432\u0430\u0435\u0442 \u0434\u0430\u043d\u043d\u044b\u0435, \u043e\u0431\u0443\u0447\u0430\u044f \u043c\u043e\u0434\u0435\u043b\u044c \u043d\u0430 \u0431\u0443\u0434\u0443\u0449\u0435\u043c \u0438 \u0442\u0435\u0441\u0442\u0438\u0440\u0443\u044f \u043d\u0430 \u043f\u0440\u043e\u0448\u043b\u043e\u043c. \u0422\u043e \u0435\u0441\u0442\u044c \u0431\u0443\u043a\u0432\u0430\u043b\u044c\u043d\u043e \u0437\u0430\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 \u0435\u0451 \u0437\u043d\u0430\u0442\u044c \u0431\u0443\u0434\u0443\u0449\u0435\u0435, \u0447\u0442\u043e\u0431\u044b \u043f\u0440\u0435\u0434\u0441\u043a\u0430\u0437\u0430\u0442\u044c \u043f\u0440\u043e\u0448\u043b\u043e\u0435.<\/p>\n<p>\u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440:<\/p>\n<pre><code class=\"python\">from sklearn.model_selection import KFold, cross_val_score from sklearn.ensemble import RandomForestRegressor  cv = KFold(n_splits=5, shuffle=True, random_state=42) score = cross_val_score(RandomForestRegressor(), X, y, cv=cv)<\/code><\/pre>\n<p>\u0412\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u0430\u043a\u043a\u0443\u0440\u0430\u0442\u043d\u043e, \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u0431\u0435\u0437 \u043e\u0448\u0438\u0431\u043e\u043a \u2014 \u0430 \u0432\u043d\u0443\u0442\u0440\u0438 \u0441\u043a\u0440\u044b\u0432\u0430\u0435\u0442\u0441\u044f <strong>\u043f\u043e\u043b\u043d\u0435\u0439\u0448\u0435\u0435 \u043d\u0430\u0440\u0443\u0448\u0435\u043d\u0438\u0435 \u043f\u0440\u0438\u0447\u0438\u043d\u043d\u043e\u0441\u0442\u0438<\/strong>: \u043c\u043e\u0434\u0435\u043b\u044c \u0432\u0438\u0434\u0438\u0442 \u043a\u0443\u0441\u043a\u0438 \u0431\u0443\u0434\u0443\u0449\u0435\u0433\u043e, \u043a\u043e\u0433\u0434\u0430 \u043e\u0431\u0443\u0447\u0430\u0435\u0442\u0441\u044f, \u0438 \u043f\u043e\u0442\u043e\u043c \u0440\u0430\u0434\u0443\u0435\u0442\u0441\u044f &#171;\u043e, \u043a\u0430\u043a \u044f \u043a\u0440\u0443\u0442\u043e \u043f\u0440\u0435\u0434\u0441\u043a\u0430\u0437\u0430\u043b&#187;.<\/p>\n<p>\u0422\u043e\u043b\u044c\u043a\u043e \u0432\u043e\u0442 \u0442\u044b \u043d\u0435 \u043e\u0431\u0443\u0447\u0430\u043b \u043c\u043e\u0434\u0435\u043b\u044c \u2014 \u0442\u044b \u0441\u043a\u043e\u0440\u043c\u0438\u043b \u0435\u0439 \u0431\u0443\u0434\u0443\u0449\u0443\u044e \u043f\u0440\u0430\u0432\u0434\u0443, \u043e\u043d\u0430 \u0435\u0451 \u0432\u044b\u0443\u0447\u0438\u043b\u0430 \u0438 \u043f\u043e\u0442\u043e\u043c \u043f\u0440\u043e\u0441\u0442\u043e \u0435\u0451 \u0432\u043e\u0441\u043f\u0440\u043e\u0438\u0437\u0432\u0435\u043b\u0430. <\/p>\n<p><strong>\u0412\u0441\u044f \u0441\u0443\u0442\u044c \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0433\u043e \u0440\u044f\u0434\u0430 \u0432 \u0442\u043e\u043c, \u0447\u0442\u043e \u0442\u044b \u043d\u0435 \u043c\u043e\u0436\u0435\u0448\u044c \u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c \u0432\u043f\u0435\u0440\u0451\u0434<\/strong>. \u042d\u0442\u043e \u0438 \u0434\u0435\u043b\u0430\u0435\u0442 \u0437\u0430\u0434\u0430\u0447\u0443 \u0441\u043b\u043e\u0436\u043d\u043e\u0439. \u041c\u044b \u043c\u043e\u0434\u0435\u043b\u0438\u0440\u0443\u0435\u043c \u0440\u0435\u0430\u043b\u044c\u043d\u043e\u0441\u0442\u044c, \u0433\u0434\u0435 \u0437\u0430\u0432\u0442\u0440\u0430 \u0442\u044b \u043f\u0440\u0435\u0434\u0441\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0448\u044c, \u0438\u043c\u0435\u044f \u043c\u0430\u043a\u0441\u0438\u043c\u0443\u043c \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044e \u043d\u0430 \u0432\u0447\u0435\u0440\u0430 \u0438 \u0447\u0443\u0442\u044c-\u0447\u0443\u0442\u044c \u043d\u0430 \u0441\u0435\u0433\u043e\u0434\u043d\u044f.<\/p>\n<p>\u041f\u043e\u044d\u0442\u043e\u043c\u0443 \u043c\u0435\u0442\u0440\u0438\u043a\u0438 \u0431\u0443\u0434\u0443\u0442 \u043b\u0436\u0438\u0432\u044b\u043c\u0438, \u0430 \u0444\u0438\u0447\u0438 \u0441 look-ahead \u044d\u0444\u0444\u0435\u043a\u0442\u043e\u043c \u043f\u0440\u043e\u0439\u0434\u0443\u0442 \u043d\u0435\u0437\u0430\u043c\u0435\u0447\u0435\u043d\u043d\u044b\u043c\u0438. <\/p>\n<h3>\u0427\u0442\u043e \u0434\u0435\u043b\u0430\u0442\u044c \u0432\u043c\u0435\u0441\u0442\u043e<\/h3>\n<h4>TimeSeriesSplit<\/h4>\n<p>\u041a\u0430\u0436\u0434\u044b\u0439 \u0444\u043e\u043b\u0434 \u0431\u0435\u0440\u0451\u0442 <strong>\u0432\u0441\u0451, \u0447\u0442\u043e \u043d\u0430\u043a\u043e\u043f\u0438\u043b\u043e\u0441\u044c \u0440\u0430\u043d\u044c\u0448\u0435<\/strong>, \u0438 \u043f\u0440\u0438\u0441\u0442\u0451\u0433\u0438\u0432\u0430\u0435\u0442 \u043e\u0447\u0435\u0440\u0435\u0434\u043d\u043e\u0439 \u0442\u0435\u0441\u0442-\u043e\u0442\u0440\u0435\u0437\u043e\u043a \u0441\u043f\u0440\u0430\u0432\u0430:<\/p>\n<pre><code class=\"python\">from sklearn.model_selection import TimeSeriesSplit  tscv = TimeSeriesSplit(     n_splits=5,          # \u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0440\u0430\u0437 \u00ab\u043a\u0430\u0442\u043d\u0451\u043c\u00bb \u0432\u0430\u043b\u0438\u0434\u0430\u0446\u0438\u044e     test_size=90,        # \u0434\u043b\u0438\u043d\u0430 \u0442\u0435\u0441\u0442\u0430 \u0432 \u0434\u043d\u044f\u0445\/\u0447\u0430\u0441\u0430\u0445\/\u043c\u0438\u043d\u0443\u0442\u043a\u0430\u0445     gap=7,               # \u0431\u0443\u0444\u0435\u0440 \u043c\u0435\u0436\u0434\u0443 train \u0438 test, \u0441\u043f\u0430\u0441\u0430\u0435\u0442 \u043e\u0442 look-ahead \u043f\u0440\u0438 \u043b\u0430\u0433\u0430\u0445 )  for tr, ts in tscv.split(data):     print(data.index[tr][0], '\u2026', data.index[tr][-1], '\u2192', data.index[ts][0], '\u2026', data.index[ts][-1])<\/code><\/pre>\n<p><code>gap<\/code> \u043f\u043e\u044f\u0432\u0438\u043b\u0441\u044f \u0432 \u0432\u0435\u0440\u0441\u0438\u0438 0.24 \u0438 \u0434\u0430\u0451\u0442 \u043c\u043e\u0434\u0435\u043b\u0438 \u00ab\u0441\u0430\u043d\u0438\u0442\u0430\u0440\u043d\u0443\u044e \u0437\u043e\u043d\u0443\u00bb, \u0435\u0441\u043b\u0438 \u0432\u044b \u0441\u0442\u0440\u043e\u0438\u0442\u0435 \u043f\u0440\u0438\u0437\u043d\u0430\u043a\u0438, \u0437\u0430\u0433\u043b\u044f\u0434\u044b\u0432\u0430\u044e\u0449\u0438\u0435 \u043d\u0430 \u043e\u0434\u0438\u043d-\u0434\u0432\u0430 \u0448\u0430\u0433\u0430 \u0432\u043f\u0435\u0440\u0451\u0434.<\/p>\n<p><strong>\u041f\u043b\u044e\u0441\u044b<\/strong><\/p>\n<ul>\n<li>\n<p>\u0432\u0441\u0442\u0440\u043e\u0435\u043d\u043e \u0432 <code>sklearn<\/code>, \u0437\u043d\u0430\u0447\u0438\u0442 \u2014 \u043d\u0438\u043a\u0430\u043a\u043e\u0433\u043e \u0432\u043d\u0435\u0448\u043d\u0435\u0433\u043e \u0437\u043e\u043e\u043f\u0430\u0440\u043a\u0430;<\/p>\n<\/li>\n<li>\n<p>\u0440\u0430\u0441\u0442\u0443\u0449\u0430\u044f \u0438\u0441\u0442\u043e\u0440\u0438\u044f \u043f\u0440\u0430\u0432\u0434\u043e\u043f\u043e\u0434\u043e\u0431\u043d\u0430 \u0434\u043b\u044f \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u043e\u0432, \u0433\u0434\u0435 \u0434\u0430\u043d\u043d\u044b\u0435 <em>\u0442\u043e\u043b\u044c\u043a\u043e \u043f\u0440\u0438\u0431\u0430\u0432\u043b\u044f\u044e\u0442\u0441\u044f<\/em>.<\/p>\n<\/li>\n<\/ul>\n<p><strong>\u041c\u0438\u043d\u0443\u0441\u044b<\/strong><\/p>\n<ul>\n<li>\n<p>\u043c\u043e\u0434\u0435\u043b\u044c \u0442\u0430\u0449\u0438\u0442 \u0432 Train <strong>\u0432\u0441\u044e \u0434\u0440\u0435\u0432\u043d\u043e\u0441\u0442\u044c<\/strong>; \u043f\u0440\u0438 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u043d\u044b\u0445 \u0441\u0434\u0432\u0438\u0433\u0430\u0445 (\u0441\u043c. \u044d\u043a\u043e\u043d\u043e\u043c\u0435\u0442\u0440\u0438\u043a\u0443 \u043f\u0440\u043e structural breaks) \u044d\u0442\u043e \u043f\u0440\u0438\u0432\u043e\u0434\u0438\u0442 \u043a \u0434\u0435\u0433\u0440\u0430\u0434\u0430\u0446\u0438\u0438;<\/p>\n<\/li>\n<li>\n<p>\u043d\u0435\u0442 \u043f\u0440\u044f\u043c\u043e\u0439 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u0438 \u0437\u0430\u0431\u044b\u0442\u044c \u0441\u0442\u0430\u0440\u044b\u0435 \u044d\u043f\u043e\u0445\u0438, \u0435\u0441\u043b\u0438 \u043e\u043d\u0438 \u043c\u0435\u0448\u0430\u044e\u0442.<\/p>\n<\/li>\n<\/ul>\n<p>\u041c\u043e\u0436\u043d\u043e \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0438\u0442\u044c <code>max_train_size<\/code>, \u0447\u0442\u043e\u0431\u044b \u043d\u0435 \u0441\u043a\u0430\u0440\u043c\u043b\u0438\u0432\u0430\u0442\u044c \u043c\u043e\u0434\u0435\u043b\u0438 \u0434\u043e\u0438\u0441\u0442\u043e\u0440\u0438\u0447\u0435\u0441\u043a\u0438\u0445 \u043c\u0430\u043c\u043e\u043d\u0442\u043e\u0432 \u2014 \u0438 \u0443\u0441\u043a\u043e\u0440\u0438\u0448\u044c \u043e\u0431\u0443\u0447\u0435\u043d\u0438\u0435, \u0438 \u0437\u0430\u0449\u0438\u0442\u0438\u0448\u044c\u0441\u044f \u043e\u0442 \u0441\u043b\u043e\u043c\u0430\u043d\u043d\u044b\u0445 \u0437\u0430\u043a\u043e\u043d\u043e\u043c\u0435\u0440\u043d\u043e\u0441\u0442\u0435\u0439.<\/p>\n<h3>Expanding window<\/h3>\n<p><code>TimeSeriesSplit<\/code> \u043e\u043a, \u043d\u043e \u0438\u043d\u043e\u0433\u0434\u0430 \u043d\u0443\u0436\u043d\u0430 \u0442\u043e\u043d\u043a\u0430\u044f \u043d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0430 \u2014 \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u043e \u0437\u0430\u0434\u0430\u0442\u044c <strong>\u043f\u0435\u0440\u0432\u044b\u0435 X \u0442\u043e\u0447\u0435\u043a<\/strong>, \u043f\u043e\u0442\u043e\u043c \u0441\u0434\u0432\u0438\u0433\u0430\u0442\u044c\u0441\u044f \u043f\u043e <strong>\u043c\u0435\u0441\u044f\u0446\u0430\u043c<\/strong>, \u0430 horizon \u043e\u0441\u0442\u0430\u0432\u0438\u0442\u044c \u0432 <strong>N \u0434\u043d\u0435\u0439<\/strong>. \u0422\u0443\u0442 \u0437\u0430\u0445\u043e\u0434\u0438\u0442 <code>sktime<\/code>:<\/p>\n<pre><code class=\"python\">from sktime.split import ExpandingWindowSplitter  splitter = ExpandingWindowSplitter(     initial_window=365*2,   # 2 \u0433\u043e\u0434\u0430 \u2014 \u043f\u0435\u0440\u0432\u044b\u0439 train     step_length=30,         # \u043a\u0430\u0436\u0434\u044b\u0435 30 \u0442\u043e\u0447\u0435\u043a\/\u0434\u043d\u0435\u0439 \u0441\u0434\u0432\u0438\u0433\u0430\u0435\u043c\u0441\u044f     fh=[1, 7, 30],          # \u043f\u0440\u043e\u0433\u043d\u043e\u0437\u0438\u0440\u0443\u0435\u043c \u0437\u0430\u0432\u0442\u0440\u0430, \u0447\u0435\u0440\u0435\u0437 \u043d\u0435\u0434\u0435\u043b\u044e \u0438 \u0447\u0435\u0440\u0435\u0437 \u043c\u0435\u0441\u044f\u0446 )<\/code><\/pre>\n<p>\u041a\u043b\u0430\u0441\u0441\u0438\u043a\u0430 \u0434\u043b\u044f \u044d\u043a\u043e\u043d\u043e\u043c\u0435\u0442\u0440\u0438\u043a\u0438: \u0442\u0440\u0435\u043d\u0438\u0440\u0443\u0435\u043c\u0441\u044f \u043d\u0430 \u0432\u0441\u0451\u043c, \u0447\u0442\u043e \u0431\u044b\u043b\u043e, \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u044e\u044e \u043f\u043e\u0440\u0446\u0438\u044e \u0434\u0430\u043d\u043d\u044b\u0445, \u043f\u0440\u0435\u0434\u0441\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u043c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 \u0433\u043e\u0440\u0438\u0437\u043e\u043d\u0442. \u0423\u0434\u043e\u0431\u043d\u043e \u0434\u043b\u044f \u0431\u0435\u043d\u0447\u043c\u0430\u0440\u043a\u043e\u0432: \u0432\u0441\u0435 \u0444\u043e\u043b\u0434\u044b \u0438\u043c\u0435\u044e\u0442 <strong>\u043e\u0434\u0438\u043d\u0430\u043a\u043e\u0432\u044b\u0439<\/strong> \u0442\u0435\u0441\u0442-\u0440\u0430\u0437\u043c\u0435\u0440, \u0441\u0442\u0430\u0442\u0438\u0441\u0442\u0438\u043a\u0430 \u043c\u0435\u0442\u0440\u0438\u043a \u0447\u0438\u0449\u0435.<\/p>\n<p>\u041d\u043e \u043f\u0440\u0438 <strong>\u043d\u0435\u0441\u0442\u0430\u0446\u0438\u043e\u043d\u0430\u0440\u043d\u044b\u0445<\/strong> \u0440\u044f\u0434\u0430\u0445 \u043e\u0448\u0438\u0431\u043a\u0438 \u0441\u0442\u0430\u0440\u044b\u0445 \u044d\u043f\u043e\u0445 \u043c\u043e\u0433\u0443\u0442 \u0442\u044f\u043d\u0443\u0442\u044c \u043c\u043e\u0434\u0435\u043b\u044c \u0432\u043d\u0438\u0437. \u0417\u0434\u0435\u0441\u044c \u0441\u043f\u0430\u0441\u0430\u0435\u0442 \u043b\u0438\u0431\u043e \u0440\u0435\u0433\u0443\u043b\u044f\u0440\u0438\u0437\u0430\u0446\u0438\u044f, \u043b\u0438\u0431\u043e\u2026 \u043d\u0430\u0448 \u0442\u0440\u0435\u0442\u0438\u0439 \u0441\u043f\u043b\u0438\u0442\u0442\u0435\u0440.<\/p>\n<h3>Rolling (Sliding) window<\/h3>\n<p>\u041a\u043e\u0433\u0434\u0430 \u043d\u0443\u0436\u043d\u043e <strong>\u0437\u0430\u0431\u044b\u0432\u0430\u0442\u044c \u0441\u0442\u0430\u0440\u043e\u0435<\/strong> \u2014 \u0434\u0435\u0440\u0436\u0438\u043c train-\u043e\u043a\u043d\u043e \u0444\u0438\u043a\u0441\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0439 \u0434\u043b\u0438\u043d\u044b \u0438 \u043a\u0430\u0442\u0438\u043c \u0435\u0433\u043e \u0432\u043f\u0440\u0430\u0432\u043e.<\/p>\n<p>\u0412 <code>sklearn<\/code> \u043f\u043e\u043a\u0430 \u043d\u0435\u0442 \u0440\u043e\u0434\u043d\u043e\u0433\u043e \u043a\u043b\u0430\u0441\u0441\u0430, \u043d\u043e BaseCrossValidator \u043f\u0438\u0441\u0430\u0442\u044c \u043f\u0440\u043e\u0449\u0435, \u0447\u0435\u043c SQL \u0441 \u0442\u0440\u0435\u043c\u044f <code>JOIN<\/code>-\u0430\u043c\u0438:<\/p>\n<pre><code class=\"python\">from sklearn.model_selection import BaseCrossValidator import numpy as np  class RollingWindowCV(BaseCrossValidator):     def __init__(self, window, horizon, step=1):         self.window, self.horizon, self.step = window, horizon, step      def split(self, X, y=None, groups=None):         n = len(X)         for start in range(0, n - self.window - self.horizon + 1, self.step):             train = np.arange(start, start + self.window)             test  = np.arange(start + self.window, start + self.window + self.horizon)             yield train, test      def get_n_splits(self, X=None, y=None, groups=None):         return (len(X) - self.window - self.horizon) \/\/ self.step + 1<\/code><\/pre>\n<p>\u0418\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 \u043e\u0434\u0438\u043d-\u0432-\u043e\u0434\u0438\u043d \u0441\u043e \u0432\u0441\u0435\u043c\u0438 <code>cross_val_score<\/code>, <code>GridSearchCV<\/code> \u0438 \u0434\u0440\u0443\u0437\u044c\u044f\u043c\u0438:<\/p>\n<pre><code class=\"python\">cv = RollingWindowCV(window=365, horizon=30, step=30) scores = cross_val_score(model, X, y, cv=cv, scoring='neg_mean_absolute_error')<\/code><\/pre>\n<p>\u041c\u043e\u0434\u0435\u043b\u044c \u0432\u0441\u0435\u0433\u0434\u0430 \u0441\u043c\u043e\u0442\u0440\u0438\u0442 \u0442\u043e\u043b\u044c\u043a\u043e \u043d\u0430 \u00ab\u0430\u043a\u0442\u0443\u0430\u043b\u044c\u043d\u044b\u0439\u00bb \u0433\u043e\u0434 (\u0438\u043b\u0438 \u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0437\u0430\u0434\u0430\u0448\u044c) \u2014 \u0440\u0435\u0437\u043a\u0430\u044f \u0441\u043c\u0435\u043d\u0430 \u0442\u0440\u0435\u043d\u0434\u043e\u0432 \u043d\u0435 \u043f\u0440\u043e\u0431\u0438\u0432\u0430\u0435\u0442. \u041a\u043e\u043b\u0447\u0435\u0441\u0442\u0432\u043e \u0444\u043e\u043b\u0434\u043e\u0432 \u043e\u0431\u044b\u0447\u043d\u043e \u043c\u0435\u043d\u044c\u0448\u0435, \u0447\u0435\u043c \u0443 expanding window \u0438 \u043e\u0442 \u044d\u0442\u043e\u0433\u043e \u0441\u043b\u043e\u0436\u043d\u0435\u0435 \u043e\u0446\u0435\u043d\u0438\u0432\u0430\u0442\u044c \u0440\u0430\u0437\u0431\u0440\u043e\u0441 \u043c\u0435\u0442\u0440\u0438\u043a.<\/p>\n<h3>\u041a\u043e\u0433\u0434\u0430 \u043a\u0430\u043a\u043e\u0439 \u0441\u043f\u043b\u0438\u0442\u0442\u0435\u0440 \u0431\u0440\u0430\u0442\u044c<\/h3>\n<div>\n<div class=\"table\">\n<table>\n<tbody>\n<tr>\n<th>\n<p align=\"left\">\u0421\u0438\u0442\u0443\u0430\u0446\u0438\u044f<\/p>\n<\/th>\n<th>\n<p align=\"left\">\u041b\u0443\u0447\u0448\u0438\u0439 \u0432\u044b\u0431\u043e\u0440<\/p>\n<\/th>\n<\/tr>\n<tr>\n<td>\n<p align=\"left\">\u0421\u0442\u0430\u0431\u0438\u043b\u044c\u043d\u044b\u0439, \u0434\u043b\u0438\u043d\u043d\u044b\u0439 \u0440\u044f\u0434 \u0431\u0435\u0437 \u0440\u0435\u0437\u043a\u0438\u0445 \u0440\u0435\u0436\u0438\u043c-\u0448\u0430\u0431\u043e\u0432<\/p>\n<\/td>\n<td>\n<p align=\"left\"><code>TimeSeriesSplit<\/code><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p align=\"left\">\u041d\u0443\u0436\u043d\u043e \u0430\u043a\u043a\u0443\u043c\u0443\u043b\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0432\u0441\u044e \u0438\u0441\u0442\u043e\u0440\u0438\u044e, \u043c\u0435\u0442\u0440\u0438\u043a\u0438 \u043f\u043e \u043e\u0434\u0438\u043d\u0430\u043a\u043e\u0432\u044b\u043c \u0433\u043e\u0440\u0438\u0437\u043e\u043d\u0442\u0430\u043c<\/p>\n<\/td>\n<td>\n<p align=\"left\"><code>ExpandingWindowSplitter<\/code><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p align=\"left\">\u0415\u0441\u0442\u044c \u043f\u043e\u0434\u043e\u0437\u0440\u0435\u043d\u0438\u0435 \u043d\u0430 drifts\/structural breaks, \u0432\u0430\u0436\u043d\u0430 \u0441\u0432\u0435\u0436\u0435\u0441\u0442\u044c \u0434\u0430\u043d\u043d\u044b\u0445<\/p>\n<\/td>\n<td>\n<p align=\"left\"><code>RollingWindowCV<\/code> <\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p align=\"left\">\u0412 \u0441\u0435\u0440\u0438\u044e \u0432\u043c\u0435\u0448\u0438\u0432\u0430\u044e\u0442\u0441\u044f \u043b\u0430\u0433\u043e\u0432\u044b\u0435 \u0444\u0438\u0447\u0438 \u2192 \u0440\u0438\u0441\u043a \u0443\u0442\u0435\u0447\u043a\u0438<\/p>\n<\/td>\n<td>\n<p align=\"left\">\u043b\u044e\u0431\u043e\u0439 \u0438\u0437 \u0442\u0440\u0451\u0445 <strong>\u0441 <\/strong><code><strong>gap &gt; max(lag)<\/strong><\/code><\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n<p><strong>\u0418\u0442\u043e\u0433:<\/strong> \u0432\u044b\u0431\u0438\u0440\u0430\u044f \u0441\u043f\u043b\u0438\u0442\u0442\u0435\u0440, \u0437\u0430\u0434\u0430\u0432\u0430\u0439 \u0441\u0435\u0431\u0435 \u0434\u0432\u0430 \u0432\u043e\u043f\u0440\u043e\u0441\u0430:<\/p>\n<ol>\n<li>\n<p><strong>\u041d\u0430\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0434\u0430\u043b\u0435\u043a\u043e \u043c\u043e\u0435\u043c\u0443 \u0431\u0438\u0437\u043d\u0435\u0441\u0443 \u0432\u0430\u0436\u043d\u043e \u043f\u043e\u043c\u043d\u0438\u0442\u044c \u043f\u0440\u043e\u0448\u043b\u043e\u0435?<\/strong><\/p>\n<\/li>\n<li>\n<p><strong>\u041a\u0430\u043a \u0447\u0430\u0441\u0442\u043e \u043f\u043e\u0432\u0435\u0434\u0435\u043d\u0438\u0435 \u0440\u044f\u0434\u0430 \u043c\u0435\u043d\u044f\u0435\u0442\u0441\u044f \u0440\u0430\u0434\u0438\u043a\u0430\u043b\u044c\u043d\u043e?<\/strong><\/p>\n<\/li>\n<\/ol>\n<h3>\u0410\u0433\u0440\u0435\u0433\u0430\u0442\u044b \u0438 \u043b\u0430\u0433\u0438: \u0433\u0434\u0435 \u043f\u0440\u044f\u0447\u0435\u0442\u0441\u044f leakage<\/h3>\n<h4>\u041a\u043e\u0432\u0430\u0440\u043d\u044b\u0435 \u0430\u0433\u0440\u0435\u0433\u0430\u0442\u044b<\/h4>\n<p>\u0421\u0440\u0435\u0434\u043d\u0435\u0435 \u0437\u0430 <strong>\u0431\u0443\u0434\u0443\u0449\u0438\u0435<\/strong> 7 \u0434\u043d\u0435\u0439 \u0432 \u0442\u0440\u0435\u043d\u0438\u0440\u043e\u0432\u043a\u0435 \u0434\u0430\u0451\u0442 \u043c\u043e\u0434\u0435\u043b\u0438 \u0438\u043d\u0444\u0443, \u043d\u0435\u0434\u043e\u0441\u0442\u0443\u043f\u043d\u0443\u044e \u043d\u0430 \u043c\u043e\u043c\u0435\u043d\u0442 \u043f\u0440\u043e\u0433\u043d\u043e\u0437\u0430. \u041a\u043b\u0430\u0441\u0441\u0438\u043a\u0430:<\/p>\n<pre><code class=\"python\">df['mean_7d'] = df['sales'].rolling(7).mean()  # \u043d\u0443\u0436\u043d\u043e .shift(1)!<\/code><\/pre>\n<p>\u041e\u0441\u0435\u0447\u043a\u0430: \u0431\u0435\u0437 <code>shift(1)<\/code> \u043e\u043a\u043d\u043e \u0437\u0430\u0433\u043b\u044f\u0434\u044b\u0432\u0430\u0435\u0442 \u043d\u0430 \u0442\u0435\u043a\u0443\u0449\u0438\u0439 \u0434\u0435\u043d\u044c, \u0441\u043e\u0432\u043f\u0430\u0434\u0430\u044e\u0449\u0438\u0439 \u0441 \u043b\u0435\u0439\u0431\u043b\u043e\u043c \u2014 \u043f\u0440\u0438\u0432\u0435\u0442, data leakage. \u0422\u0430\u043a\u0430\u044f \u043e\u0448\u0438\u0431\u043a\u0430 \u0441\u043f\u043e\u0441\u043e\u0431\u043d\u0430 \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c +15\u2026+40 pp <code>MAPE<\/code> \u0440\u0430\u0437\u043d\u0438\u0446\u044b \u043c\u0435\u0436\u0434\u0443 offline \u0438 prod.<\/p>\n<h4>\u00ab\u0426\u0435\u043b\u0435\u0432\u044b\u0435\u00bb \u043b\u0430\u0433\u0438<\/h4>\n<p>\u041f\u0440\u0430\u0432\u0438\u043b\u043e \u043f\u0440\u043e\u0441\u0442\u043e\u0435: <strong>\u0432\u0441\u0435<\/strong> \u043f\u0440\u0438\u0437\u043d\u0430\u043a\u0438, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0449\u0438\u0435 <code>y<\/code>, \u0434\u043e\u043b\u0436\u043d\u044b \u0431\u044b\u0442\u044c \u0440\u0430\u0441\u0441\u0447\u0438\u0442\u0430\u043d\u044b \u0442\u0430\u043a, \u0447\u0442\u043e\u0431\u044b \u043c\u043e\u0434\u0435\u043b\u044c \u0432\u0438\u0434\u0435\u043b\u0430 \u043f\u0440\u043e\u0448\u043b\u043e\u0435, \u0430 \u043d\u0435 \u0431\u0443\u0434\u0443\u0449\u0435\u0435.<\/p>\n<pre><code class=\"python\">for lag in range(1, 8):     df[f'sales_lag_{lag}'] = df['sales'].shift(lag)<\/code><\/pre>\n<p>\u041f\u0440\u043e\u0432\u0435\u0440\u043a\u0430: \u043d\u0438\u043a\u0430\u043a\u0438\u0445 <code>NaN<\/code> \u0432 train-\u043e\u0442\u0440\u0435\u0437\u043a\u0435? \u0417\u043d\u0430\u0447\u0438\u0442 shift \u043e\u043a.<\/p>\n<h3>\u0412\u0430\u043b\u0438\u0434\u0438\u0440\u0443\u0435\u043c \u0444\u0438\u0447\u0438 \u0441 look-ahead \u2014 \u043f\u043e\u0448\u0430\u0433\u043e\u0432\u043e<\/h3>\n<ol>\n<li>\n<p><strong>\u0421\u043e\u0431\u0438\u0440\u0430\u0435\u043c \u0444\u0438\u0447\u0438 \u0432 \u043e\u0434\u043d\u043e\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u0438<\/strong> (\u0447\u0442\u043e\u0431\u044b \u0433\u0430\u0440\u0430\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c).<\/p>\n<\/li>\n<li>\n<p><strong>\u041e\u0431\u043e\u0440\u0430\u0447\u0438\u0432\u0430\u0435\u043c \u0432 <\/strong><code><strong>FunctionTransformer<\/strong><\/code> \u2014 \u0442\u043e\u0433\u0434\u0430 pipeline \u043f\u043e\u0437\u0430\u0431\u043e\u0442\u0438\u0442\u0441\u044f \u043e \u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u043e\u043c \u0440\u0430\u0441\u0447\u0451\u0442\u0435 \u0432\u043d\u0443\u0442\u0440\u0438 \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u0444\u043e\u043b\u0434\u0430.<\/p>\n<\/li>\n<li>\n<p><strong>\u041f\u0438\u0448\u0435\u043c unit-\u0442\u0435\u0441\u0442<\/strong>: \u0431\u0435\u0440\u0451\u043c \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u0439 train-\u0438\u043d\u0434\u0435\u043a\u0441 \u0438 \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u043c, \u0447\u0442\u043e \u0432\u0441\u0435 \u0434\u0430\u0442\u044b \u0444\u0438\u0447\u0435\u0439 \u2264 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0435\u0439 \u0434\u0430\u0442\u044b \u0442\u0440\u0435\u043d\u0438\u0440\u043e\u0432\u043a\u0438 (\u0440\u0443\u0447\u043d\u043e\u0439 assert \u0441\u043f\u0430\u0441\u0451\u0442 \u043e\u0442 \u043d\u043e\u0447\u043d\u044b\u0445 \u0440\u0435\u043b\u0438\u0437\u043e\u0432-\u0443\u0431\u0438\u0439\u0446).<\/p>\n<\/li>\n<\/ol>\n<pre><code class=\"python\">import pandas as pd from sklearn.preprocessing import FunctionTransformer from sklearn.pipeline import Pipeline from sklearn.ensemble import RandomForestRegressor  def make_features(df: pd.DataFrame) -&gt; pd.DataFrame:     out = df.copy()     # \u0431\u0435\u0437\u043e\u043f\u0430\u0441\u043d\u044b\u0439 \u043b\u0430\u0433     out['lag_1'] = out['target'].shift(1)     # rolling mean \u0431\u0435\u0437 \u0443\u0442\u0435\u0447\u043a\u0438     out['roll_mean_7'] = out['target'].shift(1).rolling(7).mean()     return out.drop(columns=['target'])  # \u043f\u0440\u0438\u0437\u043d\u0430\u043a\u0438 \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u043e  feature_maker = FunctionTransformer(make_features, validate=False)  pipe = Pipeline([     ('feat', feature_maker),     ('model', RandomForestRegressor(n_estimators=300, random_state=0)) ])<\/code><\/pre>\n<h3>\u0428\u0430\u0431\u043b\u043e\u043d pipeline \u0434\u043b\u044f time-series CV<\/h3>\n<p>\u041d\u0430\u043f\u0438\u0448\u0435\u043c \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u0438\u0441\u0442\u0438\u0447\u043d\u044b\u0439, \u043d\u043e \u0433\u043e\u0442\u043e\u0432\u0439 \u0441\u043a\u0435\u043b\u0435\u0442. \u041e\u043d:<\/p>\n<ul>\n<li>\n<p>\u0448\u0438\u0444\u0443\u0435\u0442 \u0434\u0430\u0442\u044b \u0432 \u0438\u043d\u0434\u0435\u043a\u0441,<\/p>\n<\/li>\n<li>\n<p>\u0441\u0442\u0440\u043e\u0438\u0442 \u0444\u0438\u0447\u0438 \u0431\u0435\u0437 \u0443\u0442\u0435\u0447\u0435\u043a,<\/p>\n<\/li>\n<li>\n<p>\u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442 <code>TimeSeriesSplit<\/code> (\u043c\u043e\u0436\u043d\u043e \u043f\u043e\u0434\u043c\u0435\u043d\u0438\u0442\u044c \u043d\u0430 \u043b\u044e\u0431\u043e\u0439 \u043d\u0430\u0448),<\/p>\n<\/li>\n<li>\n<p>\u0441\u0447\u0438\u0442\u0430\u0435\u0442 \u043c\u0435\u0442\u0440\u0438\u043a\u0438 \u043d\u0430 \u043a\u0440\u043e\u0441\u0441-\u0432\u0430\u043b\u0438\u0434\u0430\u0446\u0438\u0438 \u0438 <strong>\u043d\u0435<\/strong> \u0442\u0440\u043e\u0433\u0430\u0435\u0442 \u0442\u0435\u0441\u0442 \u0434\u043e \u0444\u0438\u043d\u0430\u043b\u0430.<\/p>\n<\/li>\n<\/ul>\n<pre><code class=\"python\">import pandas as pd from sklearn.metrics import mean_absolute_percentage_error as mape from sklearn.compose import ColumnTransformer from sklearn.preprocessing import StandardScaler from sklearn.model_selection import cross_val_score, TimeSeriesSplit  NUM_COLS = ['lag_1', 'roll_mean_7', 'promo']   # \u043f\u0440\u0438\u043c\u0435\u0440 CAT_COLS = ['store_id', 'item_id']  num_pipe = Pipeline([     ('scaler', StandardScaler(with_mean=False))  # with_mean=False safe \u0434\u043b\u044f \u0441\u043f\u0430\u0440\u0441 ])  preprocessor = ColumnTransformer([     ('num', num_pipe, NUM_COLS),     ('cat', 'passthrough', CAT_COLS) ])  full_pipe = Pipeline([     ('features', feature_maker),     ('prep',      preprocessor),     ('model',     GradientBoostingRegressor(random_state=0)) ])  tscv = TimeSeriesSplit(n_splits=5, test_size=90, gap=7)  cv_scores = cross_val_score(     full_pipe,     X=df,                # df \u0441 target \u0432\u043d\u0443\u0442\u0440\u0438     y=df['target'],     cv=tscv,     scoring='neg_mean_absolute_percentage_error',     n_jobs=-1 )  print('CV MAPE:', -cv_scores.mean())<\/code><\/pre>\n<p><code>gap=7<\/code> \u0441\u043e\u0437\u0434\u0430\u0451\u0442 \u0441\u0430\u043d\u0438\u0442\u0430\u0440\u043d\u0443\u044e \u0437\u043e\u043d\u0443: \u043c\u043e\u0434\u0435\u043b\u044c \u043d\u0435 \u0432\u0438\u0434\u0438\u0442 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0443\u044e \u043d\u0435\u0434\u0435\u043b\u044e \u0442\u0435\u0441\u0442\u0430, \u0433\u0434\u0435 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u044b \u043b\u0430\u0433\u0438.<\/p>\n<p><code>ColumnTransformer<\/code> \u2014 \u0434\u0440\u0443\u0436\u0438\u0442 \u0441 <code>pandas<\/code> \u0438 \u043d\u0435 \u0434\u0430\u0451\u0442 \u0441\u043b\u0443\u0447\u0430\u0439\u043d\u043e \u043f\u0440\u0438\u0442\u0430\u0449\u0438\u0442\u044c \u043a\u0430\u0442\u0435\u0433\u043e\u0440\u0438\u044e \u0432 \u043d\u043e\u0440\u043c\u0430\u043b\u0430\u0439\u0437\u0435\u0440.<\/p>\n<p><code>StandardScaler<\/code> \u0432\u043d\u0443\u0442\u0440\u0438 CV (!) \u2014 \u0441\u0442\u0430\u0442\u0438\u0441\u0442\u0438\u043a\u0438 \u0441\u0447\u0438\u0442\u0430\u044e\u0442\u0441\u044f <strong>\u0442\u043e\u043b\u044c\u043a\u043e<\/strong> \u043d\u0430 train-\u0447\u0430\u0441\u0442\u0438 \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u0444\u043e\u043b\u0434\u0430, \u0431\u043e\u0440\u0435\u043c\u0441\u044f \u0441 look-ahead bias, \u043e \u043a\u043e\u0442\u043e\u0440\u043e\u043c \u043b\u044e\u0431\u044f\u0442 \u043d\u0430\u043f\u043e\u043c\u0438\u043d\u0430\u0442\u044c \u043e\u043b\u0434\u044b \u043d\u0430 CrossValidated.<\/p>\n<p>\u0415\u0441\u043b\u0438 \u0443 \u0432\u0430\u0441 \u0435\u0441\u0442\u044c \u0441\u0432\u043e\u0439 \u0431\u043e\u0435\u0432\u043e\u0439 \u043e\u043f\u044b\u0442 \u0441 \u0432\u0430\u043b\u0438\u0434\u0430\u0446\u0438\u0435\u0439 \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0445 \u0440\u044f\u0434\u043e\u0432 \u2014 \u043e\u0441\u043e\u0431\u0435\u043d\u043d\u043e \u0432 \u0433\u0440\u044f\u0437\u043d\u044b\u0445 \u0438 \u043d\u0435\u0441\u0442\u0430\u0431\u0438\u043b\u044c\u043d\u044b\u0445 \u043f\u0440\u043e\u0435\u043a\u0442\u0430\u0445 \u2014 \u0440\u0430\u0441\u0441\u043a\u0430\u0436\u0438\u0442\u0435 \u0432 \u043a\u043e\u043c\u043c\u0435\u043d\u0442\u0430\u0440\u0438\u044f\u0445.<\/p>\n<hr\/>\n<p>\u0415\u0441\u043b\u0438 \u0432\u044b \u0445\u043e\u0442\u0438\u0442\u0435 \u0433\u043b\u0443\u0431\u0436\u0435 \u0440\u0430\u0437\u043e\u0431\u0440\u0430\u0442\u044c\u0441\u044f \u0432 \u0442\u043e\u043c, \u043a\u0430\u043a\u00a0\u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u043e \u043f\u043e\u0434\u0433\u043e\u0442\u0430\u0432\u043b\u0438\u0432\u0430\u0442\u044c \u0434\u0430\u043d\u043d\u044b\u0435 \u0434\u043b\u044f\u00a0\u043c\u0430\u0448\u0438\u043d\u043d\u043e\u0433\u043e \u043e\u0431\u0443\u0447\u0435\u043d\u0438\u044f \u0438 \u043a\u0430\u043a \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u043e \u043f\u0440\u0438\u043c\u0435\u043d\u044f\u0442\u044c \u043c\u043e\u0449\u043d\u044b\u0435 \u043c\u0435\u0442\u043e\u0434\u044b, \u0442\u0430\u043a\u0438\u0435 \u043a\u0430\u043a\u00a0Random Forest, \u043f\u0440\u0438\u0433\u043b\u0430\u0448\u0430\u0435\u043c \u0432\u0430\u0441 \u043d\u0430\u00a0\u0434\u0432\u0430 \u0431\u043b\u0438\u0436\u0430\u0439\u0448\u0438\u0445 \u043e\u0442\u043a\u0440\u044b\u0442\u044b\u0445 \u0443\u0440\u043e\u043a\u0430:<\/p>\n<ul>\n<li>\n<p><a href=\"https:\/\/otus.pw\/fqfw\/\" rel=\"noopener noreferrer nofollow\">\u00ab\u041a\u0430\u043a \u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u043e \u0433\u043e\u0442\u043e\u0432\u0438\u0442\u044c \u0434\u0430\u043d\u043d\u044b\u0435 \u0434\u043b\u044f\u00a0ML\u2011\u043c\u043e\u0434\u0435\u043b\u0435\u0439?\u00bb<\/a>\u00a0\u2014 <em>3\u00a0\u0438\u044e\u043b\u044f \u0432 18:00<\/em><\/p>\n<\/li>\n<li>\n<p>\u00ab<a href=\"https:\/\/otus.pw\/B58B\/\" rel=\"noopener noreferrer nofollow\">Random Forest\u00a0\u2014 \u043c\u043e\u0449\u043d\u044b\u0439 \u043c\u0435\u0442\u043e\u0434 \u0430\u043d\u0441\u0430\u043c\u0431\u043b\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u0432\u00a0ML<\/a>\u00bb\u00a0\u2014 16\u00a0\u0438\u044e\u043b\u044f \u0432 18:00<\/p>\n<\/li>\n<\/ul>\n<p>\u042d\u0442\u0438 \u0437\u0430\u043d\u044f\u0442\u0438\u044f \u043f\u043e\u043c\u043e\u0433\u0443\u0442 \u0432\u0430\u043c \u0438\u0437\u0431\u0435\u0436\u0430\u0442\u044c \u043a\u043b\u0430\u0441\u0441\u0438\u0447\u0435\u0441\u043a\u0438\u0445 \u043e\u0448\u0438\u0431\u043e\u043a \u043f\u0440\u0438\u00a0\u0440\u0430\u0431\u043e\u0442\u0435 \u0441\u00a0\u0434\u0430\u043d\u043d\u044b\u043c\u0438 \u0438 \u043d\u0430\u0443\u0447\u0430\u0442 \u0441\u0442\u0440\u043e\u0438\u0442\u044c \u043d\u0430\u0434\u0435\u0436\u043d\u044b\u0435 \u043c\u043e\u0434\u0435\u043b\u0438, \u0447\u0442\u043e\u00a0\u043e\u0441\u043e\u0431\u0435\u043d\u043d\u043e \u0432\u0430\u0436\u043d\u043e \u043f\u0440\u0438\u00a0\u0430\u043d\u0430\u043b\u0438\u0437\u0435 \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0445 \u0440\u044f\u0434\u043e\u0432.<\/p>\n<p>\u0421\u043b\u0435\u0434\u0438\u0442\u0435 \u0437\u0430\u00a0\u043f\u043e\u043b\u043d\u044b\u043c \u0440\u0430\u0441\u043f\u0438\u0441\u0430\u043d\u0438\u0435\u043c \u043e\u0442\u043a\u0440\u044b\u0442\u044b\u0445 \u0443\u0440\u043e\u043a\u043e\u0432 \u0432\u00a0\u043d\u0430\u0448\u0435\u043c <a href=\"https:\/\/otus.pw\/IQuW\/\" rel=\"noopener noreferrer nofollow\">\u043a\u0430\u043b\u0435\u043d\u0434\u0430\u0440\u0435<\/a>\u00a0\u2014 \u0442\u0430\u043c \u0432\u044b \u043d\u0430\u0439\u0434\u0435\u0442\u0435 \u0435\u0449\u0435 \u043c\u043d\u043e\u0433\u043e \u043f\u043e\u043b\u0435\u0437\u043d\u044b\u0445 \u043c\u0435\u0440\u043e\u043f\u0440\u0438\u044f\u0442\u0438\u0439 \u043f\u043e\u00a0Data Science \u0438 \u0441\u043c\u0435\u0436\u043d\u044b\u043c \u043d\u0430\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u044f\u043c. \u0410\u00a0\u0447\u0442\u043e\u0431\u044b \u0441\u0438\u0441\u0442\u0435\u043c\u043d\u043e \u043f\u0440\u043e\u043a\u0430\u0447\u0430\u0442\u044c \u043d\u0430\u0432\u044b\u043a\u0438, \u043e\u0437\u043d\u0430\u043a\u043e\u043c\u044c\u0442\u0435\u0441\u044c \u0441\u00a0\u043d\u0430\u0448\u0438\u043c <a href=\"https:\/\/otus.pw\/6l3Z\/\" rel=\"noopener noreferrer nofollow\">\u043a\u0430\u0442\u0430\u043b\u043e\u0433\u043e\u043c \u043a\u0443\u0440\u0441\u043e\u0432 \u043f\u043e\u00a0Data Science<\/a>\u00a0\u2014 \u0432\u044b\u0431\u0435\u0440\u0438\u0442\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0443, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u043f\u043e\u0434\u043e\u0439\u0434\u0435\u0442 \u0438\u043c\u0435\u043d\u043d\u043e \u0432\u0430\u043c.<\/p>\n<\/div>\n<\/div>\n<\/div>\n<p><!----><!----><\/div>\n<p><!----><!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/articles\/921604\/\"> https:\/\/habr.com\/ru\/articles\/921604\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p>\u041f\u0440\u0438\u0432\u0435\u0442, \u0425\u0430\u0431\u0440!<\/p>\n<p>\u0421\u0435\u0433\u043e\u0434\u043d\u044f \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u0442\u043e, \u0447\u0442\u043e \u0447\u0430\u0449\u0435 \u0432\u0441\u0435\u0433\u043e \u043b\u043e\u043c\u0430\u0435\u0442 \u0434\u0430\u0436\u0435 \u043a\u0440\u0443\u0442\u043e \u0432\u044b\u0433\u043b\u044f\u0434\u044f\u0449\u0438\u0435 \u043c\u043e\u0434\u0435\u043b\u0438 \u043f\u0440\u0438 \u0440\u0430\u0431\u043e\u0442\u0435 \u0441 \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u043c\u0438 \u0440\u044f\u0434\u0430\u043c\u0438 \u2014 <strong>\u043d\u0435\u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u0430\u044f \u043a\u0440\u043e\u0441\u0441-\u0432\u0430\u043b\u0438\u0434\u0430\u0446\u0438\u044f<\/strong>. \u0420\u0430\u0437\u0431\u0435\u0440\u0435\u043c, \u043f\u043e\u0447\u0435\u043c\u0443 <code>KFold<\/code> \u0442\u0443\u0442 \u043d\u0435 \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442, \u043a\u0430\u043a \u043b\u0435\u0433\u043a\u043e \u0441\u043b\u043e\u0432\u0438\u0442\u044c \u0443\u0442\u0435\u0447\u043a\u0443 \u0431\u0443\u0434\u0443\u0449\u0435\u0433\u043e, \u043a\u0430\u043a\u0438\u0435 \u0441\u043f\u043b\u0438\u0442\u0442\u0435\u0440\u044b \u0440\u0435\u0430\u043b\u044c\u043d\u043e \u0447\u0435\u0441\u0442\u043d\u044b \u043f\u043e \u043e\u0442\u043d\u043e\u0448\u0435\u043d\u0438\u044e \u043a\u043e \u0432\u0440\u0435\u043c\u0435\u043d\u0438, \u043a\u0430\u043a \u0432\u0430\u043b\u0438\u0434\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0444\u0438\u0447\u0438 \u0441 \u043b\u0430\u0433\u0430\u043c\u0438 \u0438 \u0430\u0433\u0440\u0435\u0433\u0430\u0442\u0430\u043c\u0438.<\/p>\n<h3>\u041f\u043e\u0447\u0435\u043c\u0443 KFold \u2014 \u043f\u043b\u043e\u0445\u0430\u044f \u0438\u0434\u0435\u044f \u0434\u043b\u044f time-series<\/h3>\n<p><code>KFold<\/code> \u2014 \u0448\u0442\u0443\u043a\u0430 \u043a\u043b\u0430\u0441\u0441\u043d\u0430\u044f\u2026 <strong>\u043d\u043e \u0442\u043e\u043b\u044c\u043a\u043e \u0435\u0441\u043b\u0438 \u0442\u0432\u043e\u0438 \u0434\u0430\u043d\u043d\u044b\u0435 \u043d\u0435 \u0437\u0430\u0432\u0438\u0441\u044f\u0442 \u043e\u0442 \u0432\u0440\u0435\u043c\u0435\u043d\u0438<\/strong>. \u041e\u043d \u0431\u044b\u043b \u0441\u043e\u0437\u0434\u0430\u043d \u0434\u043b\u044f \u043c\u0438\u0440\u0430, \u0433\u0434\u0435 \u043a\u0430\u0436\u0434\u044b\u0439 \u043e\u0431\u044a\u0435\u043a\u0442 \u043d\u0435\u0437\u0430\u0432\u0438\u0441\u0438\u043c. \u0414\u043b\u044f \u0437\u0430\u0434\u0430\u0447 \u043a\u043b\u0430\u0441\u0441\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u0438 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0439 \u0438\u043b\u0438 \u0430\u043d\u0430\u043b\u0438\u0437\u0430 \u0442\u0430\u0431\u043b\u0438\u0447\u043d\u044b\u0445 \u0434\u0430\u043d\u043d\u044b\u0445 <code>KFold<\/code> \u2014 \u043f\u043e\u0447\u0442\u0438 \u0440\u0435\u0444\u043b\u0435\u043a\u0441. \u041d\u043e \u0432\u043e\u0442 \u0438\u0434\u0435\u0442 \u0440\u0435\u0447\u044c \u043f\u0440\u043e \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0435 \u0440\u044f\u0434\u044b \u2014 \u0441\u043f\u0440\u043e\u0441 \u043d\u0430 \u0437\u0434\u0440\u0430\u0432\u044b\u0439 \u0441\u043c\u044b\u0441\u043b \u0440\u0435\u0437\u043a\u043e \u0432\u044b\u0440\u0430\u0441\u0442\u0430\u0435\u0442.<\/p>\n<p>\u0412\u0440\u0435\u043c\u044f \u0432 \u0442\u0430\u043a\u0438\u0445 \u0437\u0430\u0434\u0430\u0447\u0430\u0445 \u2014 \u043d\u0435 \u043f\u0440\u043e\u0441\u0442\u043e \u043a\u043e\u043b\u043e\u043d\u043a\u0430 <code>date<\/code>, \u043a\u043e\u0442\u043e\u0440\u0443\u044e \u043c\u043e\u0436\u043d\u043e \u0431\u0440\u043e\u0441\u0438\u0442\u044c \u0432 <code>dropna()<\/code> \u0438 \u0437\u0430\u0431\u044b\u0442\u044c. \u042d\u0442\u043e <strong>\u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442, \u043f\u0440\u0438\u0447\u0438\u043d\u043d\u043e-\u0441\u043b\u0435\u0434\u0441\u0442\u0432\u0435\u043d\u043d\u0430\u044f \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430<\/strong>.  \u0410 <code>KFold<\/code> \u0441 \u0435\u0433\u043e <code>shuffle=True<\/code> \u043e\u0442\u043d\u043e\u0441\u0438\u0442\u0441\u044f \u043a\u043e \u0432\u0440\u0435\u043c\u0435\u043d\u0438, \u043a\u0430\u043a \u043a \u0441\u043a\u0443\u0447\u043d\u043e\u0439 \u043f\u043e\u0434\u043f\u0438\u0441\u0438 \u043d\u0430 \u0433\u0440\u0430\u0444\u0438\u043a\u0435. \u041e\u043d \u043f\u0435\u0440\u0435\u043c\u0435\u0448\u0438\u0432\u0430\u0435\u0442 \u0434\u0430\u043d\u043d\u044b\u0435, \u043e\u0431\u0443\u0447\u0430\u044f \u043c\u043e\u0434\u0435\u043b\u044c \u043d\u0430 \u0431\u0443\u0434\u0443\u0449\u0435\u043c \u0438 \u0442\u0435\u0441\u0442\u0438\u0440\u0443\u044f \u043d\u0430 \u043f\u0440\u043e\u0448\u043b\u043e\u043c. \u0422\u043e \u0435\u0441\u0442\u044c \u0431\u0443\u043a\u0432\u0430\u043b\u044c\u043d\u043e \u0437\u0430\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 \u0435\u0451 \u0437\u043d\u0430\u0442\u044c \u0431\u0443\u0434\u0443\u0449\u0435\u0435, \u0447\u0442\u043e\u0431\u044b \u043f\u0440\u0435\u0434\u0441\u043a\u0430\u0437\u0430\u0442\u044c \u043f\u0440\u043e\u0448\u043b\u043e\u0435.<\/p>\n<p>\u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440:<\/p>\n<pre><code class=\"python\">from sklearn.model_selection import KFold, cross_val_score from sklearn.ensemble import RandomForestRegressor  cv = KFold(n_splits=5, shuffle=True, random_state=42) score = cross_val_score(RandomForestRegressor(), X, y, cv=cv)<\/code><\/pre>\n<p>\u0412\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u0430\u043a\u043a\u0443\u0440\u0430\u0442\u043d\u043e, \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u0431\u0435\u0437 \u043e\u0448\u0438\u0431\u043e\u043a \u2014 \u0430 \u0432\u043d\u0443\u0442\u0440\u0438 \u0441\u043a\u0440\u044b\u0432\u0430\u0435\u0442\u0441\u044f <strong>\u043f\u043e\u043b\u043d\u0435\u0439\u0448\u0435\u0435 \u043d\u0430\u0440\u0443\u0448\u0435\u043d\u0438\u0435 \u043f\u0440\u0438\u0447\u0438\u043d\u043d\u043e\u0441\u0442\u0438<\/strong>: \u043c\u043e\u0434\u0435\u043b\u044c \u0432\u0438\u0434\u0438\u0442 \u043a\u0443\u0441\u043a\u0438 \u0431\u0443\u0434\u0443\u0449\u0435\u0433\u043e, \u043a\u043e\u0433\u0434\u0430 \u043e\u0431\u0443\u0447\u0430\u0435\u0442\u0441\u044f, \u0438 \u043f\u043e\u0442\u043e\u043c \u0440\u0430\u0434\u0443\u0435\u0442\u0441\u044f &#171;\u043e, \u043a\u0430\u043a \u044f \u043a\u0440\u0443\u0442\u043e \u043f\u0440\u0435\u0434\u0441\u043a\u0430\u0437\u0430\u043b&#187;.<\/p>\n<p>\u0422\u043e\u043b\u044c\u043a\u043e \u0432\u043e\u0442 \u0442\u044b \u043d\u0435 \u043e\u0431\u0443\u0447\u0430\u043b \u043c\u043e\u0434\u0435\u043b\u044c \u2014 \u0442\u044b \u0441\u043a\u043e\u0440\u043c\u0438\u043b \u0435\u0439 \u0431\u0443\u0434\u0443\u0449\u0443\u044e \u043f\u0440\u0430\u0432\u0434\u0443, \u043e\u043d\u0430 \u0435\u0451 \u0432\u044b\u0443\u0447\u0438\u043b\u0430 \u0438 \u043f\u043e\u0442\u043e\u043c \u043f\u0440\u043e\u0441\u0442\u043e \u0435\u0451 \u0432\u043e\u0441\u043f\u0440\u043e\u0438\u0437\u0432\u0435\u043b\u0430. <\/p>\n<p><strong>\u0412\u0441\u044f \u0441\u0443\u0442\u044c \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0433\u043e \u0440\u044f\u0434\u0430 \u0432 \u0442\u043e\u043c, \u0447\u0442\u043e \u0442\u044b \u043d\u0435 \u043c\u043e\u0436\u0435\u0448\u044c \u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c \u0432\u043f\u0435\u0440\u0451\u0434<\/strong>. \u042d\u0442\u043e \u0438 \u0434\u0435\u043b\u0430\u0435\u0442 \u0437\u0430\u0434\u0430\u0447\u0443 \u0441\u043b\u043e\u0436\u043d\u043e\u0439. \u041c\u044b \u043c\u043e\u0434\u0435\u043b\u0438\u0440\u0443\u0435\u043c \u0440\u0435\u0430\u043b\u044c\u043d\u043e\u0441\u0442\u044c, \u0433\u0434\u0435 \u0437\u0430\u0432\u0442\u0440\u0430 \u0442\u044b \u043f\u0440\u0435\u0434\u0441\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0448\u044c, \u0438\u043c\u0435\u044f \u043c\u0430\u043a\u0441\u0438\u043c\u0443\u043c \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044e \u043d\u0430 \u0432\u0447\u0435\u0440\u0430 \u0438 \u0447\u0443\u0442\u044c-\u0447\u0443\u0442\u044c \u043d\u0430 \u0441\u0435\u0433\u043e\u0434\u043d\u044f.<\/p>\n<p>\u041f\u043e\u044d\u0442\u043e\u043c\u0443 \u043c\u0435\u0442\u0440\u0438\u043a\u0438 \u0431\u0443\u0434\u0443\u0442 \u043b\u0436\u0438\u0432\u044b\u043c\u0438, \u0430 \u0444\u0438\u0447\u0438 \u0441 look-ahead \u044d\u0444\u0444\u0435\u043a\u0442\u043e\u043c \u043f\u0440\u043e\u0439\u0434\u0443\u0442 \u043d\u0435\u0437\u0430\u043c\u0435\u0447\u0435\u043d\u043d\u044b\u043c\u0438. <\/p>\n<h3>\u0427\u0442\u043e \u0434\u0435\u043b\u0430\u0442\u044c \u0432\u043c\u0435\u0441\u0442\u043e<\/h3>\n<h4>TimeSeriesSplit<\/h4>\n<p>\u041a\u0430\u0436\u0434\u044b\u0439 \u0444\u043e\u043b\u0434 \u0431\u0435\u0440\u0451\u0442 <strong>\u0432\u0441\u0451, \u0447\u0442\u043e \u043d\u0430\u043a\u043e\u043f\u0438\u043b\u043e\u0441\u044c \u0440\u0430\u043d\u044c\u0448\u0435<\/strong>, \u0438 \u043f\u0440\u0438\u0441\u0442\u0451\u0433\u0438\u0432\u0430\u0435\u0442 \u043e\u0447\u0435\u0440\u0435\u0434\u043d\u043e\u0439 \u0442\u0435\u0441\u0442-\u043e\u0442\u0440\u0435\u0437\u043e\u043a \u0441\u043f\u0440\u0430\u0432\u0430:<\/p>\n<pre><code class=\"python\">from sklearn.model_selection import TimeSeriesSplit  tscv = TimeSeriesSplit(     n_splits=5,          # \u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0440\u0430\u0437 \u00ab\u043a\u0430\u0442\u043d\u0451\u043c\u00bb \u0432\u0430\u043b\u0438\u0434\u0430\u0446\u0438\u044e     test_size=90,        # \u0434\u043b\u0438\u043d\u0430 \u0442\u0435\u0441\u0442\u0430 \u0432 \u0434\u043d\u044f\u0445\/\u0447\u0430\u0441\u0430\u0445\/\u043c\u0438\u043d\u0443\u0442\u043a\u0430\u0445     gap=7,               # \u0431\u0443\u0444\u0435\u0440 \u043c\u0435\u0436\u0434\u0443 train \u0438 test, \u0441\u043f\u0430\u0441\u0430\u0435\u0442 \u043e\u0442 look-ahead \u043f\u0440\u0438 \u043b\u0430\u0433\u0430\u0445 )  for tr, ts in tscv.split(data):     print(data.index[tr][0], '\u2026', data.index[tr][-1], '\u2192', data.index[ts][0], '\u2026', data.index[ts][-1])<\/code><\/pre>\n<p><code>gap<\/code> \u043f\u043e\u044f\u0432\u0438\u043b\u0441\u044f \u0432 \u0432\u0435\u0440\u0441\u0438\u0438 0.24 \u0438 \u0434\u0430\u0451\u0442 \u043c\u043e\u0434\u0435\u043b\u0438 \u00ab\u0441\u0430\u043d\u0438\u0442\u0430\u0440\u043d\u0443\u044e \u0437\u043e\u043d\u0443\u00bb, \u0435\u0441\u043b\u0438 \u0432\u044b \u0441\u0442\u0440\u043e\u0438\u0442\u0435 \u043f\u0440\u0438\u0437\u043d\u0430\u043a\u0438, \u0437\u0430\u0433\u043b\u044f\u0434\u044b\u0432\u0430\u044e\u0449\u0438\u0435 \u043d\u0430 \u043e\u0434\u0438\u043d-\u0434\u0432\u0430 \u0448\u0430\u0433\u0430 \u0432\u043f\u0435\u0440\u0451\u0434.<\/p>\n<p><strong>\u041f\u043b\u044e\u0441\u044b<\/strong><\/p>\n<ul>\n<li>\n<p>\u0432\u0441\u0442\u0440\u043e\u0435\u043d\u043e \u0432 <code>sklearn<\/code>, \u0437\u043d\u0430\u0447\u0438\u0442 \u2014 \u043d\u0438\u043a\u0430\u043a\u043e\u0433\u043e \u0432\u043d\u0435\u0448\u043d\u0435\u0433\u043e \u0437\u043e\u043e\u043f\u0430\u0440\u043a\u0430;<\/p>\n<\/li>\n<li>\n<p>\u0440\u0430\u0441\u0442\u0443\u0449\u0430\u044f \u0438\u0441\u0442\u043e\u0440\u0438\u044f \u043f\u0440\u0430\u0432\u0434\u043e\u043f\u043e\u0434\u043e\u0431\u043d\u0430 \u0434\u043b\u044f \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u043e\u0432, \u0433\u0434\u0435 \u0434\u0430\u043d\u043d\u044b\u0435 <em>\u0442\u043e\u043b\u044c\u043a\u043e \u043f\u0440\u0438\u0431\u0430\u0432\u043b\u044f\u044e\u0442\u0441\u044f<\/em>.<\/p>\n<\/li>\n<\/ul>\n<p><strong>\u041c\u0438\u043d\u0443\u0441\u044b<\/strong><\/p>\n<ul>\n<li>\n<p>\u043c\u043e\u0434\u0435\u043b\u044c \u0442\u0430\u0449\u0438\u0442 \u0432 Train <strong>\u0432\u0441\u044e \u0434\u0440\u0435\u0432\u043d\u043e\u0441\u0442\u044c<\/strong>; \u043f\u0440\u0438 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u043d\u044b\u0445 \u0441\u0434\u0432\u0438\u0433\u0430\u0445 (\u0441\u043c. \u044d\u043a\u043e\u043d\u043e\u043c\u0435\u0442\u0440\u0438\u043a\u0443 \u043f\u0440\u043e structural breaks) \u044d\u0442\u043e \u043f\u0440\u0438\u0432\u043e\u0434\u0438\u0442 \u043a \u0434\u0435\u0433\u0440\u0430\u0434\u0430\u0446\u0438\u0438;<\/p>\n<\/li>\n<li>\n<p>\u043d\u0435\u0442 \u043f\u0440\u044f\u043c\u043e\u0439 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u0438 \u0437\u0430\u0431\u044b\u0442\u044c \u0441\u0442\u0430\u0440\u044b\u0435 \u044d\u043f\u043e\u0445\u0438, \u0435\u0441\u043b\u0438 \u043e\u043d\u0438 \u043c\u0435\u0448\u0430\u044e\u0442.<\/p>\n<\/li>\n<\/ul>\n<p>\u041c\u043e\u0436\u043d\u043e \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0438\u0442\u044c <code>max_train_size<\/code>, \u0447\u0442\u043e\u0431\u044b \u043d\u0435 \u0441\u043a\u0430\u0440\u043c\u043b\u0438\u0432\u0430\u0442\u044c \u043c\u043e\u0434\u0435\u043b\u0438 \u0434\u043e\u0438\u0441\u0442\u043e\u0440\u0438\u0447\u0435\u0441\u043a\u0438\u0445 \u043c\u0430\u043c\u043e\u043d\u0442\u043e\u0432 \u2014 \u0438 \u0443\u0441\u043a\u043e\u0440\u0438\u0448\u044c \u043e\u0431\u0443\u0447\u0435\u043d\u0438\u0435, \u0438 \u0437\u0430\u0449\u0438\u0442\u0438\u0448\u044c\u0441\u044f \u043e\u0442 \u0441\u043b\u043e\u043c\u0430\u043d\u043d\u044b\u0445 \u0437\u0430\u043a\u043e\u043d\u043e\u043c\u0435\u0440\u043d\u043e\u0441\u0442\u0435\u0439.<\/p>\n<h3>Expanding window<\/h3>\n<p><code>TimeSeriesSplit<\/code> \u043e\u043a, \u043d\u043e \u0438\u043d\u043e\u0433\u0434\u0430 \u043d\u0443\u0436\u043d\u0430 \u0442\u043e\u043d\u043a\u0430\u044f \u043d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0430 \u2014 \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u043e \u0437\u0430\u0434\u0430\u0442\u044c <strong>\u043f\u0435\u0440\u0432\u044b\u0435 X \u0442\u043e\u0447\u0435\u043a<\/strong>, \u043f\u043e\u0442\u043e\u043c \u0441\u0434\u0432\u0438\u0433\u0430\u0442\u044c\u0441\u044f \u043f\u043e <strong>\u043c\u0435\u0441\u044f\u0446\u0430\u043c<\/strong>, \u0430 horizon \u043e\u0441\u0442\u0430\u0432\u0438\u0442\u044c \u0432 <strong>N \u0434\u043d\u0435\u0439<\/strong>. \u0422\u0443\u0442 \u0437\u0430\u0445\u043e\u0434\u0438\u0442 <code>sktime<\/code>:<\/p>\n<pre><code class=\"python\">from sktime.split import ExpandingWindowSplitter  splitter = ExpandingWindowSplitter(     initial_window=365*2,   # 2 \u0433\u043e\u0434\u0430 \u2014 \u043f\u0435\u0440\u0432\u044b\u0439 train     step_length=30,         # \u043a\u0430\u0436\u0434\u044b\u0435 30 \u0442\u043e\u0447\u0435\u043a\/\u0434\u043d\u0435\u0439 \u0441\u0434\u0432\u0438\u0433\u0430\u0435\u043c\u0441\u044f     fh=[1, 7, 30],          # \u043f\u0440\u043e\u0433\u043d\u043e\u0437\u0438\u0440\u0443\u0435\u043c \u0437\u0430\u0432\u0442\u0440\u0430, \u0447\u0435\u0440\u0435\u0437 \u043d\u0435\u0434\u0435\u043b\u044e \u0438 \u0447\u0435\u0440\u0435\u0437 \u043c\u0435\u0441\u044f\u0446 )<\/code><\/pre>\n<p>\u041a\u043b\u0430\u0441\u0441\u0438\u043a\u0430 \u0434\u043b\u044f \u044d\u043a\u043e\u043d\u043e\u043c\u0435\u0442\u0440\u0438\u043a\u0438: \u0442\u0440\u0435\u043d\u0438\u0440\u0443\u0435\u043c\u0441\u044f \u043d\u0430 \u0432\u0441\u0451\u043c, \u0447\u0442\u043e \u0431\u044b\u043b\u043e, \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u044e\u044e \u043f\u043e\u0440\u0446\u0438\u044e \u0434\u0430\u043d\u043d\u044b\u0445, \u043f\u0440\u0435\u0434\u0441\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u043c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 \u0433\u043e\u0440\u0438\u0437\u043e\u043d\u0442. \u0423\u0434\u043e\u0431\u043d\u043e \u0434\u043b\u044f \u0431\u0435\u043d\u0447\u043c\u0430\u0440\u043a\u043e\u0432: \u0432\u0441\u0435 \u0444\u043e\u043b\u0434\u044b \u0438\u043c\u0435\u044e\u0442 <strong>\u043e\u0434\u0438\u043d\u0430\u043a\u043e\u0432\u044b\u0439<\/strong> \u0442\u0435\u0441\u0442-\u0440\u0430\u0437\u043c\u0435\u0440, \u0441\u0442\u0430\u0442\u0438\u0441\u0442\u0438\u043a\u0430 \u043c\u0435\u0442\u0440\u0438\u043a \u0447\u0438\u0449\u0435.<\/p>\n<p>\u041d\u043e \u043f\u0440\u0438 <strong>\u043d\u0435\u0441\u0442\u0430\u0446\u0438\u043e\u043d\u0430\u0440\u043d\u044b\u0445<\/strong> \u0440\u044f\u0434\u0430\u0445 \u043e\u0448\u0438\u0431\u043a\u0438 \u0441\u0442\u0430\u0440\u044b\u0445 \u044d\u043f\u043e\u0445 \u043c\u043e\u0433\u0443\u0442 \u0442\u044f\u043d\u0443\u0442\u044c \u043c\u043e\u0434\u0435\u043b\u044c \u0432\u043d\u0438\u0437. \u0417\u0434\u0435\u0441\u044c \u0441\u043f\u0430\u0441\u0430\u0435\u0442 \u043b\u0438\u0431\u043e \u0440\u0435\u0433\u0443\u043b\u044f\u0440\u0438\u0437\u0430\u0446\u0438\u044f, \u043b\u0438\u0431\u043e\u2026 \u043d\u0430\u0448 \u0442\u0440\u0435\u0442\u0438\u0439 \u0441\u043f\u043b\u0438\u0442\u0442\u0435\u0440.<\/p>\n<h3>Rolling (Sliding) window<\/h3>\n<p>\u041a\u043e\u0433\u0434\u0430 \u043d\u0443\u0436\u043d\u043e <strong>\u0437\u0430\u0431\u044b\u0432\u0430\u0442\u044c \u0441\u0442\u0430\u0440\u043e\u0435<\/strong> \u2014 \u0434\u0435\u0440\u0436\u0438\u043c train-\u043e\u043a\u043d\u043e \u0444\u0438\u043a\u0441\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0439 \u0434\u043b\u0438\u043d\u044b \u0438 \u043a\u0430\u0442\u0438\u043c \u0435\u0433\u043e \u0432\u043f\u0440\u0430\u0432\u043e.<\/p>\n<p>\u0412 <code>sklearn<\/code> \u043f\u043e\u043a\u0430 \u043d\u0435\u0442 \u0440\u043e\u0434\u043d\u043e\u0433\u043e \u043a\u043b\u0430\u0441\u0441\u0430, \u043d\u043e BaseCrossValidator \u043f\u0438\u0441\u0430\u0442\u044c \u043f\u0440\u043e\u0449\u0435, \u0447\u0435\u043c SQL \u0441 \u0442\u0440\u0435\u043c\u044f <code>JOIN<\/code>-\u0430\u043c\u0438:<\/p>\n<pre><code class=\"python\">from sklearn.model_selection import BaseCrossValidator import numpy as np  class RollingWindowCV(BaseCrossValidator):     def __init__(self, window, horizon, step=1):         self.window, self.horizon, self.step = window, horizon, step      def split(self, X, y=None, groups=None):         n = len(X)         for start in range(0, n - self.window - self.horizon + 1, self.step):             train = np.arange(start, start + self.window)             test  = np.arange(start + self.window, start + self.window + self.horizon)             yield train, test      def get_n_splits(self, X=None, y=None, groups=None):         return (len(X) - self.window - self.horizon) \/\/ self.step + 1<\/code><\/pre>\n<p>\u0418\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 \u043e\u0434\u0438\u043d-\u0432-\u043e\u0434\u0438\u043d \u0441\u043e \u0432\u0441\u0435\u043c\u0438 <code>cross_val_score<\/code>, <code>GridSearchCV<\/code> \u0438 \u0434\u0440\u0443\u0437\u044c\u044f\u043c\u0438:<\/p>\n<pre><code class=\"python\">cv = RollingWindowCV(window=365, horizon=30, step=30) scores = cross_val_score(model, X, y, cv=cv, scoring='neg_mean_absolute_error')<\/code><\/pre>\n<p>\u041c\u043e\u0434\u0435\u043b\u044c \u0432\u0441\u0435\u0433\u0434\u0430 \u0441\u043c\u043e\u0442\u0440\u0438\u0442 \u0442\u043e\u043b\u044c\u043a\u043e \u043d\u0430 \u00ab\u0430\u043a\u0442\u0443\u0430\u043b\u044c\u043d\u044b\u0439\u00bb \u0433\u043e\u0434 (\u0438\u043b\u0438 \u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0437\u0430\u0434\u0430\u0448\u044c) \u2014 \u0440\u0435\u0437\u043a\u0430\u044f \u0441\u043c\u0435\u043d\u0430 \u0442\u0440\u0435\u043d\u0434\u043e\u0432 \u043d\u0435 \u043f\u0440\u043e\u0431\u0438\u0432\u0430\u0435\u0442. \u041a\u043e\u043b\u0447\u0435\u0441\u0442\u0432\u043e \u0444\u043e\u043b\u0434\u043e\u0432 \u043e\u0431\u044b\u0447\u043d\u043e \u043c\u0435\u043d\u044c\u0448\u0435, \u0447\u0435\u043c \u0443 expanding window \u0438 \u043e\u0442 \u044d\u0442\u043e\u0433\u043e \u0441\u043b\u043e\u0436\u043d\u0435\u0435 \u043e\u0446\u0435\u043d\u0438\u0432\u0430\u0442\u044c \u0440\u0430\u0437\u0431\u0440\u043e\u0441 \u043c\u0435\u0442\u0440\u0438\u043a.<\/p>\n<h3>\u041a\u043e\u0433\u0434\u0430 \u043a\u0430\u043a\u043e\u0439 \u0441\u043f\u043b\u0438\u0442\u0442\u0435\u0440 \u0431\u0440\u0430\u0442\u044c<\/h3>\n<div>\n<div class=\"table\">\n<table>\n<tbody>\n<tr>\n<th>\n<p align=\"left\">\u0421\u0438\u0442\u0443\u0430\u0446\u0438\u044f<\/p>\n<\/th>\n<th>\n<p align=\"left\">\u041b\u0443\u0447\u0448\u0438\u0439 \u0432\u044b\u0431\u043e\u0440<\/p>\n<\/th>\n<\/tr>\n<tr>\n<td>\n<p align=\"left\">\u0421\u0442\u0430\u0431\u0438\u043b\u044c\u043d\u044b\u0439, \u0434\u043b\u0438\u043d\u043d\u044b\u0439 \u0440\u044f\u0434 \u0431\u0435\u0437 \u0440\u0435\u0437\u043a\u0438\u0445 \u0440\u0435\u0436\u0438\u043c-\u0448\u0430\u0431\u043e\u0432<\/p>\n<\/td>\n<td>\n<p align=\"left\"><code>TimeSeriesSplit<\/code><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p align=\"left\">\u041d\u0443\u0436\u043d\u043e \u0430\u043a\u043a\u0443\u043c\u0443\u043b\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0432\u0441\u044e \u0438\u0441\u0442\u043e\u0440\u0438\u044e, \u043c\u0435\u0442\u0440\u0438\u043a\u0438 \u043f\u043e \u043e\u0434\u0438\u043d\u0430\u043a\u043e\u0432\u044b\u043c \u0433\u043e\u0440\u0438\u0437\u043e\u043d\u0442\u0430\u043c<\/p>\n<\/td>\n<td>\n<p align=\"left\"><code>ExpandingWindowSplitter<\/code><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p align=\"left\">\u0415\u0441\u0442\u044c \u043f\u043e\u0434\u043e\u0437\u0440\u0435\u043d\u0438\u0435 \u043d\u0430 drifts\/structural breaks, \u0432\u0430\u0436\u043d\u0430 \u0441\u0432\u0435\u0436\u0435\u0441\u0442\u044c \u0434\u0430\u043d\u043d\u044b\u0445<\/p>\n<\/td>\n<td>\n<p align=\"left\"><code>RollingWindowCV<\/code> <\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p align=\"left\">\u0412 \u0441\u0435\u0440\u0438\u044e \u0432\u043c\u0435\u0448\u0438\u0432\u0430\u044e\u0442\u0441\u044f \u043b\u0430\u0433\u043e\u0432\u044b\u0435 \u0444\u0438\u0447\u0438 \u2192 \u0440\u0438\u0441\u043a \u0443\u0442\u0435\u0447\u043a\u0438<\/p>\n<\/td>\n<td>\n<p align=\"left\">\u043b\u044e\u0431\u043e\u0439 \u0438\u0437 \u0442\u0440\u0451\u0445 <strong>\u0441 <\/strong><code><strong>gap &gt; max(lag)<\/strong><\/code><\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n<p><strong>\u0418\u0442\u043e\u0433:<\/strong> \u0432\u044b\u0431\u0438\u0440\u0430\u044f \u0441\u043f\u043b\u0438\u0442\u0442\u0435\u0440, \u0437\u0430\u0434\u0430\u0432\u0430\u0439 \u0441\u0435\u0431\u0435 \u0434\u0432\u0430 \u0432\u043e\u043f\u0440\u043e\u0441\u0430:<\/p>\n<ol>\n<li>\n<p><strong>\u041d\u0430\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0434\u0430\u043b\u0435\u043a\u043e \u043c\u043e\u0435\u043c\u0443 \u0431\u0438\u0437\u043d\u0435\u0441\u0443 \u0432\u0430\u0436\u043d\u043e \u043f\u043e\u043c\u043d\u0438\u0442\u044c \u043f\u0440\u043e\u0448\u043b\u043e\u0435?<\/strong><\/p>\n<\/li>\n<li>\n<p><strong>\u041a\u0430\u043a \u0447\u0430\u0441\u0442\u043e \u043f\u043e\u0432\u0435\u0434\u0435\u043d\u0438\u0435 \u0440\u044f\u0434\u0430 \u043c\u0435\u043d\u044f\u0435\u0442\u0441\u044f \u0440\u0430\u0434\u0438\u043a\u0430\u043b\u044c\u043d\u043e?<\/strong><\/p>\n<\/li>\n<\/ol>\n<h3>\u0410\u0433\u0440\u0435\u0433\u0430\u0442\u044b \u0438 \u043b\u0430\u0433\u0438: \u0433\u0434\u0435 \u043f\u0440\u044f\u0447\u0435\u0442\u0441\u044f leakage<\/h3>\n<h4>\u041a\u043e\u0432\u0430\u0440\u043d\u044b\u0435 \u0430\u0433\u0440\u0435\u0433\u0430\u0442\u044b<\/h4>\n<p>\u0421\u0440\u0435\u0434\u043d\u0435\u0435 \u0437\u0430 <strong>\u0431\u0443\u0434\u0443\u0449\u0438\u0435<\/strong> 7 \u0434\u043d\u0435\u0439 \u0432 \u0442\u0440\u0435\u043d\u0438\u0440\u043e\u0432\u043a\u0435 \u0434\u0430\u0451\u0442 \u043c\u043e\u0434\u0435\u043b\u0438 \u0438\u043d\u0444\u0443, \u043d\u0435\u0434\u043e\u0441\u0442\u0443\u043f\u043d\u0443\u044e \u043d\u0430 \u043c\u043e\u043c\u0435\u043d\u0442 \u043f\u0440\u043e\u0433\u043d\u043e\u0437\u0430. \u041a\u043b\u0430\u0441\u0441\u0438\u043a\u0430:<\/p>\n<pre><code class=\"python\">df['mean_7d'] = df['sales'].rolling(7).mean()  # \u043d\u0443\u0436\u043d\u043e .shift(1)!<\/code><\/pre>\n<p>\u041e\u0441\u0435\u0447\u043a\u0430: \u0431\u0435\u0437 <code>shift(1)<\/code> \u043e\u043a\u043d\u043e \u0437\u0430\u0433\u043b\u044f\u0434\u044b\u0432\u0430\u0435\u0442 \u043d\u0430 \u0442\u0435\u043a\u0443\u0449\u0438\u0439 \u0434\u0435\u043d\u044c, \u0441\u043e\u0432\u043f\u0430\u0434\u0430\u044e\u0449\u0438\u0439 \u0441 \u043b\u0435\u0439\u0431\u043b\u043e\u043c \u2014 \u043f\u0440\u0438\u0432\u0435\u0442, data leakage. \u0422\u0430\u043a\u0430\u044f \u043e\u0448\u0438\u0431\u043a\u0430 \u0441\u043f\u043e\u0441\u043e\u0431\u043d\u0430 \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c +15\u2026+40 pp <code>MAPE<\/code> \u0440\u0430\u0437\u043d\u0438\u0446\u044b \u043c\u0435\u0436\u0434\u0443 offline \u0438 prod.<\/p>\n<h4>\u00ab\u0426\u0435\u043b\u0435\u0432\u044b\u0435\u00bb \u043b\u0430\u0433\u0438<\/h4>\n<p>\u041f\u0440\u0430\u0432\u0438\u043b\u043e \u043f\u0440\u043e\u0441\u0442\u043e\u0435: <strong>\u0432\u0441\u0435<\/strong> \u043f\u0440\u0438\u0437\u043d\u0430\u043a\u0438, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0449\u0438\u0435 <code>y<\/code>, \u0434\u043e\u043b\u0436\u043d\u044b \u0431\u044b\u0442\u044c \u0440\u0430\u0441\u0441\u0447\u0438\u0442\u0430\u043d\u044b \u0442\u0430\u043a, \u0447\u0442\u043e\u0431\u044b \u043c\u043e\u0434\u0435\u043b\u044c \u0432\u0438\u0434\u0435\u043b\u0430 \u043f\u0440\u043e\u0448\u043b\u043e\u0435, \u0430 \u043d\u0435 \u0431\u0443\u0434\u0443\u0449\u0435\u0435.<\/p>\n<pre><code class=\"python\">for lag in range(1, 8):     df[f'sales_lag_{lag}'] = df['sales'].shift(lag)<\/code><\/pre>\n<p>\u041f\u0440\u043e\u0432\u0435\u0440\u043a\u0430: \u043d\u0438\u043a\u0430\u043a\u0438\u0445 <code>NaN<\/code> \u0432 train-\u043e\u0442\u0440\u0435\u0437\u043a\u0435? \u0417\u043d\u0430\u0447\u0438\u0442 shift \u043e\u043a.<\/p>\n<h3>\u0412\u0430\u043b\u0438\u0434\u0438\u0440\u0443\u0435\u043c \u0444\u0438\u0447\u0438 \u0441 look-ahead \u2014 \u043f\u043e\u0448\u0430\u0433\u043e\u0432\u043e<\/h3>\n<ol>\n<li>\n<p><strong>\u0421\u043e\u0431\u0438\u0440\u0430\u0435\u043c \u0444\u0438\u0447\u0438 \u0432 \u043e\u0434\u043d\u043e\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u0438<\/strong> (\u0447\u0442\u043e\u0431\u044b \u0433\u0430\u0440\u0430\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c).<\/p>\n<\/li>\n<li>\n<p><strong>\u041e\u0431\u043e\u0440\u0430\u0447\u0438\u0432\u0430\u0435\u043c \u0432 <\/strong><code><strong>FunctionTransformer<\/strong><\/code> \u2014 \u0442\u043e\u0433\u0434\u0430 pipeline \u043f\u043e\u0437\u0430\u0431\u043e\u0442\u0438\u0442\u0441\u044f \u043e \u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u043e\u043c \u0440\u0430\u0441\u0447\u0451\u0442\u0435 \u0432\u043d\u0443\u0442\u0440\u0438 \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u0444\u043e\u043b\u0434\u0430.<\/p>\n<\/li>\n<li>\n<p><strong>\u041f\u0438\u0448\u0435\u043c unit-\u0442\u0435\u0441\u0442<\/strong>: \u0431\u0435\u0440\u0451\u043c \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u0439 train-\u0438\u043d\u0434\u0435\u043a\u0441 \u0438 \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u043c, \u0447\u0442\u043e \u0432\u0441\u0435 \u0434\u0430\u0442\u044b \u0444\u0438\u0447\u0435\u0439 \u2264 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0435\u0439 \u0434\u0430\u0442\u044b \u0442\u0440\u0435\u043d\u0438\u0440\u043e\u0432\u043a\u0438 (\u0440\u0443\u0447\u043d\u043e\u0439 assert \u0441\u043f\u0430\u0441\u0451\u0442 \u043e\u0442 \u043d\u043e\u0447\u043d\u044b\u0445 \u0440\u0435\u043b\u0438\u0437\u043e\u0432-\u0443\u0431\u0438\u0439\u0446).<\/p>\n<\/li>\n<\/ol>\n<pre><code class=\"python\">import pandas as pd from sklearn.preprocessing import FunctionTransformer from sklearn.pipeline import Pipeline from sklearn.ensemble import RandomForestRegressor  def make_features(df: pd.DataFrame) -&gt; pd.DataFrame:     out = df.copy()     # \u0431\u0435\u0437\u043e\u043f\u0430\u0441\u043d\u044b\u0439 \u043b\u0430\u0433     out['lag_1'] = out['target'].shift(1)     # rolling mean \u0431\u0435\u0437 \u0443\u0442\u0435\u0447\u043a\u0438     out['roll_mean_7'] = out['target'].shift(1).rolling(7).mean()     return out.drop(columns=['target'])  # \u043f\u0440\u0438\u0437\u043d\u0430\u043a\u0438 \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u043e  feature_maker = FunctionTransformer(make_features, validate=False)  pipe = Pipeline([     ('feat', feature_maker),     ('model', RandomForestRegressor(n_estimators=300, random_state=0)) ])<\/code><\/pre>\n<h3>\u0428\u0430\u0431\u043b\u043e\u043d pipeline \u0434\u043b\u044f time-series CV<\/h3>\n<p>\u041d\u0430\u043f\u0438\u0448\u0435\u043c \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u0438\u0441\u0442\u0438\u0447\u043d\u044b\u0439, \u043d\u043e \u0433\u043e\u0442\u043e\u0432\u0439 \u0441\u043a\u0435\u043b\u0435\u0442. \u041e\u043d:<\/p>\n<ul>\n<li>\n<p>\u0448\u0438\u0444\u0443\u0435\u0442 \u0434\u0430\u0442\u044b \u0432 \u0438\u043d\u0434\u0435\u043a\u0441,<\/p>\n<\/li>\n<li>\n<p>\u0441\u0442\u0440\u043e\u0438\u0442 \u0444\u0438\u0447\u0438 \u0431\u0435\u0437 \u0443\u0442\u0435\u0447\u0435\u043a,<\/p>\n<\/li>\n<li>\n<p>\u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442 <code>TimeSeriesSplit<\/code> (\u043c\u043e\u0436\u043d\u043e \u043f\u043e\u0434\u043c\u0435\u043d\u0438\u0442\u044c \u043d\u0430 \u043b\u044e\u0431\u043e\u0439 \u043d\u0430\u0448),<\/p>\n<\/li>\n<li>\n<p>\u0441\u0447\u0438\u0442\u0430\u0435\u0442 \u043c\u0435\u0442\u0440\u0438\u043a\u0438 \u043d\u0430 \u043a\u0440\u043e\u0441\u0441-\u0432\u0430\u043b\u0438\u0434\u0430\u0446\u0438\u0438 \u0438 <strong>\u043d\u0435<\/strong> \u0442\u0440\u043e\u0433\u0430\u0435\u0442 \u0442\u0435\u0441\u0442 \u0434\u043e \u0444\u0438\u043d\u0430\u043b\u0430.<\/p>\n<\/li>\n<\/ul>\n<pre><code class=\"python\">import pandas as pd from sklearn.metrics import mean_absolute_percentage_error as mape from sklearn.compose import ColumnTransformer from sklearn.preprocessing import StandardScaler from sklearn.model_selection import cross_val_score, TimeSeriesSplit  NUM_COLS = ['lag_1', 'roll_mean_7', 'promo']   # \u043f\u0440\u0438\u043c\u0435\u0440 CAT_COLS = ['store_id', 'item_id']  num_pipe = Pipeline([     ('scaler', StandardScaler(with_mean=False))  # with_mean=False safe \u0434\u043b\u044f \u0441\u043f\u0430\u0440\u0441 ])  preprocessor = ColumnTransformer([     ('num', num_pipe, NUM_COLS),     ('cat', 'passthrough', CAT_COLS) ])  full_pipe = Pipeline([     ('features', feature_maker),     ('prep',      preprocessor),     ('model',     GradientBoostingRegressor(random_state=0)) ])  tscv = TimeSeriesSplit(n_splits=5, test_size=90, gap=7)  cv_scores = cross_val_score(     full_pipe,     X=df,                # df \u0441 target \u0432\u043d\u0443\u0442\u0440\u0438     y=df['target'],     cv=tscv,     scoring='neg_mean_absolute_percentage_error',     n_jobs=-1 )  print('CV MAPE:', -cv_scores.mean())<\/code><\/pre>\n<p><code>gap=7<\/code> \u0441\u043e\u0437\u0434\u0430\u0451\u0442 \u0441\u0430\u043d\u0438\u0442\u0430\u0440\u043d\u0443\u044e \u0437\u043e\u043d\u0443: \u043c\u043e\u0434\u0435\u043b\u044c \u043d\u0435 \u0432\u0438\u0434\u0438\u0442 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0443\u044e \u043d\u0435\u0434\u0435\u043b\u044e \u0442\u0435\u0441\u0442\u0430, \u0433\u0434\u0435 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u044b \u043b\u0430\u0433\u0438.<\/p>\n<p><code>ColumnTransformer<\/code> \u2014 \u0434\u0440\u0443\u0436\u0438\u0442 \u0441 <code>pandas<\/code> \u0438 \u043d\u0435 \u0434\u0430\u0451\u0442 \u0441\u043b\u0443\u0447\u0430\u0439\u043d\u043e \u043f\u0440\u0438\u0442\u0430\u0449\u0438\u0442\u044c \u043a\u0430\u0442\u0435\u0433\u043e\u0440\u0438\u044e \u0432 \u043d\u043e\u0440\u043c\u0430\u043b\u0430\u0439\u0437\u0435\u0440.<\/p>\n<p><code>StandardScaler<\/code> \u0432\u043d\u0443\u0442\u0440\u0438 CV (!) \u2014 \u0441\u0442\u0430\u0442\u0438\u0441\u0442\u0438\u043a\u0438 \u0441\u0447\u0438\u0442\u0430\u044e\u0442\u0441\u044f <strong>\u0442\u043e\u043b\u044c\u043a\u043e<\/strong> \u043d\u0430 train-\u0447\u0430\u0441\u0442\u0438 \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u0444\u043e\u043b\u0434\u0430, \u0431\u043e\u0440\u0435\u043c\u0441\u044f \u0441 look-ahead bias, \u043e \u043a\u043e\u0442\u043e\u0440\u043e\u043c \u043b\u044e\u0431\u044f\u0442 \u043d\u0430\u043f\u043e\u043c\u0438\u043d\u0430\u0442\u044c \u043e\u043b\u0434\u044b \u043d\u0430 CrossValidated.<\/p>\n<p>\u0415\u0441\u043b\u0438 \u0443 \u0432\u0430\u0441 \u0435\u0441\u0442\u044c \u0441\u0432\u043e\u0439 \u0431\u043e\u0435\u0432\u043e\u0439 \u043e\u043f\u044b\u0442 \u0441 \u0432\u0430\u043b\u0438\u0434\u0430\u0446\u0438\u0435\u0439 \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0445 \u0440\u044f\u0434\u043e\u0432 \u2014 \u043e\u0441\u043e\u0431\u0435\u043d\u043d\u043e \u0432 \u0433\u0440\u044f\u0437\u043d\u044b\u0445 \u0438 \u043d\u0435\u0441\u0442\u0430\u0431\u0438\u043b\u044c\u043d\u044b\u0445 \u043f\u0440\u043e\u0435\u043a\u0442\u0430\u0445 \u2014 \u0440\u0430\u0441\u0441\u043a\u0430\u0436\u0438\u0442\u0435 \u0432 \u043a\u043e\u043c\u043c\u0435\u043d\u0442\u0430\u0440\u0438\u044f\u0445.<\/p>\n<hr\/>\n<p>\u0415\u0441\u043b\u0438 \u0432\u044b \u0445\u043e\u0442\u0438\u0442\u0435 \u0433\u043b\u0443\u0431\u0436\u0435 \u0440\u0430\u0437\u043e\u0431\u0440\u0430\u0442\u044c\u0441\u044f \u0432 \u0442\u043e\u043c, \u043a\u0430\u043a\u00a0\u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u043e \u043f\u043e\u0434\u0433\u043e\u0442\u0430\u0432\u043b\u0438\u0432\u0430\u0442\u044c \u0434\u0430\u043d\u043d\u044b\u0435 \u0434\u043b\u044f\u00a0\u043c\u0430\u0448\u0438\u043d\u043d\u043e\u0433\u043e \u043e\u0431\u0443\u0447\u0435\u043d\u0438\u044f \u0438 \u043a\u0430\u043a \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u043e \u043f\u0440\u0438\u043c\u0435\u043d\u044f\u0442\u044c \u043c\u043e\u0449\u043d\u044b\u0435 \u043c\u0435\u0442\u043e\u0434\u044b, \u0442\u0430\u043a\u0438\u0435 \u043a\u0430\u043a\u00a0Random Forest, \u043f\u0440\u0438\u0433\u043b\u0430\u0448\u0430\u0435\u043c \u0432\u0430\u0441 \u043d\u0430\u00a0\u0434\u0432\u0430 \u0431\u043b\u0438\u0436\u0430\u0439\u0448\u0438\u0445 \u043e\u0442\u043a\u0440\u044b\u0442\u044b\u0445 \u0443\u0440\u043e\u043a\u0430:<\/p>\n<ul>\n<li>\n<p><a href=\"https:\/\/otus.pw\/fqfw\/\" rel=\"noopener noreferrer nofollow\">\u00ab\u041a\u0430\u043a \u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u043e \u0433\u043e\u0442\u043e\u0432\u0438\u0442\u044c \u0434\u0430\u043d\u043d\u044b\u0435 \u0434\u043b\u044f\u00a0ML\u2011\u043c\u043e\u0434\u0435\u043b\u0435\u0439?\u00bb<\/a>\u00a0\u2014 <em>3\u00a0\u0438\u044e\u043b\u044f \u0432 18:00<\/em><\/p>\n<\/li>\n<li>\n<p>\u00ab<a href=\"https:\/\/otus.pw\/B58B\/\" rel=\"noopener noreferrer nofollow\">Random Forest\u00a0\u2014 \u043c\u043e\u0449\u043d\u044b\u0439 \u043c\u0435\u0442\u043e\u0434 \u0430\u043d\u0441\u0430\u043c\u0431\u043b\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u0432\u00a0ML<\/a>\u00bb\u00a0\u2014 16\u00a0\u0438\u044e\u043b\u044f \u0432 18:00<\/p>\n<\/li>\n<\/ul>\n<p>\u042d\u0442\u0438 \u0437\u0430\u043d\u044f\u0442\u0438\u044f \u043f\u043e\u043c\u043e\u0433\u0443\u0442 \u0432\u0430\u043c \u0438\u0437\u0431\u0435\u0436\u0430\u0442\u044c \u043a\u043b\u0430\u0441\u0441\u0438\u0447\u0435\u0441\u043a\u0438\u0445 \u043e\u0448\u0438\u0431\u043e\u043a \u043f\u0440\u0438\u00a0\u0440\u0430\u0431\u043e\u0442\u0435 \u0441\u00a0\u0434\u0430\u043d\u043d\u044b\u043c\u0438 \u0438 \u043d\u0430\u0443\u0447\u0430\u0442 \u0441\u0442\u0440\u043e\u0438\u0442\u044c \u043d\u0430\u0434\u0435\u0436\u043d\u044b\u0435 \u043c\u043e\u0434\u0435\u043b\u0438, \u0447\u0442\u043e\u00a0\u043e\u0441\u043e\u0431\u0435\u043d\u043d\u043e \u0432\u0430\u0436\u043d\u043e \u043f\u0440\u0438\u00a0\u0430\u043d\u0430\u043b\u0438\u0437\u0435 \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0445 \u0440\u044f\u0434\u043e\u0432.<\/p>\n<p>\u0421\u043b\u0435\u0434\u0438\u0442\u0435 \u0437\u0430\u00a0\u043f\u043e\u043b\u043d\u044b\u043c \u0440\u0430\u0441\u043f\u0438\u0441\u0430\u043d\u0438\u0435\u043c \u043e\u0442\u043a\u0440\u044b\u0442\u044b\u0445 \u0443\u0440\u043e\u043a\u043e\u0432 \u0432\u00a0\u043d\u0430\u0448\u0435\u043c <a href=\"https:\/\/otus.pw\/IQuW\/\" rel=\"noopener noreferrer nofollow\">\u043a\u0430\u043b\u0435\u043d\u0434\u0430\u0440\u0435<\/a>\u00a0\u2014 \u0442\u0430\u043c \u0432\u044b \u043d\u0430\u0439\u0434\u0435\u0442\u0435 \u0435\u0449\u0435 \u043c\u043d\u043e\u0433\u043e \u043f\u043e\u043b\u0435\u0437\u043d\u044b\u0445 \u043c\u0435\u0440\u043e\u043f\u0440\u0438\u044f\u0442\u0438\u0439 \u043f\u043e\u00a0Data Science \u0438 \u0441\u043c\u0435\u0436\u043d\u044b\u043c \u043d\u0430\u043f\u0440\u0430\u0432\u043b\u0435\u043d\u0438\u044f\u043c. \u0410\u00a0\u0447\u0442\u043e\u0431\u044b \u0441\u0438\u0441\u0442\u0435\u043c\u043d\u043e \u043f\u0440\u043e\u043a\u0430\u0447\u0430\u0442\u044c \u043d\u0430\u0432\u044b\u043a\u0438, \u043e\u0437\u043d\u0430\u043a\u043e\u043c\u044c\u0442\u0435\u0441\u044c \u0441\u00a0\u043d\u0430\u0448\u0438\u043c <a href=\"https:\/\/otus.pw\/6l3Z\/\" rel=\"noopener noreferrer nofollow\">\u043a\u0430\u0442\u0430\u043b\u043e\u0433\u043e\u043c \u043a\u0443\u0440\u0441\u043e\u0432 \u043f\u043e\u00a0Data Science<\/a>\u00a0\u2014 \u0432\u044b\u0431\u0435\u0440\u0438\u0442\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0443, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u043f\u043e\u0434\u043e\u0439\u0434\u0435\u0442 \u0438\u043c\u0435\u043d\u043d\u043e \u0432\u0430\u043c.<\/p>\n<\/div>\n<\/div>\n<\/div>\n<p><!----><!----><\/div>\n<p><!----><!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/articles\/921604\/\"> https:\/\/habr.com\/ru\/articles\/921604\/<\/a><br \/><\/br><\/br><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-465772","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/465772","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=465772"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/465772\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=465772"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=465772"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=465772"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}