I have the following simple function:
def f1(y_true, y_pred):
return {"f1": 100 * sklearn.metrics.f1_score(y_true, y_pred)}
According to the scikit-learn documentation, the arguments to f1_score can have the following types:
y_true: 1d array-like, or label indicator array / sparse matrixy_pred: 1d array-like, or label indicator array / sparse matrix
and the output is of type:
- float or array of float, shape = [n_unique_labels]
How do I add type hints to this function so that mypy doesn't complain?
I tried variations of the following:
Array1D = NewType('Array1D', Union[np.ndarray, List[np.float64]])
def f1(y_true: Union[List[float], Array1D], y_pred: Union[List[float], Array1D]) -> Dict[str, Union[List[float], Array1D]]:
return {"f1": 100 * sklearn.metrics.f1_score(y_true, y_pred)}
but that gave errors.