I am creating an app to train/use Sklearn models. The user should be able to select the various Sklearn models, at which point the arguments to the selected algorithm must be change-able by the user before training/running. To achieve this efficiently for all (future) Sklearn models, I want to retrieve the typehints and default values for the arguments to the methods during runtime and create the UI automatically based on these.
The Sklearn modules do not use type-hints, though VSCode autocomplete does ‘know’ the types and default values (from what I could find Vscode uses the Typeshed but it seems to be difficult to access it during runtime).
In short:
I want to pass sklearn.linear_model.LinearRegression to something and receive:
(*, fit_intercept: bool = True, copy_X: bool = True, n_jobs: Int | None = None, positive: bool = False)
How would I go about receiving this information during runtime?
2
Answers
All Sklearn classes have a private variable called
_parameter_constraints
which contains a list of contraints for each parameter. In my case, it seems to be easiest to deduce the possible types and UI element from these contraints.E.g., for linear regression:
sklearn
usesnumpy
docstrings, so you can usenumpydoc
and useinspect
to get the signatureUsage: