-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2812e48
commit 1c21871
Showing
3 changed files
with
171 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
import pandas as pd | ||
import matplotlib.pyplot as plt | ||
from sklearn.metrics import roc_auc_score, accuracy_score | ||
from omegaconf import OmegaConf | ||
from torch.utils.data import Dataset, DataLoader | ||
|
||
from utils import * | ||
|
||
conf = """ | ||
base: | ||
api_key_path: "token/token.json" | ||
train_path: "../data/otto-train.csv" | ||
seed: 67 | ||
dataset: | ||
img_size: 20 | ||
features: 93 # 特徴量の数 | ||
val_size: 300 # validationに使うデータ数 | ||
target: 0 # ラベル | ||
model: | ||
timeout: 3000 # ms 計算時間 | ||
# 訓練に使うデータは(batch_size * n_iter)個 | ||
batch_size: 30 # バッチサイズ | ||
n_iter: 5 # ループ数 | ||
l: 3 # 正則化項 | ||
each_weight: 2.5 # 重み係数 | ||
length_weight: 5 # 重みの層の数 | ||
multiprocessing: true | ||
""" | ||
target_dict = { | ||
"Class_1": 0, | ||
"Class_2": 1, | ||
"Class_3": 2, | ||
"Class_4": 3, | ||
"Class_5": 4, | ||
"Class_6": 5, | ||
"Class_7": 6, | ||
"Class_8": 7, | ||
"Class_9": 8 | ||
} | ||
|
||
cfg = OmegaConf.create(conf) | ||
|
||
init_client(cfg) | ||
|
||
|
||
# PyTorch形式 | ||
class MyDataset(Dataset): | ||
def __init__(self, data, label): | ||
self.label = label | ||
self.data = np.apply_along_axis(minmax, 1, data) | ||
|
||
self.label = np.where(self.label == cfg.dataset.target, 1, 0) | ||
self.data[self.data == 0] = -1 | ||
|
||
def __len__(self): | ||
return self.data.shape[0] | ||
|
||
def __getitem__(self, idx): | ||
return self.data[idx, :], self.label[idx] | ||
|
||
|
||
def get_ds(n, seed): | ||
true_ = dataset.loc[dataset.iloc[:, -1] == cfg.dataset.target, :].sample(n // 2) | ||
false_ = dataset.loc[dataset.iloc[:, -1] != cfg.dataset.target, :].sample(n // 2) | ||
return pd.concat( | ||
[ | ||
true_, | ||
false_ | ||
] | ||
).sample( | ||
frac=1, | ||
random_state=seed | ||
).values | ||
|
||
|
||
dataset = pd.read_csv(cfg.base.train_path).iloc[:, 1:] | ||
dataset["target"] = dataset["target"].map(target_dict) | ||
for i in range(9): | ||
init_client(cfg) | ||
cfg.dataset.target = i | ||
train = get_ds( | ||
int( | ||
cfg.model.n_iter * cfg.model.batch_size | ||
), | ||
cfg.base.seed | ||
) | ||
|
||
val = get_ds( | ||
cfg.dataset.val_size, | ||
cfg.base.seed + 1 | ||
) | ||
|
||
train_ds = MyDataset(train[:, :-1], train[:, -1]) | ||
valid_ds = MyDataset(val[:, :-1], val[:, -1]) | ||
train_dl = DataLoader( | ||
train_ds, | ||
batch_size=cfg.model.batch_size, | ||
shuffle=True | ||
) | ||
valid_dl = DataLoader( | ||
valid_ds, | ||
batch_size=64, | ||
shuffle=False | ||
) | ||
weight = np.zeros( | ||
( | ||
cfg.dataset.features, | ||
cfg.model.length_weight | ||
) | ||
) | ||
|
||
weight = run_model(cfg, train_dl, weight, cfg.model.multiprocessing) | ||
pred, label = eval_model(cfg, valid_dl, weight) | ||
|
||
print("=" * 20, i, "=" * 20) | ||
print("AUC:", roc_auc_score(label, pred)) | ||
print("ACC:", accuracy_score(label, np.round(pred))) | ||
print("=" * 43) |