Skip to content

Commit e958a34

Browse files
xisen-wTPLin22
andauthored
feat: supporting Mnist competition (#375)
* Initialised a version * Fixes * feat mnist & fix some bugs(with TODO) --------- Co-authored-by: TPLin22 <tplin2@163.com>
1 parent c41686f commit e958a34

5 files changed

Lines changed: 234 additions & 0 deletions

File tree

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
import os
2+
3+
import numpy as np
4+
import pandas as pd
5+
from sklearn.impute import SimpleImputer
6+
from sklearn.model_selection import train_test_split
7+
8+
9+
def prepreprocess():
10+
"""
11+
This method loads the data, drops the unnecessary columns, and splits it into train and validation sets.
12+
"""
13+
# Load and preprocess the data
14+
data_df = pd.read_csv("/kaggle/input/train.csv")
15+
# data_df = data_df.drop(["ImageId"], axis=1)
16+
17+
X = data_df.drop(["label"], axis=1)
18+
y = data_df["label"]
19+
20+
# Split the data into training and validation sets
21+
X_train, X_valid, y_train, y_valid = train_test_split(X, y, test_size=0.20, random_state=42)
22+
23+
return X_train, X_valid, y_train, y_valid
24+
25+
26+
def preprocess_script():
27+
"""
28+
This method applies the preprocessing steps to the training, validation, and test datasets.
29+
"""
30+
if os.path.exists("/kaggle/input/X_train.pkl"):
31+
X_train = pd.read_pickle("/kaggle/input/X_train.pkl")
32+
X_valid = pd.read_pickle("/kaggle/input/X_valid.pkl")
33+
y_train = pd.read_pickle("/kaggle/input/y_train.pkl")
34+
y_valid = pd.read_pickle("/kaggle/input/y_valid.pkl")
35+
X_test = pd.read_pickle("/kaggle/input/X_test.pkl")
36+
others = pd.read_pickle("/kaggle/input/others.pkl")
37+
38+
return X_train, X_valid, y_train, y_valid, X_test, *others
39+
40+
X_train, X_valid, y_train, y_valid = prepreprocess()
41+
42+
# Load and preprocess the test data
43+
submission_df = pd.read_csv("/kaggle/input/test.csv")
44+
# ids = submission_df["ImageId"]
45+
X_test = submission_df
46+
47+
X_train = X_train / 255
48+
X_valid = X_valid / 255
49+
X_test = X_test / 255
50+
51+
return X_train, X_valid, y_train, y_valid, X_test
52+
53+
54+
def clean_and_impute_data(X_train, X_valid, X_test):
55+
"""
56+
Handles inf and -inf values by replacing them with NaN,
57+
then imputes missing values using the mean strategy.
58+
Also removes duplicate columns.
59+
"""
60+
# Replace inf and -inf with NaN
61+
X_train.replace([np.inf, -np.inf], np.nan, inplace=True)
62+
X_valid.replace([np.inf, -np.inf], np.nan, inplace=True)
63+
X_test.replace([np.inf, -np.inf], np.nan, inplace=True)
64+
65+
# Impute missing values
66+
imputer = SimpleImputer(strategy="mean")
67+
X_train = pd.DataFrame(imputer.fit_transform(X_train), columns=X_train.columns)
68+
X_valid = pd.DataFrame(imputer.transform(X_valid), columns=X_valid.columns)
69+
X_test = pd.DataFrame(imputer.transform(X_test), columns=X_test.columns)
70+
71+
# Remove duplicate columns
72+
X_train = X_train.loc[:, ~X_train.columns.duplicated()]
73+
X_valid = X_valid.loc[:, ~X_valid.columns.duplicated()]
74+
X_test = X_test.loc[:, ~X_test.columns.duplicated()]
75+
76+
return X_train, X_valid, X_test
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import pandas as pd
2+
3+
"""
4+
Here is the feature engineering code for each task, with a class that has a fit and transform method.
5+
Remember
6+
"""
7+
8+
9+
class IdentityFeature:
10+
def fit(self, train_df: pd.DataFrame):
11+
"""
12+
Fit the feature engineering model to the training data.
13+
"""
14+
pass
15+
16+
def transform(self, X: pd.DataFrame):
17+
"""
18+
Transform the input data.
19+
"""
20+
return X
21+
22+
23+
feature_engineering_cls = IdentityFeature
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
"""
2+
motivation of the model
3+
"""
4+
5+
import pandas as pd
6+
import xgboost as xgb
7+
8+
9+
def fit(X_train, y_train, X_valid, y_valid):
10+
"""Define and train the model. Merge feature_select"""
11+
dtrain = xgb.DMatrix(X_train, label=y_train)
12+
dvalid = xgb.DMatrix(X_valid, label=y_valid)
13+
14+
params = {
15+
"objective": "multi:softmax",
16+
"eval_metric": "mlogloss",
17+
"num_class": 10,
18+
"nthread": -1,
19+
"tree_method": "gpu_hist",
20+
"device": "cuda",
21+
}
22+
num_round = 100
23+
24+
evallist = [(dtrain, "train"), (dvalid, "eval")]
25+
model = xgb.train(params, dtrain, num_round, evallist, early_stopping_rounds=10)
26+
27+
return model
28+
29+
30+
def predict(model, X):
31+
"""
32+
Keep feature select's consistency.
33+
"""
34+
dtest = xgb.DMatrix(X)
35+
return model.predict(dtest).astype(int)
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import pandas as pd
2+
3+
4+
def select(X: pd.DataFrame) -> pd.DataFrame:
5+
"""
6+
Select relevant features. To be used in fit & predict function.
7+
"""
8+
# For now, we assume all features are relevant. This can be expanded to feature selection logic.
9+
if X.columns.nlevels == 1:
10+
return X
11+
X.columns = ["_".join(str(col)).strip() for col in X.columns.values]
12+
return X
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
import importlib.util
2+
import random
3+
from pathlib import Path
4+
5+
import numpy as np
6+
import pandas as pd
7+
from fea_share_preprocess import clean_and_impute_data, preprocess_script
8+
from sklearn.metrics import accuracy_score
9+
10+
# Set random seed for reproducibility
11+
SEED = 42
12+
random.seed(SEED)
13+
np.random.seed(SEED)
14+
DIRNAME = Path(__file__).absolute().resolve().parent
15+
16+
17+
def compute_metrics_for_classification(y_true, y_pred):
18+
"""Compute accuracy for classification."""
19+
return accuracy_score(y_true, y_pred)
20+
21+
22+
def import_module_from_path(module_name, module_path):
23+
spec = importlib.util.spec_from_file_location(module_name, module_path)
24+
module = importlib.util.module_from_spec(spec)
25+
spec.loader.exec_module(module)
26+
return module
27+
28+
29+
# 1) Preprocess the data
30+
X_train, X_valid, y_train, y_valid, X_test = preprocess_script()
31+
32+
# 2) Auto feature engineering
33+
X_train_l, X_valid_l = [], []
34+
X_test_l = []
35+
36+
for f in DIRNAME.glob("feature/feat*.py"):
37+
cls = import_module_from_path(f.stem, f).feature_engineering_cls()
38+
cls.fit(X_train)
39+
X_train_f = cls.transform(X_train)
40+
X_valid_f = cls.transform(X_valid)
41+
X_test_f = cls.transform(X_test)
42+
43+
if X_train_f.shape[-1] == X_valid_f.shape[-1] and X_train_f.shape[-1] == X_test_f.shape[-1]:
44+
X_train_l.append(X_train_f)
45+
X_valid_l.append(X_valid_f)
46+
X_test_l.append(X_test_f)
47+
48+
X_train = pd.concat(X_train_l, axis=1, keys=[f"feature_{i}" for i in range(len(X_train_l))])
49+
X_valid = pd.concat(X_valid_l, axis=1, keys=[f"feature_{i}" for i in range(len(X_valid_l))])
50+
X_test = pd.concat(X_test_l, axis=1, keys=[f"feature_{i}" for i in range(len(X_test_l))])
51+
52+
print(X_train.shape, X_valid.shape, X_test.shape)
53+
54+
# Handle inf and -inf values
55+
X_train, X_valid, X_test = clean_and_impute_data(X_train, X_valid, X_test)
56+
57+
58+
model_l = [] # list[tuple[model, predict_func]]
59+
for f in DIRNAME.glob("model/model*.py"):
60+
select_python_path = f.with_name(f.stem.replace("model", "select") + f.suffix)
61+
select_m = import_module_from_path(select_python_path.stem, select_python_path)
62+
X_train_selected = select_m.select(X_train.copy())
63+
X_valid_selected = select_m.select(X_valid.copy())
64+
65+
m = import_module_from_path(f.stem, f)
66+
model_l.append((m.fit(X_train_selected, y_train, X_valid_selected, y_valid), m.predict, select_m))
67+
68+
# 4) Evaluate the model on the validation set
69+
metrics_all = []
70+
for model, predict_func, select_m in model_l:
71+
X_valid_selected = select_m.select(X_valid.copy())
72+
y_valid_pred = predict_func(model, X_valid_selected)
73+
accuracy = accuracy_score(y_valid, y_valid_pred)
74+
print(f"final accuracy on valid set: {accuracy}")
75+
metrics_all.append(accuracy)
76+
77+
# 5) Save the validation accuracy
78+
min_index = np.argmax(metrics_all)
79+
pd.Series(data=[metrics_all[min_index]], index=["multi-class accuracy"]).to_csv("submission_score.csv")
80+
81+
# 6) Submit predictions for the test
82+
ids = range(1, len(X_test) + 1)
83+
84+
# TODO: fix selection
85+
print(X_valid_selected.columns)
86+
y_test_pred = model_l[min_index][1](model_l[min_index][0], model_l[min_index][2].select(X_test))
87+
submission_result = pd.DataFrame({"ImageId": ids, "Label": y_test_pred})
88+
submission_result.to_csv("submission.csv", index=False)

0 commit comments

Comments
 (0)