Ask Your Question
2

What is an efficient way to find combinations of length l from a large NumPy array?

asked 2021-10-15 11:00:00 +0000

djk gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2021-07-26 11:00:00 +0000

lalupa gravatar image

One efficient way to find combinations of length l from a large NumPy array is to use the itertools module in Python. Specifically, the itertools.combinations function can be used to generate all possible combinations of length l from an iterable.

Here is an example code snippet:

import numpy as np
from itertools import combinations

# Define numpy array
arr = np.array([1, 2, 3, 4, 5])

# Set the desired combination length
l = 3

# Generate all combinations of length l
combos = list(combinations(arr, l))

print(combos)

In this example, we first define a NumPy array with values [1, 2, 3, 4, 5]. We then set the desired combination length to be 3. Finally, we use the itertools.combinations function to generate all possible combinations of length 3 from the array.

The resulting combos variable is a list of tuples, where each tuple contains a combination of length 3. This method efficiently generates all combinations of a desired length from a large NumPy array.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2021-10-15 11:00:00 +0000

Seen: 8 times

Last updated: Jul 26 '21