qiskit-machine-learning icon indicating copy to clipboard operation
qiskit-machine-learning copied to clipboard

Extend type hints in the classifiers and regressors

Open adekusar-drl opened this issue 3 years ago • 3 comments

What is the expected behavior?

Since we are going to introduce categorical labels (and perhaps categorical features), I think, we can re-consider type hints for the classifiers and regressors and make them more like in Scikit-Learn: array_like. This may allow to pass plain lists or other types. As an example take a look at the interface here: https://scikit-learn.org/stable/modules/generated/sklearn.linear_model.LinearRegression.html

This issue is just for discussion.

adekusar-drl avatar Jul 13 '21 10:07 adekusar-drl

@adekusar-drl, could you please share an example or template of how you'd match the SKlearn type hinting for QML?

edoaltamura avatar Feb 26 '24 17:02 edoaltamura

It has been a low priority for a long time, things might have changed. While scikit learn does not expose type hints by default, one of the possible options to take a look at https://numpy.org/doc/stable/reference/typing.html#module-numpy.typing. Numpy has ArrayLike type hint and it may work. This issue is more exploratory one rather something that can be easily implemented. Even if numpy's hinting is good enough, I'd first double check if such hinting brings in any value, not problems.

adekusar-drl avatar Feb 27 '24 09:02 adekusar-drl

For instance https://github.com/qiskit-community/qiskit-machine-learning/blob/c59063af02afbb56cfdb27408068aa1c0f5bc718/qiskit_machine_learning/algorithms/classifiers/pegasos_qsvc.py#L128-L130 would become

import numpy.typing as npt

def fit( 
     self, X: npt.ArrayLike, y: npt.ArrayLike, sample_weight: npt.ArrayLike | None = None 
 ) -> "PegasosQSVC": 

or, more strictly,

import numpy.typing as npt

def fit( 
     self, X: npt.NDArray[np.float64], y: npt.NDArray[np.float64], sample_weight: npt.NDArray[np.float64] | None = None 
 ) -> "PegasosQSVC": 

While young, the new typing in Numpy is covered by tests and CI, and made it into a major release. So I'd consider the basic features relatively stable.

edoaltamura avatar Feb 29 '24 22:02 edoaltamura