Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is a "scalar" in numpy?

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.

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.

like image 870
chopper draw lion4 Avatar asked Feb 23 '14 13:02

chopper draw lion4


People also ask

What does scalar mean in Python?

Python defines only one type of a particular data class (there is only one integer type, one floating-point type, etc.). This can be convenient in applications that don't need to be concerned with all the ways data can be represented in a computer.

What is a scalar vs array?

I would also add that in addition to arrays, a scalar is not a compound data structure such as a struct or class. The difference being an array is multiple elements of the same type, while a structure is multiple elements of arbitrary types. Also, a string could be a non-scalar value.

What is scalar and vector in Python?

A scalar is a single number. A vector is an array of numbers.

What is a scalar object?

Scalar objects are used for singular variables that are not part of a table or an array. If your MIB contains scalar objects, you must run mib2c with a scalar-specific configuration file on the MIB nodes that contain the scalars.


3 Answers

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.

like image 186
unutbu Avatar answered Oct 12 '22 21:10

unutbu


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.

like image 27
user2357112 supports Monica Avatar answered Oct 12 '22 21:10

user2357112 supports Monica


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:

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
like image 20
zhangxaochen Avatar answered Oct 12 '22 20:10

zhangxaochen