def one_cln(self, array, train = 0.6, cross = 0.2): #array is the 1-D array -> the column to be stratified s3 = sss(array, n_iter = 1, test_size = (1.0 - train - cross), train_size = train, random_state = int(time.time()%60)) for train_index, test_index in s3: pass index = np.array(xrange(array.size)) cross_index = np.setdiff1d(index, np.concatenate((train_index, test_index)), assume_unique = True) np.random.shuffle(cross_index) return (train_index, cross_index, test_index)
def one_cln(self, array, train = 0.6, cross = 0.2): """Same as method <one_cln3>, the difference is that one_cln3 is a class method.""" s3 = sss(array, n_iter = 1, test_size = (1.0 - train - cross), train_size = train, random_state = int(time.time()%60)) for train_index, test_index in s3: pass index = np.array(xrange(array.size)) cross_index = np.setdiff1d(index, np.concatenate((train_index, test_index)), assume_unique = True) np.random.shuffle(cross_index) return (train_index, cross_index, test_index)
def one_cln2(cls, array, train = 0.8): """Split a 1-D array into 2 segments, one for training, the other for testing. Input -> array : 1-D array, the column to be stratified, taken from the whole 2-D array train : the ratio of training data, ranging from 0 to 1 ============================================================================================ Output -> Return a tuple of 2 arrays, one contains the index of training data, and the other contains index of testing data """ random_state = 8#int(time.time()%60) s3 = sss(array, n_iter = 1, test_size = (1.0 - train), random_state = random_state) print 'random_state is ', random_state for train_index, test_index in s3: pass return (train_index, test_index)
def one_cln3(cls, array, train = 0.6, cross = 0.2): """Split a 1-D array into 3 segments, one for training, one for cross validation and the third for testing. Input -> array : 1-D array, the column to be stratified, taken from the whole 2-D array train : the ratio of training data, ranging from 0 to 1 cross : the ratio of cross validation data, ranging from (1 - train) to 1 ============================================================================================ Output -> Return a tuple of 3 arrays, one contains the index of training data, one contains the index of cross validation data, and the third contains index of testing data """ s3 = sss(array, n_iter = 1, test_size = (1.0 - train - cross), train_size = train, random_state = int(time.time()%60)) for train_index, test_index in s3: pass index = np.array(xrange(array.size)) cross_index = np.setdiff1d(index, np.concatenate((train_index, test_index)), assume_unique = True) np.random.shuffle(cross_index) return (train_index, cross_index, test_index)
def one_cln( self, array, train=0.6, cross=0.2): #array is the 1-D array -> the column to be stratified s3 = sss(array, n_iter=1, test_size=(1.0 - train - cross), train_size=train, random_state=int(time.time() % 60)) for train_index, test_index in s3: pass index = np.array(xrange(array.size)) cross_index = np.setdiff1d(index, np.concatenate((train_index, test_index)), assume_unique=True) np.random.shuffle(cross_index) return (train_index, cross_index, test_index)
import scipy.optimize as opt from LibPoisonOCSVM import * import matplotlib.pyplot as plt from sklearn.cross_validation import StratifiedShuffleSplit as sss import sklearn.preprocessing as prep from OCSVM import * # X = sio.loadmat('../data/toy_sample.mat')['X'] # y = -sio.loadmat('../data/toy_sample.mat')['y'].ravel() X, y = utils.gen_noise_gauss(500, cov=.25) # X, y = gen_svc_data([0, 0], 2.5, cluster_sizes=[100, 20, 10, 10]) # X = prep.scale(X) cvfolds = sss(y, n_iter=1, test_size=0.85) data = dict() for tr_id, tt_id in cvfolds: data['train'] = X[tr_id] data['test'] = X[tt_id] data['train_labels'] = y[tr_id] data['test_labels'] = y[tt_id] n, d = X.shape clf = OCSVM(nu=0.2, gamma=.5) clf.fit(data['train']) big_x, big_y = utils.getBoxbyX(data['train'], grid=30) big_xy = np.c_[big_x.reshape(big_x.size, 1), big_y.reshape(big_x.size, 1)] big_z = clf.decision_function(big_xy) #