Python numpy.ndenumerate() Examples
The following are 30
code examples of numpy.ndenumerate().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
numpy
, or try the search function
.
Example #1
Source File: plotting.py From kvae with MIT License | 7 votes |
def hinton(matrix, max_weight=None, ax=None): """Draw Hinton diagram for visualizing a weight matrix.""" ax = ax if ax is not None else plt.gca() if not max_weight: max_weight = 2 ** np.ceil(np.log(np.abs(matrix).max()) / np.log(2)) ax.patch.set_facecolor('gray') ax.set_aspect('equal', 'box') ax.xaxis.set_major_locator(plt.NullLocator()) ax.yaxis.set_major_locator(plt.NullLocator()) for (x, y), w in np.ndenumerate(matrix): color = 'white' if w > 0 else 'black' size = np.sqrt(np.abs(w) / max_weight) rect = plt.Rectangle([x - size / 2, y - size / 2], size, size, facecolor=color, edgecolor=color) ax.add_patch(rect) ax.autoscale_view() ax.invert_yaxis()
Example #2
Source File: core.py From PyBloqs with GNU Lesser General Public License v2.1 | 6 votes |
def _flatten_data(data, chart_cfg, switch_zy=False): plot_axes_def = [(0, XAxis), (1, YAxis)] # Inject categories into the axis definitions of the plot if isinstance(data, NDFrame): for i, plot_axis in plot_axes_def[:data.ndim]: categories = data.axes[i] # Skip numeric indices if not categories.is_numeric(): chart_cfg = chart_cfg.inherit_many(plot_axis(categories=list(categories))) data = [list(index) + [value] for index, value in list(np.ndenumerate(data))] if switch_zy: for i in range(len(data)): tmp = data[i][-1] data[i][-1] = data[i][-2] data[i][-2] = tmp return data, chart_cfg
Example #3
Source File: callbacks.py From nevergrad with MIT License | 6 votes |
def load_flattened(self, max_list_elements: int = 24) -> tp.List[tp.Dict[str, tp.Any]]: """Loads data from the log file, and splits lists (arrays) into multiple arguments Parameters ---------- max_list_elements: int Maximum number of elements displayed from the array, each element is given a unique id of type list_name#i0_i1_... """ data = self.load() flat_data: tp.List[tp.Dict[str, tp.Any]] = [] for element in data: list_keys = {key for key, val in element.items() if isinstance(val, list)} flat_data.append({key: val for key, val in element.items() if key not in list_keys}) for key in list_keys: for k, (indices, value) in enumerate(np.ndenumerate(element[key])): if k >= max_list_elements: break flat_data[-1][key + "#" + "_".join(str(i) for i in indices)] = value return flat_data
Example #4
Source File: CellularSprites.py From seagull with MIT License | 6 votes |
def add_outline(mat: np.ndarray) -> np.ndarray: """Pad the matrix""" m = np.ones(mat.shape) for idx, orig_val in np.ndenumerate(mat): x, y = idx neighbors = [(x, y + 1), (x + 1, y), (x, y - 1), (x - 1, y)] if orig_val == 0: m[idx] = 0 # Set the coordinate in the new matrix as 0 for n_coord in neighbors: try: m[n_coord] = 0.5 if mat[n_coord] == 1 else 0 except IndexError: pass m = np.pad(m, mode="constant", pad_width=1, constant_values=1) # Let's do a switcheroo, I know this isn't elegant but please feel free to # do a PR to make this more efficient! m[m == 1] = np.inf m[m == 0.5] = 1 m[m == np.inf] = 0.5 return m
Example #5
Source File: core.py From scikit-downscale with Apache License 2.0 | 6 votes |
def xenumerate(arr): """ Multidimensional index iterator for xarray objects Return an iterator yielding pairs of array indexers (dicts) and values. Parameters ---------- arr : xarray.DataArray Input array. See Also -------- numpy.ndenumerate """ for index, _ in np.ndenumerate(arr): xindex = dict(zip(arr.dims, index)) yield xindex, arr.isel(**xindex)
Example #6
Source File: test_index_tricks.py From Computable with MIT License | 6 votes |
def test_ndindex(): x = list(np.ndindex(1, 2, 3)) expected = [ix for ix, e in np.ndenumerate(np.zeros((1, 2, 3)))] assert_array_equal(x, expected) x = list(np.ndindex((1, 2, 3))) assert_array_equal(x, expected) # Test use of scalars and tuples x = list(np.ndindex((3,))) assert_array_equal(x, list(np.ndindex(3))) # Make sure size argument is optional x = list(np.ndindex()) assert_equal(x, [()]) x = list(np.ndindex(())) assert_equal(x, [()]) # Make sure 0-sized ndindex works correctly x = list(np.ndindex(*[0])) assert_equal(x, [])
Example #7
Source File: grid_world.py From reinforcement-learning-an-introduction with MIT License | 6 votes |
def draw_image(image): fig, ax = plt.subplots() ax.set_axis_off() tb = Table(ax, bbox=[0, 0, 1, 1]) nrows, ncols = image.shape width, height = 1.0 / ncols, 1.0 / nrows # Add cells for (i, j), val in np.ndenumerate(image): tb.add_cell(i, j, width, height, text=val, loc='center', facecolor='white') # Row and column labels... for i in range(len(image)): tb.add_cell(i, -1, width, height, text=i+1, loc='right', edgecolor='none', facecolor='none') tb.add_cell(-1, i, width, height/2, text=i+1, loc='center', edgecolor='none', facecolor='none') ax.add_table(tb)
Example #8
Source File: grid_world.py From reinforcement-learning-an-introduction with MIT License | 6 votes |
def draw_image(image): fig, ax = plt.subplots() ax.set_axis_off() tb = Table(ax, bbox=[0, 0, 1, 1]) nrows, ncols = image.shape width, height = 1.0 / ncols, 1.0 / nrows # Add cells for (i, j), val in np.ndenumerate(image): tb.add_cell(i, j, width, height, text=val, loc='center', facecolor='white') # Row and column labels... for i in range(len(image)): tb.add_cell(i, -1, width, height, text=i+1, loc='right', edgecolor='none', facecolor='none') tb.add_cell(-1, i, width, height/2, text=i+1, loc='center', edgecolor='none', facecolor='none') ax.add_table(tb)
Example #9
Source File: matching_histogram.py From MatchZoo-py with Apache License 2.0 | 6 votes |
def transform(self, input_: list) -> list: """Transform the input text.""" text_left, text_right = input_ matching_hist = np.ones((len(text_left), self._hist_bin_size), dtype=np.float32) embed_left = self._embedding_matrix[text_left] embed_right = self._embedding_matrix[text_right] matching_matrix = embed_left.dot(np.transpose(embed_right)) for (i, j), value in np.ndenumerate(matching_matrix): bin_index = int((value + 1.) / 2. * (self._hist_bin_size - 1.)) matching_hist[i][bin_index] += 1.0 if self._mode == 'NH': matching_sum = matching_hist.sum(axis=1) matching_hist = matching_hist / matching_sum[:, np.newaxis] elif self._mode == 'LCH': matching_hist = np.log(matching_hist) return matching_hist.tolist()
Example #10
Source File: dataset.py From ektelo with Apache License 2.0 | 6 votes |
def reduce_data(self,p_grid,data): """reduce data to the domain indicated by p_grid""" assert data.shape == p_grid.shape, 'Shape of x and shape of partition vector must match.' #get out_shape if p_grid.ndim == 1: out_shape =(len(numpy.unique(p_grid)), ) else: out = [] for i in range(p_grid.ndim): ind_slice = [0] * p_grid.ndim ind_slice[i] = slice(0, p_grid.shape[i]) out.append(len(numpy.unique(p_grid[ind_slice]))) # This is doesn't work for dimension >2, the compressed array is not a strip of the domain, # but the first element along the axis. It could be an nd array itself. #out = [len(numpy.unique(numpy.compress([True], p_grid, axis=i))) for i in range(p_grid.ndim)] #out.reverse() # rows/cols need to be reversed here out_shape = tuple(out) #reduce unique, indices, inverse, counts = numpy.unique(p_grid, return_index=True, return_inverse=True, return_counts=True) res = numpy.zeros_like(unique, dtype=float) for index, c in numpy.ndenumerate(data.ravel()): # needs to be flattened for parallel indexing with output of unique res[ inverse[index] ] += c return numpy.array(res).reshape(out_shape)
Example #11
Source File: rnnlmWithHierarchicalSoftmax.py From cs224d with MIT License | 6 votes |
def grad_check_hierarchicalU(self,node,grad_computed,grad_approx,eps,x,y): if node.isLeaf == True: return if node.grad == None: return theta = node.hActs for ij,v in np.ndenumerate(node.hActs): tij = theta[ij] theta[ij] = tij + eps Jplus = self.compute_loss(x,y) theta[ij] = tij - eps Jminus = self.compute_loss(x, y) theta[ij] = tij # reset approx = (Jplus - Jminus)/(2*eps) grad_computed.append(node.grad[ij]) grad_approx.append(approx) self.grad_check_hierarchicalU(node.left,grad_computed,grad_approx,eps,x,y) self.grad_check_hierarchicalU(node.right,grad_computed,grad_approx,eps,x,y)
Example #12
Source File: hinton_demo.py From python3_ios with BSD 3-Clause "New" or "Revised" License | 6 votes |
def hinton(matrix, max_weight=None, ax=None): """Draw Hinton diagram for visualizing a weight matrix.""" ax = ax if ax is not None else plt.gca() if not max_weight: max_weight = 2 ** np.ceil(np.log(np.abs(matrix).max()) / np.log(2)) ax.patch.set_facecolor('gray') ax.set_aspect('equal', 'box') ax.xaxis.set_major_locator(plt.NullLocator()) ax.yaxis.set_major_locator(plt.NullLocator()) for (x, y), w in np.ndenumerate(matrix): color = 'white' if w > 0 else 'black' size = np.sqrt(np.abs(w) / max_weight) rect = plt.Rectangle([x - size / 2, y - size / 2], size, size, facecolor=color, edgecolor=color) ax.add_patch(rect) ax.autoscale_view() ax.invert_yaxis()
Example #13
Source File: flir_image_extractor.py From read_thermal.py with Creative Commons Zero v1.0 Universal | 6 votes |
def export_thermal_to_csv(self, csv_filename): """ Convert thermal data in numpy to json :return: """ with open(csv_filename, 'w') as fh: writer = csv.writer(fh, delimiter=',') writer.writerow(['x', 'y', 'temp (c)']) pixel_values = [] for e in np.ndenumerate(self.thermal_image_np): x, y = e[0] c = e[1] pixel_values.append([x, y, c]) writer.writerows(pixel_values)
Example #14
Source File: safe_io.py From safepy with GNU General Public License v3.0 | 6 votes |
def calculate_edge_lengths(G, verbose=True): # Calculate the lengths of the edges if verbose: print('Calculating edge lengths...') x = np.matrix(G.nodes.data('x'))[:, 1] y = np.matrix(G.nodes.data('y'))[:, 1] node_coordinates = np.concatenate([x, y], axis=1) node_distances = squareform(pdist(node_coordinates, 'euclidean')) adjacency_matrix = np.array(nx.adjacency_matrix(G).todense()) adjacency_matrix = adjacency_matrix.astype('float') adjacency_matrix[adjacency_matrix == 0] = np.nan edge_lengths = np.multiply(node_distances, adjacency_matrix) edge_attr_dict = {index: v for index, v in np.ndenumerate(edge_lengths) if ~np.isnan(v)} nx.set_edge_attributes(G, edge_attr_dict, 'length') return G
Example #15
Source File: hinton.py From color_recognizer with MIT License | 6 votes |
def hinton(matrix, max_weight=None, ax=None): """Draw Hinton diagram for visualizing a weight matrix.""" ax = ax if ax is not None else plt.gca() if not max_weight: max_weight = 2 ** np.ceil(np.log(np.abs(matrix).max()) / np.log(2)) ax.patch.set_facecolor('gray') ax.set_aspect('equal', 'box') ax.xaxis.set_major_locator(plt.NullLocator()) ax.yaxis.set_major_locator(plt.NullLocator()) for (x, y), w in np.ndenumerate(matrix): color = 'white' if w > 0 else 'black' size = np.sqrt(np.abs(w) / max_weight) rect = plt.Rectangle([x - size / 2, y - size / 2], size, size, facecolor=color, edgecolor=color) ax.add_patch(rect) ax.autoscale_view() ax.invert_yaxis() return ax
Example #16
Source File: matrix.py From NPRF with Apache License 2.0 | 6 votes |
def hist_from_matrix(text_maxlen, hist_size, sim_mat): ''' References: https://github.com/faneshion/MatchZoo/blob/master/matchzoo/inputs/preprocess.py#L425 Args: text_maxlen: hist_size: sim_mat: Returns: ''' hist = np.zeros((text_maxlen, hist_size), dtype=np.float32) # mm = sim_mat for (i, j), v in np.ndenumerate(sim_mat): if i >= text_maxlen: break vid = int((v + 1.) / 2. * (hist_size - 1.)) hist[i][vid] += 1 hist += 1 hist = np.log10(hist) # yield hist return hist
Example #17
Source File: WindowDistance.py From MobileNetV2-PoseEstimation with MIT License | 6 votes |
def generateDistanceMatrix(width, height): """ Generates a matrix specifying the distance of each point in a window to its centre. """ # Determine the coordinates of the exact centre of the window originX = width / 2 originY = height / 2 # Generate the distance matrix distances = zerosFactory((height,width), dtype=np.float) for index, val in np.ndenumerate(distances): y,x = index distances[(y,x)] = math.sqrt( math.pow(x - originX, 2) + math.pow(y - originY, 2) ) return distances
Example #18
Source File: schematic.py From GDMC with ISC License | 6 votes |
def toSchematic(self): schem = MCSchematic(shape=self.Size, mats=self._mat) for (x, y, z), value in ndenumerate(self._blocks): b_id, b_data = value schem.Blocks[x, z, y] = b_id schem.Data[x, z, y] = b_data for (x, y, z), value in ndenumerate(self._tile_entities): if not value: continue tag = value tag["x"] = nbt.TAG_Int(x) tag["y"] = nbt.TAG_Int(y) tag["z"] = nbt.TAG_Int(z) schem.addTileEntity(tag) entity_list = nbt.TAG_List() for e in self._entities: entity_list.append(e) schem.root_tag["Entities"] = entity_list return schem
Example #19
Source File: schematic.py From GDMC with ISC License | 6 votes |
def fromSchematic(cls, schematic): structure = cls(size=(schematic.Width, schematic.Height, schematic.Length), mats=namedMaterials[getattr(schematic, "Materials", 'Alpha')]) schematic = copy.deepcopy(schematic) for (x, z, y), b_id in ndenumerate(schematic.Blocks): data = schematic.Data[x, z, y] structure._blocks[x, y, z] = (b_id, data) for te in schematic.TileEntities: x, y, z = te["x"].value, te["y"].value, te["z"].value del te["x"] del te["y"] del te["z"] structure._tile_entities[x, y, z] = te for e in schematic.Entities: structure._entities.append(e) return structure
Example #20
Source File: matching_histogram.py From MatchZoo with Apache License 2.0 | 6 votes |
def transform(self, input_: list) -> list: """Transform the input text.""" text_left, text_right = input_ matching_hist = np.ones((len(text_left), self._hist_bin_size), dtype=np.float32) embed_left = self._embedding_matrix[text_left] embed_right = self._embedding_matrix[text_right] matching_matrix = embed_left.dot(np.transpose(embed_right)) for (i, j), value in np.ndenumerate(matching_matrix): bin_index = int((value + 1.) / 2. * (self._hist_bin_size - 1.)) matching_hist[i][bin_index] += 1.0 if self._mode == 'NH': matching_sum = matching_hist.sum(axis=1) matching_hist = matching_hist / matching_sum[:, np.newaxis] elif self._mode == 'LCH': matching_hist = np.log(matching_hist) return matching_hist.tolist()
Example #21
Source File: pair_generator.py From NeuralResponseRanking with MIT License | 6 votes |
def cal_hist(self, t1, t2, data1_maxlen, hist_size): mhist = np.zeros((data1_maxlen, hist_size), dtype=np.float32) d1len = len(self.data1[t1]) if self.use_hist_feats: assert (t1, t2) in self.hist_feats caled_hist = np.reshape(self.hist_feats[(t1, t2)], (d1len, hist_size)) if d1len < data1_maxlen: mhist[:d1len, :] = caled_hist[:, :] else: mhist[:, :] = caled_hist[:data1_maxlen, :] else: t1_rep = self.embed[self.data1[t1]] t2_rep = self.embed[self.data2[t2]] mm = t1_rep.dot(np.transpose(t2_rep)) for (i,j), v in np.ndenumerate(mm): if i >= data1_maxlen: break vid = int((v + 1.) / 2. * ( hist_size - 1.)) mhist[i][vid] += 1. mhist += 1. mhist = np.log10(mhist) return mhist
Example #22
Source File: list_generator.py From NeuralResponseRanking with MIT License | 6 votes |
def cal_hist(self, t1, t2, data1_maxlen, hist_size): mhist = np.zeros((data1_maxlen, hist_size), dtype=np.float32) d1len = len(self.data1[t1]) if self.use_hist_feats: assert (t1, t2) in self.hist_feats caled_hist = np.reshape(self.hist_feats[(t1, t2)], (d1len, hist_size)) if d1len < data1_maxlen: mhist[:d1len, :] = caled_hist[:, :] else: mhist[:, :] = caled_hist[:data1_maxlen, :] else: t1_rep = self.embed[self.data1[t1]] t2_rep = self.embed[self.data2[t2]] mm = t1_rep.dot(np.transpose(t2_rep)) for (i,j), v in np.ndenumerate(mm): if i >= data1_maxlen: break vid = int((v + 1.) / 2. * ( hist_size - 1.)) mhist[i][vid] += 1. mhist += 1. mhist = np.log10(mhist) return mhist
Example #23
Source File: point_generator.py From NeuralResponseRanking with MIT License | 6 votes |
def cal_hist(self, t1, t2, data1_maxlen, hist_size): mhist = np.zeros((data1_maxlen, hist_size), dtype=np.float32) d1len = len(self.data1[t1]) if self.use_hist_feats: assert (t1, t2) in self.hist_feats caled_hist = np.reshape(self.hist_feats[(t1, t2)], (d1len, hist_size)) if d1len < data1_maxlen: mhist[:d1len, :] = caled_hist[:, :] else: mhist[:, :] = caled_hist[:data1_maxlen, :] else: t1_rep = self.embed[self.data1[t1]] t2_rep = self.embed[self.data2[t2]] mm = t1_rep.dot(np.transpose(t2_rep)) for (i,j), v in np.ndenumerate(mm): if i >= data1_maxlen: break vid = int((v + 1.) / 2. * ( hist_size - 1.)) mhist[i][vid] += 1. mhist += 1. mhist = np.log10(mhist) return mhist
Example #24
Source File: matplotlibwidget.py From CNNArt with Apache License 2.0 | 6 votes |
def Weights_opt(self, matrix, max_weight=None, ax=None): ax = ax if ax is not None else plt.gca() if not max_weight: max_weight = 2 ** np.ceil(np.log(np.abs(matrix).max()) / np.log(2)) ax.patch.set_facecolor('gray') ax.set_aspect('equal', 'box') ax.xaxis.set_major_locator(plt.NullLocator()) ax.yaxis.set_major_locator(plt.NullLocator()) for (x, y), w in np.ndenumerate(matrix): color = 'white' if w > 0 else 'black' size = np.sqrt(np.abs(w)) rect = plt.Rectangle([x - size / 2, y - size / 2], size, size, facecolor=color, edgecolor=color) ax.add_patch(rect) ax.autoscale_view() ax.invert_yaxis()
Example #25
Source File: gates.py From quantumflow with Apache License 2.0 | 6 votes |
def print_gate(gate: Gate, ndigits: int = 2, file: TextIO = None) -> None: """Pretty print a gate tensor Args: gate: ndigits: file: Stream to which to write. Defaults to stdout """ N = gate.qubit_nb gate_tensor = gate.vec.asarray() lines = [] for index, amplitude in np.ndenumerate(gate_tensor): ket = "".join([str(n) for n in index[0:N]]) bra = "".join([str(index[n]) for n in range(N, 2*N)]) if round(abs(amplitude)**2, ndigits) > 0.0: lines.append('{} -> {} : {}'.format(bra, ket, amplitude)) lines.sort(key=lambda x: int(x[0:N])) print('\n'.join(lines), file=file)
Example #26
Source File: states.py From quantumflow with Apache License 2.0 | 6 votes |
def __str__(self) -> str: state = self.vec.asarray() s = [] count = 0 MAX_ELEMENTS = 64 for index, amplitude in np.ndenumerate(state): if not np.isclose(amplitude, 0.0): ket = '|' + ''.join([str(n) for n in index]) + '>' s.append('({c.real:0.04g}{c.imag:+0.04g}i) {k}' .format(c=amplitude, k=ket)) count += 1 if count > MAX_ELEMENTS: s.append('...') break return ' + '.join(s) # End class State
Example #27
Source File: states.py From quantumflow with Apache License 2.0 | 6 votes |
def print_probabilities(state: State, ndigits: int = 4, file: TextIO = None) -> None: """ Pretty print state probabilities. Args: state: ndigits: Number of digits of accuracy file: Output stream (Defaults to stdout) """ prob = bk.evaluate(state.probabilities()) for index, prob in np.ndenumerate(prob): prob = round(prob, ndigits) if prob == 0.0: continue ket = "".join([str(n) for n in index]) print(ket, ":", prob, file=file) # -- Mixed Quantum States --
Example #28
Source File: test_states.py From quantumflow with Apache License 2.0 | 6 votes |
def test_state_to_density(): density = qf.ghz_state(4).asdensity() assert list(density.vec.asarray().shape) == [2]*8 prob = qf.asarray(density.probabilities()) assert prob[0, 0, 0, 0] - 0.5 == ALMOST_ZERO assert prob[0, 1, 0, 0] == ALMOST_ZERO assert prob[1, 1, 1, 1] - 0.5 == ALMOST_ZERO ket = qf.random_state(3) density = ket.asdensity() ket_prob = qf.asarray(ket.probabilities()) density_prob = qf.asarray(density.probabilities()) for index, prob in np.ndenumerate(ket_prob): assert prob - density_prob[index] == ALMOST_ZERO
Example #29
Source File: uisrnn.py From uis-rnn with Apache License 2.0 | 6 votes |
def _calculate_score(self, beam_state, look_ahead_seq): """Calculate negative log likelihoods for all possible state allocations of a look ahead sequence, according to the current beam state. Args: beam_state: A BeamState object. look_ahead_seq: Look ahead sequence, size: look_ahead*D. look_ahead: number of step to look ahead in the beam search. D: observation dimension Returns: beam_score_set: a set of scores for each possible state allocation. """ look_ahead, _ = look_ahead_seq.shape beam_num_clusters = len(beam_state.mean_set) beam_score_set = float('inf') * np.ones( beam_num_clusters + 1 + np.arange(look_ahead)) for cluster_seq, _ in np.ndenumerate(beam_score_set): updated_beam_state = self._update_beam_state(beam_state, look_ahead_seq, cluster_seq) beam_score_set[cluster_seq] = updated_beam_state.neg_likelihood return beam_score_set
Example #30
Source File: np_conserved.py From tenpy with GNU General Public License v3.0 | 5 votes |
def _nontrivial_grid_entries(grid): """Return a list [(idx, entry)] of non-``None`` entries in an array_like grid.""" grid = np.asarray(grid, dtype=np.object) entries = [(idx, entry) for idx, entry in np.ndenumerate(grid) if entry is not None] if len(entries) == 0: raise ValueError("No non-trivial entries in grid") return grid.shape, entries