首页 \ 问答 \ 使用stride_tricks创建重叠子阵列(Creating overlapping subarrays using stride_tricks)

使用stride_tricks创建重叠子阵列(Creating overlapping subarrays using stride_tricks)

我发现下面的代码创建了给定长度的重叠子数组。 它做了我想做的事情,除了它还颠倒了我不想要的元素的顺序。 我无法找到关于'shape'和'strides'参数的文档,所以不知道如何改变这种行为。 如何更改'out = ...'行以使元素不反转?

import numpy as np

x = np.array([2,3,1,0])
L = 3 # Row length
strided = np.lib.stride_tricks.as_strided
n = x.strides[0]
out = strided(x[L-1:],shape=(x.size-L+1,L),strides=(n,-n))
print out

I found the below code that creates overlapping subarrays of a given length. It does what I want except that it also reverses the order of the elements, which I do not want. I couldn't really find documentation on the 'shape' and 'strides' arguments and so do not know how to change that behavior. How do I change the 'out = ...' line so that the elements are not reversed?

import numpy as np

x = np.array([2,3,1,0])
L = 3 # Row length
strided = np.lib.stride_tricks.as_strided
n = x.strides[0]
out = strided(x[L-1:],shape=(x.size-L+1,L),strides=(n,-n))
print out

原文:https://stackoverflow.com/questions/42074680
更新时间:2021-10-01 21:10

最满意答案

我最终用元解决方案解决了这个问题,我的一个朋友提出了这个问题。

由于选择位置X的产品推断出其他允许的选择(即X + 1和X + 2),因此优化产品组而不是单个产品是有意义的。 我已经建立了它并且它的工作非常好。

谢谢你的回复!


I ended up solving this problem with a meta solution, that a friend of mine came up with.

Since choosing the product with position X infers the other allowed choices (namely X+1 and X+2) it makes sense to optimize on groups of products, not on individual products. I've built this and it works beautifully.

Thanks for the responses!

相关问答

更多
  • 如果你也给出一个积极的例子,而不仅仅是一个消极的例子,那会有所帮助。 我可能在需求/定义中遗漏了一些东西,但这是在Constraint Programming(CP)系统MiniZinc( http://minizinc.org/ )中进行的一种方式。 它不使用CP系统特有的任何特定约束 - 除了函数语法之外,因此应该可以将其转换为其他CP或IP系统。 % dimensions int: rows = 3; int: cols = 4; % the matrix array[1..rows, 1..cols ...
  • 约束 ((x1 <= x2) AND (x1 <= x3)) OR ((x1 >= x2) AND (x1 >= x3)) 可以用一个额外的二进制变量来表达: x1 <= x2 + delta*M x1 <= x3 + delta*M x1 >= x2 - (1-delta)*M x1 >= x3 - (1-delta)*M delta in {0,1} 大多数先进的解算器都有指标限制,使我们可以在不使用大M的情况下编写这些指令: delta = 0 -> x1 <= x2 delta ...
  • 我最终用元解决方案解决了这个问题,我的一个朋友提出了这个问题。 由于选择位置X的产品推断出其他允许的选择(即X + 1和X + 2),因此优化产品组而不是单个产品是有意义的。 我已经建立了它并且它的工作非常好。 谢谢你的回复! I ended up solving this problem with a meta solution, that a friend of mine came up with. Since choosing the product with position X infers th ...
  • 我建议稍微写一些其他案例。 因此,似乎您想要约束所有行和和所有列总和: 对于N = 3,有9个变量(我在这里假设一个方形的情况;你没有提供完整的信息): x00 x01 x02 x10 x11 x12 x20 x21 x22 现在约束矩阵看起来像: x00 x01 x02 | x10 x11 x12 | x20 x21 x22 --------------------------------------- 1 1 1 1 1 1 ...
  • 通常类似于: 5 - M(1-c) <= x <= 5 + M(1-c) 5.001 - Mc - Md <= x <= 4.999 + Mc + M(1-d) c,d in {0,1} x in [-M,M] 在实践中,我们通常可以稍微简化一下。 In general something like: 5 - M(1-c) <= x <= 5 + M(1-c) 5.001 - Mc - Md <= x <= 4.999 + Mc + M(1-d) c,d in {0,1} x in [-M,M] In ...
  • 数学 Mathematica内置此功能(注意:Mathematica不是免费软件。) LinearProgramming[c, m, b, Automatic, Integers] 输出: {1, 1, 0} Mathematica Mathematica has this built in. (NB: Mathematica is not free software.) LinearProgramming[c, m, b, Automatic, Integers] outputs: {1, 1, 0 ...
  • 具有您定义的约束的数学程序不能表示为线性程序,因此无法使用未修改的单纯形实现来解决。 推理很简单 - 线性程序的可行集必须是凸的。 像{x = 0 or x >= 2}这样的集合不是凸的,因为它包含点x=0和x=2但不包含x=1 。 结果,你将被迫使用其他数学编程技术; 我想到的是混合整数线性规划(MILP)。 对于具有x_i = 0 or x_i >= c_i形式约束的每个变量x_i ,您将定义辅助变量y_i以及以下约束: x_i >= c_iy_i x_i <= My_i y_i binary 如果y ...
  • 你可以改写: 1 QB 2 RB 2 WR 1 TE 1 FLEX (either a RB, WR, or TE) 如 num(QB) == 1 2 <= num(RB) <= 3 2 <= num(WR) <= 3 1 <= num(TE) <= 2 num(RB) + num(WR) + num(TE) == 6 You can rewrite: 1 QB 2 RB 2 WR 1 TE 1 FLEX (either a RB, WR, or TE) As num(QB) == 1 2 <= ...
  • 你的目标是真的 ExpWin(t) = choose(N,t)*(A-C*t) 其中t是一个整数变量,N,A,C是常数。 这是一个非线性函数,所以线性MIP求解器将无法处理这个问题。 对于这个问题它很愚蠢,但总的来说,我们可以将其线性化。 引入二元变量x(i): x(i) = 1 if tickets=i 0 otherwise 这可以通过执行 sum(i,x(i)) = 1 sum(i,i*x(i)) = tickets 这只有在可变门票的范围很小时才有意义(在你的情况下为10)。 ...
  • 我假设所有x_i都是整数。 设L和U为常数 L <= x_1+x_2 + ... +x_n <= U 和一个二进制变量。 这些约束表达了您的期望: x_1+x_2 + ... +x_n >= d+1 + (L-d-1)y x_1+x_2 + ... +x_n <= d-1 + (U-d+1)(1-y) 如果y=0则第一约束x_1 +x_2 + ... +x_n >= d+1必须保持,并且x_1+x_2 + ... +x_n <= U的定义满足第二约束x_1+x_2 + ... +x_n <= U 如果 ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)