44

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?

3
  • 12
    The term "Pythonic" isn't really usable in the way you're using it. Commented Feb 23, 2014 at 13:14
  • So he made up a word. Big Deal! Commented Jan 7 at 21:52
  • Sorry if my comment was inappropriate! Commented Jan 7 at 21:53

4 Answers 4

54

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.

Sign up to request clarification or add additional context in comments.

7 Comments

def isscalar(num): return isinstance(num, generic) or type(num) in ScalarType # Sorry. This was bothering me.
Does "array scalar" means an array of scalars or something else?
@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 -> stackoverflow.com/q/773030/674039
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.
@flow2k: In Python2 there were strs and unicodes. The corresponding objects in Python3 are bytes and strs 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.
|
14

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.

1 Comment

Well done for noticing OP's request for the simplest terms possible
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

2 Comments

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?
@user3268003 Sorry, it's just my basic understanding, thus I upvoted your question and am waiting for a precise explanation as well ;)
0

Drawing on @unutbu's answer...

A numpy scalar is created typically from a "regular" python variable. Typically, you as a user don't need to worry and it just works. However, there are some cases where it can be handy to cast to a numpy scalar.

Consider wanting to add a constant to both values in an array:

[3, 4] + 5

That won't work. None of these types are numpy's and python doesn't know how to do that operation. Two ways that you might do that operation:

np.array([3, 4]) + 5
...or...
[3, 4] + np.int32(5)

Using the second syntax might clean up your code in some cases:

bias = 5
np.array([3, 4, 6]) + bias
np.array([7, 4, 3]) + bias
...could become...
bias = np.int32(5)
[3, 4, 6] + bias
[7, 4, 3] + bias

You might imagine cases where this code looks better.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.