162 KiB
162 KiB
In [1]:
import pickle
import os
import glob
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import sklearn.model_selection as skms
import sklearn.linear_model as skl
import sklearn.metrics as skm
import tqdm
import copy
import time
from IPython.display import display
%matplotlib inline
sns.set(color_codes=True)In [2]:
%%javascript
// Fill entire cell with output
IPython.OutputArea.prototype._should_scroll = function(lines) {
return false;
}<IPython.core.display.Javascript object>
In [3]:
filenames = glob.glob(os.path.join("..", "dat", "*"))
label_filename = list(filter(lambda x: "label" in x, filenames))[0]
dat_filename = list(filter(lambda x: "label" not in x, filenames))[0]
# Read in the labels
with open(label_filename, "rb") as f:
labels = pickle.load(f)
# Read in the corresponding configurations
with open(dat_filename, "rb") as f:
data = np.unpackbits(pickle.load(f)).reshape(-1, 1600).astype("int")
# Set spin-down to -1
data[data == 0] = -1In [4]:
# Set up slices of the dataset
ordered = slice(0, 70000)
critical = slice(70000, 100000)
disordered = slice(100000, 160000)
X_train, X_test, y_train, y_test = skms.train_test_split(
np.concatenate((data[ordered], data[disordered])),
np.concatenate((labels[ordered], labels[disordered])),
test_size=0.95
)In [5]:
lambdas = np.logspace(-7, -1, 7)
param_grid = {
"C": list(1.0/lambdas),
"penalty": ["l1", "l2"]
}In [6]:
clf = skms.GridSearchCV(
skl.LogisticRegression(),
param_grid=param_grid,
n_jobs=-1,
return_train_score=True
)In [7]:
t0 = time.time()
clf.fit(X_train, y_train)
t1 = time.time()
print (
"Time spent fitting GridSearchCV(LogisticRegression): {0:.3f} sec".format(
t1 - t0
)
)Time spent fitting GridSearchCV(LogisticRegression): 949.417 sec
In [8]:
logreg_df = pd.DataFrame(clf.cv_results_)
display(logreg_df)| mean_fit_time | std_fit_time | mean_score_time | std_score_time | param_C | param_penalty | params | split0_test_score | split1_test_score | split2_test_score | mean_test_score | std_test_score | rank_test_score | split0_train_score | split1_train_score | split2_train_score | mean_train_score | std_train_score | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | 133.985549 | 43.129200 | 0.043782 | 0.002575 | 1e+07 | l1 | {'C': 10000000.0, 'penalty': 'l1'} | 0.694047 | 0.709737 | 0.700831 | 0.701538 | 0.006425 | 12 | 1.0 | 1.000000 | 1.0 | 1.000000 | 0.000000 |
| 1 | 51.797149 | 7.266426 | 0.058810 | 0.008064 | 1e+07 | l2 | {'C': 10000000.0, 'penalty': 'l2'} | 0.694509 | 0.711583 | 0.702216 | 0.702769 | 0.006982 | 8 | 1.0 | 1.000000 | 1.0 | 1.000000 | 0.000000 |
| 2 | 125.188562 | 46.865003 | 0.028435 | 0.009611 | 1e+06 | l1 | {'C': 1000000.0, 'penalty': 'l1'} | 0.693586 | 0.714352 | 0.700831 | 0.702923 | 0.008606 | 6 | 1.0 | 1.000000 | 1.0 | 1.000000 | 0.000000 |
| 3 | 46.434756 | 6.818460 | 0.047470 | 0.021621 | 1e+06 | l2 | {'C': 1000000.0, 'penalty': 'l2'} | 0.694509 | 0.711121 | 0.702216 | 0.702615 | 0.006789 | 9 | 1.0 | 1.000000 | 1.0 | 1.000000 | 0.000000 |
| 4 | 131.480284 | 49.637990 | 0.025367 | 0.001607 | 100000 | l1 | {'C': 99999.99999999999, 'penalty': 'l1'} | 0.693586 | 0.711583 | 0.700831 | 0.702000 | 0.007394 | 11 | 1.0 | 1.000000 | 1.0 | 1.000000 | 0.000000 |
| 5 | 48.442208 | 4.821450 | 0.025852 | 0.000660 | 100000 | l2 | {'C': 99999.99999999999, 'penalty': 'l2'} | 0.694509 | 0.712506 | 0.703139 | 0.703385 | 0.007350 | 3 | 1.0 | 1.000000 | 1.0 | 1.000000 | 0.000000 |
| 6 | 136.005517 | 63.967902 | 0.035429 | 0.009921 | 10000 | l1 | {'C': 10000.0, 'penalty': 'l1'} | 0.693586 | 0.712967 | 0.700831 | 0.702462 | 0.007997 | 10 | 1.0 | 1.000000 | 1.0 | 1.000000 | 0.000000 |
| 7 | 45.492904 | 4.962405 | 0.024428 | 0.003608 | 10000 | l2 | {'C': 10000.0, 'penalty': 'l2'} | 0.693586 | 0.712506 | 0.703601 | 0.703231 | 0.007729 | 5 | 1.0 | 1.000000 | 1.0 | 1.000000 | 0.000000 |
| 8 | 109.751415 | 29.097978 | 0.036205 | 0.012117 | 1000 | l1 | {'C': 1000.0, 'penalty': 'l1'} | 0.693586 | 0.712044 | 0.703139 | 0.702923 | 0.007538 | 6 | 1.0 | 1.000000 | 1.0 | 1.000000 | 0.000000 |
| 9 | 40.780339 | 5.498792 | 0.022993 | 0.002149 | 1000 | l2 | {'C': 1000.0, 'penalty': 'l2'} | 0.693124 | 0.711583 | 0.705448 | 0.703385 | 0.007676 | 3 | 1.0 | 1.000000 | 1.0 | 1.000000 | 0.000000 |
| 10 | 171.943614 | 10.621141 | 0.046334 | 0.018095 | 100 | l1 | {'C': 100.0, 'penalty': 'l1'} | 0.684818 | 0.708814 | 0.699908 | 0.697846 | 0.009905 | 14 | 1.0 | 1.000000 | 1.0 | 1.000000 | 0.000000 |
| 11 | 35.711317 | 5.045432 | 0.021141 | 0.000833 | 100 | l2 | {'C': 100.0, 'penalty': 'l2'} | 0.694509 | 0.712506 | 0.705448 | 0.704154 | 0.007405 | 1 | 1.0 | 1.000000 | 1.0 | 1.000000 | 0.000000 |
| 12 | 111.015806 | 21.466425 | 0.031371 | 0.011843 | 10 | l1 | {'C': 10.0, 'penalty': 'l1'} | 0.685741 | 0.716198 | 0.698523 | 0.700154 | 0.012488 | 13 | 1.0 | 1.000000 | 1.0 | 1.000000 | 0.000000 |
| 13 | 30.197917 | 4.607725 | 0.021942 | 0.004698 | 10 | l2 | {'C': 10.0, 'penalty': 'l2'} | 0.694047 | 0.714352 | 0.702216 | 0.703538 | 0.008342 | 2 | 1.0 | 0.999769 | 1.0 | 0.999923 | 0.000109 |
In [9]:
train_accuracy = skm.accuracy_score(y_train, clf.predict(X_train))
test_accuracy = skm.accuracy_score(y_test, clf.predict(X_test))
critical_accuracy = skm.accuracy_score(labels[critical], clf.predict(data[critical]))
print ("Accuracy on train data: {0}".format(train_accuracy))
print ("Accuracy on test data: {0}".format(test_accuracy))
print ("Accuracy on critical data: {0}".format(critical_accuracy))Accuracy on train data: 0.876 Accuracy on test data: 0.6896275303643725 Accuracy on critical data: 0.5445
In [10]:
fig = plt.figure(figsize=(20, 14))
for (_X, _y), label in zip(
[
(X_train, y_train),
(X_test, y_test),
(data[critical], labels[critical])
],
["Train", "Test", "Critical"]
):
proba = clf.predict_proba(_X)
fpr, tpr, _ = skm.roc_curve(_y, proba[:, 1])
roc_auc = skm.auc(fpr, tpr)
print ("LogisticRegression AUC ({0}): {1}".format(label, roc_auc))
plt.plot(fpr, tpr, label="{0} (AUC = {1})".format(label, roc_auc), linewidth=4.0)
plt.plot([0, 1], [0, 1], "--", label="Guessing (AUC = 0.5)", linewidth=4.0)
plt.title(r"The ROC curve for LogisticRegression", fontsize=18)
plt.xlabel(r"False positive rate", fontsize=18)
plt.ylabel(r"True positive rate", fontsize=18)
plt.axis([-0.01, 1.01, -0.01, 1.01])
plt.xticks(fontsize=18)
plt.yticks(fontsize=18)
plt.legend(loc="best", fontsize=18)
plt.show()LogisticRegression AUC (Train): 0.927466526230532 LogisticRegression AUC (Test): 0.4975344101021899 LogisticRegression AUC (Critical): 0.500251495
