Download scorer.py from ClarusC64/cascade-f1-teamstrategy-coupling-rival-response-tyre-delta-gap-v0.1: direct link, hf CLI and curl.
- Browser
- Download file 2.67 kB
-
https://huggingface.co/datasets/ClarusC64/cascade-f1-teamstrategy-coupling-rival-response-tyre-delta-gap-v0.1/resolve/main/scorer.py
- Command line
-
hf download hf://datasets/ClarusC64/cascade-f1-teamstrategy-coupling-rival-response-tyre-delta-gap-v0.1/scorer.py
-
curl -L -o scorer.py https://huggingface.co/datasets/ClarusC64/cascade-f1-teamstrategy-coupling-rival-response-tyre-delta-gap-v0.1/resolve/main/scorer.py
2.67 kB
| import json | |
| from pathlib import Path | |
| import numpy as np | |
| import pandas as pd | |
| from sklearn.linear_model import LogisticRegression | |
| from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score, confusion_matrix | |
| DATA_DIR = Path("data") | |
| TRAIN_PATH = DATA_DIR / "train.csv" | |
| TEST_PATH = DATA_DIR / "tester.csv" | |
| def find_label_column(df: pd.DataFrame) -> str: | |
| label_cols = [c for c in df.columns if c.startswith("label_")] | |
| if not label_cols: | |
| raise ValueError("No label column found. Expected a column like label_<target_name>.") | |
| return sorted(label_cols)[0] | |
| def to_int_labels(y: pd.Series) -> np.ndarray: | |
| if y.dtype == bool: | |
| return y.astype(int).to_numpy() | |
| if np.issubdtype(y.dtype, np.number): | |
| return y.astype(int).to_numpy() | |
| y_str = y.astype(str).str.strip().str.lower() | |
| mapping = { | |
| "0": 0, "1": 1, | |
| "false": 0, "true": 1, | |
| "no": 0, "yes": 1, | |
| "neg": 0, "pos": 1, | |
| "negative": 0, "positive": 1, | |
| "green": 0, "red": 1, | |
| "amber": 1, | |
| } | |
| if not y_str.isin(mapping.keys()).all(): | |
| unknown = sorted(set(y_str.unique()) - set(mapping.keys())) | |
| raise ValueError(f"Unknown label values: {unknown}") | |
| return y_str.map(mapping).astype(int).to_numpy() | |
| def main() -> None: | |
| if not TRAIN_PATH.exists(): | |
| raise FileNotFoundError(f"Missing {TRAIN_PATH}") | |
| if not TEST_PATH.exists(): | |
| raise FileNotFoundError(f"Missing {TEST_PATH}") | |
| train = pd.read_csv(TRAIN_PATH) | |
| test = pd.read_csv(TEST_PATH) | |
| label_col = find_label_column(train) | |
| if label_col not in test.columns: | |
| raise ValueError(f"Label column {label_col} missing from tester.csv") | |
| feature_cols = [c for c in train.columns if c != label_col] | |
| X_train = train[feature_cols].to_numpy(dtype=float) | |
| y_train = to_int_labels(train[label_col]) | |
| X_test = test[feature_cols].to_numpy(dtype=float) | |
| y_test = to_int_labels(test[label_col]) | |
| model = LogisticRegression(max_iter=2000, solver="lbfgs") | |
| model.fit(X_train, y_train) | |
| y_pred = model.predict(X_test) | |
| metrics = { | |
| "label_column": label_col, | |
| "n_train": int(len(train)), | |
| "n_test": int(len(test)), | |
| "accuracy": float(accuracy_score(y_test, y_pred)), | |
| "precision": float(precision_score(y_test, y_pred, zero_division=0)), | |
| "recall": float(recall_score(y_test, y_pred, zero_division=0)), | |
| "f1": float(f1_score(y_test, y_pred, zero_division=0)), | |
| "confusion_matrix": confusion_matrix(y_test, y_pred).tolist(), | |
| } | |
| print(json.dumps(metrics, indent=2)) | |
| if __name__ == "__main__": | |
| main() |