Basic unit tests


In [45]:
%load_ext autoreload
%autoreload 2

import sys
import os
sys.path.append("..")

from sklearn import svm, datasets
from sklearn.grid_search import ParameterGrid, GridSearchCV
from sklearn.base import BaseEstimator, clone 
from sklearn.cross_validation import KFold
from sklearn.svm import SVC
import os
import math
import numpy as np
import sklearn.metrics
from multiprocessing import Pool
from functools import partial

import sklearn
from sklearn.preprocessing import MinMaxScaler, LabelBinarizer
from sklearn.metrics import accuracy_score
from sklearn.cross_validation import KFold
from sklearn.preprocessing import MinMaxScaler

from misc.data_api import *

from r2 import *


from misc.config import c
from data_api import *
import cPickle
import pandas as pd
from data_api import *
results_dir = c['RESULTS_DIR']

from scripts.fit_models import *

%matplotlib inline


The autoreload extension is already loaded. To reload it, use:
  %reload_ext autoreload

0. Basic tests


In [ ]:

1. Seed R2 models


In [30]:
glass = fetch_uci_datasets(["glass"])[0]
X, Y = glass.data, glass.target
assert(sklearn.metrics.accuracy_score(R2SVMLearner(seed=3, beta=0.8).fit(X, Y).predict(X), Y) == sklearn.metrics.accuracy_score(R2SVMLearner(seed=3, beta=0.8).fit(X, Y).predict(X), Y))

In [31]:
glass = fetch_uci_datasets(["glass"])[0]
X, Y = glass.data, glass.target
assert(sklearn.metrics.accuracy_score(R2ELMLearner(seed=3, beta=0.8).fit(X, Y).predict(X), Y) == sklearn.metrics.accuracy_score(R2ELMLearner(seed=3, beta=0.8).fit(X, Y).predict(X), Y))

2. Reproduce results


In [47]:
fourclass = fetch_uci_datasets(['fourclass'])[0]
r2svm_exp = cPickle.load(open(os.path.join(results_dir, 
                                           'unit_test_r2svm_fourclass', 
                                           'unit_test_r2svm_fourclass_uF_rT_b0.10_d7_sT_fNo_.experiment')))
r2svm_params = r2svm_exp['config']['params']
rep_exp = k_fold(R2SVMLearner, r2svm_params, fourclass, exp_name='unit_test', model_name='r2svm', save_model=False)

assert r2svm_exp['config'] == rep_exp['config']
assert r2svm_exp['results'] == rep_exp['results']

In [49]:
fourclass = fetch_uci_datasets(['fourclass'])[0]
svm_exp = cPickle.load(open(os.path.join(results_dir, 
                                           'unit_test_svc_fourclass', 
                                           'unit_test_svc_fourclass_C1_g0_.experiment')))
svm_params = svm_exp['config']['params']
rep_exp = extern_k_fold(SVC(), svm_params, fourclass, exp_name='unit_test', model_name='svc', save_model=False)


assert svm_exp['config'] == rep_exp['config']
assert svm_exp['results'] == rep_exp['results']

3. Depth checking


In [69]:
indians = fetch_uci_datasets(['iris'])[0]
indian_params_random = {}
indian_params_random['fit_c'] = 'random'
indian_params_random['use_prev'] = True
indian_params_random['recurrent'] = False
indian_params_random['seed'] = 697
indian_params_random['beta'] = 0.1
indian_params_random['scale'] = True
indian_params_random['fixed_prediction'] = None

In [77]:
results_layer_wise = [k_fold(R2SVMLearner, params=dict({"depth":i+1}, **indian_params_random), all_layers=False, \
       data=indians, exp_name="check_cons", model_name="r2svm", store_clf=True, save_model=False)['results']['mean_acc'] \
           for i in range(10)]

results_all_layers = [k_fold(R2SVMLearner, params=dict({"depth":i+1}, **indian_params_random), all_layers=True, \
       data=indians, exp_name="check_cons", model_name="r2svm", store_clf=True, save_model=False)['results']['mean_acc'] \
           for i in range(10)]

assert(all(max(results_layer_wise[0:i+1]) == results_all_layers[i] for i in range(10) ) )

In [ ]: