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 )
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
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
In [7]:
print np.linalg.norm( model.coefs )
print 1 / np.sqrt( 1e-7 )
print model.coefs
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))
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()
In [11]:
In [12]:
np.linalg.pinv( np.linalg.pinv( simplex_coding( 3 ) ) )
Out[12]:
In [13]:
simplex_coding( 3 ) - np.linalg.pinv( np.linalg.pinv( simplex_coding( 3 ) ) )
Out[13]:
In [8]:
A = np.random.rand( 5,5 )
print
[ A[ 1, x ] for x in ]
In [ ]: