什么是“标量"?在numpy中? [英] What is a "scalar" in numpy?

查看:273
本文介绍了什么是“标量"?在numpy中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

文档说明了标量的用途,例如传统的Python数字(例如float和integer)太原始了,因此需要更复杂的数据类型.

它还说明某些类型的标量(数据类型层次结构);以及标量的几个属性.
但它从未给出关于在Python上下文中标量确切是什么的具体定义.

The documentation states 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.

我想在这个问题上成为问题的核心.因此,我的问题是,以最简单的方式向我解释什么是pythonic标量.

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.

推荐答案

NumPy标量是任何

A NumPy scalar is any object which is an instance of np.generic or whose type is in 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)

此定义来自查看源代码对于 np.isscalar :

def isscalar(num):
    if isinstance(num, generic):
        return True
    else:
        return type(num) in ScalarType


请注意,您可以使用np.isscalar来测试某物是否为标量:


Note that you can test if something is a scalar by using 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.

我通过使用 IPython 发现了这一点.使用其TAB补全功能,键入

I found this out by using IPython. Using its TAB-completion feature, typing

In [19]: import numpy as np
In [20]: np.[TAB]

使IPython在np模块名称空间中显示所有变量.搜索字符串"scalar"将引导您进入np.ScalarTypenp.isscalar.键入

causes IPython to display all variables in the np module namespace. A search for the string "scalar" will lead you to np.ScalarType and np.isscalar. Typing

In [20]: np.isscalar?

(请注意末尾的问号)会提示IPython向您显示定义np.isscalar的位置:

(note the question mark at the end) prompts IPython to show you where np.isscalar is defined:

File:       /data1/unutbu/.virtualenvs/dev/lib/python2.7/site-packages/numpy/core/numeric.py

这就是我对isscalar的定义的方式.另外, isscalar 的numpy文档具有指向源代码的链接.以及.

which is how I got to the definition of isscalar. Alternatively, the numpy documentation for isscalar has a link to the source code as well.

这篇关于什么是“标量"?在numpy中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆