admin管理员组文章数量:1334914
I generate test data using Python's hypothesis
library.
For example, I am generating a numpy array that may contain 0
and 1
like this:
arr = hypothesis.extra.numpy.arrays(dtype=np.int8, shape=10, elements=st.integers(0, 1))
How can I make sure, that the generated arr
always contains all allowed elements? That is, arr
needs to contain at least one 0
and one 1
.
I also need to ensure that I return an np.array
of dtype=np.int8
with a specific shape.
I generate test data using Python's hypothesis
library.
For example, I am generating a numpy array that may contain 0
and 1
like this:
arr = hypothesis.extra.numpy.arrays(dtype=np.int8, shape=10, elements=st.integers(0, 1))
How can I make sure, that the generated arr
always contains all allowed elements? That is, arr
needs to contain at least one 0
and one 1
.
I also need to ensure that I return an np.array
of dtype=np.int8
with a specific shape.
- This question is similar to: How can I generate a hypothesis strategy to generate a list that contains at least one of each element it samples from?. If you believe it’s different, please edit the question, make it clear how it’s different and/or how the answers on that question are not helpful for your problem. – Kraigolas Commented Nov 20, 2024 at 8:08
- This question points in the right direction, however, I need a numpy array where I can specify the dtype and the exact shape. – Andi Commented Nov 20, 2024 at 8:24
1 Answer
Reset to default 0In this case, I think that filtering is probably the best way forward: generate any array, and throw away the small fraction that don't meet your requirements.
arrays(
dtype=np.int8,
shape=10, # or `(2, 3)`, or whatever shape you desire
elements=st.integers(0, 1)
).filter(
lambda arr: {0, 1}.issubset(arr.flatten())
)
本文标签: Forcing numpy arrays generated by Python hypothesis to contain all allowed elementsStack Overflow
版权声明:本文标题:Forcing numpy arrays generated by Python hypothesis to contain all allowed elements - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742372857a2462577.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论