Most efficient way to create an (n*m by m) numpy array with the first row all zeros and last row all n in Python?

huangapple go评论96阅读模式
英文:

Most efficient way to create an (n*m by m) numpy array with the first row all zeros and last row all n in Python?

问题

以下是翻译好的内容,去除了代码部分:

我正在寻找创建一个具有m列的数组的最有效方法,以便最终结果如下:

如果n = 2,m = 3,我们将得到:

我尽量避免使用for循环,并希望能够将其扩展,以便可以给出任何正整数(n,m)。

额外问题:是否有一个这样的数组的名称?

我尝试过np.meshgrid(),但那不会产生正确的数组。我尝试过np.repeat(),也失败了。

英文:

I am looking for the most effective way to create an array that has m columns so that the final outcome is:

  1. [[0, 0, 0, ..., 0]
  2. [1, 0, 0, ..., 0]
  3. [2, 0, 0, ..., 0]
  4. [3, 0, 0, ..., 0]
  5. [4, 0, 0, ..., 0]
  6. [5, 0, 0, ..., 0]
  7. ...
  8. [n, 0, 0, ..., 0]
  9. [n, 1, 0, ..., 0]
  10. [n, 2, 0, ..., 0]
  11. ...
  12. [n, n, 0, ..., 0]
  13. ...
  14. [n, n, n, ..., n-3]
  15. [n, n, n, ..., n-2]
  16. [n, n, n, ..., n-1]
  17. [n, n, n, ..., n]]

If n = 2 and m = 3 we would have:

  1. [[0, 0, 0]
  2. [1, 0, 0]
  3. [2, 0, 0]
  4. [0, 1, 0]
  5. [1, 1, 0]
  6. [2, 1, 0]
  7. [0, 2, 0]
  8. [1, 2, 0]
  9. [2, 2, 0]
  10. [0, 0, 1]
  11. [1, 0, 1]
  12. [2, 0, 1]
  13. [0, 1, 1]
  14. [1, 1, 1]
  15. [2, 1, 1]
  16. [0, 2, 1]
  17. [1, 2, 1]
  18. [2, 2, 1]
  19. [0, 0, 2]
  20. [1, 0, 2]
  21. [2, 0, 2]
  22. [0, 1, 2]
  23. [1, 1, 2]
  24. [2, 1, 2]
  25. [0, 2, 2]
  26. [1, 2, 2]
  27. [2, 2, 2]]

I am trying to avoid as much as possible for-loops, and I would like to be able to scale it up so that I can give any positive, non-null (n,m).

Bonus question: is there a name for such an array?

I have tried np.meshgrid(), but that does not yield to the right array.
I tried np.repeat(), and also failed.

答案1

得分: 1

你可以使用 itertools 的 product 函数,然后执行以下代码:

  1. n = 2
  2. m = 3
  3. # 注意:我使用 perm[::-1] 来颠倒 product 输出的顺序,以匹配问题中所需的顺序
  4. output = np.array(
  5. [perm[::-1] for perm in product(range(n + 1), repeat=m)]
  6. )
  7. print(output)
  8. [[0 0 0]
  9. [1 0 0]
  10. [2 0 0]
  11. [0 1 0]
  12. [1 1 0]
  13. [2 1 0]
  14. [0 2 0]
  15. [1 2 0]
  16. [2 2 0]
  17. [0 0 1]
  18. [1 0 1]
  19. [2 0 1]
  20. [0 1 1]
  21. [1 1 1]
  22. [2 1 1]
  23. [0 2 1]
  24. [1 2 1]
  25. [2 2 1]
  26. [0 0 2]
  27. [1 0 2]
  28. [2 0 2]
  29. [0 1 2]
  30. [1 1 2]
  31. [2 1 2]
  32. [0 2 2]
  33. [1 2 2]
  34. [2 2 2]]
英文:

You can use the itertools product function and do:

  1. n = 2
  2. m = 3
  3. # note: I do perm[::-1] to reverse the output of
  4. # product to match the required order in the question
  5. output = np.array(
  6. [perm[::-1] for perm in product(range(n + 1), repeat=m)]
  7. )
  8. print(output)
  9. [[0 0 0]
  10. [1 0 0]
  11. [2 0 0]
  12. [0 1 0]
  13. [1 1 0]
  14. [2 1 0]
  15. [0 2 0]
  16. [1 2 0]
  17. [2 2 0]
  18. [0 0 1]
  19. [1 0 1]
  20. [2 0 1]
  21. [0 1 1]
  22. [1 1 1]
  23. [2 1 1]
  24. [0 2 1]
  25. [1 2 1]
  26. [2 2 1]
  27. [0 0 2]
  28. [1 0 2]
  29. [2 0 2]
  30. [0 1 2]
  31. [1 1 2]
  32. [2 1 2]
  33. [0 2 2]
  34. [1 2 2]
  35. [2 2 2]]

huangapple
  • 本文由 发表于 2023年3月7日 16:59:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/75659825.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定