pythonnumpyscipynumpy-dtype

Scipy: change dtype of interpolation


Is it possible to force scipy's interpolation to output arrays with a specific numpy dtype?

For example, output a float32 array from scipy.interpolate.Rbf()?


Solution

  • No, you always get float64 type. For anything else, cast the output after receiving it, with .astype(np.float32), etc.

    I take Rbf for example: the first thing it does with input data is cast it to np.float_, an alias for float64. As seen here:

    self.xi = np.asarray([np.asarray(a, dtype=np.float_).flatten()
                               for a in args[:-1]])
    

    The nodes of interpolation are computed from that, so they are float64 too. The call method of Rbf object does not cast input data, but NumPy does that anyway within np.dot, because of the datatype of self.nodes:

    return np.dot(self._function(r), self.nodes).reshape(shp)