In [1]:
from sklearn.datasets import fetch_mldata
from sklearn import preprocessing
from sklearn.cross_validation import train_test_split
from sklearn.metrics import accuracy_score

import OVFM.FeatureMap as fm
import OVFM.Model as mdl
import OVFM.Risk as rsk
import OVFM.LearningRate as lr
# import OVFM.DataGeneration as dg
import OVFM.SGD as sgd

import numpy as np
from joblib import Parallel, delayed  
import multiprocessing

import time

def simplex_map( i ):
    if i > 2:
        return np.bmat( [ [ [ [ 1 ] ], np.repeat( -1.0 / ( i - 1 ), i - 1 ).reshape( ( 1, i - 1 ) ) ], [ np.zeros( ( i - 2, 1 ) ), simplex_map( i - 1 ) * np.sqrt( 1.0 - 1.0 / ( i - 1 ) ** 2 ) ] ] )
    elif i == 2:
        return np.array( [ [ 1, -1 ] ] )
    else:
        raise "invalid number of classes"
        
print simplex_map( 3 )


[[ 1.        -0.5       -0.5      ]
 [ 0.         0.8660254 -0.8660254]]

In [2]:
mnist = fetch_mldata( 'MNIST original', data_home='mnist_data' )
data_X = np.asarray( mnist.data, np.float )
data_y = mnist.target

# data_X = preprocessing.StandardScaler( ).fit_transform( data_X )
data_X = 2 * ( data_X / 255.0 ) - 1
# data_X = ( data_X.T - np.mean( data_X, axis = 1 ) ).T
# data_X = ( data_X.T / np.std( data_X, axis = 1 ) ).T
# data_X = preprocessing.StandardScaler( ).fit_transform( data_X )
# print data_X.mean( )

lb = preprocessing.LabelBinarizer( neg_label=0, pos_label=1, sparse_output=False )
data_y = np.dot( np.asarray( lb.fit_transform( data_y ), np.float ), simplex_map( 10 ).T )

In [3]:
gamma = 0.02
train_X, test_X, train_y, test_y = train_test_split( data_X, data_y, test_size = 1.0 / 7.0, random_state = 0 )

In [4]:
D = 1000
lbda = 1.0

# L = np.cov( train_y ).T
# L = np.empty( 10 )
# for i in xrange( 0, 10 ):
#     L[ i ] = np.mean( lb.inverse_transform( train_y ) == i )
# L = np.diag( L )
# L = np.eye( 10 )

model = mdl.Model( fm.DecomposableFF( gamma, train_X.shape[ 1 ], D, A = np.eye( 9 )  ) )
risk  = rsk.SVM( 10, 1e-5 )

In [5]:
lcoefs = lr.optimal( 1 )
lbias = lr.InvScaling( 0, 0.5 )
ovksgd = sgd.SGD( risk, 1, lcoefs, lbias, 10, 0 )

t_start = time.time( )
model.reset( )
ovksgd.fit( model, train_X, train_y )
print time.time( ) - t_start


16.4297330379

In [6]:
pred = model( test_X )
S = simplex_map( 10 )
pred = np.argmax( np.dot( pred, S ), axis = 1 )
ground_truth = np.argmax( np.dot( test_y, S ), axis = 1 )

print model.bias
print np.mean( pred != ground_truth ) * 100

# print np.mean( pred != lb.inverse_transform( np.argmax( np.dot( test_y, S ) ) ) ) * 100


[[ 0.  0.  0.  0.  0.  0.  0.  0.  0.]]
74.9

In [7]:
print np.linalg.norm( model.coefs )
print 1 / np.sqrt( 1e-7 )
print model.coefs


11562.7740177
3162.27766017
[[ -19.60302948   10.62332612   38.37068035 ..., -220.37197392
    33.94726554   60.51199063]]

In [7]:
from sklearn.metrics import confusion_matrix, classification_report
print("Classification report for classifier:\n%s\n" % classification_report( ground_truth, pred))
print("Confusion matrix:\n%s" % confusion_matrix( ground_truth, pred))


Classification report for classifier:
             precision    recall  f1-score   support

          0       0.97      0.98      0.98       936
          1       0.98      0.98      0.98      1163
          2       0.94      0.96      0.95       982
          3       0.96      0.95      0.95      1038
          4       0.94      0.97      0.95       948
          5       0.98      0.94      0.96       921
          6       0.97      0.99      0.98      1013
          7       0.94      0.96      0.95      1029
          8       0.97      0.93      0.95       978
          9       0.94      0.93      0.93       992

avg / total       0.96      0.96      0.96     10000


Confusion matrix:
[[ 919    0    3    1    1    0    7    4    0    1]
 [   0 1139   13    2    5    0    1    3    0    0]
 [   4    3  946    3    5    0    2   11    8    0]
 [   1    3   14  986    1    4    1   10    8   10]
 [   0    5    0    0  918    1    7    2    0   15]
 [   6    0    3   10    5  863   13    4    9    8]
 [   4    2    2    0    2    3  999    0    1    0]
 [   4    4   11    0   11    1    0  986    1   11]
 [   4    7    8   12    6    9    4    6  912   10]
 [   6    3    2   13   21    1    0   25    3  918]]

In [8]:
from sklearn.metrics import roc_curve, auc
import matplotlib.pyplot as plt

# Compute ROC curve and ROC area for each class
fpr = dict()
tpr = dict()
roc_auc = dict()
for i in range(10):
    fpr[i], tpr[i], _ = roc_curve(test_y[:,i], model( test_X )[:, i])
    roc_auc[i] = auc(fpr[i], tpr[i])

# Compute micro-average ROC curve and ROC area
fpr["micro"], tpr["micro"], _ = roc_curve(test_y.ravel(), model( test_X ).ravel())
roc_auc["micro"] = auc(fpr["micro"], tpr["micro"])

# Plot of a ROC curve for a specific class
plt.figure()
plt.plot(fpr[2], tpr[2], label='ROC curve (area = %0.2f)' % roc_auc[2])
plt.plot([0, 1], [0, 1], 'k--')
plt.xlim([0.0, 1.0])
plt.ylim([0.0, 1.05])
plt.xlabel('False Positive Rate')
plt.ylabel('True Positive Rate')
plt.title('Receiver operating characteristic example')
plt.legend(loc="lower right")
plt.show()

# Plot ROC curve
plt.figure()
plt.plot(fpr["micro"], tpr["micro"],
         label='micro-average ROC curve (area = {0:0.2f})'
               ''.format(roc_auc["micro"]))
for i in range(10):
    plt.plot(fpr[i], tpr[i], label='ROC curve of class {0} (area = {1:0.2f})'
                                   ''.format(i, roc_auc[i]))

plt.plot([0, 1], [0, 1], 'k--')
plt.xlim([0.0, 1.0])
plt.ylim([0.0, 1.05])
plt.xlabel('False Positive Rate')
plt.ylabel('True Positive Rate')
plt.title('Some extension of Receiver operating characteristic to multi-class')
plt.legend(loc="lower right")
plt.show()


---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-8-9d294486e6af> in <module>()
      7 roc_auc = dict()
      8 for i in range(10):
----> 9     fpr[i], tpr[i], _ = roc_curve(test_y[:,i], model( test_X )[:, i])
     10     roc_auc[i] = auc(fpr[i], tpr[i])
     11 

/Library/Python/2.7/site-packages/sklearn/metrics/ranking.pyc in roc_curve(y_true, y_score, pos_label, sample_weight)
    475     """
    476     fps, tps, thresholds = _binary_clf_curve(
--> 477         y_true, y_score, pos_label=pos_label, sample_weight=sample_weight)
    478 
    479     if tps.size == 0 or fps[0] != 0:

/Library/Python/2.7/site-packages/sklearn/metrics/ranking.pyc in _binary_clf_curve(y_true, y_score, pos_label, sample_weight)
    295              np.all(classes == [-1]) or
    296              np.all(classes == [1]))):
--> 297         raise ValueError("Data is not binary and pos_label is not specified")
    298     elif pos_label is None:
    299         pos_label = 1.

ValueError: Data is not binary and pos_label is not specified

In [11]:


In [12]:
np.linalg.pinv( np.linalg.pinv( simplex_coding( 3 ) ) )


Out[12]:
matrix([[  1.00000000e+00,  -5.00000000e-01,  -5.00000000e-01],
        [  1.23498149e-16,   8.66025404e-01,  -8.66025404e-01]])

In [13]:
simplex_coding( 3 ) - np.linalg.pinv( np.linalg.pinv( simplex_coding( 3 ) ) )


Out[13]:
matrix([[  1.11022302e-16,   0.00000000e+00,   0.00000000e+00],
        [ -1.23498149e-16,  -6.66133815e-16,   3.33066907e-16]])

In [8]:
A = np.random.rand( 5,5 )
print
[ A[ 1, x ] for x in  ]


[[ 0.70553572  0.57259449  0.86869372  0.31830899  0.42495934]
 [ 0.60689144  0.91647829  0.4404636   0.87144091  0.0998809 ]
 [ 0.20676205  0.18787808  0.33036263  0.19609073  0.40921875]
 [ 0.85481895  0.25750404  0.04930061  0.45061209  0.8459887 ]
 [ 0.98516047  0.83687615  0.53974799  0.47980101  0.1840602 ]]

In [ ]: