pythonnumpynupic

Using MultiEncoder, I have an array encoders[] and I want to be able to tell, what data-type does the encoder accept. How is that possible?


Eg.:

encoders = {SDRCategoryEncoder, ScalarEncoder} 
do_magic_and_answer_me_type(encoders[0]) // I want string
do_magic_and_answer_me_type(encoders[1]) // int   (or python equivalents)

Longer: The reason why I'm asking, python's list behaves correctly and keeps data types for various elements, while numpy array converts to a common type.

    >>>a=[1, 'sweet', 2]
    >>>type(a)
    type 'list'>

    >>> type(a[0])
    type 'int'>

    >>> type(a[1])
    type 'str'>

    >>> import numpy
    >>> na = numpy.array(a)
    >>> type(na)
    type 'numpy.ndarray'>

    >>> type(na[0])
    type 'numpy.string_'>

    >>> type(na[1])
    type 'numpy.string_'>

    >>> 

To sum up, I want to either tell what data-type of input an encoder expects, or make numpy.array() behave like python list and keep different data-types.


Solution

  • Python's array is not an array like in C, but instead is an array of pointers to objects which contain the data you put in. The objects retain their own type information, and python is happy because the real array is uniform (all pointers).

    Numpy, on the other hand, takes your data and changes its type so that all the contents are the same type. In this case they all get changed to string because you gave it at least one string. So you shouldn't be using numpy arrays in the way you cite (as input vectors to encoders), use python arrays instead.

    If you're talking about lists of encoders, you can ask the encoder its class and that will tell you the type of data it expects (a test against the base class of encoders accepting strings/categories would probably do it).

    In NuPIC, the MultiEncoder.encoders array is a python array, not a numpy one.