no_variance_data_check =================================================== .. py:module:: evalml.data_checks.no_variance_data_check .. autoapi-nested-parse:: Data check that checks if the target or any of the features have no variance. Module Contents --------------- Classes Summary ~~~~~~~~~~~~~~~ .. autoapisummary:: evalml.data_checks.no_variance_data_check.NoVarianceDataCheck Contents ~~~~~~~~~~~~~~~~~~~ .. py:class:: NoVarianceDataCheck(count_nan_as_value=False) Check if the target or any of the features have no variance. :param count_nan_as_value: If True, missing values will be counted as their own unique value. Additionally, if true, will return a DataCheckWarning instead of an error if the feature has mostly missing data and only one unique value. Defaults to False. :type count_nan_as_value: bool **Methods** .. autoapisummary:: :nosignatures: evalml.data_checks.no_variance_data_check.NoVarianceDataCheck.name evalml.data_checks.no_variance_data_check.NoVarianceDataCheck.validate .. py:method:: name(cls) Return a name describing the data check. .. py:method:: validate(self, X, y=None) Check if the target or any of the features have no variance (1 unique value). :param X: The input features. :type X: pd.DataFrame, np.ndarray :param y: Optional, the target data. :type y: pd.Series, np.ndarray :returns: A dict of warnings/errors corresponding to features or target with no variance. :rtype: dict .. rubric:: Examples >>> import pandas as pd Columns or target data that have only one unique value will raise an error. >>> X = pd.DataFrame([2, 2, 2, 2, 2, 2, 2, 2], columns=["First_Column"]) >>> y = pd.Series([1, 1, 1, 1, 1, 1, 1, 1]) ... >>> novar_dc = NoVarianceDataCheck() >>> assert novar_dc.validate(X, y) == [ ... { ... "message": "'First_Column' has 1 unique value.", ... "data_check_name": "NoVarianceDataCheck", ... "level": "warning", ... "details": {"columns": ["First_Column"], "rows": None}, ... "code": "NO_VARIANCE", ... "action_options": [ ... { ... "code": "DROP_COL", ... "data_check_name": "NoVarianceDataCheck", ... "parameters": {}, ... "metadata": {"columns": ["First_Column"], "rows": None} ... }, ... ] ... }, ... { ... "message": "Y has 1 unique value.", ... "data_check_name": "NoVarianceDataCheck", ... "level": "warning", ... "details": {"columns": ["Y"], "rows": None}, ... "code": "NO_VARIANCE", ... "action_options": [] ... } ... ] By default, NaNs will not be counted as distinct values. In the first example, there are still two distinct values besides None. In the second, there are no distinct values as the target is entirely null. >>> X["First_Column"] = [2, 2, 2, 3, 3, 3, None, None] >>> y = pd.Series([1, 1, 1, 2, 2, 2, None, None]) >>> assert novar_dc.validate(X, y) == [] ... ... >>> y = pd.Series([None] * 7) >>> assert novar_dc.validate(X, y) == [ ... { ... "message": "Y has 0 unique values.", ... "data_check_name": "NoVarianceDataCheck", ... "level": "warning", ... "details": {"columns": ["Y"], "rows": None}, ... "code": "NO_VARIANCE_ZERO_UNIQUE", ... "action_options":[] ... } ... ] As None is not considered a distinct value by default, there is only one unique value in X and y. >>> X["First_Column"] = [2, 2, 2, 2, None, None, None, None] >>> y = pd.Series([1, 1, 1, 1, None, None, None, None]) >>> assert novar_dc.validate(X, y) == [ ... { ... "message": "'First_Column' has 1 unique value.", ... "data_check_name": "NoVarianceDataCheck", ... "level": "warning", ... "details": {"columns": ["First_Column"], "rows": None}, ... "code": "NO_VARIANCE", ... "action_options": [ ... { ... "code": "DROP_COL", ... "data_check_name": "NoVarianceDataCheck", ... "parameters": {}, ... "metadata": {"columns": ["First_Column"], "rows": None} ... }, ... ] ... }, ... { ... "message": "Y has 1 unique value.", ... "data_check_name": "NoVarianceDataCheck", ... "level": "warning", ... "details": {"columns": ["Y"], "rows": None}, ... "code": "NO_VARIANCE", ... "action_options": [] ... } ... ] If count_nan_as_value is set to True, then NaNs are counted as unique values. In the event that there is an adequate number of unique values only because count_nan_as_value is set to True, a warning will be raised so the user can encode these values. >>> novar_dc = NoVarianceDataCheck(count_nan_as_value=True) >>> assert novar_dc.validate(X, y) == [ ... { ... "message": "'First_Column' has two unique values including nulls. Consider encoding the nulls for this column to be useful for machine learning.", ... "data_check_name": "NoVarianceDataCheck", ... "level": "warning", ... "details": {"columns": ["First_Column"], "rows": None}, ... "code": "NO_VARIANCE_WITH_NULL", ... "action_options": [ ... { ... "code": "DROP_COL", ... "data_check_name": "NoVarianceDataCheck", ... "parameters": {}, ... "metadata": {"columns": ["First_Column"], "rows": None} ... }, ... ] ... }, ... { ... "message": "Y has two unique values including nulls. Consider encoding the nulls for this column to be useful for machine learning.", ... "data_check_name": "NoVarianceDataCheck", ... "level": "warning", ... "details": {"columns": ["Y"], "rows": None}, ... "code": "NO_VARIANCE_WITH_NULL", ... "action_options": [] ... } ... ]