41

The documentation states the purpose of scalars, such as the fact that conventional Python numbers like float and integer are too primitive, and therefore more complex data types are necessary.

It also states certain kinds of scalars (data type hierarchy); as well as a couple of attributes of scalar. But it never gives a concrete definition of exactly what a scalar is in the context of Python.

I want to get to the heart of the issue on this. In the simplest terms possible, what is a Pythonic scalar?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
chopper draw lion4
  • 12,401
  • 13
  • 53
  • 100

3 Answers3

50

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)

This definition comes from looking at the source code for 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(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

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

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?

(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

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
unutbu
  • 842,883
  • 184
  • 1,785
  • 1,677
  • `def isscalar(num): return isinstance(num, generic) or type(num) in ScalarType # Sorry. This was bothering me.` – Blacklight Shining Feb 23 '14 at 17:40
  • Does "array scalar" means an array of scalars or something else? – zhangxaochen Feb 24 '14 at 12:02
  • @unutbu I think that's not correct. An "array scalar" in numpy context is a 0-dim array, e.g. an object like `np.array(3)` . related -> http://stackoverflow.com/q/773030/674039 – wim May 05 '15 at 12:23
  • Array scalars aren't quite 0-dimensional arrays - 0-dimensional arrays are different. Particularly, 0-dimensional arrays can have their element reassigned. Array scalars are the things you get if you index a single element from an array. – user2357112 Oct 18 '17 at 20:28
  • @user2357112: Ah. I've been oblivious to the distinction. Thanks very much for the correction. – unutbu Oct 18 '17 at 21:02
  • @unutbu How do we reconcile the `np.ScalarType` list with what's shown on the documentation linked to by OP https://docs.scipy.org/doc/numpy/reference/arrays.scalars.html#arrays-scalars? Neither is a subset of the other. e.g. `byte` in the doc is not in this list... – flow2k Aug 19 '19 at 03:40
  • 1
    @flow2k: In Python2 there were `str`s and `unicode`s. [The corresponding objects in Python3](https://docs.python.org/3.0/whatsnew/3.0.html#text-vs-data-instead-of-unicode-vs-8-bit) are `bytes` and `str`s respectively. My original answer was written while using Python2 so `np.ScalarType` listed `str` and `unicode`. Now on Python3, `np.ScalarType` lists `bytes` and `str`. The docs are written for Python3. – unutbu Aug 19 '19 at 11:32
13

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.

user2357112
  • 260,549
  • 28
  • 431
  • 505
2

Just non-vectors. NumPy tries to parse its vectors to single numbers (namely, Python scalars) when passed as arguments and would fail when the length of vector is not 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
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
zhangxaochen
  • 32,744
  • 15
  • 77
  • 108
  • Thanks for your hasty reply Zhang. That's not really describing what a vector is though. You only described what a scalar is NOT, IE, (not a vector). Are all data types in numpy divisible into scalars and vectors? Or would there be other categories as well? – chopper draw lion4 Feb 23 '14 at 13:38
  • @user3268003 Sorry, it's just my basic understanding, thus I upvoted your question and am waiting for a precise explanation as well ;) – zhangxaochen Feb 23 '14 at 13:43