def mrmr_selection(input_data_x, input_data_y): max_mi = np.argmax(mutual_info_classif(input_data_x, input_data_y)) input_data_y = np.reshape(input_data_y, (-1, 1)) select_index = [] select_index.append(max_mi) selected_features = input_data_x[:, max_mi].reshape(-1, 1) diff = [] for x in range(0, num_of_features): #print(str(x)+' / '+str(num_of_features)) for i in range(0, input_data_x.shape[1]): key = 0 for j in range(0, len(select_index)): if i == select_index[j]: key = 1 if key == 0: feature = input_data_x[:, i] MI = 0 count = 0 for k in range(0, selected_features.shape[1]): MI = MI + mutinf( 100, feature.reshape(-1, 1), np.reshape(selected_features[:, k], (-1, 1))) count = count + 1 diff.append( mutinf(100, feature.reshape(-1, 1), input_data_y) - float(MI) / count) else: diff.append(-99.9) max_new = np.argmax(diff) select_index.append(max_new) del diff[:] selected_features = np.append(selected_features, input_data_x[:, max_new].reshape(-1, 1), axis=1) return select_index
def bias_value(input_data_x, input_data_y): bias_num = [] input_data_y = np.reshape(input_data_y, (-1, 1)) input_data_x = np.array(input_data_x) input_data_x_old = input_data_x high_score = mutinf(100, input_data_x, input_data_y) input_data_x_old = input_data_x for i in range(0, input_data_x.shape[1]): input_data_x = np.delete(input_data_x, i, 1) diff = high_score - mutinf(100, input_data_x, input_data_y) bias_num.append(diff) input_data_x = input_data_x_old bias_num = np.array(bias_num) bias_num[bias_num < 0] = 0 return bias_num
def JMI_selection(input_data_x, input_data_y): max_mi = np.argmax(mutual_info_classif(input_data_x, input_data_y)) input_data_y = np.reshape(input_data_y, (-1, 1)) select_index = [] select_index.append(max_mi) selected_features = input_data_x[:, max_mi].reshape(-1, 1) diff = [] for x in range(0, num_of_features): for i in range(0, input_data_x.shape[1]): key = 0 for j in range(0, len(select_index)): if i == select_index[j]: key = 1 if key == 0: feature = input_data_x[:, i].reshape(-1, 1) MI = 0 for k in range(0, selected_features.shape[1]): MI = MI + mutinf( 100, feature, np.reshape(selected_features[:, k], (-1, 1))) diff.append(MI) else: diff.append(-99.9) max_new = np.argmax(diff) select_index.append(max_new) del diff[:] selected_features = np.append(selected_features, input_data_x[:, max_new].reshape(-1, 1), axis=1) return select_index
def GSA_selection_UR(input_data_x, input_data_y): bias = bias_value(input_data_x, input_data_y) max_mi = np.argmax(mutual_info_classif(input_data_x, input_data_y)) input_data_y = np.reshape(input_data_y, (-1, 1)) data_new = input_data_x[:, max_mi] all_data = np.reshape(data_new, (-1, 1)) select_index = [] select_index.append(max_mi) for i in range(0, num_of_features): score = [] all_data_old = all_data for j in range(0, input_data_x.shape[1]): key = 1 for k in range(0, len(select_index)): if j == select_index[k]: key = 0 if key == 1: all_data = np.append(all_data, np.reshape(input_data_x[:, j], (-1, 1)), axis=1) score.append( mutinf(100, np.reshape(all_data, (-1, i + 2)), input_data_y) * (1 - beta) + beta * bias[j]) else: score.append(-99.9) all_data = all_data_old new_index = np.argmax(score) select_index.append(new_index) all_data = np.append(all_data, np.reshape(input_data_x[:, new_index], (-1, 1)), axis=1) return select_index
def JMIM_selection_UR(input_data_x, input_data_y): bias = bias_value(input_data_x, input_data_y) max_mi = np.argmax(mutual_info_classif(input_data_x, input_data_y)) input_data_y = np.reshape(input_data_y, (-1, 1)) select_index = [] select_index.append(max_mi) selected_features = input_data_x[:, max_mi].reshape(-1, 1) for x in range(0, num_of_features): all_min = [] for i in range(0, input_data_x.shape[1]): key = 0 for j in range(0, len(select_index)): if i == select_index[j]: key = 1 if key == 0: feature = input_data_x[:, i] MI = [] for k in range(0, selected_features.shape[1]): MI.append( mutinf(100, np.reshape(feature, (-1, 1)), np.reshape(selected_features[:, k], (-1, 1)))) all_min.append((1 - beta) * np.min(MI) + beta * bias[i]) else: all_min.append(-99.9) max_new = np.argmax(all_min) select_index.append(max_new) selected_features = np.append(selected_features, input_data_x[:, max_new].reshape(-1, 1), axis=1) return select_index
def MI_selection(all_data): all_MI = [] original_number = np.unique(all_data.values[:,0]) for i in range(0,len(original_number)): MI = 0 sample_num = original_number[i] for j in range(0,len(original_number)): sample_num_2 = original_number[j] if sample_num_2 != sample_num: P1 = all_data[all_data['Subject No']==sample_num].T P2 = all_data[all_data['Subject No']==sample_num_2].T MI = MI + mutinf(3,P1,P2) all_MI.append(MI) order = np.argsort(all_MI)[::-1] new_order = original_number[order] return new_order
def normalize_trace(tr): ts, rs = zip(*tr) ts = np.array(ts) - ts[0] interp = interp1d(x=ts, y=rs, kind='linear') return interp(norm_trace_times) print 'Normalizing reward traces...' norm_reward_traces = [normalize_trace(tr) for tr in reward_traces] norm_reward_traces = np.array(norm_reward_traces) # TODO Consider context as well # 2. Compute MI between pre/actions, reward values pre_act_mi = [ mde.mutinf(n_bins=11, x=pre_actions_contexts, y=norm_reward_traces[:, i]) for i in range(num_trace_times) ] post_act_mi = [ mde.mutinf(n_bins=11, x=actions_contexts, y=norm_reward_traces[:, i]) for i in range(num_trace_times) ] plt.ion() plt.figure() plt.title('Instantaneous reward MI vs. time') plt.plot(norm_trace_times, pre_act_mi, 'r.-', label='prev_act') plt.plot(norm_trace_times, post_act_mi, 'b.-', label='curr_act') plt.xlabel('Time after action, t (s)') plt.ylabel('Mutual Information') plt.legend(loc='best')