|
| 1 | +import pandas as pd |
| 2 | +import numpy as np |
| 3 | +import warnings |
| 4 | + |
| 5 | + |
| 6 | +def check_inputs( |
| 7 | + X, |
| 8 | + y=None, |
| 9 | + numerical_columns=None, |
| 10 | + categorical_columns=None, |
| 11 | + task_type=None, |
| 12 | + min_samples=5, |
| 13 | +): |
| 14 | + """ |
| 15 | + Perform thorough validation on input features and target. |
| 16 | +
|
| 17 | + Parameters |
| 18 | + ---------- |
| 19 | + X : pd.DataFrame or dict |
| 20 | + Input features. |
| 21 | + y : array-like, optional |
| 22 | + Target values. |
| 23 | + numerical_columns : list of str |
| 24 | + Columns expected to be numerical. |
| 25 | + categorical_columns : list of str |
| 26 | + Columns expected to be categorical. |
| 27 | + task_type : str, optional |
| 28 | + One of {"regression", "binary", "multiclass"}. If specified, target checks will apply accordingly. |
| 29 | + min_samples : int, optional |
| 30 | + Minimum number of distinct values required in any feature or target. |
| 31 | +
|
| 32 | + Raises |
| 33 | + ------ |
| 34 | + ValueError |
| 35 | + If any feature or target fails validation checks. |
| 36 | + """ |
| 37 | + if isinstance(X, dict): |
| 38 | + X = pd.DataFrame(X) |
| 39 | + |
| 40 | + if not isinstance(X, pd.DataFrame): |
| 41 | + raise TypeError("X must be a DataFrame or a dict convertible to DataFrame.") |
| 42 | + |
| 43 | + if X.empty: |
| 44 | + raise ValueError("X must not be empty.") |
| 45 | + |
| 46 | + if numerical_columns is None: |
| 47 | + numerical_columns = [] |
| 48 | + if categorical_columns is None: |
| 49 | + categorical_columns = [] |
| 50 | + |
| 51 | + all_cols = set(numerical_columns) | set(categorical_columns) |
| 52 | + missing_cols = all_cols - set(X.columns) |
| 53 | + if missing_cols: |
| 54 | + raise ValueError( |
| 55 | + f"The following specified columns are missing in X: {missing_cols}" |
| 56 | + ) |
| 57 | + |
| 58 | + # Check numerical features |
| 59 | + for col in numerical_columns: |
| 60 | + series = X[col] |
| 61 | + if series.nunique(dropna=False) < min_samples: |
| 62 | + raise ValueError( |
| 63 | + f"Numerical feature '{col}' has less than {min_samples} unique values." |
| 64 | + ) |
| 65 | + if not np.issubdtype(series.dtype, np.number): |
| 66 | + raise TypeError(f"Numerical feature '{col}' must be numeric.") |
| 67 | + if not np.all(np.isfinite(series.dropna())): |
| 68 | + raise ValueError( |
| 69 | + f"Numerical feature '{col}' contains non-finite values (inf or NaN)." |
| 70 | + ) |
| 71 | + |
| 72 | + # Check categorical features |
| 73 | + for col in categorical_columns: |
| 74 | + series = X[col] |
| 75 | + if series.nunique(dropna=False) < 2: |
| 76 | + raise ValueError( |
| 77 | + f"Categorical feature '{col}' has less only a single value ." |
| 78 | + ) |
| 79 | + if pd.api.types.is_numeric_dtype( |
| 80 | + series |
| 81 | + ) and not pd.api.types.is_categorical_dtype(series): |
| 82 | + # allow numerical dtypes only if user intends to encode them |
| 83 | + pass # optionally warn or convert |
| 84 | + if series.isnull().all(): |
| 85 | + raise ValueError(f"Categorical feature '{col}' contains only NaNs.") |
| 86 | + |
| 87 | + # Check y |
| 88 | + if y is not None: |
| 89 | + y = np.array(y) |
| 90 | + |
| 91 | + if y.ndim != 1: |
| 92 | + raise ValueError("y must be a 1D array or Series.") |
| 93 | + |
| 94 | + if len(y) != len(X): |
| 95 | + raise ValueError("X and y must have the same number of samples.") |
| 96 | + |
| 97 | + unique_targets = np.unique(y[~pd.isnull(y)]) |
| 98 | + n_classes = len(unique_targets) |
| 99 | + |
| 100 | + if task_type == "regression": |
| 101 | + if not np.issubdtype(y.dtype, np.number): |
| 102 | + raise TypeError("For regression, target y must be numeric.") |
| 103 | + if not np.all(np.isfinite(y)): |
| 104 | + raise ValueError("Target y contains non-finite values.") |
| 105 | + |
| 106 | + if n_classes <= 10: |
| 107 | + warnings.warn( |
| 108 | + f"Target y has only {n_classes} unique values. " |
| 109 | + "Consider if this should be a classification problem instead of regression.", |
| 110 | + UserWarning, |
| 111 | + ) |
| 112 | + |
| 113 | + elif task_type == "classification": |
| 114 | + if n_classes < 2: |
| 115 | + raise ValueError("Classification tasks requires more than 1 class.") |
0 commit comments