-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.py
More file actions
executable file
·62 lines (48 loc) · 1.65 KB
/
test.py
File metadata and controls
executable file
·62 lines (48 loc) · 1.65 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
#!/usr/bin/env python
"""Basic test running close-k and baselines on monk3."""
import numpy as np
import sklearn.linear_model
import pmlb
import closek
if __name__ == "__main__":
np.random.seed(0)
# Load dataset
X, y = pmlb.fetch_data("monk3", return_X_y=True)
# Shuffle dataset
n = X.shape[0]
index = np.arange(n)
np.random.shuffle(index)
X = X[index, :]
y = y[index]
# Split into train and test folds
train = 0.75
k = round(n * train)
Xtrain, ytrain = X[:k, :], y[:k]
Xtest, ytest = X[k:, :], y[k:]
# List of hyperparameters
alpha = [1e-3, 1e-2, 1e-1]
k = [10 ** i for i in range(len(str(k)))] + [k]
model = sklearn.model_selection.GridSearchCV(
closek.CloseKClassifier(loss="hinge"),
{"alpha": alpha, "k": k},
cv=5)
model.fit(Xtrain, ytrain)
print("Close-k (hinge): " + str(model.score(Xtest, ytest)))
model = sklearn.model_selection.GridSearchCV(
closek.CloseKClassifier(loss="log"),
{"alpha": alpha, "k": k},
cv=5)
model.fit(Xtrain, ytrain)
print("Close-k (logistic): " + str(model.score(Xtest, ytest)))
model = sklearn.model_selection.GridSearchCV(
sklearn.linear_model.SGDClassifier(loss="hinge", max_iter=1000),
{"alpha": alpha},
cv=5)
model.fit(Xtrain, ytrain)
print("Linear SVM: " + str(model.score(Xtest, ytest)))
model = sklearn.model_selection.GridSearchCV(
sklearn.linear_model.SGDClassifier(loss="log", max_iter=1000),
{"alpha": alpha},
cv=5)
model.fit(Xtrain, ytrain)
print("Logistic Regression: " + str(model.score(Xtest, ytest)))