tabvisml 0.1.1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- tabvisml/__init__.py +7 -0
- tabvisml/models.py +71 -0
- tabvisml/preprocessing.py +46 -0
- tabvisml/vision.py +41 -0
- tabvisml-0.1.1.dist-info/METADATA +73 -0
- tabvisml-0.1.1.dist-info/RECORD +9 -0
- tabvisml-0.1.1.dist-info/WHEEL +5 -0
- tabvisml-0.1.1.dist-info/licenses/LICENSE +21 -0
- tabvisml-0.1.1.dist-info/top_level.txt +1 -0
tabvisml/__init__.py
ADDED
tabvisml/models.py
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import numpy as np
|
|
2
|
+
import pandas as pd
|
|
3
|
+
from sklearn.ensemble import RandomForestClassifier, RandomForestRegressor
|
|
4
|
+
from sklearn.linear_model import LinearRegression, LogisticRegression
|
|
5
|
+
from sklearn.metrics import (
|
|
6
|
+
accuracy_score,
|
|
7
|
+
f1_score,
|
|
8
|
+
mean_absolute_error,
|
|
9
|
+
mean_absolute_percentage_error,
|
|
10
|
+
r2_score,
|
|
11
|
+
)
|
|
12
|
+
from sklearn.neighbors import KNeighborsClassifier, KNeighborsRegressor
|
|
13
|
+
from sklearn.svm import SVC, SVR
|
|
14
|
+
from sklearn.tree import DecisionTreeClassifier, DecisionTreeRegressor
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
def _infer_task(y) -> str:
|
|
18
|
+
y_arr = np.asarray(y)
|
|
19
|
+
if y_arr.dtype.kind in {"U", "S", "O", "b"}:
|
|
20
|
+
return "classification"
|
|
21
|
+
if np.issubdtype(y_arr.dtype, np.integer) and len(np.unique(y_arr)) <= 20:
|
|
22
|
+
return "classification"
|
|
23
|
+
return "regression"
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
def auto_train(X_train, X_test, y_train, y_test, task: str = "auto", verbose: bool = True):
|
|
27
|
+
resolved_task = _infer_task(y_train) if task == "auto" else task
|
|
28
|
+
|
|
29
|
+
if resolved_task == "regression":
|
|
30
|
+
models = {
|
|
31
|
+
"lr": LinearRegression(),
|
|
32
|
+
"knn": KNeighborsRegressor(),
|
|
33
|
+
"dt": DecisionTreeRegressor(random_state=1),
|
|
34
|
+
"svm": SVR(),
|
|
35
|
+
"rf": RandomForestRegressor(random_state=1),
|
|
36
|
+
}
|
|
37
|
+
elif resolved_task == "classification":
|
|
38
|
+
models = {
|
|
39
|
+
"logreg": LogisticRegression(max_iter=1000),
|
|
40
|
+
"knn": KNeighborsClassifier(),
|
|
41
|
+
"dt": DecisionTreeClassifier(random_state=1),
|
|
42
|
+
"svm": SVC(),
|
|
43
|
+
"rf": RandomForestClassifier(random_state=1),
|
|
44
|
+
}
|
|
45
|
+
else:
|
|
46
|
+
raise ValueError("task must be 'auto', 'regression', or 'classification'")
|
|
47
|
+
|
|
48
|
+
rows = []
|
|
49
|
+
fitted_models = {}
|
|
50
|
+
|
|
51
|
+
for name, model in models.items():
|
|
52
|
+
model.fit(X_train, y_train)
|
|
53
|
+
y_pred = model.predict(X_test)
|
|
54
|
+
fitted_models[name] = model
|
|
55
|
+
|
|
56
|
+
if resolved_task == "regression":
|
|
57
|
+
r2 = r2_score(y_test, y_pred)
|
|
58
|
+
mae = mean_absolute_error(y_test, y_pred)
|
|
59
|
+
mape = 100 * mean_absolute_percentage_error(y_test, y_pred)
|
|
60
|
+
rows.append({"model": name, "r2": r2, "mae": mae, "mape": mape})
|
|
61
|
+
if verbose:
|
|
62
|
+
print(f"{name} => r2: {r2:.4f}, mae: {mae:.2f}, mape: {mape:.4f}")
|
|
63
|
+
else:
|
|
64
|
+
acc = accuracy_score(y_test, y_pred)
|
|
65
|
+
f1 = f1_score(y_test, y_pred, average="weighted", zero_division=0)
|
|
66
|
+
rows.append({"model": name, "accuracy": acc, "f1_weighted": f1})
|
|
67
|
+
if verbose:
|
|
68
|
+
print(f"{name} => accuracy: {acc:.4f}, f1_weighted: {f1:.4f}")
|
|
69
|
+
|
|
70
|
+
results_df = pd.DataFrame(rows)
|
|
71
|
+
return results_df, fitted_models
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
from sklearn.compose import ColumnTransformer
|
|
2
|
+
from sklearn.impute import SimpleImputer
|
|
3
|
+
from sklearn.model_selection import train_test_split
|
|
4
|
+
from sklearn.pipeline import Pipeline
|
|
5
|
+
from sklearn.preprocessing import MinMaxScaler, OneHotEncoder
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
def auto_preprocess(
|
|
9
|
+
X,
|
|
10
|
+
y,
|
|
11
|
+
test_size: float = 0.2,
|
|
12
|
+
random_state: int = 1,
|
|
13
|
+
num_strategy: str = "mean",
|
|
14
|
+
cat_strategy: str = "most_frequent",
|
|
15
|
+
stratify: bool = False,
|
|
16
|
+
):
|
|
17
|
+
num_cols = X.select_dtypes(include="number").columns
|
|
18
|
+
cat_cols = X.select_dtypes(exclude="number").columns
|
|
19
|
+
|
|
20
|
+
num_pipe = Pipeline([
|
|
21
|
+
("imputer", SimpleImputer(strategy=num_strategy)),
|
|
22
|
+
("scaler", MinMaxScaler()),
|
|
23
|
+
])
|
|
24
|
+
|
|
25
|
+
cat_pipe = Pipeline([
|
|
26
|
+
("imputer", SimpleImputer(strategy=cat_strategy)),
|
|
27
|
+
("encoder", OneHotEncoder(handle_unknown="ignore", sparse_output=False)),
|
|
28
|
+
])
|
|
29
|
+
|
|
30
|
+
transformer = ColumnTransformer([
|
|
31
|
+
("num", num_pipe, num_cols),
|
|
32
|
+
("cat", cat_pipe, cat_cols),
|
|
33
|
+
])
|
|
34
|
+
|
|
35
|
+
X_train, X_test, y_train, y_test = train_test_split(
|
|
36
|
+
X,
|
|
37
|
+
y,
|
|
38
|
+
test_size=test_size,
|
|
39
|
+
random_state=random_state,
|
|
40
|
+
stratify=y if stratify else None,
|
|
41
|
+
)
|
|
42
|
+
|
|
43
|
+
X_train_proc = transformer.fit_transform(X_train)
|
|
44
|
+
X_test_proc = transformer.transform(X_test)
|
|
45
|
+
|
|
46
|
+
return X_train_proc, X_test_proc, y_train, y_test, transformer
|
tabvisml/vision.py
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
from pathlib import Path
|
|
2
|
+
|
|
3
|
+
import numpy as np
|
|
4
|
+
from PIL import Image
|
|
5
|
+
|
|
6
|
+
VALID_EXTENSIONS = {".jpg", ".jpeg", ".png", ".bmp", ".webp", ".tif", ".tiff"}
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
def load_images_from_folder(
|
|
10
|
+
image_dir,
|
|
11
|
+
image_size=(128, 128),
|
|
12
|
+
flatten: bool = True,
|
|
13
|
+
as_array: bool = True,
|
|
14
|
+
normalize: bool = True,
|
|
15
|
+
):
|
|
16
|
+
root = Path(image_dir)
|
|
17
|
+
images = []
|
|
18
|
+
labels = []
|
|
19
|
+
|
|
20
|
+
for label_dir in sorted(root.iterdir()):
|
|
21
|
+
if not label_dir.is_dir():
|
|
22
|
+
continue
|
|
23
|
+
label_name = label_dir.name
|
|
24
|
+
|
|
25
|
+
for file_path in sorted(label_dir.iterdir()):
|
|
26
|
+
if not file_path.is_file() or file_path.suffix.lower() not in VALID_EXTENSIONS:
|
|
27
|
+
continue
|
|
28
|
+
|
|
29
|
+
with Image.open(file_path) as img:
|
|
30
|
+
img = img.convert("RGB").resize(image_size)
|
|
31
|
+
arr = np.asarray(img)
|
|
32
|
+
if normalize:
|
|
33
|
+
arr = arr.astype(np.float32) / 255.0
|
|
34
|
+
if flatten:
|
|
35
|
+
arr = arr.flatten()
|
|
36
|
+
images.append(arr)
|
|
37
|
+
labels.append(label_name)
|
|
38
|
+
|
|
39
|
+
if as_array:
|
|
40
|
+
return np.array(images), np.array(labels)
|
|
41
|
+
return images, labels
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: tabvisml
|
|
3
|
+
Version: 0.1.1
|
|
4
|
+
Summary: Lightweight wrappers for tabular preprocessing, baseline model comparison, and image loading.
|
|
5
|
+
Author-email: Milad Asghari <miladasghari13@gmail.com>
|
|
6
|
+
Maintainer-email: Milad Asghari <miladasghari13@gmail.com>
|
|
7
|
+
License-Expression: MIT
|
|
8
|
+
Project-URL: Homepage, https://github.com/milad-asghari/tabvisml
|
|
9
|
+
Keywords: machine-learning,scikit-learn,preprocessing,baseline,images
|
|
10
|
+
Classifier: Programming Language :: Python :: 3
|
|
11
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
12
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
15
|
+
Classifier: Intended Audience :: Developers
|
|
16
|
+
Classifier: Intended Audience :: Science/Research
|
|
17
|
+
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
|
|
18
|
+
Requires-Python: >=3.9
|
|
19
|
+
Description-Content-Type: text/markdown
|
|
20
|
+
License-File: LICENSE
|
|
21
|
+
Requires-Dist: numpy
|
|
22
|
+
Requires-Dist: pandas
|
|
23
|
+
Requires-Dist: scikit-learn>=1.2
|
|
24
|
+
Requires-Dist: pillow
|
|
25
|
+
Dynamic: license-file
|
|
26
|
+
|
|
27
|
+
**tabvisml**
|
|
28
|
+
|
|
29
|
+
Lightweight wrappers for tabular preprocessing, baseline model comparison, and image loading.
|
|
30
|
+
|
|
31
|
+
**Installation**
|
|
32
|
+
```
|
|
33
|
+
pip install tabvisml
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
```
|
|
37
|
+
**Tabular data**
|
|
38
|
+
```
|
|
39
|
+
import pandas as pd
|
|
40
|
+
from tabvisml import auto_preprocess, auto_train
|
|
41
|
+
|
|
42
|
+
df = pd.read_csv("data.csv")
|
|
43
|
+
X = df.drop(columns="target")
|
|
44
|
+
y = df["target"]
|
|
45
|
+
|
|
46
|
+
X_train, X_test, y_train, y_test, transformer = auto_preprocess(X, y)
|
|
47
|
+
results, models = auto_train(X_train, X_test, y_train, y_test)
|
|
48
|
+
print(results)
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
```
|
|
52
|
+
`auto_preprocess` imputes missing values, min-max scales numeric columns, one-hot encodes categorical columns, and splits the data. `auto_train` fits Linear/Logistic Regression, KNN, Decision Tree, SVM and Random Forest, and returns a metrics table plus the fitted models. The task type is inferred automatically, or set with `task="regression"` or `task="classification"`.
|
|
53
|
+
|
|
54
|
+
Pass `stratify=True` to `auto_preprocess` to keep class proportions in the split.
|
|
55
|
+
|
|
56
|
+
**Images**
|
|
57
|
+
|
|
58
|
+
Images are read from a folder with one sub-folder per class:
|
|
59
|
+
```
|
|
60
|
+
images/
|
|
61
|
+
cats/
|
|
62
|
+
dogs/
|
|
63
|
+
from tabvisml import load_images_from_folder
|
|
64
|
+
|
|
65
|
+
X, y = load_images_from_folder("images", image_size=(64, 64))
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
```
|
|
69
|
+
Pixel values are scaled to the 0-1 range by default; use `normalize=False` to keep raw 0-255 values.
|
|
70
|
+
|
|
71
|
+
**License**
|
|
72
|
+
|
|
73
|
+
MIT
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
tabvisml/__init__.py,sha256=gyy8g2-Yn8RkJyQ6l8pbkfJHG9dowfCphOLwOgE7gaM,228
|
|
2
|
+
tabvisml/models.py,sha256=kmDpwPXorcMH2-7Mt2FhiPrDzHFfwEAYa0EhrHGEwGc,2610
|
|
3
|
+
tabvisml/preprocessing.py,sha256=T0sXfCeaSmuWPDtJ0R58K13HaJIuJUgDRi_iPdcWnuo,1337
|
|
4
|
+
tabvisml/vision.py,sha256=iA-7U7D5IgUNm3x8uX2JK5m8PyGWnSj2g1eBNCB2QiM,1145
|
|
5
|
+
tabvisml-0.1.1.dist-info/licenses/LICENSE,sha256=0wA4Uwmod4OrVptbpmYQ-sxKQ2Bw8ejlM9bEmVF1kkc,1070
|
|
6
|
+
tabvisml-0.1.1.dist-info/METADATA,sha256=ohZDmgqSzqhrCtAqBcS4sgmhRPNLOfutC7plLzmld7c,2586
|
|
7
|
+
tabvisml-0.1.1.dist-info/WHEEL,sha256=mOdXR9C3wMVov7cSY8PY-XCzo3SmmWCHvGQr5csJoao,91
|
|
8
|
+
tabvisml-0.1.1.dist-info/top_level.txt,sha256=Yho29PCoXYyeVxDCTj5AmfbPgDW0f3rIPtqhZk-Tp70,9
|
|
9
|
+
tabvisml-0.1.1.dist-info/RECORD,,
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Milad Asghari
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
tabvisml
|