def nanargmin( values: np.ndarray, axis: Optional[int] = None, skipna: bool = True, mask: Optional[np.ndarray] = None, ) -> int: """ Parameters ---------- values : ndarray axis: int, optional skipna : bool, default True mask : ndarray[bool], optional nan-mask if known Returns ------- result : int The index of min value in specified axis or -1 in the NA case Examples -------- >>> import pandas.core.nanops as nanops >>> s = pd.Series([1, 2, 3, np.nan, 4]) >>> nanops.nanargmin(s) 0 """ values, mask, dtype, _, _ = _get_values(values, True, fill_value_typ="+inf", mask=mask) result = values.argmin(axis) result = _maybe_arg_null_out(result, axis, mask, skipna) return result
def arg_statistics(data: np.ndarray): """ Perform similar analysis to pipeline.statistics, but return the indexes instead of the values. :param data: np.ndarray the data used to create statistics/features from :return: np.ndarray new features as columns """ NUM_STATS = 7 result = np.zeros(shape=(len(data), NUM_STATS)) result[:, 0] = data.argmax(axis=1) result[:, 1] = data.argmin(axis=1) # Unfortunately vectorized arg functions end here... for idx, row in enumerate(data): sys.stdout.write( f"\r[-] Arg-Stat: {idx} of {len(data)} ({idx / len(data) * 100: .2f}%)" ) sys.stdout.flush() result[idx, 2] = np.argsort(row)[len(row) // 2] # nlogn each row, n * nlogn total result[idx, 3] = np.argwhere( row == (np.percentile(row, 0, interpolation='nearest')))[0] result[idx, 4] = np.argwhere( row == (np.percentile(row, 25, interpolation='nearest')))[0] result[idx, 5] = np.argwhere( row == (np.percentile(row, 50, interpolation='nearest')))[0] result[idx, 6] = np.argwhere( row == (np.percentile(row, 75, interpolation='nearest')))[0] sys.stdout.write(f"\r[-] Arg-Stat: Completed {len(data)} (100%)\n") sys.stdout.flush() return result
def _choice_best_order(cost: np.ndarray, margin_improvement : float = 20., verbose: bool = False) -> int: """ Choice of the best order (polynomial, sum of sinusoids) with a margin of improvement. The best cost value does not necessarily mean the best predictive fit because high-degree polynomials tend to overfit, and sum of sinusoids as well. To mitigate this issue, we should choose the lesser order from which improvement becomes negligible. :param cost: cost function residuals to the polynomial :param margin_improvement: improvement margin (percentage) below which the lesser degree polynomial is kept :param verbose: if text should be printed :return: degree: degree for the best-fit polynomial """ # get percentage of spread from the minimal cost ind_min = cost.argmin() min_cost = cost[ind_min] perc_cost_improv = (cost - min_cost) / min_cost # costs below threshold and lesser degrees below_margin = np.logical_and(perc_cost_improv < margin_improvement / 100., np.arange(len(cost))<=ind_min) costs_below_thresh = cost[below_margin] # minimal costs subindex = costs_below_thresh.argmin() # corresponding index (degree) ind = np.arange(len(cost))[below_margin][subindex] if verbose: print('Order '+str(ind_min+1)+ ' has the minimum cost value of '+str(min_cost)) print('Order '+str(ind+1)+ ' is selected within a '+str(margin_improvement)+' % margin of' ' the minimum cost, with a cost value of '+str(min_cost)) return ind
def embed(self, window: np.ndarray, bit: int) -> np.ndarray: imin, imax = window.argmin(), window.argmax() for i in range(len(window)): if i != imin and i != imax: window[i] = window[imax] if bit else window[imin] return window
def _r_mat_to_y(r_mat: np.ndarray): if sp.issparse(r_mat): r_mat = r_mat.toarray() # Restore y from r_mat y = r_mat.argmin(axis=1) y[r_mat.sum(axis=1) < r_mat.shape[1] - 1] = -1 return y
def extract(self, window: np.ndarray) -> int: cnt = 0 imin, imax = window.argmin(), window.argmax() for i, x in enumerate(window): if i != imin and i != imax: mid = (window[imin] + window[imax]) / 2 cnt += bit2sign(x >= mid) return int(cnt >= 0)
def first_choice_matching(cost_matrix: np.ndarray) -> List[Tuple[int, int]]: """ Returns match indices where each row gets matched to best column. The means that multiple rows might be matched to the same column. """ row_count = len(cost_matrix) best_matches_vector = cost_matrix.argmin(axis=1) match_indices = list(zip(range(row_count), best_matches_vector)) return match_indices
def from_cost_matrix( cls, cost_matrix: np.ndarray, instances: List[InstanceType], tracks: List[Track], matching_function: Callable, ): matches = [] match_instance_inds = [] if instances and tracks: match_inds = matching_function(cost_matrix) # Determine the first-choice match for each instance since we want # to know whether or not all the matches in the frame were # uncontested. best_matches_vector = cost_matrix.argmin(axis=1) # Assign each matched instance. for i, j in match_inds: match_instance_inds.append(i) match_instance = instances[i] match_track = tracks[j] match_similarity = -cost_matrix[i, j] is_first_choice = best_matches_vector[i] == j # return matches as tuples matches.append( Match( instance=match_instance, track=match_track, score=match_similarity, is_first_choice=is_first_choice, ) ) # Make list of untracked instances which we didn't match to anything unmatched_instances = [ untracked_instance for i, untracked_instance in enumerate(instances) if i not in match_instance_inds ] return cls( cost_matrix=cost_matrix, matches=matches, unmatched_instances=unmatched_instances, )
def get_next_cheapest(fees: np.ndarray, current_index: Union[int, None] = None): if current_index is not None: current_thresholds = fees[:, current_index].reshape( (fees.shape[0], -1)) masked_arr: np.ndarray = cast( np.ndarray, np.hstack( (fees[:, :current_index], fees[:, current_index + 1:])).copy()) masked_arr[masked_arr < current_thresholds] = fees.max().max() + 1 argmin = masked_arr.argmin(axis=1) argmin[argmin >= current_index] += 1 return argmin else: return fees.argmin(axis=1)
def reduce_chunk(chunk: np.ndarray, start: int): # Block rejected assignments _set_rejected_inf(chunk, r_mat, slice(start, start + chunk.shape[0])) assert (np.isfinite(chunk).any( axis=1).all()), "Some samples do not have a feasible label" # Select best label labels = chunk.argmin(axis=1) sample_distances = np.take_along_axis(chunk, np.expand_dims(labels, axis=1), axis=1).reshape(-1) return labels, sample_distances
def nanargmin( values: np.ndarray, *, axis: Optional[int] = None, skipna: bool = True, mask: Optional[np.ndarray] = None, ) -> Union[int, np.ndarray]: """ Parameters ---------- values : ndarray axis: int, optional skipna : bool, default True mask : ndarray[bool], optional nan-mask if known Returns ------- result : int or ndarray[int] The index/indices of min value in specified axis or -1 in the NA case Examples -------- >>> import pandas.core.nanops as nanops >>> arr = np.array([1, 2, 3, np.nan, 4]) >>> nanops.nanargmin(arr) 0 >>> arr = np.array(range(12), dtype=np.float64).reshape(4, 3) >>> arr[2:, 0] = np.nan >>> arr array([[ 0., 1., 2.], [ 3., 4., 5.], [nan, 7., 8.], [nan, 10., 11.]]) >>> nanops.nanargmin(arr, axis=1) array([0, 0, 1, 1], dtype=int64) """ values, mask, _, _, _ = _get_values(values, True, fill_value_typ="+inf", mask=mask) result = values.argmin(axis) result = _maybe_arg_null_out(result, axis, mask, skipna) return result
def hunt( boids_dxy: np.ndarray, boids_dist: np.ndarray, boids_towards: np.ndarray, max_dist: np.ndarray, obstacles_dist: np.ndarray, obstacles_towards: np.ndarray, checkpoint_towards: np.ndarray, wall_dist: np.ndarray, wall_towards: np.ndarray, objects: List[BaseBoid], kill_dist: np.ndarray, ) -> np.ndarray: which_calc = (boids_dist <= max_dist.astype(int)).astype(float) which_calc2 = np.zeros(which_calc.shape) which_calc2[np.arange(len(boids_dist)), boids_dist.argmin(axis=1)] = 1 which_calc = which_calc * which_calc2 result = np.multiply(which_calc, -boids_towards).sum(axis=1) return result / np.absolute(result)
def select_parents_pool(fitness: np.ndarray, population: np.ndarray) -> Tuple[Optional[np.ndarray], Optional[int]]: """ Select parents from <population> based on <fitness>. (The more fitness - the more probability to be selected) """ minimum_error_idx = fitness.argmin() min_error = fitness[minimum_error_idx] if min_error == 0: return None, minimum_error_idx parents = np.zeros((COLOR_PARENTS, *population.shape[1:])) error_sum = np.sum(fitness) to_max = np.true_divide(error_sum, fitness) to_max_cumsum = np.cumsum(to_max) to_max_cumsum_norm = np.true_divide(to_max_cumsum, to_max_cumsum[-1]) for i in range(COLOR_PARENTS): p = np.random.rand(1) for j in range(len(to_max_cumsum_norm)): if p <= to_max_cumsum_norm[j]: parents[i] = population[j] break return parents, None
def bipartite_matching_greedy(C: np.ndarray): """ Computes the bipartite matching between the rows and columns, given the cost matrix, C. """ C = C.copy() # to avoid affecting the original matrix prev_ids = [] cur_ids = [] row_ids = np.arange(C.shape[0]) col_ids = np.arange(C.shape[1]) while C.size > 0: # Find the lowest cost element i, j = np.unravel_index(C.argmin(), C.shape) # Add to results and remove from the cost matrix row_id = row_ids[i] col_id = col_ids[j] prev_ids.append(row_id) cur_ids.append(col_id) C = np.delete(C, i, 0) C = np.delete(C, j, 1) row_ids = np.delete(row_ids, i, 0) col_ids = np.delete(col_ids, j, 0) return prev_ids, cur_ids
def match(self, distance_matrix: np.ndarray) -> [list, list]: """ Matches detections with tracked_objects from a distance matrix Args: distance_matrix: Distance matrix between tracked_objects and detections Returns: det_ids: List of matched detection indexes track_ids: List of matched track indexes """ distance_matrix = distance_matrix.copy() if distance_matrix.size > 0: det_ids = [] track_ids = [] current_min = distance_matrix.min() # This part should be vectorized while current_min < self._distance_threshold: # Get the index of minimum values flattened_arg_min = distance_matrix.argmin() # Find index of detections det_idx = flattened_arg_min // distance_matrix.shape[1] # Find index of tracks track_idx = flattened_arg_min % distance_matrix.shape[1] det_ids.append(det_idx) track_ids.append(track_idx) distance_matrix[det_idx, :] = self._distance_threshold + 1 distance_matrix[:, track_idx] = self._distance_threshold + 1 current_min = distance_matrix.min() return det_ids, track_ids else: return [], []
def find_extremum_index(segment: np.ndarray, selector: bool) -> int: if selector: return segment.argmax() else: return segment.argmin()
def get_alignment_matrix(dist: np.ndarray) -> Tuple[np.ndarray, np.ndarray]: m, n = dist.shape forward = np.eye(n)[dist.argmin(axis=1)] # m x n backward = np.eye(m)[dist.argmin(axis=0)] # n x m return forward, backward.transpose()
def min_index(array: np.ndarray): k = array.argmin() w = array.shape[1] return k % w, k // w