Python numpy 中的“标量”是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21968643/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is a "scalar" in numpy?
提问by chopper draw lion4
The documentationstates the purpose of scalars, such as the fact that conventional Python numbers like float and integer are too primitive therefore more complex data types are neccessary.
It also states certain kinds of scalars(data type hierarchy); as well as a couple attributes of scalar.
But it never gives a concrete definition of exactly what a scalar is in the context of Python.
文档说明了标量的用途,例如传统的 Python 数字(如浮点数和整数)过于原始,因此需要更复杂的数据类型。
它还说明了某些类型的标量(数据类型层次结构);以及标量的几个属性。
但它从未给出 Python 上下文中标量究竟是什么的具体定义。
I want to get to the heart of the issue on this. So my question is, in the simplest terms possible, explain to me what a pythonic scalar is.
我想触及这个问题的核心。所以我的问题是,用最简单的术语向我解释什么是 pythonic 标量。
采纳答案by unutbu
A NumPy scalar is any object which is an instance of np.genericor whose typeis in np.ScalarType:
NumPy 标量是任何对象,它是 的实例np.generic或type在 中np.ScalarType:
In [12]: np.ScalarType
Out[13]:
(int,
float,
complex,
long,
bool,
str,
unicode,
buffer,
numpy.int16,
numpy.float16,
numpy.int8,
numpy.uint64,
numpy.complex192,
numpy.void,
numpy.uint32,
numpy.complex128,
numpy.unicode_,
numpy.uint32,
numpy.complex64,
numpy.string_,
numpy.uint16,
numpy.timedelta64,
numpy.bool_,
numpy.uint8,
numpy.datetime64,
numpy.object_,
numpy.int64,
numpy.float96,
numpy.int32,
numpy.float64,
numpy.int32,
numpy.float32)
This definition comes from looking at the source codefor np.isscalar:
这个定义来自看源代码的np.isscalar:
def isscalar(num):
if isinstance(num, generic):
return True
else:
return type(num) in ScalarType
Note that you can test if something is a scalar by using np.isscalar:
请注意,您可以使用以下方法测试某事物是否为标量np.isscalar:
>>> np.isscalar(3.1)
True
>>> np.isscalar([3.1])
False
>>> np.isscalar(False)
True
How do we know what we know?I like learning how people know what they know -- more than the answers themselves. So let me try to explain where the above answer comes from.
我们如何知道我们所知道的?我喜欢学习人们如何知道他们所知道的——而不是答案本身。所以让我试着解释一下上面的答案是从哪里来的。
Having the right tools can help you figure out things like this for yourself.
拥有正确的工具可以帮助您自己解决此类问题。
I found this out by using IPython. Using its TAB-completion feature, typing
我通过使用IPython发现了这一点。使用其 TAB 完成功能,键入
In [19]: import numpy as np
In [20]: np.[TAB]
causes IPython to display all variables in the npmodule namespace. A search for the string "scalar"will lead you to np.ScalarTypeand np.isscalar. Typing
导致 IPython 显示np模块命名空间中的所有变量。搜索字符串"scalar"将引导您到np.ScalarType和np.isscalar。打字
In [20]: np.isscalar?
(note the question mark at the end) prompts IPython to show you where np.isscalaris defined:
(注意末尾的问号)提示 IPython 显示np.isscalar定义的位置:
File: /data1/unutbu/.virtualenvs/dev/lib/python2.7/site-packages/numpy/core/numeric.py
which is how I got to the definition of isscalar. Alternatively, the numpy documentation for isscalarhas a link to the source code as well.
这就是我如何得到isscalar. 或者,numpy 文档isscalar也有指向源代码的链接。
回答by user2357112 supports Monica
In this context, a scalar is one of the things you put in an array. A single 64-bit float or 32-bit int, for example, rather than a whole array of them.
在这种情况下,标量是您放入数组的内容之一。例如,单个 64 位浮点数或 32 位整数,而不是它们的整个数组。
回答by zhangxaochen
Just non-vectors. numpy tries to parse it's vectors to a single numbers (namely, python scalars) when passed as arguments, and would fail when the length of vector is not 1:
只是非向量。当作为参数传递时,numpy 尝试将其向量解析为单个数字(即 python 标量),并且当向量的长度不是1时会失败:
In [44]: float(a)
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-44-93d25633ffc4> in <module>()
----> 1 float(a)
TypeError: only length-1 arrays can be converted to Python scalars

