for fid, predict_idx in enumerate(predict_idx_list): print "Current fold is %d" % fid train_idx = list(set(range(nsamples)) - set(predict_idx)) # extract predictions using RF on static print " Extracting predictions on static data with RF..." rf = RandomForestClassifier(n_estimators=nestimators) rf.fit(static_all[train_idx], labels_all[train_idx]) predictions_all_rf[predict_idx] = rf.predict_log_proba(static_all[predict_idx]) predictions_all_rf[predictions_all_rf == -inf] = np.min(predictions_all_rf[predictions_all_rf != -inf]) # extract predictions using LSTM on dynamic print " Extracting predictions on dynamic data with LSTM..." lstmcl = LSTMClassifier(lstmsize, lstmdropout, lstmoptim, lstmnepochs, lstmbatchsize) model_pos, model_neg = lstmcl.train(dynamic_all[train_idx], labels_all[train_idx]) mse_pos, mse_neg = lstmcl.predict_mse(model_pos, model_neg, dynamic_all[predict_idx]) predictions_all_lstm[predict_idx] = np.vstack((mse_pos, mse_neg)).T ratios_all_lstm[predict_idx] = lstmcl.pos_neg_ratios(model_pos, model_neg, dynamic_all[predict_idx]) # # Prepare combined datasets for the future experiments # # datasets for ensemble learning predictions_combined_rf_lstm = np.concatenate((predictions_all_rf, predictions_all_lstm), axis=1) # datasets for hybrid learning enriched_by_lstm = np.concatenate((static_all, np.matrix(ratios_all_lstm).T), axis=1) # dataset to check how generative models perform if provided with static features along with dynamic
# extract predictions using RF on static rf = RandomForestClassifier(n_estimators=nestimators) rf.fit(trainA_static, trainA_labels) predictions_trainB_rf = rf.predict_log_proba(trainB_static) predictions_trainB_rf[predictions_trainB_rf == -inf] = np.min( predictions_trainB_rf[predictions_trainB_rf != -inf]) predictions_test_rf = rf.predict_log_proba(test_static) predictions_test_rf[predictions_test_rf == -inf] = np.min( predictions_test_rf[predictions_test_rf != -inf]) # extract predictions using LSTM on dynamic lstmcl = LSTMClassifier(lstmsize, lstmdropout, lstmoptim, lstmnepochs, lstmbatchsize) model_pos, model_neg = lstmcl.train(trainA_dynamic, trainA_labels) mse_pos, mse_neg = lstmcl.predict_mse(model_pos, model_neg, trainB_dynamic) predictions_trainB_lstm = np.vstack((mse_pos, mse_neg)).T ratios_trainB_lstm = lstmcl.pos_neg_ratios(model_pos, model_neg, trainB_dynamic) mse_pos, mse_neg = lstmcl.predict_mse(model_pos, model_neg, test_dynamic) predictions_test_lstm = np.vstack((mse_pos, mse_neg)).T ratios_test_lstm = lstmcl.pos_neg_ratios(model_pos, model_neg, test_dynamic) # # Prepare combined datasets for the future experiments # # datasets for ensemble learning trainB_predictions_combined_rf_lstm = np.concatenate( (predictions_trainB_rf, ratios_trainB_lstm.reshape((ratios_trainB_lstm.shape[0], 1))),
train_idx = list(set(range(nsamples)) - set(predict_idx)) # extract predictions using HMM on dynamic hmmcl = HMMClassifier() model_pos, model_neg = hmmcl.train(nhmmstates, nhmmiter, hmmcovtype, dynamic_all[train_idx], labels_all[train_idx]) ratios_all_hmm[predict_idx] = hmmcl.pos_neg_ratios( model_pos, model_neg, dynamic_all[predict_idx]) # extract predictions using LSTM on dynamic lstmcl = LSTMClassifier(lstmsize, lstmdropout, lstmoptim, lstmnepochs, lstmbatchsize) model_pos, model_neg = lstmcl.train(dynamic_all[train_idx], labels_all[train_idx]) mse_pos, mse_neg = lstmcl.predict_mse(model_pos, model_neg, dynamic_all[predict_idx]) ratios_all_lstm[predict_idx] = lstmcl.pos_neg_ratios( model_pos, model_neg, dynamic_all[predict_idx]) # datasets for hybrid learning enriched_by_hmm = np.concatenate((static_all, np.matrix(ratios_all_hmm).T), axis=1) enriched_by_lstm = np.concatenate( (static_all, np.matrix(ratios_all_lstm).T), axis=1) # k-fold CV for performance estimation val_idx_list = np.array_split(range(nsamples), nfolds) scores = { 1: [], 2: [], 3: [],
predictions_trainB_rf[predictions_trainB_rf == -inf] = np.min(predictions_trainB_rf[predictions_trainB_rf != -inf]) predictions_test_rf = rf.predict_log_proba(test_static) predictions_test_rf[predictions_test_rf == -inf] = np.min(predictions_test_rf[predictions_test_rf != -inf]) # extract predictions using HMM on dynamic hmmcl = HMMClassifier() model_pos, model_neg = hmmcl.train(nhmmstates, nhmmiter, hmmcovtype, trainA_dynamic, trainA_labels) predictions_trainB_hmm = hmmcl.predict_log_proba(model_pos, model_neg, trainB_dynamic) ratios_trainB_hmm = hmmcl.pos_neg_ratios(model_pos, model_neg, trainB_dynamic) predictions_test_hmm = hmmcl.predict_log_proba(model_pos, model_neg, test_dynamic) ratios_test_hmm = hmmcl.pos_neg_ratios(model_pos, model_neg, test_dynamic) # extract predictions using LSTM on dynamic lstmcl = LSTMClassifier(lstmsize, lstmdropout, lstmoptim, lstmnepochs, lstmbatchsize) model_pos, model_neg = lstmcl.train(trainA_dynamic, trainA_labels) mse_pos, mse_neg = lstmcl.predict_mse(model_pos, model_neg, trainB_dynamic) predictions_trainB_lstm = np.vstack((mse_pos, mse_neg)).T ratios_trainB_lstm = lstmcl.pos_neg_ratios(model_pos, model_neg, trainB_dynamic) mse_pos, mse_neg = lstmcl.predict_mse(model_pos, model_neg, test_dynamic) predictions_test_lstm = np.vstack((mse_pos, mse_neg)).T ratios_test_lstm = lstmcl.pos_neg_ratios(model_pos, model_neg, test_dynamic) # # Prepare combined datasets for the future experiments # # datasets for ensemble learning trainB_predictions_combined_rf_hmm = np.concatenate((predictions_trainB_rf, ratios_trainB_hmm.reshape((ratios_trainB_hmm.shape[0], 1))), axis=1) test_predictions_combined_rf_hmm = np.concatenate((predictions_test_rf, ratios_test_hmm.reshape((ratios_test_hmm.shape[0], 1))), axis=1) trainB_predictions_combined_rf_lstm = np.concatenate((predictions_trainB_rf, ratios_trainB_lstm.reshape((ratios_trainB_lstm.shape[0], 1))), axis=1)