-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproblem.py
More file actions
69 lines (48 loc) · 1.98 KB
/
Copy pathproblem.py
File metadata and controls
69 lines (48 loc) · 1.98 KB
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
import os
import pandas as pd
import numpy as np
from sklearn.metrics import f1_score
from sklearn.model_selection import ShuffleSplit
from sklearn.preprocessing import OrdinalEncoder
import rampwf as rw
from rampwf.workflows import FeatureExtractorClassifier
from rampwf.score_types.classifier_base import ClassifierBaseScoreType
problem_title = "Who wrote this? Predicting the author of a paragraph"
_target_column_name = "author"
_prediction_label_names = list(range(0, 10))
Predictions = rw.prediction_types.make_multiclass(label_names=_prediction_label_names)
# An object implementing the workflow
class WhoWroteThis(FeatureExtractorClassifier):
def __init__(self, workflow_element_names=["feature_extractor", "classifier"]):
super().__init__()
self.element_names = workflow_element_names
workflow = WhoWroteThis()
# define the score (basic multiclass F1-score)
class F1Score(ClassifierBaseScoreType):
is_lower_the_better = False
minimum = 0.0
maximum = 1
def __init__(self, name="F1-score", precision=2):
self.name = name
self.precision = precision
def __call__(self, y_true, y_pred):
return f1_score(y_true, y_pred, average="micro")
score_types = [F1Score()]
def get_cv(X, y):
cv = ShuffleSplit(n_splits=6, test_size=0.20, random_state=hash("UwU") % 1000)
return cv.split(X, y)
def _read_data(path, f_name, sep="|"):
data = pd.read_csv(os.path.join(path, "data", f_name), sep=sep, low_memory=False)
y_array = OrdinalEncoder().fit_transform(
data[_target_column_name].values[:, np.newaxis]
)
X_df = data.drop(columns=[_target_column_name])
return X_df, y_array.flatten()
def get_train_data(sep="|", path="."):
f_name = "who_wrote_this_corpus_train.csv"
X_df, y_array = _read_data(path, f_name, sep=sep)
return X_df, y_array
def get_test_data(sep="|", path="."):
f_name = "who_wrote_this_corpus_test.csv"
X_df, y_array = _read_data(path, f_name, sep=sep)
return X_df, y_array