{"id":299187,"date":"2020-02-24T21:00:22","date_gmt":"2020-02-24T21:00:22","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=299187"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=299187","title":{"rendered":"\u0421\u043a\u0430\u043d\u0438\u0440\u0443\u044e\u0449\u0435\u0435 \u043e\u043a\u043d\u043e \u043f\u043e \u043c\u0430\u0441\u0441\u0438\u0432\u0430\u043c NumPy"},"content":{"rendered":"\n<div class=\"post__text post__text-html\" id=\"post-content-body\" data-io-article-url=\"https:\/\/habr.com\/ru\/post\/489734\/\">\n<p><a href=\"https:\/\/colab.research.google.com\/drive\/1Zru_-zzbtylgitbwxbi0eDBNhwr8qYl6\">CoLab \u0431\u043b\u043e\u043a\u043d\u043e\u0442<\/a> \u0441 \u043f\u0440\u0438\u043c\u0435\u0440\u0430\u043c\u0438<\/p>\n<p>  <\/p>\n<p>\u0412\u043e\u0437\u043c\u043e\u0436\u043d\u043e \u0441\u0434\u0435\u043b\u0430\u0442\u044c <a href=\"https:\/\/wiki.loginom.ru\/articles\/windowing-method.html\">\u0441\u043a\u043e\u043b\u044c\u0437\u044f\u0449\u0435\u0435 \u043e\u043a\u043d\u043e<\/a> (rolling window, <a href=\"https:\/\/stackoverflow.com\/questions\/8269916\/what-is-sliding-window-algorithm-examples\">sliding window<\/a>, moving window) \u043f\u043e \u043c\u0430\u0441\u0441\u0438\u0432\u0430\u043c NumPy \u043d\u0430 \u044f\u0437\u044b\u043a\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f Python <strong>\u0431\u0435\u0437 \u044f\u0432\u043d\u044b\u0445 \u0446\u0438\u043a\u043b\u043e\u0432<\/strong>. \u0412 \u0434\u0430\u043d\u043d\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u0440\u0430\u0441\u0441\u043c\u0430\u0442\u0440\u0438\u0432\u0430\u0435\u0442\u0441\u044f \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u043e\u0434\u043d\u043e-, \u0434\u0432\u0443\u0445-, \u0442\u0440\u0435\u0445- \u0438 N-\u043c\u0435\u0440\u043d\u044b\u0445 \u0441\u043a\u043e\u043b\u044c\u0437\u044f\u0449\u0438\u0445 \u043e\u043a\u043e\u043d \u043f\u043e \u043c\u0430\u0441\u0441\u0438\u0432\u0430\u043c NumPy. \u0412 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0435 \u0441\u043a\u043e\u0440\u043e\u0441\u0442\u044c \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0438 \u0434\u0430\u043d\u043d\u044b\u0445 \u0443\u0432\u0435\u043b\u0438\u0447\u0438\u0432\u0430\u0435\u0442\u0441\u044f \u0432 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0442\u044b\u0441\u044f\u0447 \u0440\u0430\u0437 \u0438 \u0441\u0440\u0430\u0432\u043d\u0438\u043c\u0430 \u043f\u043e \u0441\u043a\u043e\u0440\u043e\u0441\u0442\u0438 \u0441 \u044f\u0437\u044b\u043a\u043e\u043c \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f <strong>\u0421<\/strong>.<\/p>\n<p>  <\/p>\n<p>C\u043a\u043e\u043b\u044c\u0437\u044f\u0449\u0435\u0435 \u043e\u043a\u043d\u043e \u043f\u0440\u0438\u043c\u0435\u043d\u044f\u0435\u0442\u0441\u044f \u0432: \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0435 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0439, \u0438\u0441\u043a\u0443\u0441\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u0445 \u043d\u0435\u0439\u0440\u043e\u043d\u043d\u044b\u0445 \u0441\u0435\u0442\u044f\u0445, \u0438\u043d\u0442\u0435\u0440\u043d\u0435\u0442 \u043f\u0440\u043e\u0442\u043e\u043a\u043e\u043b\u0435 TCP, \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0435 \u0433\u0435\u043d\u043e\u043c\u043d\u044b\u0445 \u0434\u0430\u043d\u043d\u044b\u0445, \u043f\u0440\u043e\u0433\u043d\u043e\u0437\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0438 \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0445 \u0440\u044f\u0434\u043e\u0432 \u0438 \u0442.\u0434.<\/p>\n<p>  <\/p>\n<p><strong>\u041e\u0442\u043a\u0430\u0437 \u043e\u0442 \u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0435\u043d\u043d\u043e\u0441\u0442\u0438<\/strong>: <em>\u0432 \u0438\u0441\u0445\u043e\u0434\u043d\u043e\u043c \u043a\u043e\u0434\u0435 \u043c\u043e\u0433\u0443\u0442 \u0431\u044b\u0442\u044c \u043e\u0448\u0438\u0431\u043a\u0438!<\/em> \u0415\u0441\u043b\u0438 \u0432\u044b \u0432\u0438\u0434\u0438\u0442\u0435 \u043e\u0448\u0438\u0431\u043a\u0443, \u043f\u043e\u0436\u0430\u043b\u0443\u0439\u0441\u0442\u0430, \u043d\u0430\u043f\u0438\u0448\u0438\u0442\u0435 \u043c\u043d\u0435.<\/p>\n<p>  <\/p>\n<ul>\n<li><a href=\"#introduction\">\u0412\u0432\u0435\u0434\u0435\u043d\u0438\u0435<\/a><\/li>\n<li><a href=\"#1d\">\u0421\u043a\u043e\u043b\u044c\u0437\u044f\u0449\u0435\u0435 1D \u043e\u043a\u043d\u043e \u043f\u043e ND \u043c\u0430\u0441\u0441\u0438\u0432\u0443 \u0432 Numpy<\/a><\/li>\n<li><a href=\"#2d\">\u0421\u043a\u043e\u043b\u044c\u0437\u044f\u0449\u0435\u0435 2D \u043e\u043a\u043d\u043e \u043f\u043e ND \u043c\u0430\u0441\u0441\u0438\u0432\u0443 \u0432 Numpy<\/a><\/li>\n<li><a href=\"#3d\">\u0421\u043a\u043e\u043b\u044c\u0437\u044f\u0449\u0435\u0435 3D \u043e\u043a\u043d\u043e \u043f\u043e ND \u043c\u0430\u0441\u0441\u0438\u0432\u0443 \u0432 Numpy<\/a><\/li>\n<li><a href=\"#md\">\u0421\u043a\u043e\u043b\u044c\u0437\u044f\u0449\u0435\u0435 MD \u043e\u043a\u043d\u043e \u043f\u043e ND \u043c\u0430\u0441\u0441\u0438\u0432\u0443, \u0433\u0434\u0435 M \u2264 N<\/a><\/li>\n<li><a href=\"#md-extended\">\u0421\u043a\u043e\u043b\u044c\u0437\u044f\u0449\u0435\u0435 MD \u043e\u043a\u043d\u043e \u043f\u043e ND \u043c\u0430\u0441\u0441\u0438\u0432\u0443 \u0434\u043b\u044f \u043b\u044e\u0431\u044b\u0445 M \u0438 N<\/a><\/li>\n<\/ul>\n<p><a name=\"habracut\"><\/a>  <\/p>\n<hr>\n<p>  <\/p>\n<h2 id=\"a-nameintroductionvvedeniea\">\u0412\u0432\u0435\u0434\u0435\u043d\u0438\u0435<\/h2>\n<p>  <\/p>\n<p>\u042d\u0442\u0430 \u0441\u0442\u0430\u0442\u044c\u044f \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0435\u043d\u0438\u0435\u043c <a href=\"https:\/\/stackoverflow.com\/a\/46237736\/7550928\">\u043c\u043e\u0435\u0433\u043e \u043e\u0442\u0432\u0435\u0442\u0430<\/a> \u043d\u0430 \u0441\u0430\u0439\u0442\u0435 StackOverflow. \u041c\u043e\u0438 \u043f\u0435\u0440\u0432\u044b\u0435 \u044d\u043a\u0441\u043f\u0435\u0440\u0438\u043c\u0435\u043d\u0442\u044b \u0441\u043e \u0441\u043a\u043e\u043b\u044c\u0437\u044f\u0449\u0438\u043c \u043e\u043a\u043d\u043e\u043c <a href=\"https:\/\/github.com\/foobar167\/junkyard\/blob\/master\/rolling_window.py\">\u0437\u0434\u0435\u0441\u044c<\/a> \u0438 <a href=\"https:\/\/github.com\/foobar167\/junkyard\/blob\/master\/rolling_window_advanced.py\">\u0437\u0434\u0435\u0441\u044c<\/a>.<\/p>\n<p>  <\/p>\n<p><strong>\u041f\u0440\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u0430\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f<\/strong> \u0441\u043a\u043e\u043b\u044c\u0437\u044f\u0449\u0435\u0433\u043e \u0434\u0432\u0443\u043c\u0435\u0440\u043d\u043e\u0433\u043e \u043e\u043a\u043d\u0430 \u043f\u043e \u0434\u0432\u0443\u043c\u0435\u0440\u043d\u043e\u043c\u0443 \u043c\u0430\u0441\u0441\u0438\u0432\u0443 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 <code>roll<\/code> \u0444\u0430\u0439\u043b\u0430 <a href=\"https:\/\/github.com\/foobar167\/junkyard\/blob\/master\/manual_image_annotation1\/polygon\/logic_tools.py\"><code>logic_tools.py<\/code><\/a> \u043f\u0440\u043e\u0435\u043a\u0442\u0430 <a href=\"https:\/\/github.com\/foobar167\/junkyard\/tree\/master\/manual_image_annotation1\">\u0420\u0443\u0447\u043d\u0430\u044f \u0440\u0430\u0437\u043c\u0435\u0442\u043a\u0430 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0439 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043c\u043d\u043e\u0433\u043e\u0443\u0433\u043e\u043b\u044c\u043d\u0438\u043a\u043e\u0432<\/a>.<\/p>\n<p>  <\/p>\n<p>\u0410\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u044b \u0434\u043b\u044f \u043e\u0434\u043d\u043e\u043c\u0435\u0440\u043d\u043e\u0433\u043e \u0441\u043a\u043e\u043b\u044c\u0437\u044f\u0449\u0435\u0433\u043e \u043e\u043a\u043d\u0430 \u0443\u0436\u0435 \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u043d\u044b <a href=\"https:\/\/stackoverflow.com\/a\/7100681\/7550928\">\u0437\u0434\u0435\u0441\u044c<\/a>, <a href=\"https:\/\/rigtorp.se\/2011\/01\/01\/rolling-statistics-numpy.html\">\u0437\u0434\u0435\u0441\u044c<\/a> \u0438 <a href=\"https:\/\/stackoverflow.com\/questions\/6811183\/rolling-window-for-1d-arrays-in-numpy\">\u0437\u0434\u0435\u0441\u044c<\/a>.<\/p>\n<p>  <\/p>\n<p>\u0414\u043b\u044f \u043f\u043e\u043d\u0438\u043c\u0430\u043d\u0438\u044f \u0442\u0435\u043c\u044b, \u0432\u0430\u043c \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0437\u043d\u0430\u0442\u044c, \u0447\u0442\u043e \u0442\u0430\u043a\u043e\u0435 <a href=\"https:\/\/stackoverflow.com\/a\/53099870\/7550928\">\u0441\u0442\u0440\u0430\u0439\u0434\u044b (strides, \u0448\u0430\u0433\u0438)<\/a>.<\/p>\n<p>  <\/p>\n<p>\u041a\u0430\u043a\u043e\u0435-\u0442\u043e \u0441\u043a\u043e\u043b\u044c\u0437\u044f\u0449\u0435\u0435 \u043e\u043a\u043d\u043e \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u043d\u043e <a href=\"https:\/\/pandas.pydata.org\/pandas-docs\/stable\/reference\/api\/pandas.DataFrame.rolling.html\">\u0432 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0435 Pandas<\/a>, \u043e\u0434\u043d\u0430\u043a\u043e \u0430\u0432\u0442\u043e\u0440 \u043f\u043b\u043e\u0445\u043e \u0437\u043d\u0430\u043a\u043e\u043c \u0441 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u043e\u0439 Pandas, \u0447\u0442\u043e\u0431\u044b \u0441\u043a\u0430\u0437\u0430\u0442\u044c, \u043d\u0430\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0443\u043d\u0438\u0432\u0435\u0440\u0441\u0430\u043b\u044c\u043d\u0430\u044f \u0438 \u0431\u044b\u0441\u0442\u0440\u0430\u044f \u043f\u0440\u0438\u0432\u0435\u0434\u0435\u043d\u043d\u0430\u044f \u0442\u0430\u043c \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f. \u041a \u0442\u043e\u043c\u0443 \u0436\u0435 \u0432\u0441\u0435\u0433\u0434\u0430 \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u0435\u0435 \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0444\u0443\u043d\u043a\u0446\u0438\u043e\u043d\u0430\u043b \u0441\u0430\u043c\u043e\u043c\u0443, \u0435\u0441\u043b\u0438 \u0435\u0441\u0442\u044c \u0442\u0430\u043a\u0430\u044f \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044c. \u041a\u0430\u043a \u0430\u043b\u044c\u0442\u0435\u0440\u043d\u0430\u0442\u0438\u0432\u0443, \u043c\u043e\u0436\u043d\u043e \u0437\u0430\u0434\u0435\u0439\u0441\u0442\u0432\u043e\u0432\u0430\u0442\u044c \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0443 <a href=\"https:\/\/cython.org\/\">Cython<\/a>, \u043e\u0434\u043d\u0430\u043a\u043e \u0441\u043a\u043e\u0440\u043e\u0441\u0442\u044c \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0438 \u0432\u0441\u0435-\u0440\u0430\u0432\u043d\u043e \u0431\u0443\u0434\u0435\u0442 \u043d\u0438\u0436\u0435, \u0447\u0435\u043c \u0432 NumPy.<\/p>\n<p>  <\/p>\n<hr>\n<p>  <\/p>\n<h2 id=\"a-name1d-1-skolzyaschee-1d-okno-po-nd-massivu-v-numpya\"> 1. \u0421\u043a\u043e\u043b\u044c\u0437\u044f\u0449\u0435\u0435 1D \u043e\u043a\u043d\u043e \u043f\u043e ND \u043c\u0430\u0441\u0441\u0438\u0432\u0443 \u0432 Numpy<\/h2>\n<p>  <\/p>\n<p><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/webt\/sa\/ct\/en\/sactenxhxtjip1t7bg7o6kq61fs.png\" alt=\"Rolling 1D window for ND array in Numpy\"><\/p>\n<p>  <\/p>\n<p>\u041f\u0440\u043e\u0441\u0442\u0435\u0439\u0448\u0435\u0435 \u043e\u0434\u043d\u043e\u043c\u0435\u0440\u043d\u043e\u0435 \u0441\u043a\u043e\u043b\u044c\u0437\u044f\u0449\u0435\u0435 \u043e\u043a\u043d\u043e \u043f\u043e \u043c\u043d\u043e\u0433\u043e\u043c\u0435\u0440\u043d\u043e\u043c\u0443 \u043c\u0430\u0441\u0441\u0438\u0432\u0443 \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u0442\u0430\u043a:<\/p>\n<p>  <\/p>\n<pre><code class=\"python\"># Rolling 1D window for ND array def roll(a,      # ND array          b,      # rolling 1D window array          dx=1):  # step size (horizontal)     shape = a.shape[:-1] + (int((a.shape[-1] - b.shape[-1]) \/ dx) + 1,) + b.shape     strides = a.strides[:-1] + (a.strides[-1] * dx,) + a.strides[-1:]     return np.lib.stride_tricks.as_strided(a, shape=shape, strides=strides)<\/code><\/pre>\n<p>  <\/p>\n<p>\u0424\u0443\u043d\u043a\u0446\u0438\u044f <a href=\"https:\/\/docs.scipy.org\/doc\/numpy-1.13.0\/reference\/generated\/numpy.lib.stride_tricks.as_strided.html\"><code>numpy.lib.stride_tricks.as_strided<\/code><\/a> \u0441\u043e\u0437\u0434\u0430\u0435\u0442 \u043d\u043e\u0432\u044b\u0439 \u0432\u0438\u0434 (view) \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0441 \u0437\u0430\u0434\u0430\u043d\u043d\u043e\u0439 \u0444\u043e\u0440\u043c\u043e\u0439 (shape) \u0438 \u0448\u0430\u0433\u0430\u043c\u0438 (strides).<\/p>\n<p>  <\/p>\n<p>\u0424\u043e\u0440\u043c\u0430 (shape) \u043d\u043e\u0432\u043e\u0433\u043e \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0441\u043e\u0437\u0434\u0430\u0435\u0442\u0441\u044f \u0438\u0437 \u0444\u043e\u0440\u043c\u044b \u0432\u0445\u043e\u0434\u043d\u043e\u0433\u043e \u043c\u043d\u043e\u0433\u043e\u043c\u0435\u0440\u043d\u043e\u0433\u043e \u043c\u0430\u0441\u0441\u0438\u0432\u0430, \u043f\u043e \u043a\u043e\u0442\u043e\u0440\u043e\u043c\u0443 \u043e\u0441\u0443\u0449\u0435\u0441\u0442\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0434\u0432\u0438\u0436\u0435\u043d\u0438\u0435 \u0441\u043a\u0430\u043d\u0438\u0440\u0443\u044e\u0449\u0435\u0433\u043e \u043e\u043a\u043d\u0430, \u0438 \u0444\u043e\u0440\u043c\u044b \u043e\u0434\u043d\u043e\u043c\u0435\u0440\u043d\u043e\u0433\u043e \u0441\u043a\u0430\u043d\u0438\u0440\u0443\u044e\u0449\u0435\u0433\u043e \u043e\u043a\u043d\u0430. \u0422\u043e\u0433\u0434\u0430 \u043a\u0430\u043a \u0448\u0430\u0433\u0438 (strides) \u0441\u043e\u0437\u0434\u0430\u044e\u0442\u0441\u044f \u0442\u043e\u043b\u044c\u043a\u043e \u0438\u0437 \u0448\u0430\u0433\u043e\u0432 \u0432\u0445\u043e\u0434\u043d\u043e\u0433\u043e \u043c\u043d\u043e\u0433\u043e\u043c\u0435\u0440\u043d\u043e\u0433\u043e \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0431\u0435\u0437 \u0443\u0447\u0430\u0441\u0442\u0438\u044f \u043e\u0434\u043d\u043e\u043c\u0435\u0440\u043d\u043e\u0433\u043e \u0441\u043a\u0430\u043d\u0438\u0440\u0443\u044e\u0449\u0435\u0433\u043e \u043e\u043a\u043d\u0430.<\/p>\n<p>  <\/p>\n<p>\u0424\u043e\u0440\u043c\u0430 (shape) \u0441\u043e\u0441\u0442\u043e\u0438\u0442 \u0438\u0437 \u0442\u0440\u0435\u0445 \u0441\u043b\u0430\u0433\u0430\u0435\u043c\u044b\u0445:<\/p>\n<p>  <\/p>\n<ul>\n<li><code>a.shape[:-1]<\/code> \u2014 \u044d\u0442\u043e \u043e\u0441\u0442\u0430\u0442\u043e\u043a \u043e\u0442 \u0444\u043e\u0440\u043c\u044b ND-\u043c\u0430\u0441\u0441\u0438\u0432\u0430, \u0433\u0434\u0435 <code>N &gt; 1<\/code>. \u0415\u0441\u043b\u0438 <code>N == 1<\/code>, \u0442\u043e \u044d\u0442\u043e\u0442 \u043e\u0441\u0442\u0430\u0442\u043e\u043a \u0431\u0443\u0434\u0435\u0442 \u0440\u0430\u0432\u0435\u043d \u043f\u0443\u0441\u0442\u043e\u043c\u0443 <a href=\"https:\/\/pythonworld.ru\/tipy-dannyx-v-python\/kortezhi-tuple.html\">\u043a\u043e\u0440\u0442\u0435\u0436\u0443<\/a> <code>t == ()<\/code>, \u0442\u0430\u043a\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c \u044d\u0442\u043e \u0441\u043b\u0430\u0433\u0430\u0435\u043c\u043e\u0435 \u043d\u0435 \u0432\u0430\u0436\u043d\u043e \u0434\u043b\u044f <code>N == 1<\/code>.<\/li>\n<li><code>(int((a.shape[-1] - b.shape[-1]) \/ dx) + 1,)<\/code> \u2014 \u044d\u0442\u043e \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0448\u0430\u0433\u043e\u0432 \u0441\u043a\u043e\u043b\u044c\u0437\u044f\u0449\u0435\u0433\u043e \u043e\u043a\u043d\u0430 \u043f\u043e \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0435\u0439 \u0440\u0430\u0437\u043c\u0435\u0440\u043d\u043e\u0441\u0442\u0438 <code>[-1]<\/code> \u043c\u0430\u0441\u0441\u0438\u0432\u0430. \u0428\u0430\u0433 \u0441\u043a\u0430\u043d\u0438\u0440\u0443\u044e\u0449\u0435\u0433\u043e \u043e\u043a\u043d\u0430 <code>dx<\/code> \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u0440\u0430\u0432\u043d\u044b\u043c: 1, 2, 3 \u0438 \u0442.\u0434.<\/li>\n<li><code>b.shape<\/code> \u2014 \u044d\u0442\u043e \u0444\u043e\u0440\u043c\u0430 \u0441\u043a\u043e\u043b\u044c\u0437\u044f\u0449\u0435\u0433\u043e \u043e\u043a\u043d\u0430.<\/li>\n<\/ul>\n<p>  <\/p>\n<p>\u0428\u0430\u0433\u0438 (strides) \u0442\u0430\u043a\u0436\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u0442 \u0438\u0437 \u0442\u0440\u0435\u0445 \u0441\u043b\u0430\u0434\u0430\u0435\u043c\u044b\u0445:<\/p>\n<p>  <\/p>\n<ul>\n<li><code>a.strides[:-1]<\/code> \u2014 \u044d\u0442\u043e \u043e\u0441\u0442\u0430\u0442\u043e\u043a \u043e\u0442 \u0448\u0430\u0433\u043e\u0432 ND-\u043c\u0430\u0441\u0441\u0438\u0432\u0430, \u0433\u0434\u0435 <code>N &gt; 1<\/code>. \u0415\u0441\u043b\u0438 <code>N == 1<\/code>, \u0442\u043e \u044d\u0442\u043e\u0442 \u043e\u0441\u0442\u0430\u0442\u043e\u043a \u0431\u0443\u0434\u0435\u0442 \u0440\u0430\u0432\u0435\u043d \u043f\u0443\u0441\u0442\u043e\u043c\u0443 \u043a\u043e\u0440\u0442\u0435\u0436\u0443 <code>t == ()<\/code>, \u0442\u0430\u043a\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c \u044d\u0442\u043e \u0441\u043b\u0430\u0433\u0430\u0435\u043c\u043e\u0435 \u043d\u0435 \u0432\u0430\u0436\u043d\u043e \u0434\u043b\u044f <code>N == 1<\/code>.<\/li>\n<li><code>(a.strides[-1] * dx,)<\/code> \u2014 \u044d\u0442\u043e \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0431\u0430\u0439\u0442 \u043c\u0435\u0436\u0434\u0443 \u0448\u0430\u0433\u0430\u043c\u0438 \u0441\u043a\u043e\u043b\u044c\u0437\u044f\u0449\u0435\u0433\u043e \u043e\u043a\u043d\u0430. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0446\u0435\u043b\u043e\u0447\u0438\u0441\u043b\u0435\u043d\u043d\u044b\u0439 <code>int<\/code> \u043c\u0430\u0441\u0441\u0438\u0432 \u0438\u043c\u0435\u0435\u0442 4 \u0431\u0430\u0439\u0442\u0430 \u0432 \u043e\u0434\u043d\u043e\u043c \u0448\u0430\u0433\u0435 \u043c\u0435\u0436\u0434\u0443 \u0441\u043e\u0441\u0435\u0434\u043d\u0438\u043c\u0438 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\u043c\u0438, \u0442\u0430\u043a\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c \u0434\u043b\u044f \u0448\u0430\u0433\u0430 <code>dx == 2<\/code> \u0448\u0430\u0433 \u0432 \u0431\u0430\u0439\u0442\u0430\u0445 \u0441\u043e\u0441\u0442\u0430\u0432\u0438\u0442 <code>4 * 2 = 8<\/code> \u0431\u0430\u0439\u0442.<\/li>\n<li><code>a.strides[-1:]<\/code> \u2014 \u044d\u0442\u043e \u0448\u0430\u0433 \u0432 \u0431\u0430\u0439\u0442\u0430\u0445 \u043c\u0435\u0436\u0434\u0443 \u0441\u043e\u0441\u0435\u0434\u043d\u0438\u043c\u0438 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\u043c\u0438. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0446\u0435\u043b\u043e\u0447\u0438\u0441\u043b\u0435\u043d\u043d\u044b\u0439 <code>int<\/code> \u043c\u0430\u0441\u0441\u0438\u0432 \u0438\u043c\u0435\u0435\u0442 \u0448\u0430\u0433 4 \u0431\u0430\u0439\u0442\u0430 \u043c\u0435\u0436\u0434\u0443 \u0441\u043e\u0441\u0435\u0434\u043d\u0438\u043c\u0438 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\u043c\u0438, \u0442\u043e\u0433\u0434\u0430 \u0434\u0430\u043d\u043d\u044b\u0439 \u043a\u043e\u0440\u0442\u0435\u0436 \u0440\u0430\u0432\u0435\u043d <code>(4,)<\/code>.<\/li>\n<\/ul>\n<p>  <\/p>\n<hr>\n<p>  <\/p>\n<h2 id=\"a-name2d2-skolzyaschee-2d-okno-po-nd-massivu-v-numpya\">2. \u0421\u043a\u043e\u043b\u044c\u0437\u044f\u0449\u0435\u0435 2D \u043e\u043a\u043d\u043e \u043f\u043e ND \u043c\u0430\u0441\u0441\u0438\u0432\u0443 \u0432 Numpy<\/h2>\n<p>  <\/p>\n<p><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/webt\/9y\/6c\/th\/9y6cthfdhm1pjij1d6-mygsi9yg.png\" alt=\"Rolling 2D window for ND array in Numpy\"><\/p>\n<p>  <\/p>\n<p>\u041f\u0440\u0438\u043c\u0435\u0440\u044b \u0441\u043a\u043e\u043b\u044c\u0437\u044f\u0449\u0435\u0433\u043e 2D \u043e\u043a\u043d\u0430 \u043f\u043e 2D \u043c\u0430\u0441\u0441\u0438\u0432\u0443:<\/p>\n<p>  <\/p>\n<ul>\n<li>\u043d\u0430\u0439\u0442\u0438 \u043c\u0435\u043d\u044c\u0448\u0435\u0435 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0435 \u0432 \u0431\u043e\u043b\u044c\u0448\u0435\u043c;<\/li>\n<li>\u0441\u0434\u0435\u043b\u0430\u0442\u044c \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u044e \u0441\u0432\u0435\u0440\u0442\u043a\u0438 \u0432 \u0438\u0441\u043a\u0443\u0441\u0441\u0442\u0432\u0435\u043d\u043d\u043e\u0439 \u043d\u0435\u0439\u0440\u043e\u043d\u043d\u043e\u0439 \u0441\u0435\u0442\u0438;<\/li>\n<li>\u043f\u0440\u0438\u043c\u0435\u043d\u0438\u0442\u044c \u0446\u0438\u0444\u0440\u043e\u0432\u043e\u0439 \u0444\u0438\u043b\u044c\u0442\u0440 \u043a \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044e (\u0444\u0438\u043b\u044c\u0442\u0440\u044b \u0421\u043e\u0431\u0435\u043b\u044f, \u0413\u0430\u0443\u0441\u0441\u0438\u0430\u043d, \u0440\u0430\u0437\u043c\u044b\u0442\u0438\u044f \u0438 \u0442.\u0434.).<\/li>\n<\/ul>\n<p>  <\/p>\n<p>\u0412 \u043e\u0431\u0449\u0435\u043c, \u0441\u043a\u043e\u043b\u044c\u0437\u044f\u0449\u0435\u0435 2D \u043e\u043a\u043d\u043e \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u043f\u0440\u043e\u0432\u043e\u0434\u0438\u0442\u044c <em>\u043f\u0435\u0440\u0438\u043e\u0434\u0438\u0447\u0435\u0441\u043a\u0438\u0435 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438<\/em> \u043d\u0430 \u043c\u0430\u0442\u0440\u0438\u0446\u0435 \u0441 \u043a\u0430\u043a\u0438\u043c-\u043b\u0438\u0431\u043e \u0448\u0430\u0433\u043e\u043c. \u041e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u0441\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u044f, \u0441\u0432\u0435\u0440\u0442\u043a\u0438, \u0432\u044b\u0447\u0438\u0442\u0430\u043d\u0438\u044f, \u0443\u043c\u043d\u043e\u0436\u0435\u043d\u0438\u044f, \u043f\u0440\u0438\u043c\u0435\u043d\u0435\u043d\u0438\u044f \u0446\u0438\u0444\u0440\u043e\u0432\u043e\u0433\u043e \u0444\u0438\u043b\u044c\u0442\u0440\u0430 \u0438 \u0442.\u0434. \u0417\u0434\u0435\u0441\u044c \u0442\u0430\u043a \u0436\u0435, \u043a\u0430\u043a \u0432 <a href=\"#1d\">\u0440\u0430\u0437\u0434\u0435\u043b\u0435 \u043e\u0434\u0438\u043d<\/a>, \u0441\u043e\u0437\u0434\u0430\u0435\u0442\u0441\u044f \u0432\u0438\u0434 \u043d\u043e\u0432\u043e\u0433\u043e \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0444\u043e\u0440\u043c\u044b \u0438 \u0448\u0430\u0433\u043e\u0432.<\/p>\n<p>  <\/p>\n<pre><code class=\"python\"># Rolling 2D window for ND array def roll(a,      # ND array          b,      # rolling 2D window array          dx=1,   # horizontal step, abscissa, number of columns          dy=1):  # vertical step, ordinate, number of rows     shape = a.shape[:-2] + \\             ((a.shape[-2] - b.shape[-2]) \/\/ dy + 1,) + \\             ((a.shape[-1] - b.shape[-1]) \/\/ dx + 1,) + \\             b.shape  # sausage-like shape with 2D cross-section     strides = a.strides[:-2] + \\               (a.strides[-2] * dy,) + \\               (a.strides[-1] * dx,) + \\               a.strides[-2:]     return np.lib.stride_tricks.as_strided(a, shape=shape, strides=strides)<\/code><\/pre>\n<p>  <\/p>\n<p><em>\u0424\u043e\u0440\u043c\u0430<\/em> \u0441\u043e\u0441\u0442\u043e\u0438\u0442 \u0438\u0437 \u0447\u0435\u0442\u044b\u0440\u0435\u0445 \u0441\u043b\u0430\u0433\u0430\u0435\u043c\u044b\u0445: \u0442\u0440\u0438 \u0441\u043b\u0430\u0433\u0430\u0435\u043c\u044b\u0445 \u0442\u0430\u043a\u0438\u0435 \u0436\u0435, \u043a\u0430\u043a \u0432 <a href=\"#1d\">\u0440\u0430\u0437\u0434\u0435\u043b\u0435 \u043e\u0434\u0438\u043d<\/a>, \u0430 \u0447\u0435\u0442\u0432\u0435\u0440\u0442\u043e\u0435 \u0441\u043b\u0430\u0433\u0430\u0435\u043c\u043e\u0435 \u2014 \u044d\u0442\u043e \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0432\u0435\u0440\u0442\u0438\u043a\u0430\u043b\u044c\u043d\u044b\u0445 \u0448\u0430\u0433\u043e\u0432 \u0441\u043a\u043e\u043b\u044c\u0437\u044f\u0449\u0435\u0433\u043e \u043e\u043a\u043d\u0430 <code>((a.shape[-2] - b.shape[-2]) \/\/ dy + 1,)<\/code>. \u0412\u044b\u0440\u0430\u0436\u0435\u043d\u0438\u0435:<\/p>\n<p>  <\/p>\n<pre><code class=\"python\">    (int((a.shape[-1] - b.shape[-1]) \/ dx) + 1,)<\/code><\/pre>\n<p>  <\/p>\n<p>\u0431\u044b\u043b\u043e \u0437\u0430\u043c\u0435\u043d\u0435\u043d\u043e \u043d\u0430<\/p>\n<p>  <\/p>\n<pre><code class=\"python\">    ((a.shape[-1] - b.shape[-1]) \/\/ dx + 1,)<\/code><\/pre>\n<p>  <\/p>\n<p>\u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e \u044d\u0442\u0438 \u0434\u0432\u0430 \u0432\u044b\u0440\u0430\u0436\u0435\u043d\u0438\u044f \u044d\u043a\u0432\u0438\u0432\u0430\u043b\u0435\u043d\u0442\u043d\u044b.<\/p>\n<p>  <\/p>\n<p><em>\u0428\u0430\u0433\u0438<\/em> (\u0441\u0442\u0440\u0430\u0439\u0434\u044b) \u0442\u0430\u043a\u0436\u0435 \u043f\u043e\u0445\u043e\u0436\u0438 \u043d\u0430 <a href=\"#1d\">\u0440\u0430\u0437\u0434\u0435\u043b \u043e\u0434\u0438\u043d<\/a>, \u043d\u043e \u0441 \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u043c \u0441\u0442\u0440\u0430\u0439\u0434\u043e\u043c <code>(a.strides[-2] * dy,)<\/code> \u0434\u043b\u044f \u0432\u0435\u0440\u0442\u0438\u043a\u0430\u043b\u044c\u043d\u043e\u0433\u043e \u0448\u0430\u0433\u0430 \u0441\u043a\u043e\u043b\u044c\u0437\u044f\u0449\u0435\u0433\u043e 2D \u043e\u043a\u043d\u0430.<\/p>\n<p>  <\/p>\n<p>\u0424\u0443\u043d\u043a\u0446\u0438\u044f \u0434\u043b\u044f \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0438 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u043e\u0432 \u0432\u044b\u0432\u043e\u0434\u0438\u0442 \u0432 \u043a\u043e\u043d\u0441\u043e\u043b\u044c \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u043d\u0430\u0439\u0434\u0435\u043d\u043d\u044b\u0445 \u0441\u043e\u0432\u043f\u0430\u0434\u0435\u043d\u0438\u0439 <code>counts<\/code>, \u0438\u0445 \u043a\u043e\u043e\u0440\u0434\u0438\u043d\u0430\u0442\u044b <code>coords<\/code> \u0438 \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c:<\/p>\n<p>  <\/p>\n<pre><code class=\"python\">def show_results(a, b, dx=1, dy=1):     axis = a.ndim  # number of dimensions     # np.all over 2 dimensions of the rolling 2D window     bool_array = np.all(np.all(             roll(a, b, dx, dy) == b,         axis=axis),axis=axis)     counts = np.count_nonzero(bool_array)     coords = np.transpose(np.nonzero(bool_array)) * [dy, dx]     print(&quot;Found {counts} elements with coordinates:\\n{coords}&quot;.format(         counts=counts, coords=coords))<\/code><\/pre>\n<p>  <\/p>\n<p>\u0417\u0434\u0435\u0441\u044c <code>np.all<\/code> \u043f\u0440\u0438\u043c\u0435\u043d\u044f\u0435\u0442\u0441\u044f \u043a \u0434\u0432\u0443\u043c \u0440\u0430\u0437\u043c\u0435\u0440\u043d\u043e\u0441\u0442\u044f\u043c 2D \u0441\u043a\u043e\u043b\u044c\u0437\u044f\u0449\u0435\u0433\u043e \u043e\u043a\u043d\u0430. \u0414\u043b\u044f \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u044f \u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u044b\u0445 \u043a\u043e\u043e\u0440\u0434\u0438\u043d\u0430\u0442 \u0441\u043e\u0432\u043f\u0430\u0434\u0435\u043d\u0438\u044f <code>coords<\/code> \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u0434\u043e\u043c\u043d\u043e\u0436\u0430\u0435\u0442\u0441\u044f \u043d\u0430 \u0432\u0435\u0440\u0442\u0438\u043a\u0430\u043b\u044c\u043d\u044b\u0439 \u0438 \u0433\u043e\u0440\u0438\u0437\u043e\u043d\u0442\u0430\u043b\u044c\u043d\u044b\u0439 \u0448\u0430\u0433\u0438 <code>[dy, dx]<\/code> \u0441\u043a\u043e\u043b\u044c\u0437\u044f\u0449\u0435\u0433\u043e \u043e\u043a\u043d\u0430.<\/p>\n<p>  <\/p>\n<hr>\n<p>  <\/p>\n<h2 id=\"a-name3d3-skolzyaschee-3d-okno-po-nd-massivu-v-numpya\">3. \u0421\u043a\u043e\u043b\u044c\u0437\u044f\u0449\u0435\u0435 3D \u043e\u043a\u043d\u043e \u043f\u043e ND \u043c\u0430\u0441\u0441\u0438\u0432\u0443 \u0432 Numpy<\/h2>\n<p>  <\/p>\n<p><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/webt\/yn\/t1\/ti\/ynt1tiflquki0zi4iufpjxqplkm.png\" alt=\"Rolling 3D window for ND array in Numpy\"><\/p>\n<p>  <\/p>\n<p>\u041c\u043e\u0436\u043d\u043e \u0443\u0432\u0438\u0434\u0435\u0442\u044c \u0448\u0430\u0431\u043b\u043e\u043d (\u043f\u0430\u0442\u0442\u0435\u0440\u043d) \u0434\u043b\u044f \u043e\u0434\u043d\u043e- \u0438 \u0434\u0432\u0443\u043c\u0435\u0440\u043d\u044b\u0445 \u0441\u043a\u043e\u043b\u044c\u0437\u044f\u0449\u0438\u0445 \u043e\u043a\u043e\u043d. \u0421\u043b\u043e\u0436\u043d\u043e \u043f\u043e\u043d\u0438\u043c\u0430\u0442\u044c \u0440\u0430\u0431\u043e\u0442\u0443 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430 \u0432 \u0431\u043e\u043b\u0435\u0435 \u0432\u044b\u0441\u043e\u043a\u0438\u0445 \u0440\u0430\u0437\u043c\u0435\u0440\u043d\u043e\u0441\u0442\u044f\u0445, \u043e\u0434\u043d\u0430\u043a\u043e \u043c\u043e\u0436\u043d\u043e \u0432\u043e\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u0437\u0430\u043c\u0435\u0447\u0435\u043d\u043d\u044b\u043c \u0448\u0430\u0431\u043b\u043e\u043d\u043e\u043c \u0434\u043b\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u0441\u043a\u043e\u043b\u044c\u0437\u044f\u0449\u0435\u0433\u043e 3D \u043e\u043a\u043d\u0430 \u043f\u043e ND-\u043c\u0435\u0440\u043d\u043e\u043c\u0443 \u043c\u0430\u0441\u0441\u0438\u0432\u0443.<\/p>\n<p>  <\/p>\n<p>\u041f\u0440\u0438\u043c\u0435\u0440 \u0441\u043a\u043e\u043b\u044c\u0437\u044f\u0449\u0435\u0433\u043e 3D \u043e\u043a\u043d\u0430 \u043f\u043e 3D \u043c\u0430\u0441\u0441\u0438\u0432\u0443 \u2014 \u044d\u0442\u043e \u0440\u0430\u0437\u043b\u0438\u0447\u043d\u044b\u0435 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u0441 \u0432\u043e\u043a\u0441\u0435\u043b\u0430\u043c\u0438 (\u0442\u0440\u0435\u0445\u043c\u0435\u0440\u043d\u044b\u043c\u0438 \u043f\u0438\u043a\u0441\u0435\u043b\u0430\u043c\u0438) \u0432 \u0442\u0440\u0435\u0445\u043c\u0435\u0440\u043d\u044b\u0445 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f\u0445. \u0412 <a href=\"https:\/\/colab.research.google.com\/drive\/1Zru_-zzbtylgitbwxbi0eDBNhwr8qYl6#3d\">\u0442\u0435\u0441\u0442\u043e\u0432\u043e\u043c CoLab \u043f\u0440\u0438\u043c\u0435\u0440\u0435<\/a> \u0438\u0449\u0443\u0442\u0441\u044f \u043e\u0431\u044b\u0447\u043d\u044b\u0435 \u0441\u043e\u0432\u043f\u0430\u0434\u0435\u043d\u0438\u044f \u043c\u0430\u043b\u043e\u0433\u043e 3D \u043f\u043e\u0434-\u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0432 \u0431\u043e\u043b\u044c\u0448\u0435\u043c, \u043e\u0434\u043d\u0430\u043a\u043e \u043c\u043e\u0436\u043d\u043e \u043f\u0440\u0438\u0434\u0443\u043c\u0430\u0442\u044c \u0438 \u0431\u043e\u043b\u0435\u0435 \u0441\u043b\u043e\u0436\u043d\u044b\u0435 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 (\u0441\u0432\u0435\u0440\u0442\u043a\u0430, \u0446\u0438\u0444\u0440\u043e\u0432\u044b\u0435 \u0444\u0438\u043b\u044c\u0442\u0440\u044b, \u0441\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u044f \u0438 \u0442.\u0434.).<\/p>\n<p>  <\/p>\n<pre><code class=\"python\"># Rolling 3D window for ND array def roll(a,      # ND array          b,      # rolling 2D window array          dx=1,   # horizontal step, abscissa, number of columns          dy=1,   # vertical step, ordinate, number of rows          dz=1):  # transverse step, applicate, number of layers     shape = a.shape[:-3] + \\             ((a.shape[-3] - b.shape[-3]) \/\/ dz + 1,) + \\             ((a.shape[-2] - b.shape[-2]) \/\/ dy + 1,) + \\             ((a.shape[-1] - b.shape[-1]) \/\/ dx + 1,) + \\             b.shape  # multidimensional &quot;sausage&quot; with 3D cross-section     strides = a.strides[:-3] + \\               (a.strides[-3] * dz,) + \\               (a.strides[-2] * dy,) + \\               (a.strides[-1] * dx,) + \\               a.strides[-3:]     #print('shape =', shape, &quot; strides =&quot;, strides)  # for debugging     return np.lib.stride_tricks.as_strided(a, shape=shape, strides=strides)<\/code><\/pre>\n<p>  <\/p>\n<p>\u0414\u043b\u044f \u043d\u0430\u0445\u043e\u0436\u0434\u0435\u043d\u0438\u044f \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u0430 \u0441\u043e\u0432\u043f\u0430\u0434\u0435\u043d\u0438\u0439 <code>counts<\/code> \u0438 \u043a\u043e\u043e\u0440\u0434\u0438\u043d\u0430\u0442 \u044d\u0442\u0438\u0445 \u0441\u043e\u0432\u043f\u0430\u0434\u0435\u043d\u0438\u0439 <code>coords<\/code> \u043f\u0440\u0438\u043c\u0435\u043d\u044f\u0435\u0442\u0441\u044f \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0430\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u044f:<\/p>\n<p>  <\/p>\n<pre><code class=\"python\">def show_results(a, b, dx=1, dy=1, dz=1):     axis = a.ndim  # number of dimensions == 3     # np.all over 3 dimensions of the rolling 3D window     bool_array = np.all(np.all(np.all(             roll(a, b, dx, dy, dz) == b,         axis=axis), axis=axis), axis=axis)     counts = np.count_nonzero(bool_array)     coords = np.transpose(np.nonzero(bool_array)) * [dz, dy, dx]     print(&quot;Found {counts} elements with coordinates:\\n{coords}&quot;.format(         counts=counts, coords=coords))<\/code><\/pre>\n<p>  <\/p>\n<hr>\n<p>  <\/p>\n<h2 id=\"a-namemd4-skolzyaschee-md-okno-po-nd-massivu-gde-m--na\">4. \u0421\u043a\u043e\u043b\u044c\u0437\u044f\u0449\u0435\u0435 MD \u043e\u043a\u043d\u043e \u043f\u043e ND \u043c\u0430\u0441\u0441\u0438\u0432\u0443, \u0433\u0434\u0435 M \u2264 N<\/h2>\n<p>  <\/p>\n<p><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/webt\/9e\/eq\/xy\/9eeqxyn9be2i8suxtldep0-5ale.png\" alt=\"Rolling MD window for ND array in Numpy\"><\/p>\n<p>  <\/p>\n<p>\u041e\u0431\u043e\u0431\u0449\u0438\u043c \u0444\u0443\u043d\u043a\u0446\u0438\u0438 <code>roll<\/code> \u0438 <code>show_results<\/code> \u043d\u0430 \u043c\u043d\u043e\u0433\u043e\u043c\u0435\u0440\u043d\u043e\u0435 \u0441\u043a\u043e\u043b\u044c\u0437\u044f\u0449\u0435\u0435 MD \u043e\u043a\u043d\u043e \u043f\u043e ND \u043c\u0430\u0441\u0441\u0438\u0432\u0443, \u0433\u0434\u0435 \u0440\u0430\u0437\u043c\u0435\u0440\u043d\u043e\u0441\u0442\u044c M \u0441\u043a\u043e\u043b\u044c\u0437\u044f\u0449\u0435\u0433\u043e \u043e\u043a\u043d\u0430 \u043c\u0435\u043d\u044c\u0448\u0435 \u043b\u0438\u0431\u043e \u0440\u0430\u0432\u043d\u0430 \u0440\u0430\u0437\u043c\u0435\u0440\u043d\u043e\u0441\u0442\u0438 N \u0438\u0441\u0445\u043e\u0434\u043d\u043e\u0433\u043e \u043c\u0430\u0441\u0441\u0438\u0432\u0430: M \u2264 N.<\/p>\n<p>  <\/p>\n<pre><code class=\"python\"># Rolling MD window for ND array def roll(a,        # ND array          b,        # rolling MD window array          d=None):  # steps array      # Make several verifications     n = a.ndim  # array dimensions     m = b.ndim  # rolling window dimensions     if m &gt; n:  # check if M \u2264 N         print(&quot;Error: rolling window dimensions is larger than the array dims&quot;)         return None     if d is None:  # steps are equal to 1 by default         d = np.ones(m, dtype=np.uint32)     elif d.ndim != 1 and d.size != m:         print(&quot;Error: steps number must be equal to rolling window dimensions&quot;)         return None     elif not np.issubdtype(d.dtype, np.integer) or \\          not (d &gt; 0).all():         print(&quot;Error: steps must be integer and &gt; 0&quot;)         return None      s = np.flip(d)  # flip the 1D array of step sizes     sub = np.subtract(a.shape[-m:], b.shape[-m:])     steps = tuple(np.divide(sub, s).astype(np.uint32) + 1)     shape = a.shape[:-m] + steps + b.shape      section = tuple(np.multiply(a.strides[-m:], s))     strides = a.strides[:-m] + section + a.strides[-m:]      #print('shape =', shape, &quot; strides =&quot;, strides)  # for debugging     return np.lib.stride_tricks.as_strided(a, shape=shape, strides=strides)<\/code><\/pre>\n<p>  <\/p>\n<p>\u0412\u043d\u0430\u0447\u0430\u043b\u0435 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 <code>roll<\/code> \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0442\u0440\u0438\u0432\u0438\u0430\u043b\u044c\u043d\u0430\u044f \u043f\u0440\u043e\u0432\u0435\u0440\u043e\u0447\u043d\u0430\u044f \u0447\u0430\u0441\u0442\u044c \u0434\u043b\u044f \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0448\u0430\u0433\u043e\u0432 \u0441\u043a\u043e\u043b\u044c\u0437\u044f\u0449\u0435\u0433\u043e \u043e\u043a\u043d\u0430. \u041d\u0435\u0442\u0440\u0438\u0432\u0438\u0430\u043b\u044c\u043d\u044b\u0435 \u0447\u0430\u0441\u0442\u0438 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0435:<\/p>\n<p>  <\/p>\n<ul>\n<li><code>steps = tuple(np.divide(sub, s).astype(np.uint32) + 1)<\/code> \u2014 \u0432\u044b\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u0435 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u0430 \u0448\u0430\u0433\u043e\u0432 \u0441\u043a\u043e\u043b\u044c\u0437\u044f\u0449\u0435\u0433\u043e \u043e\u043a\u043d\u0430 \u043f\u043e \u043c\u043d\u043e\u0433\u043e\u043c\u0435\u0440\u043d\u043e\u043c\u0443 \u043c\u0430\u0441\u0441\u0438\u0432\u0443.<\/li>\n<li><code>section = tuple(np.multiply(a.strides[-m:], s))<\/code> \u2014 \u0440\u0430\u0441\u0441\u0447\u0435\u0442 \u0441\u0435\u043a\u0446\u0438\u0438 (\u0432\u0441\u0442\u0430\u0432\u043a\u0438) \u0434\u043b\u044f \u0441\u0442\u0440\u0430\u0439\u0434\u043e\u0432 \u00ab\u043c\u043d\u043e\u0433\u043e\u043c\u0435\u0440\u043d\u043e\u0439 \u0441\u043e\u0441\u0438\u0441\u043a\u0438\u00bb.<\/li>\n<li>\u0421\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u00ab\u043c\u043d\u043e\u0433\u043e\u043c\u0435\u0440\u043d\u043e\u0439 \u0441\u043e\u0441\u0438\u0441\u043a\u0438\u00bb \u043f\u0443\u0442\u0435\u043c \u0432\u0441\u0442\u0430\u0432\u043a\u0438 <code>section<\/code> \u0432 ND-\u043c\u0430\u0441\u0441\u0438\u0432: <code>strides = a.strides[:-m] + section + a.strides[-m:]<\/code>.<\/li>\n<\/ul>\n<p>  <\/p>\n<p>\u0424\u0443\u043d\u043a\u0446\u0438\u044f \u043d\u0430\u0445\u043e\u0436\u0434\u0435\u043d\u0438\u044f \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u0430 \u0441\u043e\u0432\u043f\u0430\u0434\u0435\u043d\u0438\u0439 <code>counts<\/code> \u0438 \u043a\u043e\u043e\u0440\u0434\u0438\u043d\u0430\u0442 \u044d\u0442\u0438\u0445 \u0441\u043e\u0432\u043f\u0430\u0434\u0435\u043d\u0438\u0439 <code>coords<\/code> \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c:<\/p>\n<p>  <\/p>\n<pre><code class=\"python\">def show_results(a, b, d=None):     n = a.ndim  # array number of dimensions == N     m = b.ndim  # rolling window dimensions == M     if d is None:  # step sizes are equal to 1 by default         d = np.ones(m, dtype=np.uint32)     bool_array = roll(a, b, d) == b     # np.all over M dimensions of the rolling MD window     for i in range(m):         bool_array = np.all(bool_array, axis=n)     counts = np.count_nonzero(bool_array)     # flip 1D array of step sizes and concatenate it with remaining dimensions     s = np.concatenate((np.ones(n-m, dtype=int), np.flip(d)))     coords = np.transpose(np.nonzero(bool_array)) * s     print(&quot;Found {counts} elements with coordinates:\\n{coords}&quot;.format(         counts=counts, coords=coords))<\/code><\/pre>\n<p>  <\/p>\n<p>\u041d\u0435\u0442\u0440\u0438\u0432\u0438\u0430\u043b\u044c\u043d\u044b\u0435 \u0447\u0430\u0441\u0442\u0438 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 <code>show_results<\/code> \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0435:<\/p>\n<p>  <\/p>\n<ul>\n<li>\u0421\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u043b\u043e\u0433\u0438\u0447\u0435\u0441\u043a\u043e\u0433\u043e (\u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0433\u043e) \u043c\u0430\u0441\u0441\u0438\u0432\u0430 <code>bool_array<\/code> \u0438\u043b\u0438 <strong>\u043c\u0430\u0441\u043a\u0438<\/strong> \u0434\u043b\u044f \u043d\u0430\u0439\u0434\u0435\u043d\u043d\u044b\u0445 \u0441\u043e\u0432\u043f\u0430\u0434\u0435\u043d\u0438\u0439. \u0417\u0430\u0442\u0435\u043c \u043f\u0440\u0438\u043c\u0435\u043d\u0435\u043d\u0438\u0435 <a href=\"https:\/\/docs.scipy.org\/doc\/numpy\/reference\/generated\/numpy.all.html\"><code>numpy.all<\/code><\/a> \u043a\u043e \u0432\u0441\u0435\u043c <code>m<\/code> \u0440\u0430\u0437\u043c\u0435\u0440\u043d\u043e\u0441\u0442\u044f\u043c \u0434\u043b\u044f \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0438, \u0432\u0441\u0435 \u043b\u0438 \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u044b \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0432 \u0434\u0430\u043d\u043d\u043e\u043c \u0438\u0437\u043c\u0435\u0440\u0435\u043d\u0438\u0438 \u0438\u043c\u0435\u044e\u0442 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 <code>True<\/code>. \u041e\u0431\u0440\u0430\u0442\u0438\u0442\u0435 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435, \u0447\u0442\u043e <code>bool_array<\/code> \u2014 \u044d\u0442\u043e ND \u043c\u0430\u0441\u0441\u0438\u0432 \u0438 \u043e\u043d \u0438\u043c\u0435\u0435\u0442 <code>axis=n<\/code>, \u043d\u043e <code>np.all<\/code> \u043f\u0440\u0438\u043c\u0435\u043d\u044f\u0435\u0442\u0441\u044f <code>m<\/code> \u0440\u0430\u0437 \u043f\u043e \u0432\u0441\u0435\u043c \u0440\u0430\u0437\u043c\u0435\u0440\u043d\u043e\u0441\u0442\u044f\u043c \u0441\u043a\u043e\u043b\u044c\u0437\u044f\u0449\u0435\u0433\u043e MD \u043e\u043a\u043d\u0430:<\/li>\n<\/ul>\n<p>  <\/p>\n<pre><code class=\"python\">    bool_array = roll(a, b, d) == b     # np.all over M dimensions of the rolling MD window     for i in range(m):         bool_array = np.all(bool_array, axis=n)<\/code><\/pre>\n<p>  <\/p>\n<ul>\n<li>\u0415\u0449\u0435 \u043e\u0434\u043d\u0430 \u043d\u0435\u0442\u0440\u0438\u0432\u0438\u0430\u043b\u044c\u043d\u0430\u044f \u0447\u0430\u0441\u0442\u044c \u0434\u043b\u044f <code>M &lt; N<\/code>. \u0415\u0441\u043b\u0438 <code>M &lt; N<\/code> \u043c\u044b \u0434\u043e\u043b\u0436\u043d\u044b \u043d\u0435 \u0442\u043e\u043b\u044c\u043a\u043e \u043f\u0435\u0440\u0435\u0432\u0435\u0440\u043d\u0443\u0442\u044c \u043e\u0434\u043d\u043e\u043c\u0435\u0440\u043d\u044b\u0439 1D \u043c\u0430\u0441\u0441\u0438\u0432 \u0448\u0430\u0433\u043e\u0432 \u0441\u043a\u043e\u043b\u044c\u0437\u044f\u0449\u0435\u0433\u043e \u043e\u043a\u043d\u0430, \u043d\u043e \u0438 \u043e\u0431\u044a\u0435\u0434\u0438\u043d\u0438\u0442\u044c \u0435\u0433\u043e \u0441 \u043e\u0434\u043d\u043e\u043c\u0435\u0440\u043d\u044b\u043c \u043c\u0430\u0441\u0441\u0438\u0432\u043e\u043c \u0438\u0437 \u0435\u0434\u0438\u043d\u0438\u0446 \u0434\u043b\u044f \u043e\u0441\u0442\u0430\u0432\u0448\u0438\u0445\u0441\u044f \u0440\u0430\u0437\u043c\u0435\u0440\u043d\u043e\u0441\u0442\u0435\u0439 <code>N-M<\/code> (\u0448\u0430\u0433 \u043f\u043e \u0432\u044b\u0441\u0448\u0438\u043c \u0440\u0430\u0437\u043c\u0435\u0440\u043d\u043e\u0441\u0442\u044f\u043c \u0440\u0430\u0432\u0435\u043d 1). \u0415\u0441\u043b\u0438 <code>M == N<\/code>, \u0442\u043e \u043e\u0441\u0442\u0430\u0432\u0448\u0438\u0435\u0441\u044f \u0440\u0430\u0437\u043c\u0435\u0440\u043d\u043e\u0441\u0442\u0438 \u0440\u0430\u0432\u043d\u044b \u043d\u0443\u043b\u044e, \u0438 \u0432 \u044d\u0442\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u043a\u043e\u043d\u043a\u0430\u0442\u0435\u043d\u0430\u0446\u0438\u044f \u043d\u0435 \u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f:<br \/> \n<pre><code class=\"python\"># flip 1D array of step sizes and concatenate it with remaining dimensions s = np.concatenate((np.ones(n-m, dtype=int), np.flip(d)))<\/code><\/pre>\n<\/li>\n<\/ul>\n<p>  <\/p>\n<hr>\n<p>  <\/p>\n<h2 id=\"a-namemd-extended5-skolzyaschee-md-okno-po-nd-massivu-dlya-lyubyh-m-i-na\">5. \u0421\u043a\u043e\u043b\u044c\u0437\u044f\u0449\u0435\u0435 MD \u043e\u043a\u043d\u043e \u043f\u043e ND \u043c\u0430\u0441\u0441\u0438\u0432\u0443 \u0434\u043b\u044f \u043b\u044e\u0431\u044b\u0445 M \u0438 N<\/h2>\n<p>  <\/p>\n<p><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/webt\/bu\/p9\/ue\/bup9uemmvgrmcpriuwfnverg8z8.png\" alt=\"Rolling MD window for ND array extended\"><\/p>\n<p>  <\/p>\n<p>\u0412\u043e\u0437\u043c\u043e\u0436\u043d\u043e \u043b\u0438 \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0441\u043a\u043e\u043b\u044c\u0437\u044f\u0449\u0435\u0435 MD \u043e\u043a\u043d\u043e \u043f\u043e ND \u043c\u0430\u0441\u0441\u0438\u0432\u0443, \u0433\u0434\u0435 M &gt; N? \u0412 \u043e\u0431\u0449\u0435\u043c, \u0434\u0430! \u041e\u0434\u043d\u0430\u043a\u043e \u0442\u043e\u043b\u044c\u043a\u043e \u0447\u0430\u0441\u0442\u044c \u0441\u043a\u043e\u043b\u044c\u0437\u044f\u0449\u0435\u0433\u043e \u043e\u043a\u043d\u0430 \u0431\u0443\u0434\u0435\u0442 <strong>\u043f\u0435\u0440\u0435\u0441\u0435\u043a\u0430\u0442\u044c\u0441\u044f<\/strong> \u0441 ND \u043c\u0430\u0441\u0441\u0438\u0432\u043e\u043c, \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e \u0443 \u0441\u043a\u043e\u043b\u044c\u0437\u044f\u0449\u0435\u0433\u043e MD \u043e\u043a\u043d\u0430 \u0431\u0443\u0434\u0435\u0442 \u0431\u043e\u043b\u044c\u0448\u0435 \u0440\u0430\u0437\u043c\u0435\u0440\u043d\u043e\u0441\u0442\u0435\u0439 M &gt; N.<\/p>\n<p>  <\/p>\n<p>\u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u043d\u0430\u0439\u0434\u0435\u043c <strong>\u0441\u043e\u0432\u043f\u0430\u0434\u0435\u043d\u0438\u044f \u043c\u0435\u0436\u0434\u0443 \u043f\u0435\u0440\u0435\u0441\u0435\u0447\u0435\u043d\u0438\u044f\u043c\u0438<\/strong> MD \u0438 ND \u043c\u0430\u0441\u0441\u0438\u0432\u043e\u0432. \u0418 \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u043c \u0441\u043a\u043e\u043b\u044c\u0437\u044f\u0449\u0435\u0435 MD \u043e\u043a\u043d\u043e \u043f\u043e ND \u043c\u0430\u0441\u0441\u0438\u0432\u0443 \u0434\u043b\u044f \u043b\u044e\u0431\u044b\u0445 M \u0438 N. \u0414\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0438\u0435 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 <code>roll<\/code> \u0438 <code>show_results<\/code>.<\/p>\n<p>  <\/p>\n<pre><code class=\"python\">def get_results(a, b, d=None):  # the same as `show_results` function     n = a.ndim  # array number of dimensions == N     m = b.ndim  # rolling window dimensions == M     if d is None:  # step sizes are equal to 1 by default         d = np.ones(m, dtype=np.uint32)     bool_array = roll(a, b, d) == b     # np.all over M dimensions of the rolling MD window     for i in range(m):         bool_array = np.all(bool_array, axis=n)     counts = np.count_nonzero(bool_array)     # flip 1D array of step sizes and concatenate it with remaining dimensions     s = np.concatenate((np.ones(n-m, dtype=int), np.flip(d)))     coords = np.transpose(np.nonzero(bool_array)) * s     return (counts, coords)  def show_intersections(a, b, d=None):     d_tmp = d     n = a.ndim  # array number of dimensions == N     m = b.ndim  # rolling window dimensions == M     #     if d_tmp is None:  # step sizes are equal to 1 by default         d_tmp = np.ones(m, dtype=np.uint32)     elif m &gt; n and d_tmp.size == n:  # for m &gt; n case         # Concatenate d_tmp with remaining dimensions         d_tmp = np.concatenate((np.ones(m-n, dtype=int), d_tmp))     #     counts = 0     coords = None     if m &lt;= n:         results = get_results(a, b, d_tmp)  # return previous example         counts = results[0]         coords = results[1]     else:  # if m &gt; n         t = m - n  # excessive dimensions         layers = np.prod(b.shape[:t])  # find number of layers         # Reshape MD array into (N+1)D array.         temp = b.reshape((layers,) + b.shape[t:])         # Get results for every layer in the intersection         for i in range(layers):             results = get_results(a, temp[i], d_tmp[t:])             counts += results[0]             if coords is None:                 coords = results[1]             else:                 coords = np.concatenate((coords, results[1]))     print(&quot;Found {counts} elements with coordinates:\\n{coords}&quot;.format(         counts=counts, coords=coords))<\/code><\/pre>\n<p>  <\/p>\n<p>\u0424\u0443\u043d\u043a\u0446\u0438\u044f <code>get_results<\/code> \u0442\u0430 \u0436\u0435 \u0441\u0430\u043c\u0430\u044f, \u0447\u0442\u043e \u0438 <code>show_results<\/code> \u0441 \u043d\u0435\u0431\u043e\u043b\u044c\u0448\u0438\u043c\u0438 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u044f\u043c\u0438 \u0432 \u0432\u044b\u0432\u043e\u0434\u0435 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0430.<\/p>\n<p>  <\/p>\n<p>\u0424\u0443\u043d\u043a\u0446\u0438\u044f <code>show_intersections<\/code> \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u0442 \u043f\u0435\u0440\u0435\u0441\u0435\u0447\u0435\u043d\u0438\u044f \u043c\u0435\u0436\u0434\u0443 \u043c\u043d\u043e\u0433\u043e\u043c\u0435\u0440\u043d\u044b\u043c\u0438 \u043c\u0430\u0441\u0441\u0438\u0432\u0430\u043c\u0438. \u0415\u0441\u043b\u0438 <code>M &lt;= N<\/code>, \u0442\u043e \u0444\u0443\u043d\u043a\u0446\u0438\u044f <code>show_intersections<\/code> \u043f\u0440\u043e\u0441\u0442\u043e \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u0444\u0443\u043d\u043a\u0446\u0438\u044e <code>get_results<\/code>, \u043a\u0430\u043a \u0432 <a href=\"#md\">\u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u043c \u0440\u0430\u0437\u0434\u0435\u043b\u0435<\/a>. \u0415\u0441\u043b\u0438 <code>M &gt; N<\/code>, \u0442\u043e \u043c\u044b \u0434\u043e\u043b\u0436\u043d\u044b \u043d\u0430\u0439\u0442\u0438 \u043f\u0435\u0440\u0435\u0441\u0435\u0447\u0435\u043d\u0438\u0435 \u043c\u0435\u0436\u0434\u0443 \u043c\u0430\u0441\u0441\u0438\u0432\u0430\u043c\u0438 <code>b<\/code> \u0438 <code>a<\/code>.<\/p>\n<p>  <\/p>\n<p>\u0414\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u043d\u0430\u0439\u0434\u0435\u043c \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0438\u0437\u043b\u0438\u0448\u043d\u0438\u0445 \u0440\u0430\u0437\u043c\u0435\u0440\u043d\u043e\u0441\u0442\u0435\u0439 <code>t = m - n<\/code> \u043c\u0435\u0436\u0434\u0443 MD \u043c\u0430\u0441\u0441\u0438\u0432\u043e\u043c <code>b<\/code> \u0438 ND \u043c\u0430\u0441\u0441\u0438\u0432\u043e\u043c <code>a<\/code>. \u0418 \u043d\u0430\u0439\u0434\u0435\u043c \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0441\u043b\u043e\u0435\u0432 \u0432 \u043f\u0435\u0440\u0435\u0441\u0435\u0447\u0435\u043d\u0438\u0438 \u043c\u0435\u0436\u0434\u0443 <code>b<\/code> \u0438 <code>a<\/code>: <code>layers = np.prod(b.shape[:t])<\/code>. \u0417\u0430\u0442\u0435\u043c \u043f\u0435\u0440\u0435\u0444\u043e\u0440\u043c\u0438\u0440\u0443\u0435\u043c (\u043f\u0440\u0438\u0434\u0430\u0434\u0438\u043c \u0438\u043d\u0443\u044e \u0444\u043e\u0440\u043c\u0443, reshape) \u043c\u0430\u0441\u0441\u0438\u0432 <code>b<\/code> \u0438\u0437 MD \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0432 (N+1)D \u043c\u0430\u0441\u0441\u0438\u0432:<\/p>\n<p>  <\/p>\n<pre><code class=\"python\">    # Reshape MD array into (N+1)D array.     temp = b.reshape((layers,) + b.shape[t:])<\/code><\/pre>\n<p>  <\/p>\n<p>\u041d\u0430\u043a\u043e\u043d\u0435\u0446 \u043d\u0430\u0439\u0434\u0435\u043c \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442: \u0441\u043e\u0432\u043f\u0430\u0434\u0435\u043d\u0438\u044f \u043c\u0435\u0436\u0434\u0443 \u0434\u0432\u0443\u043c\u044f \u043c\u0430\u0441\u0441\u0438\u0432\u0430\u043c\u0438 <code>(N+1)D<\/code> \u0438 <code>ND<\/code>, \u0433\u0434\u0435 \u0440\u0430\u0437\u043c\u0435\u0440\u043d\u043e\u0441\u0442\u044c <code>(N+1)<\/code> \u0438\u043c\u0435\u0435\u0442 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0441\u043b\u043e\u0435\u0432 \u0440\u0430\u0432\u043d\u043e\u0435 <code>layers<\/code>:<\/p>\n<p>  <\/p>\n<pre><code class=\"python\">    # Get results for every layer in the intersection     for i in range(layers):         results = get_results(a, temp[i], d_tmp[t:])<\/code><\/pre>\n<p>  <\/p>\n<p>\u041e\u0431\u044a\u0435\u0434\u0438\u043d\u0438\u043c \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0441\u043e\u0432\u043f\u0430\u0434\u0435\u043d\u0438\u0439 <code>counts<\/code> \u0438 \u043d\u0430\u0439\u0434\u044b\u043d\u043d\u044b\u0445 \u043a\u043e\u043e\u0440\u0434\u0438\u043d\u0430\u0442 \u044d\u0442\u0438\u0445 \u0441\u043e\u0432\u043f\u0430\u0434\u0435\u043d\u0438\u0439 <code>coords<\/code> \u0434\u043b\u044f \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u0441\u043b\u043e\u044f:<\/p>\n<p>  <\/p>\n<pre><code class=\"python\">    # Get results for every layer in the intersection     for i in range(layers):         results = get_results(a, temp[i], d_tmp[t:])         counts += results[0]         if coords is None:             coords = results[1]         else:             coords = np.concatenate((coords, results[1]))<\/code><\/pre>\n<p>  <\/p>\n<p>\u0412\u0441\u0435 \u043f\u0440\u0438\u043c\u0435\u0440\u044b \u043d\u0430\u0445\u043e\u0434\u044f\u0442\u0441\u044f \u0432 <a href=\"https:\/\/colab.research.google.com\/drive\/1Zru_-zzbtylgitbwxbi0eDBNhwr8qYl6\">CoLab \u0431\u043b\u043e\u043a\u043d\u043e\u0442\u0435<\/a>.<\/p>\n<p>  <\/p>\n<p>\u0421\u043f\u0430\u0441\u0438\u0431\u043e \u0437\u0430 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435!<\/p>\n<\/div>\n<p> \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\/post\/489734\/\"> https:\/\/habr.com\/ru\/post\/489734\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"\n<div class=\"post__text post__text-html\" id=\"post-content-body\" data-io-article-url=\"https:\/\/habr.com\/ru\/post\/489734\/\">\n<p><a href=\"https:\/\/colab.research.google.com\/drive\/1Zru_-zzbtylgitbwxbi0eDBNhwr8qYl6\">CoLab \u0431\u043b\u043e\u043a\u043d\u043e\u0442<\/a> \u0441 \u043f\u0440\u0438\u043c\u0435\u0440\u0430\u043c\u0438<\/p>\n<p>  <\/p>\n<p>\u0412\u043e\u0437\u043c\u043e\u0436\u043d\u043e \u0441\u0434\u0435\u043b\u0430\u0442\u044c <a href=\"https:\/\/wiki.loginom.ru\/articles\/windowing-method.html\">\u0441\u043a\u043e\u043b\u044c\u0437\u044f\u0449\u0435\u0435 \u043e\u043a\u043d\u043e<\/a> (rolling window, <a href=\"https:\/\/stackoverflow.com\/questions\/8269916\/what-is-sliding-window-algorithm-examples\">sliding window<\/a>, moving window) \u043f\u043e \u043c\u0430\u0441\u0441\u0438\u0432\u0430\u043c NumPy \u043d\u0430 \u044f\u0437\u044b\u043a\u0435 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f Python <strong>\u0431\u0435\u0437 \u044f\u0432\u043d\u044b\u0445 \u0446\u0438\u043a\u043b\u043e\u0432<\/strong>. \u0412 \u0434\u0430\u043d\u043d\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u0440\u0430\u0441\u0441\u043c\u0430\u0442\u0440\u0438\u0432\u0430\u0435\u0442\u0441\u044f \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u043e\u0434\u043d\u043e-, \u0434\u0432\u0443\u0445-, \u0442\u0440\u0435\u0445- \u0438 N-\u043c\u0435\u0440\u043d\u044b\u0445 \u0441\u043a\u043e\u043b\u044c\u0437\u044f\u0449\u0438\u0445 \u043e\u043a\u043e\u043d \u043f\u043e \u043c\u0430\u0441\u0441\u0438\u0432\u0430\u043c NumPy. \u0412 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0435 \u0441\u043a\u043e\u0440\u043e\u0441\u0442\u044c \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0438 \u0434\u0430\u043d\u043d\u044b\u0445 \u0443\u0432\u0435\u043b\u0438\u0447\u0438\u0432\u0430\u0435\u0442\u0441\u044f \u0432 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0442\u044b\u0441\u044f\u0447 \u0440\u0430\u0437 \u0438 \u0441\u0440\u0430\u0432\u043d\u0438\u043c\u0430 \u043f\u043e \u0441\u043a\u043e\u0440\u043e\u0441\u0442\u0438 \u0441 \u044f\u0437\u044b\u043a\u043e\u043c \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f <strong>\u0421<\/strong>.<\/p>\n<p>  <\/p>\n<p>C\u043a\u043e\u043b\u044c\u0437\u044f\u0449\u0435\u0435 \u043e\u043a\u043d\u043e \u043f\u0440\u0438\u043c\u0435\u043d\u044f\u0435\u0442\u0441\u044f \u0432: \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0435 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0439, \u0438\u0441\u043a\u0443\u0441\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u0445 \u043d\u0435\u0439\u0440\u043e\u043d\u043d\u044b\u0445 \u0441\u0435\u0442\u044f\u0445, \u0438\u043d\u0442\u0435\u0440\u043d\u0435\u0442 \u043f\u0440\u043e\u0442\u043e\u043a\u043e\u043b\u0435 TCP, \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0435 \u0433\u0435\u043d\u043e\u043c\u043d\u044b\u0445 \u0434\u0430\u043d\u043d\u044b\u0445, \u043f\u0440\u043e\u0433\u043d\u043e\u0437\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0438 \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0445 \u0440\u044f\u0434\u043e\u0432 \u0438 \u0442.\u0434.<\/p>\n<p>  <\/p>\n<p><strong>\u041e\u0442\u043a\u0430\u0437 \u043e\u0442 \u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0435\u043d\u043d\u043e\u0441\u0442\u0438<\/strong>: <em>\u0432 \u0438\u0441\u0445\u043e\u0434\u043d\u043e\u043c \u043a\u043e\u0434\u0435 \u043c\u043e\u0433\u0443\u0442 \u0431\u044b\u0442\u044c \u043e\u0448\u0438\u0431\u043a\u0438!<\/em> \u0415\u0441\u043b\u0438 \u0432\u044b \u0432\u0438\u0434\u0438\u0442\u0435 \u043e\u0448\u0438\u0431\u043a\u0443, \u043f\u043e\u0436\u0430\u043b\u0443\u0439\u0441\u0442\u0430, \u043d\u0430\u043f\u0438\u0448\u0438\u0442\u0435 \u043c\u043d\u0435.<\/p>\n<p>  <\/p>\n<ul>\n<li><a href=\"#introduction\">\u0412\u0432\u0435\u0434\u0435\u043d\u0438\u0435<\/a><\/li>\n<li><a href=\"#1d\">\u0421\u043a\u043e\u043b\u044c\u0437\u044f\u0449\u0435\u0435 1D \u043e\u043a\u043d\u043e \u043f\u043e ND \u043c\u0430\u0441\u0441\u0438\u0432\u0443 \u0432 Numpy<\/a><\/li>\n<li><a href=\"#2d\">\u0421\u043a\u043e\u043b\u044c\u0437\u044f\u0449\u0435\u0435 2D \u043e\u043a\u043d\u043e \u043f\u043e ND \u043c\u0430\u0441\u0441\u0438\u0432\u0443 \u0432 Numpy<\/a><\/li>\n<li><a href=\"#3d\">\u0421\u043a\u043e\u043b\u044c\u0437\u044f\u0449\u0435\u0435 3D \u043e\u043a\u043d\u043e \u043f\u043e ND \u043c\u0430\u0441\u0441\u0438\u0432\u0443 \u0432 Numpy<\/a><\/li>\n<li><a href=\"#md\">\u0421\u043a\u043e\u043b\u044c\u0437\u044f\u0449\u0435\u0435 MD \u043e\u043a\u043d\u043e \u043f\u043e ND \u043c\u0430\u0441\u0441\u0438\u0432\u0443, \u0433\u0434\u0435 M \u2264 N<\/a><\/li>\n<li><a href=\"#md-extended\">\u0421\u043a\u043e\u043b\u044c\u0437\u044f\u0449\u0435\u0435 MD \u043e\u043a\u043d\u043e \u043f\u043e ND \u043c\u0430\u0441\u0441\u0438\u0432\u0443 \u0434\u043b\u044f \u043b\u044e\u0431\u044b\u0445 M \u0438 N<\/a><\/li>\n<\/ul>\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-299187","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/299187","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=299187"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/299187\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=299187"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=299187"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=299187"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}