Skip to content

Derive dtype from np.dtype in CastTransformer/Regressor #927

Description

@stephantul

Hi!

Thanks for the great work.

In numpy, any function that receives a dtype can also receive a string representation of that dtype:

import numpy as np

assert np.zeros(3, dtype="float64").dtype == np.zeros(3, dtype=np.float64).dtype
assert np.zeros(3, dtype="float64").dtype != np.zeros(3, dtype=np.float32).dtype

I expected the same behavior in CastTransformer which led to a weird error. The CastTransformer seemed to interpret my string representation of the dtype as a literal string type (i.e., it expected a string as input).

As a fix, we could replace the setting of the dtype in the constructor with an inferred dtype from numpy.

i.e.,

def __init__(self, *, dtype=np.float32):
        self.dtype = np.dtype(dtype)

This looks up the appropriate dtype, and is also a no-op if the passed value is already a dtype (and not a string). Alternatively, we could catch whether the passed value is actually a dtype.

def __init__(self, *, dtype=np.float32):
        if not _is_dtype(dtype):
             raise ValueError("informative message")
        self.dtype = dtype

We would have to implement _is_dtype somehow though.

I fully concede that this was user error on my part, but given this is how numpy behaves, maybe the behavior could be copied.

Happy to contribute a PR.

Activity

  1. xadupre commented on Nov 10, 2022

    @xadupre
    Collaborator

    Feel free to contribute and choose the method you think is the best.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions