使用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
最满意答案
我最终用元解决方案解决了这个问题,我的一个朋友提出了这个问题。
由于选择位置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!
相关问答
更多-
可以使用整数编程或约束编程来表达吗?(Can this be expressed using Integer Programming or Constraint Programming?)[2024-03-04]
如果你也给出一个积极的例子,而不仅仅是一个消极的例子,那会有所帮助。 我可能在需求/定义中遗漏了一些东西,但这是在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 ...
-
线性编程:逻辑条件(Linear programming: Logic condition)[2024-01-07]
通常类似于: 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 <= ...
-
Python线性编程 - 如何将变量约束为整数(Python linear programming - how to constrain a variable to be an integer)[2023-06-30]
你的目标是真的 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)。 ... -
整数规划不等约束(Integer Programming unequal constraint)[2022-07-15]
我假设所有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 如果 ...