In [1]:
import numpy as np
import pandas
import scipy, scipy.spatial
import sklearn
import sys

from matplotlib import pyplot as plt

%matplotlib inline

In [2]:
y = pandas.read_table("~/Downloads/data/ml/label_train.txt", sep=" ", dtype='int', header=None)

ndim= 900
y.head()


Out[2]:
0
0 161
1 163
2 56
3 119
4 138

In [3]:
ymin = 160
ysplit = 161
ymax = 161

In [4]:
np.unique(y[0], return_counts=True)


Out[4]:
(array([  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,  63,  64,  65,
         66,  67,  68,  69,  70,  71,  72,  73,  74,  75,  76,  77,  78,
         79,  80,  81,  82,  83,  84,  85,  86,  87,  88,  89,  90,  91,
         92,  93,  94,  95,  96,  97,  98,  99, 100, 101, 102, 103, 104,
        105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117,
        118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130,
        131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143,
        144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156,
        157, 158, 159, 160, 161, 162, 163, 164]),
 array([  1263,   1261,   1255,   1256,   1252,   1235,   1240,   1264,
          1256,   1281,   1245,   1278,   1278,   1253,   1255,   1255,
          1291,   1277,   1308,   1285,   1322,   1309,   1318,   1322,
          1327,   1339,   1361,   1361,   1335,   1396,   1359,   1393,
          1373,   1356,   1398,   1416,   1386,   1398,   1396,   1404,
          1430,   1398,   1416,   1406,   1420,   1445,   1433,   1445,
          1454,   1451,   1481,   1482,   1477,   1474,   1478,   1486,
          1512,   1492,   1557,   1557,   1548,   1530,   1574,   1582,
          1606,   1611,   1666,   1650,   1704,   1739,   1735,   1743,
          1728,   1796,   1737,   1810,   1822,   1864,   1847,   1838,
          1857,   1913,   1910,   1917,   2006,   1992,   2033,   2063,
          2072,   2063,   2096,   2128,   2134,   2206,   2215,   2212,
          2258,   2279,   2287,   2319,   2356,   2435,   2438,   2491,
          2486,   2485,   2502,   2555,   2594,   2629,   2575,   2587,
          2777,   2875,   2897,   2884,   2978,   3087,   3179,   3368,
          3388,   3421,   3409,   3453,   3536,   3586,   3615,   3696,
          3821,   3802,   3934,   4059,   4069,   4253,   4819,   4939,
          5038,   5259,   5310,   6080,   6487,   6623,   7256,   8279,
          9069,   9221,   9707,   9998,  10557,  10645,  11484,  12382,
         12858,  16548,  18562,  21943,  30679,  34092,  45439,  60513,
         64478,  65211,  92241, 130122]))

In [5]:
yuniq,ycount = np.unique(y[0], return_counts=True)

print(np.sum(ycount[np.where(np.in1d(yuniq, range(ymin, ysplit)))[0]]))
print(np.sum(ycount[np.where(np.in1d(yuniq, range(ysplit, ymax+1)))[0]]))


60513
64478

In [6]:
import pickle

cstat = pickle.load(open( "../data/sum_features.dat", "rb" ) )

In [10]:
### Calclulate Standardized Mean Difference Between Classes

def calStandMeanDiff(y, cstat, yneg, ypos):
    sx  = np.zeros(shape=ndim, dtype=float)
    ssx = np.zeros(shape=ndim, dtype=float)


    n1 = np.sum(np.in1d(y, yneg))
    n2 = np.sum(np.in1d(y, ypos))
    sys.stderr.write("Number of samples in NegClass: %d and PosClass: %d \n"%(n1, n2))

    for yi in yneg:
        sx += cstat[yi][0]
        ssx += cstat[yi][1]
    r1_mean = sx / float(n1)
    r1_var = (ssx - 2*sx*r1_mean + r1_mean**2) / float(n1)

    tot_mean = sx
    tot_var  = ssx
    
    sx  = np.zeros(shape=ndim, dtype=float)
    ssx = np.zeros(shape=ndim, dtype=float)
    for yi in ypos:
        sx += cstat[yi][0]
        ssx += cstat[yi][1]
    r2_mean = sx / float(n2)
    r2_var = (ssx - 2*sx*r2_mean + r2_mean**2) / float(n2)

    tot_mean += sx
    tot_var  += ssx
    tot_mean = tot_mean / float(n1 + n2)
    tot_var  = (tot_var - 2*tot_var*tot_mean + tot_mean**2) / float(n1 + n2)

    rdiff = (r1_mean - r2_mean) / np.sqrt(tot_var)

    return (rdiff)


## unit test:
mean_test = calStandMeanDiff(y, cstat, np.arange(ymin,ysplit), np.arange(ysplit, ymax+1)) 
print(np.sum(mean_test > 0.001))


200
Number of samples in NegClass: 60513 and PosClass: 64478 

Classify items belonging to first half (1) Second half (-1)

Finding Good Features


In [11]:
rdiff = calStandMeanDiff(y, cstat, np.arange(ymin,ysplit), np.arange(ysplit, ymax+1))


## Good Features:
goodfeatures = np.where(rdiff > 0.001)[0]

goodfeatures


Number of samples in NegClass: 60513 and PosClass: 64478 
Out[11]:
array([  1,   5,   6,  31,  38,  39,  40,  43,  52,  56,  59,  61,  69,
        71,  79,  81,  91,  94,  97,  99, 110, 112, 118, 119, 122, 133,
       137, 138, 139, 143, 149, 151, 155, 158, 159, 161, 166, 184, 185,
       186, 188, 196, 204, 206, 214, 215, 216, 217, 221, 222, 224, 229,
       236, 239, 247, 254, 258, 261, 262, 263, 265, 267, 274, 277, 278,
       281, 282, 288, 292, 295, 298, 307, 308, 315, 316, 322, 329, 330,
       332, 334, 340, 341, 343, 344, 348, 352, 353, 355, 367, 374, 380,
       385, 387, 388, 389, 399, 400, 408, 412, 414, 416, 428, 440, 443,
       445, 447, 461, 462, 464, 466, 467, 468, 496, 498, 500, 501, 517,
       524, 526, 527, 530, 537, 539, 542, 543, 544, 545, 547, 556, 561,
       563, 564, 566, 582, 583, 600, 601, 603, 605, 608, 610, 613, 628,
       632, 645, 658, 661, 666, 676, 680, 681, 686, 698, 703, 714, 716,
       717, 722, 732, 735, 738, 742, 748, 749, 751, 753, 758, 760, 762,
       773, 776, 779, 784, 790, 796, 811, 812, 815, 817, 818, 821, 822,
       826, 827, 828, 829, 849, 863, 870, 874, 876, 877, 879, 881, 884,
       885, 886, 892, 893, 897])

Read a Random Sample


In [12]:
def readRandomSample(data_fname, y, size, goodfeat=None, acc_miny=None, acc_maxy=None):
    """ Read a random sample
    """
    if goodfeat is None:
        goodfeat = np.arange(ndim)
    Xsub = np.empty(shape=(size,goodfeat.shape[0]), dtype=float)
    ysub = np.zeros(shape=size, dtype=int)

    if acc_miny is None:
        acc_miny = np.min(y)
    if acc_maxy is None:
        acc_maxy = np.max(y)
        
    #yuniq, ycount = np.unique(y, return_counts=True)
    #tot_acceptable = np.sum(ycount[np.where((yuniq >= acc_miny) & (yuniq <= acc_maxy))[0]])
    
    acceptable_indx = np.where((y>=acc_miny) & (y<=acc_maxy))[0]
    assert(acceptable_indx.shape[0] > size)
    choice_indx = np.sort(np.random.choice(acceptable_indx, size, replace=False))
    #print(choice_indx.shape)
    #sys.stderr.write("Total Accetables: --> %d"%(tot_acceptable))
    
    #proba = 1.0 - size/float(tot_acceptable)
    
        
    with open(data_fname, 'r') as fp:
        n = 0
        nf = 0
        for line in fp:
#            if (y[n] >= acc_miny and y[n]<=acc_maxy):
#                if np.random.uniform(low=0, high=1) > proba and nf < size:
            if nf < size:
                if n == choice_indx[nf]:
                    line = line.strip().split()
                    ix = -1
                    for i,v in enumerate(line):
                        if np.any(goodfeat == i):
                            ix += 1
                            Xsub[nf,ix] = int(v)
                    ysub[nf] = y[n]

                    nf += 1
            n += 1
    return(Xsub, ysub)

In [13]:
## unit testing readRandomSample()
gf_test = goodfeatures
Xsub, ysub = readRandomSample('/home/vahid/Downloads/data/ml/data_train.txt', y[0], \
                              size=2000, goodfeat=gf_test, acc_miny=ymin, acc_maxy=ymax)

print(Xsub.shape)
print(np.unique(ysub))


(2000, 200)
[160 161]

In [14]:
### Performance Evaluation
def evalPerformance(ytrue, ypred):
    tp = np.sum(ypred[np.where(ytrue ==  1)[0]] == 1)
    fp = np.sum(ypred[np.where(ytrue == -1)[0]] == 1)
    tn = np.sum(ypred[np.where(ytrue == -1)[0]] == -1)
    fn = ytrue.shape[0]-(tp+fp+tn)
    #sys.stderr.write('%d %d %d %d\n'%(tp,fp,tn,fn))
    prec = tp / float(tp + fp)
    recall  = tp / float(tp + fn)
    f1score = 2*tp/float(2*tp + fp + fn)

    return (prec, recall, f1score)

In [15]:
Xsub, ysub = readRandomSample('/home/vahid/Downloads/data/ml/data_train.txt', y[0], size=20000, \
                              goodfeat=goodfeatures, acc_miny=ymin, acc_maxy=ymax)

assert(np.sum(ysub < ymin) == 0)
assert(np.sum(ysub > ymax) == 0)
ysub[np.where(ysub < ysplit)[0]] = -1
ysub[np.where(ysub >= ysplit)[0]] =  1

print(np.sum(ysub == -1), np.sum(ysub==1))

#Xsub = Xsub[:, goodfeatures]
x_std = np.std(Xsub, axis=0)
print(np.where(x_std < 0.0001))
Xsub = (Xsub - np.mean(Xsub, axis=0)) / np.std(Xsub, axis=0)

Xsub.shape


(9675, 10325)
(array([], dtype=int64),)
Out[15]:
(20000, 200)

Grid-Search (coarse)


In [16]:
import sklearn.svm

ntot = Xsub.shape[0]
tr_idx = np.random.choice(ntot, size=ntot/2, replace=False)
ts_idx = np.setdiff1d(np.arange(ntot), tr_idx, assume_unique=True)
yts = ysub[ts_idx]

for c in [0.01, 0.1, 1.0, 5.0, 10.0]:
    for gm in [0.001, 0.01, 0.1, 1.0, 5.0]:
        clf = sklearn.svm.SVC(C=c, kernel='rbf', gamma=gm)
        clf.fit(Xsub[tr_idx, :], ysub[tr_idx])
        ypred = clf.predict(Xsub[ts_idx, :])
        prec, recall, f1score = evalPerformance(yts, ypred)
        print ("C=%.4f Gamma=%.4f  ==> Prec:%.3f  Recall:%.3f  F1Score:%.3f"%(c, gm, prec, recall, f1score))


C=0.0010 Gamma=0.0010  ==> Prec:0.516  Recall:1.000  F1Score:0.681
C=0.0010 Gamma=0.0100  ==> Prec:0.516  Recall:1.000  F1Score:0.681
C=0.0010 Gamma=0.1000  ==> Prec:0.516  Recall:1.000  F1Score:0.681
C=0.0010 Gamma=1.0000  ==> Prec:0.516  Recall:1.000  F1Score:0.681
C=0.0010 Gamma=5.0000  ==> Prec:0.516  Recall:1.000  F1Score:0.681
C=0.0100 Gamma=0.0010  ==> Prec:0.522  Recall:0.970  F1Score:0.679
C=0.0100 Gamma=0.0100  ==> Prec:0.607  Recall:0.431  F1Score:0.504
C=0.0100 Gamma=0.1000  ==> Prec:0.516  Recall:1.000  F1Score:0.681
C=0.0100 Gamma=1.0000  ==> Prec:0.516  Recall:1.000  F1Score:0.681
C=0.0100 Gamma=5.0000  ==> Prec:0.516  Recall:1.000  F1Score:0.681
C=0.1000 Gamma=0.0010  ==> Prec:0.583  Recall:0.751  F1Score:0.656
C=0.1000 Gamma=0.0100  ==> Prec:0.631  Recall:0.497  F1Score:0.556
C=0.1000 Gamma=0.1000  ==> Prec:0.516  Recall:1.000  F1Score:0.681
C=0.1000 Gamma=1.0000  ==> Prec:0.516  Recall:1.000  F1Score:0.681
C=0.1000 Gamma=5.0000  ==> Prec:0.516  Recall:1.000  F1Score:0.681
C=1.0000 Gamma=0.0010  ==> Prec:0.612  Recall:0.731  F1Score:0.667
C=1.0000 Gamma=0.0100  ==> Prec:0.641  Recall:0.644  F1Score:0.643
C=1.0000 Gamma=0.1000  ==> Prec:0.653  Recall:0.279  F1Score:0.391
C=1.0000 Gamma=1.0000  ==> Prec:0.516  Recall:0.998  F1Score:0.680
C=1.0000 Gamma=5.0000  ==> Prec:0.516  Recall:0.999  F1Score:0.681
C=5.0000 Gamma=0.0010  ==> Prec:0.622  Recall:0.737  F1Score:0.674
C=5.0000 Gamma=0.0100  ==> Prec:0.639  Recall:0.645  F1Score:0.642
C=5.0000 Gamma=0.1000  ==> Prec:0.666  Recall:0.268  F1Score:0.382
C=5.0000 Gamma=1.0000  ==> Prec:0.515  Recall:0.995  F1Score:0.679
C=5.0000 Gamma=5.0000  ==> Prec:0.516  Recall:0.999  F1Score:0.681

In [17]:
import sklearn.svm

ntot = Xsub.shape[0]
tr_idx = np.random.choice(ntot, size=ntot/2, replace=False)
ts_idx = np.setdiff1d(np.arange(ntot), tr_idx, assume_unique=True)
yts = ysub[ts_idx]

for c in [1, 2, 5, 8, 10]:
    for gm in [0.0005, 0.0008, 0.001, 0.0015, 0.005, 0.008]:
        clf = sklearn.svm.SVC(C=c, kernel='rbf', gamma=gm)
        clf.fit(Xsub[tr_idx, :], ysub[tr_idx])
        ypred = clf.predict(Xsub[ts_idx, :])
        prec, recall, f1score = evalPerformance(yts, ypred)
        print ("C=%.4f Gamma=%.4f  ==> Prec:%.3f  Recall:%.3f  F1Score:%.3f"%(c, gm, prec, recall, f1score))


C=1.0000 Gamma=0.0005  ==> Prec:0.616  Recall:0.739  F1Score:0.672
C=1.0000 Gamma=0.0008  ==> Prec:0.622  Recall:0.731  F1Score:0.672
C=1.0000 Gamma=0.0010  ==> Prec:0.625  Recall:0.729  F1Score:0.673
C=1.0000 Gamma=0.0015  ==> Prec:0.630  Recall:0.721  F1Score:0.673
C=1.0000 Gamma=0.0050  ==> Prec:0.644  Recall:0.683  F1Score:0.663
C=1.0000 Gamma=0.0080  ==> Prec:0.651  Recall:0.656  F1Score:0.653
C=2.0000 Gamma=0.0005  ==> Prec:0.620  Recall:0.734  F1Score:0.672
C=2.0000 Gamma=0.0008  ==> Prec:0.626  Recall:0.727  F1Score:0.673
C=2.0000 Gamma=0.0010  ==> Prec:0.627  Recall:0.726  F1Score:0.673
C=2.0000 Gamma=0.0015  ==> Prec:0.633  Recall:0.727  F1Score:0.677
C=2.0000 Gamma=0.0050  ==> Prec:0.641  Recall:0.694  F1Score:0.667
C=2.0000 Gamma=0.0080  ==> Prec:0.644  Recall:0.666  F1Score:0.655
C=5.0000 Gamma=0.0005  ==> Prec:0.624  Recall:0.727  F1Score:0.672
C=5.0000 Gamma=0.0008  ==> Prec:0.630  Recall:0.729  F1Score:0.676
C=5.0000 Gamma=0.0010  ==> Prec:0.631  Recall:0.732  F1Score:0.678
C=5.0000 Gamma=0.0015  ==> Prec:0.633  Recall:0.729  F1Score:0.678
C=5.0000 Gamma=0.0050  ==> Prec:0.633  Recall:0.687  F1Score:0.659
C=5.0000 Gamma=0.0080  ==> Prec:0.638  Recall:0.657  F1Score:0.647
C=8.0000 Gamma=0.0005  ==> Prec:0.628  Recall:0.732  F1Score:0.676
C=8.0000 Gamma=0.0008  ==> Prec:0.631  Recall:0.730  F1Score:0.677
C=8.0000 Gamma=0.0010  ==> Prec:0.632  Recall:0.731  F1Score:0.678
C=8.0000 Gamma=0.0015  ==> Prec:0.632  Recall:0.732  F1Score:0.679
C=8.0000 Gamma=0.0050  ==> Prec:0.630  Recall:0.678  F1Score:0.653
C=8.0000 Gamma=0.0080  ==> Prec:0.636  Recall:0.652  F1Score:0.644
C=10.0000 Gamma=0.0005  ==> Prec:0.628  Recall:0.730  F1Score:0.675
C=10.0000 Gamma=0.0008  ==> Prec:0.632  Recall:0.733  F1Score:0.679
C=10.0000 Gamma=0.0010  ==> Prec:0.633  Recall:0.734  F1Score:0.680
C=10.0000 Gamma=0.0015  ==> Prec:0.633  Recall:0.732  F1Score:0.679
C=10.0000 Gamma=0.0050  ==> Prec:0.627  Recall:0.671  F1Score:0.648
C=10.0000 Gamma=0.0080  ==> Prec:0.634  Recall:0.647  F1Score:0.640

In [ ]: