#compute homogeneous polynomial kernels with degrees 0,1,2,...,10. print('computing Homogeneous Polynomial Kernels...', end='') from MKLpy.metrics import pairwise KLtr = [ pairwise.homogeneous_polynomial_kernel(Xtr, degree=d) for d in range(11) ] KLte = [ pairwise.homogeneous_polynomial_kernel(Xte, Xtr, degree=d) for d in range(11) ] print('done') #MKL algorithms from MKLpy.algorithms import AverageMKL, EasyMKL, KOMD #KOMD is not a MKL algorithm but a simple kernel machine like the SVM print('training AverageMKL...', end='') clf = AverageMKL().fit(KLtr, Ytr) #a wrapper for averaging kernels print('done') K_average = clf.solution.ker_matrix #the combined kernel matrix print('training EasyMKL...', end='') clf = EasyMKL(lam=0.1).fit(KLtr, Ytr) #combining kernels with the EasyMKL algorithm #lam is a hyper-parameter in [0,1] print('done') print('the combination weights are:') print(clf.solution.weights) #evaluate the solution from sklearn.metrics import accuracy_score, roc_auc_score y_pred = clf.predict(KLte) #predictions y_score = clf.decision_function(KLte) #rank
degree=d) for d in range(4) ] KLte = [ pairwise.homogeneous_polynomial_kernel(Xte, Xtr, degree=d) for d in range(4) ] print('done') # ''' Compute RBF Kernels''' # gamma_range = np.logspace(-9, 3, 13) # ker_list = [rbf_kernel(Xtr, gamma=g) for g in gamma_range] # and train 3 classifiers ### clf = AverageMKL().fit( KLtr, ytr) # a wrapper for averaging kernels # print(clf.weights) # print the weights of the combination of base kernels print('training EasyMKL...for polynomials and RBF') clfEasy = EasyMKL(lam=0.1).fit( KLtr, ytr ) # combining kernels with the EasyMKL algorithm # clfRBF = EasyMKL(lam=0.1).fit(ker_list, ytr) print('------') print('finished training') except: count_i += 1 print(count_i) print(i, "hin failed here!") continue
rescale_01(torch.Tensor( pkl_file[model_date][0].values))) # fitting model # put the labels in a tensor format Ytr = torch.Tensor(pkl_file[model_date][1].values) print('first bit done') # force garbage collect nalsvm.gc.collect() # kernels KLrbf = generators.RBF_generator(Xtr, gamma=[.001, .01, .1]) # dont need the next bit print('done with kernel') print(forward_dates) # base learner- use c =1 or 10 # the c and lambda values need to be picked up by the cross-val results ! base_learner = SVC(C=10) clf = EasyMKL(lam=0.2, multiclass_strategy='ova', learner=base_learner).fit(KLrbf, Ytr) # try ovo as # well mkl_avg = AverageMKL().fit(KLrbf, Ytr) print('done') print('the combination weights are:') # this bit may be redundant here and we can put it somewhere else for sol in clf.solution: print( '(%d vs all): ' % sol, clf.solution[sol].weights ) #dont need this loop- can make it redundant in another file except: continue
if df_final.shape[0] < 10: print(' the ratio of classes is too low. try another label permutation') continue else: try: X_train = MinMaxScaler().fit_transform(df_final) nalsvm.logmemoryusage("After feature creation") if X_train.shape[0] == y_labels_train.shape[0]: nalsvm.logmemoryusage("Before starting training") print('Shapes Match- starting training ') # polynomial Kernels ## try: KLtr = [pairwise.homogeneous_polynomial_kernel(X_train, degree=d) for d in range(4)] # KLte = [pairwise.homogeneous_polynomial_kernel(Xte, Xtr, degree=d) for d in range(4)] print('done') clf = AverageMKL().fit(KLtr, y_labels_train) # a wrapper for averaging kernels # print(clf.weights) # print the weights of the combination of base kernels print('training EasyMKL...for polynomials and RBF') clfEasy = EasyMKL(lam=0.1).fit(KLtr, y_labels_train) # combining kernels with the EasyMKL algorithm print('------') print('finished training') # somewhere here you need to do out of sample testing and then store all that symbolForwardDates = data_cls.forwardDates(joint_keys, joint_keys[joint_key_idx]) oos_svc_predictions = defaultdict(dict) # alias to store the data : symbol, joint Date, Label Used results_predict_alias = "_".join((symbol, joint_keys[joint_key_idx, nalsvm.labels_pickle_files[alternate_label_idx])) for forward_date_idx, forward_date in enumerate(symbolForwardDates): features_oos, labels_oos = nalsvm.ticker_features_labels(nalsvm.jointLocationsDictionary[symbolForwardDates[forward_date_idx]]) if nalsvm.hmm_features_df(features_oos).isnull().values.all(): print('Problem')
Xtr,Xte,Ytr,Yte = train_test_split(X,Y, test_size=.25, random_state=42) print ('done') #compute homogeneous polynomial kernels with degrees 0,1,2,...,10. print ('computing Homogeneous Polynomial Kernels...', end='') from MKLpy.metrics import pairwise KLtr = [pairwise.homogeneous_polynomial_kernel(Xtr, degree=d) for d in range(11)] KLte = [pairwise.homogeneous_polynomial_kernel(Xte,Xtr, degree=d) for d in range(11)] print ('done') #MKL algorithms from MKLpy.algorithms import AverageMKL, EasyMKL, KOMD #KOMD is not a MKL algorithm but a simple kernel machine like the SVM print ('training AverageMKL...', end='') clf = AverageMKL().fit(KLtr,Ytr) #a wrapper for averaging kernels print ('done') print(clf.weights) #print the weights of the combination of base kernels K_average = clf.ker_matrix #the combined kernel matrix print ('training EasyMKL...', end='') clf = EasyMKL(lam=0.1).fit(KLtr,Ytr) #combining kernels with the EasyMKL algorithm #lam is a hyper-parameter in [0,1] print ('done') print (clf.weights) #evaluate the solution from sklearn.metrics import accuracy_score, roc_auc_score y_pred = clf.predict(KLte) #predictions y_score = clf.decision_function(KLte) #rank