Tabular Classification
FlexTab
tabular
foundation-model
deep-learning
in-context
tabular-regression
tabular-matching
tabular-anomaly-detection
tabular-multi-table
tabular-clustering
Instructions to use SAP/FlexTab with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- FlexTab
How to use SAP/FlexTab with FlexTab:
pip install git+https://github.com/SAP-samples/flextab
# Run a classification task from sklearn.datasets import load_breast_cancer from sklearn.metrics import accuracy_score from sklearn.model_selection import train_test_split from flextab import FlexTabClassifier # Load sample data X, y = load_breast_cancer(return_X_y=True) X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.5, random_state=42) # Initialize a classifier, 8k context and 8-fold bagging gives best performance, reduce if running out of memory clf = FlexTabClassifier(max_context_size=8192, bagging=8) clf.fit(X_train, y_train) # Predict probabilities prediction_probabilities = clf.predict_proba(X_test) # Predict labels predictions = clf.predict(X_test) print("Accuracy", accuracy_score(y_test, predictions))# Run a regression task from sklearn.datasets import fetch_openml from sklearn.metrics import r2_score from sklearn.model_selection import train_test_split from flextab import FlexTabRegressor # Load sample data df = fetch_openml(data_id=531, as_frame=True) X = df.data y = df.target.astype(float) # Train-test split X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.5, random_state=42) # Initialize the regressor, 8k context and 8-fold bagging gives best performance, reduce if running out of memory regressor = FlexTabRegressor(max_context_size=8192, bagging=8) regressor.fit(X_train, y_train) # Predict on the test set predictions = regressor.predict(X_test) r2 = r2_score(y_test, predictions) print("R² Score:", r2)# Run a matching task from sklearn.metrics import accuracy_score, roc_auc_score from flextab import FlexTabMatcher from flextab.utils.test_utils import load_febrl4 left_train, left_test, right_train, right_test, y_train, y_test = load_febrl4() matcher = FlexTabMatcher(max_context_size=8192, bagging=1) matcher.fit(left_train, right_train, y_train) predictions = matcher.predict(left_test, right_test) print(f'Accuracy {accuracy_score(y_test, predictions):.2%}') # Probabilities (e.g. for thresholding or AUROC): # probas = matcher.predict_proba_matching(left_test, right_test) # print(f'AUROC {roc_auc_score(y_test, probas[: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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"cells": [
|
| 3 |
+
{
|
| 4 |
+
"cell_type": "markdown",
|
| 5 |
+
"metadata": {
|
| 6 |
+
"id": "5ROgtvTfTdy9"
|
| 7 |
+
},
|
| 8 |
+
"source": [
|
| 9 |
+
"## FlexTab Examples\n",
|
| 10 |
+
"Model page: https://huggingface.co/SAP/FlexTab"
|
| 11 |
+
]
|
| 12 |
+
},
|
| 13 |
+
{
|
| 14 |
+
"cell_type": "markdown",
|
| 15 |
+
"metadata": {
|
| 16 |
+
"id": "N-54dB6iTdy_"
|
| 17 |
+
},
|
| 18 |
+
"source": [
|
| 19 |
+
"The model you are trying to use is gated. Please make sure you have access to it by visiting the model page.To run inference, either set HF_TOKEN in your environment variables/ Secrets or run the following cell to login. 🤗"
|
| 20 |
+
]
|
| 21 |
+
},
|
| 22 |
+
{
|
| 23 |
+
"cell_type": "code",
|
| 24 |
+
"execution_count": null,
|
| 25 |
+
"metadata": {
|
| 26 |
+
"id": "o7iIwHVHTdzA"
|
| 27 |
+
},
|
| 28 |
+
"outputs": [],
|
| 29 |
+
"source": [
|
| 30 |
+
"from huggingface_hub import login\n",
|
| 31 |
+
"login(new_session=False)"
|
| 32 |
+
]
|
| 33 |
+
},
|
| 34 |
+
{
|
| 35 |
+
"cell_type": "code",
|
| 36 |
+
"execution_count": null,
|
| 37 |
+
"metadata": {},
|
| 38 |
+
"outputs": [],
|
| 39 |
+
"source": [
|
| 40 |
+
"# Uninstall conflicting packages from the default Colab environment\n",
|
| 41 |
+
"!pip uninstall torch torchvision torchaudio -y`"
|
| 42 |
+
]
|
| 43 |
+
},
|
| 44 |
+
{
|
| 45 |
+
"cell_type": "code",
|
| 46 |
+
"execution_count": null,
|
| 47 |
+
"metadata": {
|
| 48 |
+
"id": "6GqTz9gYTdzB"
|
| 49 |
+
},
|
| 50 |
+
"outputs": [],
|
| 51 |
+
"source": [
|
| 52 |
+
"# Install FlexTab core code\n",
|
| 53 |
+
"!pip install git+https://github.com/SAP-samples/flextab"
|
| 54 |
+
]
|
| 55 |
+
},
|
| 56 |
+
{
|
| 57 |
+
"cell_type": "code",
|
| 58 |
+
"execution_count": null,
|
| 59 |
+
"metadata": {
|
| 60 |
+
"id": "jzA1dexsTdzB"
|
| 61 |
+
},
|
| 62 |
+
"outputs": [],
|
| 63 |
+
"source": [
|
| 64 |
+
"from sklearn.datasets import load_breast_cancer\n",
|
| 65 |
+
"from sklearn.metrics import accuracy_score\n",
|
| 66 |
+
"from sklearn.model_selection import train_test_split\n",
|
| 67 |
+
"\n",
|
| 68 |
+
"from flextab import FlexTabClassifier\n",
|
| 69 |
+
"\n",
|
| 70 |
+
"# Load sample data\n",
|
| 71 |
+
"X, y = load_breast_cancer(return_X_y=True)\n",
|
| 72 |
+
"X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.5, random_state=42)\n",
|
| 73 |
+
"\n",
|
| 74 |
+
"# Initialize a classifier, 8k context and 8-fold bagging gives best performance, reduce if running out of memory\n",
|
| 75 |
+
"clf = FlexTabClassifier(max_context_size=8192, bagging=8)\n",
|
| 76 |
+
"\n",
|
| 77 |
+
"clf.fit(X_train, y_train)\n",
|
| 78 |
+
"\n",
|
| 79 |
+
"# Predict probabilities\n",
|
| 80 |
+
"prediction_probabilities = clf.predict_proba(X_test)\n",
|
| 81 |
+
"# Predict labels\n",
|
| 82 |
+
"predictions = clf.predict(X_test)\n",
|
| 83 |
+
"print(\"Accuracy\", accuracy_score(y_test, predictions))"
|
| 84 |
+
]
|
| 85 |
+
},
|
| 86 |
+
{
|
| 87 |
+
"cell_type": "code",
|
| 88 |
+
"execution_count": null,
|
| 89 |
+
"metadata": {
|
| 90 |
+
"id": "ZlG2jXfWTdzC"
|
| 91 |
+
},
|
| 92 |
+
"outputs": [],
|
| 93 |
+
"source": [
|
| 94 |
+
"from sklearn.datasets import fetch_openml\n",
|
| 95 |
+
"from sklearn.metrics import r2_score\n",
|
| 96 |
+
"from sklearn.model_selection import train_test_split\n",
|
| 97 |
+
"\n",
|
| 98 |
+
"from flextab import FlexTabRegressor\n",
|
| 99 |
+
"\n",
|
| 100 |
+
"# Load sample data\n",
|
| 101 |
+
"df = fetch_openml(data_id=531, as_frame=True)\n",
|
| 102 |
+
"X = df.data\n",
|
| 103 |
+
"y = df.target.astype(float)\n",
|
| 104 |
+
"\n",
|
| 105 |
+
"# Train-test split\n",
|
| 106 |
+
"X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.5, random_state=42)\n",
|
| 107 |
+
"\n",
|
| 108 |
+
"# Initialize the regressor, 8k context and 8-fold bagging gives best performance, reduce if running out of memory\n",
|
| 109 |
+
"regressor = FlexTabRegressor(max_context_size=8192, bagging=8)\n",
|
| 110 |
+
"\n",
|
| 111 |
+
"regressor.fit(X_train, y_train)\n",
|
| 112 |
+
"\n",
|
| 113 |
+
"# Predict on the test set\n",
|
| 114 |
+
"predictions = regressor.predict(X_test)\n",
|
| 115 |
+
"\n",
|
| 116 |
+
"r2 = r2_score(y_test, predictions)\n",
|
| 117 |
+
"print(\"R² Score:\", r2)"
|
| 118 |
+
]
|
| 119 |
+
},
|
| 120 |
+
{
|
| 121 |
+
"cell_type": "code",
|
| 122 |
+
"execution_count": null,
|
| 123 |
+
"metadata": {},
|
| 124 |
+
"outputs": [],
|
| 125 |
+
"source": [
|
| 126 |
+
"from sklearn.metrics import accuracy_score, roc_auc_score\n",
|
| 127 |
+
"\n",
|
| 128 |
+
"from flextab import FlexTabMatcher\n",
|
| 129 |
+
"from flextab.utils.test_utils import load_febrl4\n",
|
| 130 |
+
"\n",
|
| 131 |
+
"left_train, left_test, right_train, right_test, y_train, y_test = load_febrl4()\n",
|
| 132 |
+
"\n",
|
| 133 |
+
"matcher = FlexTabMatcher(max_context_size=8192, bagging=1)\n",
|
| 134 |
+
"matcher.fit(left_train, right_train, y_train)\n",
|
| 135 |
+
"predictions = matcher.predict(left_test, right_test)\n",
|
| 136 |
+
"print(f'Accuracy {accuracy_score(y_test, predictions):.2%}')\n",
|
| 137 |
+
"# Probabilities (e.g. for thresholding or AUROC):\n",
|
| 138 |
+
"# probas = matcher.predict_proba_matching(left_test, right_test)\n",
|
| 139 |
+
"# print(f'AUROC {roc_auc_score(y_test, probas[:, 1]):.2%}')"
|
| 140 |
+
]
|
| 141 |
+
}
|
| 142 |
+
],
|
| 143 |
+
"metadata": {
|
| 144 |
+
"accelerator": "GPU",
|
| 145 |
+
"colab": {
|
| 146 |
+
"gpuType": "T4",
|
| 147 |
+
"machine_shape": "hm",
|
| 148 |
+
"provenance": []
|
| 149 |
+
},
|
| 150 |
+
"kaggle": {
|
| 151 |
+
"accelerator": "gpu"
|
| 152 |
+
},
|
| 153 |
+
"language_info": {
|
| 154 |
+
"name": "python"
|
| 155 |
+
}
|
| 156 |
+
},
|
| 157 |
+
"nbformat": 4,
|
| 158 |
+
"nbformat_minor": 0
|
| 159 |
+
}
|