`numpy.arange` 中的 `a` 是什么意思?

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

What is the 'a' in numpy.arange?

问题

numpy.arange 方法中的 'a' 代表什么?它与 Python 内置的 range 方法有何不同(在定义上,而非性能等方面)?

我尝试在网上寻找答案,但找到的都是类似 GeeksForGeeks 的教程,介绍如何使用 numpy.arange

英文:

What does the 'a' in numpy's numpy.arange method stand for, and how does it differ from a simple range produced by Python's builtin range method (definitionally, not in terms of performance and whatnot)?

I tried looking online for an answer to this, but all I find is tutorials for how to use numpy.arange by GeeksForGeeks and co.

答案1

得分: 2

You can inspect the return types and reason about what it could mean that way:

print(type(range(0,5)))
import numpy as np
print(type(np.arange(0,5)))

Which prints:

<class 'range'>
<class 'numpy.ndarray'>

Here's a related question: Link to Stack Overflow

  1. Some people do from numpy import * which would shadow range which causes problems.
  2. Naming the function arrayrange was not chosen because it's too long to type.
英文:

You can inspect the return types and reason about what it could mean that way:

print(type(range(0,5))) 
import numpy as np  
print(type(np.arange(0,5)))

Which prints:

&lt;class &#39;range&#39;&gt;
&lt;class &#39;numpy.ndarray&#39;&gt;

Here's a related question: https://stackoverflow.com/questions/55102806/why-was-the-name-arange-chosen-for-the-numpy-function

  1. Some people do from numpy import * which would shadow range which causes problems.
  2. Naming the function arrayrange was not chosen because it's too long to type.

答案2

得分: 1

根据之前的 Stack Overflow 帖子,我们了解到 a 在某种意义上代表着 array(数组)。arange 是一个函数,返回一个类似于 list(range(...)) 产生的列表的 NumPy 数组,在简单情况下至少是相似的。根据官方的 arange 文档:

对于整数参数,该函数大致相当于 Python 内置的 range,但返回的是一个 ndarray 而不是一个 range 实例。

在 Python 3 中,range 本身是“未评估的”,类似于生成器。这相当于 Python 2 中的 xrange

最好的“定义”是官方文档页面:

https://numpy.org/doc/stable/reference/generated/numpy.arange.html

也许你想知道何时使用其中的一个。简单的答案是 - 如果你正在进行 Python 级别的迭代,通常使用 range 更好。如果你需要一个数组,就使用 arange(或者如文档建议的 np.linspace)。

我经常使用 arange 来创建一个示例数组,比如:

np.arange(12).reshape(3,4)
# 输出:
# array([[ 0,  1,  2,  3],
#        [ 4,  5,  6,  7],
#        [ 8,  9, 10, 11]])

虽然可以从 range 创建一个数组,例如 np.array(range(5)),但这相对较慢。np.fromiter(range(5),int) 更快,但仍然不及直接使用 np.arange

英文:

From the previous SO we learn that the 'a' stands, in some sense, for 'array'. arange is a function that returns a numpy array that is similar, at least in simple cases, to the list produced by list(range(...)). From the official arange docs:

> For integer arguments the function is roughly equivalent to the Python built-in range, but returns an ndarray rather than a range instance.

In [104]: list(range(-3,10,2))
Out[104]: [-3, -1, 1, 3, 5, 7, 9]

In [105]: np.arange(-3,10,2)
Out[105]: array([-3, -1,  1,  3,  5,  7,  9])

In py3, range by itself is "unevaluated", it's generator like. It's the equivalent of the py2 xrange.

The best "definition" is the official documentation page:

https://numpy.org/doc/stable/reference/generated/numpy.arange.html

But maybe you are wondering when to use one or the other. The simple answer is - if you are doing python level iteration, range is usually better. If you need an array, use arange (or np.linspace as suggested by the docs).

In [106]: [x**2 for x in range(5)]
Out[106]: [0, 1, 4, 9, 16]

In [107]: np.arange(5)**2
Out[107]: array([ 0,  1,  4,  9, 16])

I often use arange to create a example array, as in:

In [108]: np.arange(12).reshape(3,4)
Out[108]: 
array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11]])

While it is possible to make an array from a range, e.g. np.array(range(5)), that is relatively slow. np.fromiter(range(5),int) is faster, but still not as good as the direct np.arange.

答案3

得分: 0

'a' 在 numpy.arange 中代表 'array'。Numpy.arange 是一个生成在给定区间内连续数字数组的函数。它与 Python 内置的 range() 函数不同,因为它可以处理浮点数以及任意步长。另外,numpy.arange 的输出是一个元素数组,而不是一个范围对象。

英文:

The 'a' stands for 'array' in numpy.arange. Numpy.arange is a function that produces an array of sequential numbers within a given interval. It differs from Python's builtin range() function in that it can handle floating-point numbers as well as arbitrary step sizes. Also, the output of numpy.arange is an array of elements instead of a range object.

huangapple
  • 本文由 发表于 2023年2月18日 00:51:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/75487100.html
匿名

发表评论

匿名网友

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

确定