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]:
In [3]:
ymin = 1
ysplit = 131
ymax = 156
In [4]:
np.unique(y[0], return_counts=True)
Out[4]:
In [4]:
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]]))
In [5]:
import pickle
cstat = pickle.load(open( "../data/sum_features.dat", "rb" ) )
In [6]:
### 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))
In [7]:
rdiff = calStandMeanDiff(y, cstat, np.arange(ymin,ysplit), np.arange(ysplit, ymax+1))
## Good Features:
goodfeatures = np.where(rdiff > 0.001)[0]
goodfeatures
Out[7]:
In [8]:
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 [23]:
## 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))
In [9]:
### 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 [10]:
y = pandas.read_table('../data/label_tr.lower.txt', sep=' ', header=None, dtype='int')
print(np.unique(y[1]))
Xsub, ysub = readRandomSample('../data/data_tr.lower.txt', y[1], size=20000, \
goodfeat=goodfeatures, acc_miny=ymin, acc_maxy=ymax)
print(np.unique(ysub))
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]
Xsub = (Xsub - np.mean(Xsub, axis=0)) / np.std(Xsub, axis=0)
Xsub.shape
Out[10]:
In [ ]:
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.001, 0.01, 0.1, 1.0, 5.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))
In [18]:
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.005, 0.008, 0.01, 0.015, 0.05, 0.08]:
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))
In [ ]: