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.
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.
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.
terminology - What does it mean when data is scalar? - Software Engineering Stack Exchange
What are numpy scalars used for?
Why do I keep getting runtimewarning: invalid value encountred in scalar power?
Created my first dataframe ever in Pandas, what is this scalar error?
Videos
The term "scalar" comes from linear algebra, where it is used to differentiate a single number from a vector or matrix. The meaning in computing is similar. It distinguishes a single value like an integer or float from a data structure like an array. This distinction is very prominent in Perl, where the $ sigil (which resembles an 's') is used to denote a scalar variable and an @ sigil (which resembles an 'a') denotes an array. It doesn't have anything to do with the type of the element itself. It could be a number, character, string, or object. What matters to be called a scalar is that there is one of them.
A supplemental mnemonic, to Karl Bielefeldt's great answer:
A simple way of thinking about it, is "can this be on a scale?"
An integer can be on a scale.
A fixed-size integer can be on a scale, e.g. from -2147483648 to 2147483647.
A real number can be on a scale.
A character, boolean, or fixed-precision decimal can all be on a scale. Even a string can be on a scale (we use such in sorting).
Hence "scalar".
A database row cannot be on a scale. A complex number cannot be on a scale. An object representing an email message cannot be on a scale. An array, vector or matrix cannot be on a scale.
Am I correct in treating scalars as souped-up versions of 'standard' Python data types (+ some new data types not in base Python), which happen to have the same attributes and methods that ndarrays have, even though many of such attributes/methods are meaningless to scalars?
The one part that confuses me a lot on the document is the term "array scalars". What does the word array have to do with the scalar?
https://numpy.org/doc/stable/reference/arrays.scalars.html