import os import sys import time import numpy from pprint import pprint from scipy.io import matlab import re import math from collections import OrderedDict from layers import * import random def load_ATP_data_FEATURE(): train_pos_ff_file = open('../data/ATP_ff/train.ff') train_neg_ff_file = open('../data/ATP_ff/train_neg.ff') test_pos_ff_file = open('../data/ATP_ff/test.ff') test_neg_ff_file = open('../data/ATP_ff/test_neg.ff') pos_train_FF = [] for line in train_pos_ff_file: if line[0:4]=='Env_': ele = line.split() FV=numpy.zeros((480,)) for i in range(1,481): FV[i-1]=ele[i] pos_train_FF.append(FV) neg_train_FF = [] for line in train_neg_ff_file: if line[0:4]=='Env_': ele = line.split() FV=numpy.zeros((480,)) for i in range(1,481): FV[i-1]=ele[i] neg_train_FF.append(FV) pos_test_FF = [] for line in test_pos_ff_file: if line[0:4]=='Env_': ele = line.split() FV=numpy.zeros((480,)) for i in range(1,481): FV[i-1]=ele[i] pos_test_FF.append(FV) neg_test_FF = [] for line in test_neg_ff_file: if line[0:4]=='Env_': ele = line.split() FV=numpy.zeros((480,)) for i in range(1,481): FV[i-1]=ele[i] neg_test_FF.append(FV) X_train_pos=numpy.array(pos_train_FF) X_train_neg=numpy.array(neg_train_FF) y_train_pos=numpy.ones((X_train_pos.shape[0],)) y_train_neg=numpy.zeros((X_train_neg.shape[0],)) X_train_pos=numpy.array(pos_train_FF) X_train_neg=numpy.array(neg_train_FF) y_train_pos=numpy.ones((X_train_pos.shape[0],)) y_train_neg=numpy.zeros((X_train_neg.shape[0],)) num_train_pos = X_train_pos.shape[0] X_train_neg=X_train_neg[0:num_train_pos*10] y_train_neg=y_train_neg[0:num_train_pos*10] X_test_pos=numpy.array(pos_test_FF) X_test_neg=numpy.array(neg_test_FF) y_test_pos=numpy.ones((X_test_pos.shape[0],)) y_test_neg=numpy.zeros((X_test_neg.shape[0],)) Xt_pos = X_test_pos yt_pos = y_test_pos Xt_neg = X_test_neg yt_neg = y_test_neg num_of_train_pos = int(19*float(X_train_pos.shape[0])/20) num_of_val_pos = int(1*float(X_train_pos.shape[0])/20) num_of_train_neg = int(19*float(X_train_neg.shape[0])/20) num_of_val_neg = int(1*float(X_train_neg.shape[0])/20) mask_train_pos = random.sample(xrange(X_train_pos.shape[0]), num_of_train_pos) X_tr_pos = X_train_pos[mask_train_pos] y_tr_pos = y_train_pos[mask_train_pos] mask_train_neg = random.sample(xrange(X_train_neg.shape[0]), num_of_train_neg) X_tr_neg = X_train_neg[mask_train_neg] y_tr_neg = y_train_neg[mask_train_neg] X_train_pos = numpy.delete(X_train_pos, mask_train_pos, 0) y_train_pos = numpy.delete(y_train_pos, mask_train_pos, 0) X_train_neg = numpy.delete(X_train_neg, mask_train_neg, 0) y_train_neg = numpy.delete(y_train_neg, mask_train_neg, 0) Xv_pos = X_train_pos yv_pos = y_train_pos Xv_neg = X_train_neg yv_neg = y_train_neg X = numpy.concatenate((X_tr_pos,X_tr_neg),axis=0) y = numpy.concatenate((y_tr_pos,y_tr_neg),axis=0) Xv = numpy.concatenate((Xv_pos,Xv_neg),axis=0) yv = numpy.concatenate((yv_pos,yv_neg),axis=0) Xt = numpy.concatenate((Xt_pos,Xt_neg),axis=0) yt = numpy.concatenate((yt_pos,yt_neg),axis=0) from sklearn.utils import shuffle X, y = shuffle(X, y) all_train_x=[] all_train_y=[] all_train_sizes=[] all_examples=[X,Xt,Xv] all_labels=[y,yt,yv] return [all_examples, all_labels, X.shape[0], Xt.shape[0], Xv.shape[0]] def test_fine_S_CNN_dA(): rng = numpy.random.RandomState(23455) [all_examples, all_labels, train_size, test_size, val_size]=load_ATP_data_FEATURE() Xtr=all_examples[0] Xt=all_examples[1] Xv=all_examples[2] ytr=all_labels[0] yt=all_labels[1] yv=all_labels[2] from sklearn.preprocessing import MinMaxScaler import math scaling = MinMaxScaler(feature_range=(-1,1)).fit(Xtr) Xtr = scaling.transform(Xtr) Xt = scaling.transform(Xt) Xv = scaling.transform(Xv) kernel = 'rbf' from sklearn import svm import cPickle clf = svm.SVC(kernel=kernel,class_weight='balanced',probability=True) clf.fit(Xtr, ytr) pred_y = clf.predict(Xt) prob_y = clf.predict_proba(Xt) prob_y.dump('../results/prob_scores/FEATURE_SVM/'+'prob_y.dat') yt.dump('../results/prob_scores/FEATURE_SVM/'+'true_y.dat') with open('../results/weights/FEATURE_SVM_ATP.pkl', 'wb') as fid: cPickle.dump(clf, fid) if __name__ == '__main__': test_fine_S_CNN_dA()