forked from qile2000/LAMDA-TALENT
-
Notifications
You must be signed in to change notification settings - Fork 0
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
Showing
4 changed files
with
66 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
from model.classical_methods.base import classical_methods | ||
from copy import deepcopy | ||
import os.path as ops | ||
import pickle | ||
import time | ||
|
||
class DummyMethod(classical_methods): | ||
def __init__(self, args, is_regression): | ||
super().__init__(args, is_regression) | ||
assert(args.cat_policy != 'indices') | ||
|
||
def construct_model(self, model_config = None): | ||
if model_config is None: | ||
model_config = self.args.config['model'] | ||
from sklearn.dummy import DummyClassifier, DummyRegressor | ||
if self.is_regression: | ||
self.model = DummyRegressor(strategy='mean') | ||
else: | ||
self.model = DummyClassifier() | ||
|
||
|
||
def fit(self, N, C, y, info, train=True, config=None): | ||
super().fit(N, C, y, info, train, config) | ||
# if not train, skip the training process. such as load the checkpoint and directly predict the results | ||
if not train: | ||
return | ||
tic = time.time() | ||
self.model.fit(self.N['train'], self.y['train']) | ||
self.trlog['best_res'] = self.model.score(self.N['val'], self.y['val']) | ||
time_cost = time.time() - tic | ||
with open(ops.join(self.args.save_path , 'best-val-{}.pkl'.format(self.args.seed)), 'wb') as f: | ||
pickle.dump(self.model, f) | ||
return time_cost | ||
|
||
def predict(self, N, C, y, info, model_name): | ||
with open(ops.join(self.args.save_path , 'best-val-{}.pkl'.format(self.args.seed)), 'rb') as f: | ||
self.model = pickle.load(f) | ||
self.data_format(False, N, C, y) | ||
test_label = self.y_test | ||
test_logit = self.model.predict(self.N_test) | ||
vres, metric_name = self.metric(test_logit, test_label, self.y_info) | ||
return vres, metric_name, test_logit | ||
|
||
def metric(self, predictions, labels, y_info): | ||
from sklearn import metrics as skm | ||
if self.is_regression: | ||
mae = skm.mean_absolute_error(labels, predictions) | ||
rmse = skm.mean_squared_error(labels, predictions) ** 0.5 | ||
r2 = skm.r2_score(labels, predictions) | ||
if y_info['policy'] == 'mean_std': | ||
mae *= y_info['std'] | ||
rmse *= y_info['std'] | ||
return (mae,r2,rmse), ("MAE", "R2", "RMSE") | ||
else: | ||
accuracy = skm.accuracy_score(labels, predictions) | ||
avg_precision = skm.precision_score(labels, predictions, average='macro') | ||
avg_recall = skm.recall_score(labels, predictions, average='macro') | ||
f1_score = skm.f1_score(labels, predictions, average='binary' if self.is_binclass else 'macro') | ||
return (accuracy, avg_precision, avg_recall, f1_score), ("Accuracy", "Avg_Precision", "Avg_Recall", "F1") |
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