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