Python numpy.place() Examples
The following are 30 code examples for showing how to use numpy.place(). These examples are extracted from open source projects. 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 check out the related API usage on the sidebar.
You may also want to check out all available functions/classes of the module
numpy
, or try the search function
.
Example 1
Project: blackbox-attacks Author: sunblaze-ucb File: cifar10_query_based.py License: MIT License | 6 votes |
def one_shot_method(prediction, x, curr_sample, curr_target, p_t): grad_est = np.zeros((BATCH_SIZE, IMAGE_ROWS, IMAGE_COLS, NUM_CHANNELS)) DELTA = np.random.randint(2, size=(BATCH_SIZE, IMAGE_ROWS, IMAGE_COLS, NUM_CHANNELS)) np.place(DELTA, DELTA==0, -1) y_plus = np.clip(curr_sample + args.delta * DELTA, CLIP_MIN, CLIP_MAX) y_minus = np.clip(curr_sample - args.delta * DELTA, CLIP_MIN, CLIP_MAX) if args.CW_loss == 0: pred_plus = K.get_session().run([prediction], feed_dict={x: y_plus, K.learning_phase(): 0})[0] pred_plus_t = pred_plus[np.arange(BATCH_SIZE), list(curr_target)] pred_minus = K.get_session().run([prediction], feed_dict={x: y_minus, K.learning_phase(): 0})[0] pred_minus_t = pred_minus[np.arange(BATCH_SIZE), list(curr_target)] num_est = (pred_plus_t - pred_minus_t) grad_est = num_est[:, None, None, None]/(args.delta * DELTA) # Getting gradient of the loss if args.CW_loss == 0: loss_grad = -1.0 * grad_est/p_t[:, None, None, None] return loss_grad
Example 2
Project: astroNN Author: henrysky File: normalizer.py License: MIT License | 6 votes |
def denormalize(self, data): data_array, dict_flag = self.mode_checker(data) for name in data_array.keys(): # normalize data for each named inputs magic_mask = [data_array[name] == MAGIC_NUMBER] if self._custom_denorm_func is not None: data_array[name] = self._custom_denorm_func(data_array[name]) data_array[name] *= self.std_labels[name] data_array[name] += self.mean_labels[name] np.place(data_array[name], magic_mask, MAGIC_NUMBER) if not dict_flag: data_array = data_array["Temp"] self.mean_labels = self.mean_labels['Temp'] self.std_labels = self.std_labels['Temp'] return data_array
Example 3
Project: Computable Author: ktraunmueller File: mstats_basic.py License: MIT License | 6 votes |
def kurtosis(a, axis=0, fisher=True, bias=True): a, axis = _chk_asarray(a, axis) m2 = moment(a,2,axis) m4 = moment(a,4,axis) olderr = np.seterr(all='ignore') try: vals = ma.where(m2 == 0, 0, m4 / m2**2.0) finally: np.seterr(**olderr) if not bias: n = a.count(axis) can_correct = (n > 3) & (m2 is not ma.masked and m2 > 0) if can_correct.any(): n = np.extract(can_correct, n) m2 = np.extract(can_correct, m2) m4 = np.extract(can_correct, m4) nval = 1.0/(n-2)/(n-3)*((n*n-1.0)*m4/m2**2.0-3*(n-1)**2.0) np.place(vals, can_correct, nval+3.0) if fisher: return vals - 3 else: return vals
Example 4
Project: imgclsmob Author: osmr File: svhn_cls_dataset.py License: MIT License | 6 votes |
def _get_data(self): if any(not os.path.exists(path) or not check_sha1(path, sha1) for path, sha1 in ((os.path.join(self._root, name), sha1) for _, name, sha1 in self._train_data + self._test_data)): for url, _, sha1 in self._train_data + self._test_data: download(url=url, path=self._root, sha1_hash=sha1) if self._mode == "train": data_files = self._train_data[0] else: data_files = self._test_data[0] import scipy.io as sio loaded_mat = sio.loadmat(os.path.join(self._root, data_files[1])) data = loaded_mat["X"] data = np.transpose(data, (3, 0, 1, 2)) self._data = mx.nd.array(data, dtype=data.dtype) self._label = loaded_mat["y"].astype(np.int32).squeeze() np.place(self._label, self._label == 10, 0)
Example 5
Project: pysheds Author: mdbartos File: grid.py License: GNU General Public License v3.0 | 6 votes |
def _flatten_fdir(self, fdir, flat_idx, dirmap, copy=False): # WARNING: This modifies fdir in place if copy is set to False! if copy: fdir = fdir.copy() shape = fdir.shape go_to = ( 0 - shape[1], 1 - shape[1], 1 + 0, 1 + shape[1], 0 + shape[1], -1 + shape[1], -1 + 0, -1 - shape[1] ) gotomap = dict(zip(dirmap, go_to)) for k, v in gotomap.items(): fdir[fdir == k] = v fdir.flat[flat_idx] += flat_idx
Example 6
Project: pysheds Author: mdbartos File: grid.py License: GNU General Public License v3.0 | 6 votes |
def set_nodata(self, data_name, new_nodata, old_nodata=None): """ Change nodata value of a dataset. Parameters ---------- data_name : string Attribute name of dataset to change. new_nodata : int or float New nodata value to use. old_nodata : int or float (optional) If none provided, defaults to self.<data_name>.<nodata> """ if old_nodata is None: old_nodata = getattr(self, data_name).nodata data = getattr(self, data_name) if np.isnan(old_nodata): np.place(data, np.isnan(data), new_nodata) else: np.place(data, data == old_nodata, new_nodata) data.nodata = new_nodata
Example 7
Project: GraphicDesignPatternByPython Author: Relph1119 File: _continuous_distns.py License: MIT License | 6 votes |
def _cdf(self, x, c): output = np.zeros(x.shape, dtype=x.dtype) val = (1.0+c)/(1.0-c) c1 = x < np.pi c2 = 1-c1 xp = np.extract(c1, x) xn = np.extract(c2, x) if np.any(xn): valn = np.extract(c2, np.ones_like(x)*val) xn = 2*np.pi - xn yn = np.tan(xn/2.0) on = 1.0-1.0/np.pi*np.arctan(valn*yn) np.place(output, c2, on) if np.any(xp): valp = np.extract(c1, np.ones_like(x)*val) yp = np.tan(xp/2.0) op = 1.0/np.pi*np.arctan(valp*yp) np.place(output, c1, op) return output
Example 8
Project: recruit Author: Frank-qlu File: function_base.py License: Apache License 2.0 | 5 votes |
def place(arr, mask, vals): """ Change elements of an array based on conditional and input values. Similar to ``np.copyto(arr, vals, where=mask)``, the difference is that `place` uses the first N elements of `vals`, where N is the number of True values in `mask`, while `copyto` uses the elements where `mask` is True. Note that `extract` does the exact opposite of `place`. Parameters ---------- arr : ndarray Array to put data into. mask : array_like Boolean mask array. Must have the same size as `a`. vals : 1-D sequence Values to put into `a`. Only the first N elements are used, where N is the number of True values in `mask`. If `vals` is smaller than N, it will be repeated, and if elements of `a` are to be masked, this sequence must be non-empty. See Also -------- copyto, put, take, extract Examples -------- >>> arr = np.arange(6).reshape(2, 3) >>> np.place(arr, arr>2, [44, 55]) >>> arr array([[ 0, 1, 2], [44, 55, 44]]) """ if not isinstance(arr, np.ndarray): raise TypeError("argument 1 must be numpy.ndarray, " "not {name}".format(name=type(arr).__name__)) return _insert(arr, mask, vals)
Example 9
Project: recruit Author: Frank-qlu File: function_base.py License: Apache License 2.0 | 5 votes |
def _update_dim_sizes(dim_sizes, arg, core_dims): """ Incrementally check and update core dimension sizes for a single argument. Arguments --------- dim_sizes : Dict[str, int] Sizes of existing core dimensions. Will be updated in-place. arg : ndarray Argument to examine. core_dims : Tuple[str, ...] Core dimensions for this argument. """ if not core_dims: return num_core_dims = len(core_dims) if arg.ndim < num_core_dims: raise ValueError( '%d-dimensional argument does not have enough ' 'dimensions for all core dimensions %r' % (arg.ndim, core_dims)) core_shape = arg.shape[-num_core_dims:] for dim, size in zip(core_dims, core_shape): if dim in dim_sizes: if size != dim_sizes[dim]: raise ValueError( 'inconsistent size for core dimension %r: %r vs %r' % (dim, size, dim_sizes[dim])) else: dim_sizes[dim] = size
Example 10
Project: CHAID Author: Rambatino File: column.py License: Apache License 2.0 | 5 votes |
def substitute_values(self, vect): """ Internal method to substitute integers into the vector, and construct metadata to convert back to the original vector. np.nan is always given -1, all other objects are given integers in order of apperence. Parameters ---------- vect : np.array the vector in which to substitute values in """ try: unique = np.unique(vect) except: unique = set(vect) unique = [ x for x in unique if not isinstance(x, float) or not isnan(x) ] arr = np.copy(vect) for new_id, value in enumerate(unique): np.place(arr, arr==value, new_id) self.metadata[new_id] = value arr = arr.astype(np.float) np.place(arr, np.isnan(arr), -1) self.arr = arr if -1 in arr: self.metadata[-1] = self._missing_id
Example 11
Project: lambda-packs Author: ryfeus File: function_base.py License: MIT License | 5 votes |
def place(arr, mask, vals): """ Change elements of an array based on conditional and input values. Similar to ``np.copyto(arr, vals, where=mask)``, the difference is that `place` uses the first N elements of `vals`, where N is the number of True values in `mask`, while `copyto` uses the elements where `mask` is True. Note that `extract` does the exact opposite of `place`. Parameters ---------- arr : ndarray Array to put data into. mask : array_like Boolean mask array. Must have the same size as `a`. vals : 1-D sequence Values to put into `a`. Only the first N elements are used, where N is the number of True values in `mask`. If `vals` is smaller than N, it will be repeated, and if elements of `a` are to be masked, this sequence must be non-empty. See Also -------- copyto, put, take, extract Examples -------- >>> arr = np.arange(6).reshape(2, 3) >>> np.place(arr, arr>2, [44, 55]) >>> arr array([[ 0, 1, 2], [44, 55, 44]]) """ if not isinstance(arr, np.ndarray): raise TypeError("argument 1 must be numpy.ndarray, " "not {name}".format(name=type(arr).__name__)) return _insert(arr, mask, vals)
Example 12
Project: lambda-packs Author: ryfeus File: function_base.py License: MIT License | 5 votes |
def _update_dim_sizes(dim_sizes, arg, core_dims): """ Incrementally check and update core dimension sizes for a single argument. Arguments --------- dim_sizes : Dict[str, int] Sizes of existing core dimensions. Will be updated in-place. arg : ndarray Argument to examine. core_dims : Tuple[str, ...] Core dimensions for this argument. """ if not core_dims: return num_core_dims = len(core_dims) if arg.ndim < num_core_dims: raise ValueError( '%d-dimensional argument does not have enough ' 'dimensions for all core dimensions %r' % (arg.ndim, core_dims)) core_shape = arg.shape[-num_core_dims:] for dim, size in zip(core_dims, core_shape): if dim in dim_sizes: if size != dim_sizes[dim]: raise ValueError( 'inconsistent size for core dimension %r: %r vs %r' % (dim, size, dim_sizes[dim])) else: dim_sizes[dim] = size
Example 13
Project: lambda-packs Author: ryfeus File: function_base.py License: MIT License | 5 votes |
def add_newdoc(place, obj, doc): """ Adds documentation to obj which is in module place. If doc is a string add it to obj as a docstring If doc is a tuple, then the first element is interpreted as an attribute of obj and the second as the docstring (method, docstring) If doc is a list, then each element of the list should be a sequence of length two --> [(method1, docstring1), (method2, docstring2), ...] This routine never raises an error. This routine cannot modify read-only docstrings, as appear in new-style classes or built-in functions. Because this routine never raises an error the caller must check manually that the docstrings were changed. """ try: new = getattr(__import__(place, globals(), {}, [obj]), obj) if isinstance(doc, str): add_docstring(new, doc.strip()) elif isinstance(doc, tuple): add_docstring(getattr(new, doc[0]), doc[1].strip()) elif isinstance(doc, list): for val in doc: add_docstring(getattr(new, val[0]), val[1].strip()) except Exception: pass # Based on scitools meshgrid
Example 14
Project: lambda-packs Author: ryfeus File: _util.py License: MIT License | 5 votes |
def _lazyselect(condlist, choicelist, arrays, default=0): """ Mimic `np.select(condlist, choicelist)`. Notice it assumes that all `arrays` are of the same shape, or can be broadcasted together. All functions in `choicelist` must accept array arguments in the order given in `arrays` and must return an array of the same shape as broadcasted `arrays`. Examples -------- >>> x = np.arange(6) >>> np.select([x <3, x > 3], [x**2, x**3], default=0) array([ 0, 1, 4, 0, 64, 125]) >>> _lazyselect([x < 3, x > 3], [lambda x: x**2, lambda x: x**3], (x,)) array([ 0., 1., 4., 0., 64., 125.]) >>> a = -np.ones_like(x) >>> _lazyselect([x < 3, x > 3], ... [lambda x, a: x**2, lambda x, a: a * x**3], ... (x, a), default=np.nan) array([ 0., 1., 4., nan, -64., -125.]) """ arrays = np.broadcast_arrays(*arrays) tcode = np.mintypecode([a.dtype.char for a in arrays]) out = _valarray(np.shape(arrays[0]), value=default, typecode=tcode) for index in range(len(condlist)): func, cond = choicelist[index], condlist[index] if np.all(cond is False): continue cond, _ = np.broadcast_arrays(cond, arrays[0]) temp = tuple(np.extract(cond, arr) for arr in arrays) np.place(out, cond, func(*temp)) return out
Example 15
Project: lambda-packs Author: ryfeus File: _continuous_distns.py License: MIT License | 5 votes |
def _stats(self, b, moments='mv'): mu, mu2, g1, g2 = None, None, None, None if 'm' in moments: mask = b > 1 bt = np.extract(mask, b) mu = valarray(np.shape(b), value=np.inf) np.place(mu, mask, bt / (bt-1.0)) if 'v' in moments: mask = b > 2 bt = np.extract(mask, b) mu2 = valarray(np.shape(b), value=np.inf) np.place(mu2, mask, bt / (bt-2.0) / (bt-1.0)**2) if 's' in moments: mask = b > 3 bt = np.extract(mask, b) g1 = valarray(np.shape(b), value=np.nan) vals = 2 * (bt + 1.0) * np.sqrt(bt - 2.0) / ((bt - 3.0) * np.sqrt(bt)) np.place(g1, mask, vals) if 'k' in moments: mask = b > 4 bt = np.extract(mask, b) g2 = valarray(np.shape(b), value=np.nan) vals = (6.0*np.polyval([1.0, 1.0, -6, -2], bt) / np.polyval([1.0, -7.0, 12.0, 0.0], bt)) np.place(g2, mask, vals) return mu, mu2, g1, g2
Example 16
Project: lambda-packs Author: ryfeus File: _continuous_distns.py License: MIT License | 5 votes |
def _pdf(self, x, b): # rice.pdf(x, b) = x * exp(-(x**2+b**2)/2) * I[0](x*b) # # We use (x**2 + b**2)/2 = ((x-b)**2)/2 + xb. # The factor of np.exp(-xb) is then included in the i0e function # in place of the modified Bessel function, i0, improving # numerical stability for large values of xb. return x * np.exp(-(x-b)*(x-b)/2.0) * sc.i0e(x*b)
Example 17
Project: lambda-packs Author: ryfeus File: function_base.py License: MIT License | 5 votes |
def place(arr, mask, vals): """ Change elements of an array based on conditional and input values. Similar to ``np.copyto(arr, vals, where=mask)``, the difference is that `place` uses the first N elements of `vals`, where N is the number of True values in `mask`, while `copyto` uses the elements where `mask` is True. Note that `extract` does the exact opposite of `place`. Parameters ---------- arr : ndarray Array to put data into. mask : array_like Boolean mask array. Must have the same size as `a`. vals : 1-D sequence Values to put into `a`. Only the first N elements are used, where N is the number of True values in `mask`. If `vals` is smaller than N, it will be repeated, and if elements of `a` are to be masked, this sequence must be non-empty. See Also -------- copyto, put, take, extract Examples -------- >>> arr = np.arange(6).reshape(2, 3) >>> np.place(arr, arr>2, [44, 55]) >>> arr array([[ 0, 1, 2], [44, 55, 44]]) """ if not isinstance(arr, np.ndarray): raise TypeError("argument 1 must be numpy.ndarray, " "not {name}".format(name=type(arr).__name__)) return _insert(arr, mask, vals)
Example 18
Project: lambda-packs Author: ryfeus File: function_base.py License: MIT License | 5 votes |
def _update_dim_sizes(dim_sizes, arg, core_dims): """ Incrementally check and update core dimension sizes for a single argument. Arguments --------- dim_sizes : Dict[str, int] Sizes of existing core dimensions. Will be updated in-place. arg : ndarray Argument to examine. core_dims : Tuple[str, ...] Core dimensions for this argument. """ if not core_dims: return num_core_dims = len(core_dims) if arg.ndim < num_core_dims: raise ValueError( '%d-dimensional argument does not have enough ' 'dimensions for all core dimensions %r' % (arg.ndim, core_dims)) core_shape = arg.shape[-num_core_dims:] for dim, size in zip(core_dims, core_shape): if dim in dim_sizes: if size != dim_sizes[dim]: raise ValueError( 'inconsistent size for core dimension %r: %r vs %r' % (dim, size, dim_sizes[dim])) else: dim_sizes[dim] = size
Example 19
Project: lambda-packs Author: ryfeus File: function_base.py License: MIT License | 5 votes |
def add_newdoc(place, obj, doc): """ Adds documentation to obj which is in module place. If doc is a string add it to obj as a docstring If doc is a tuple, then the first element is interpreted as an attribute of obj and the second as the docstring (method, docstring) If doc is a list, then each element of the list should be a sequence of length two --> [(method1, docstring1), (method2, docstring2), ...] This routine never raises an error. This routine cannot modify read-only docstrings, as appear in new-style classes or built-in functions. Because this routine never raises an error the caller must check manually that the docstrings were changed. """ try: new = getattr(__import__(place, globals(), {}, [obj]), obj) if isinstance(doc, str): add_docstring(new, doc.strip()) elif isinstance(doc, tuple): add_docstring(getattr(new, doc[0]), doc[1].strip()) elif isinstance(doc, list): for val in doc: add_docstring(getattr(new, val[0]), val[1].strip()) except: pass # Based on scitools meshgrid
Example 20
Project: auto-alt-text-lambda-api Author: abhisuri97 File: function_base.py License: MIT License | 5 votes |
def place(arr, mask, vals): """ Change elements of an array based on conditional and input values. Similar to ``np.copyto(arr, vals, where=mask)``, the difference is that `place` uses the first N elements of `vals`, where N is the number of True values in `mask`, while `copyto` uses the elements where `mask` is True. Note that `extract` does the exact opposite of `place`. Parameters ---------- arr : ndarray Array to put data into. mask : array_like Boolean mask array. Must have the same size as `a`. vals : 1-D sequence Values to put into `a`. Only the first N elements are used, where N is the number of True values in `mask`. If `vals` is smaller than N it will be repeated. See Also -------- copyto, put, take, extract Examples -------- >>> arr = np.arange(6).reshape(2, 3) >>> np.place(arr, arr>2, [44, 55]) >>> arr array([[ 0, 1, 2], [44, 55, 44]]) """ if not isinstance(arr, np.ndarray): raise TypeError("argument 1 must be numpy.ndarray, " "not {name}".format(name=type(arr).__name__)) return _insert(arr, mask, vals)
Example 21
Project: auto-alt-text-lambda-api Author: abhisuri97 File: function_base.py License: MIT License | 5 votes |
def add_newdoc(place, obj, doc): """ Adds documentation to obj which is in module place. If doc is a string add it to obj as a docstring If doc is a tuple, then the first element is interpreted as an attribute of obj and the second as the docstring (method, docstring) If doc is a list, then each element of the list should be a sequence of length two --> [(method1, docstring1), (method2, docstring2), ...] This routine never raises an error. This routine cannot modify read-only docstrings, as appear in new-style classes or built-in functions. Because this routine never raises an error the caller must check manually that the docstrings were changed. """ try: new = getattr(__import__(place, globals(), {}, [obj]), obj) if isinstance(doc, str): add_docstring(new, doc.strip()) elif isinstance(doc, tuple): add_docstring(getattr(new, doc[0]), doc[1].strip()) elif isinstance(doc, list): for val in doc: add_docstring(getattr(new, val[0]), val[1].strip()) except: pass # Based on scitools meshgrid
Example 22
Project: unmixing Author: arthur-e File: utils.py License: MIT License | 5 votes |
def as_mask(path, nodata=-9999): ''' Converts all non-zero values in all bands to ones. Arguments: path The path of the raster file to open as a mask array nodata The NoData value; these areas not masked ''' rast, gt, wkt = as_array(path) # Create a baseline raster base = np.empty((1, rast.shape[-2], rast.shape[-1])) base.fill(False) # Case of multiband raster if rast.ndim == 3: # Update the mask for nonzero values in any band for i in range(rast.shape[0]): np.logical_or(base, (rast[i,...].ravel() > 0).reshape(rast[i,...].shape), out=base) # Repeat the value of one (1) across the bands np.place(rast, base.repeat(rast.shape[0], axis=0), (1,)) elif rast.ndim == 2: # Create a single band (dim-3 array) rast = rast.reshape((1, rast.shape[-2], rast.shape[-1])) # Update the mask for nonzero values in any band np.logical_or(base, (rast.ravel() > 0).reshape(rast.shape), out=base) # Repeat the value of one (1) across the bands np.place(rast, base, (1,)) else: raise ValueError('Number of array dimensions must be 2 or 3') # Replace the NoData values rast[rast == nodata] = 0 return (rast, gt, wkt)
Example 23
Project: unmixing Author: arthur-e File: lsma.py License: MIT License | 5 votes |
def normalize_reflectance_within_image( rast, band_range=(0, 5), nodata=-9999, scale=100): ''' Following Wu (2004, Remote Sensing of Environment), normalizes the reflectances in each pixel by the average reflectance *across bands.* This is an attempt to mitigate within-endmember variability. Arguments: rast A gdal.Dataset or numpy.array instance nodata The NoData value to use (and value to ignore) scale (Optional) Wu's definition scales the normalized reflectance by 100 for some reason; another reasonable value would be 10,000 (approximating scale of Landsat reflectance units); set to None for no scaling. ''' # Can accept either a gdal.Dataset or numpy.array instance if not isinstance(rast, np.ndarray): rastr = rast.ReadAsArray() else: rastr = rast.copy() shp = rastr.shape b0, b1 = band_range # Get the beginning, end of band range b1 += 1 # Ranges in Python are not inclusive, so add 1 rastr_normalized = np.divide( rastr.reshape((shp[0], shp[1]*shp[2])), rastr[b0:b1,...].mean(axis=0).reshape((1, shp[1]*shp[2])).repeat(shp[0], axis=0)) # Recover original shape; scale if necessary rastr_normalized = rastr_normalized.reshape(shp) if scale is not None: rastr_normalized = np.multiply(rastr_normalized, scale) # Fill in the NoData areas from the original raster np.place(rastr_normalized, rastr == nodata, nodata) return rastr_normalized
Example 24
Project: DeepDIVA Author: DIVA-DIA File: functional.py License: GNU Lesser General Public License v3.0 | 5 votes |
def gt_to_one_hot(matrix, class_encodings): """ Convert ground truth tensor or numpy matrix to one-hot encoded matrix Parameters ------- matrix: float tensor from to_tensor() or numpy array shape (C x H x W) in the range [0.0, 1.0] or shape (H x W x C) BGR class_encodings: list of int Blue channel values that encode the different classes Returns ------- torch.LongTensor of size [#C x H x W] sparse one-hot encoded multi-class matrix, where #C is the number of classes """ num_classes = len(class_encodings) if type(matrix).__module__ == np.__name__: im_np = matrix[:, :, 2].astype(np.uint8) else: # TODO: ugly fix -> better to not normalize in the first place np_array = (matrix * 255).numpy().astype(np.uint8) im_np = np_array[2, :, :].astype(np.uint8) integer_encoded = np.array([i for i in range(num_classes)]) onehot_encoder = OneHotEncoder(sparse=False, categories='auto') integer_encoded = integer_encoded.reshape(len(integer_encoded), 1) onehot_encoded = onehot_encoder.fit_transform(integer_encoded).astype(np.int8) np.place(im_np, im_np == 0, 1) # needed to deal with 0 fillers at the borders during testing (replace with background) replace_dict = {k: v for k, v in zip(class_encodings, onehot_encoded)} # create the one hot matrix one_hot_matrix = np.asanyarray( [[replace_dict[im_np[i, j]] for j in range(im_np.shape[1])] for i in range(im_np.shape[0])]).astype( np.uint8) return torch.LongTensor(one_hot_matrix.transpose((2, 0, 1)))
Example 25
Project: vnpy_crypto Author: birforce File: function_base.py License: MIT License | 5 votes |
def place(arr, mask, vals): """ Change elements of an array based on conditional and input values. Similar to ``np.copyto(arr, vals, where=mask)``, the difference is that `place` uses the first N elements of `vals`, where N is the number of True values in `mask`, while `copyto` uses the elements where `mask` is True. Note that `extract` does the exact opposite of `place`. Parameters ---------- arr : ndarray Array to put data into. mask : array_like Boolean mask array. Must have the same size as `a`. vals : 1-D sequence Values to put into `a`. Only the first N elements are used, where N is the number of True values in `mask`. If `vals` is smaller than N, it will be repeated, and if elements of `a` are to be masked, this sequence must be non-empty. See Also -------- copyto, put, take, extract Examples -------- >>> arr = np.arange(6).reshape(2, 3) >>> np.place(arr, arr>2, [44, 55]) >>> arr array([[ 0, 1, 2], [44, 55, 44]]) """ if not isinstance(arr, np.ndarray): raise TypeError("argument 1 must be numpy.ndarray, " "not {name}".format(name=type(arr).__name__)) return _insert(arr, mask, vals)
Example 26
Project: vnpy_crypto Author: birforce File: function_base.py License: MIT License | 5 votes |
def _update_dim_sizes(dim_sizes, arg, core_dims): """ Incrementally check and update core dimension sizes for a single argument. Arguments --------- dim_sizes : Dict[str, int] Sizes of existing core dimensions. Will be updated in-place. arg : ndarray Argument to examine. core_dims : Tuple[str, ...] Core dimensions for this argument. """ if not core_dims: return num_core_dims = len(core_dims) if arg.ndim < num_core_dims: raise ValueError( '%d-dimensional argument does not have enough ' 'dimensions for all core dimensions %r' % (arg.ndim, core_dims)) core_shape = arg.shape[-num_core_dims:] for dim, size in zip(core_dims, core_shape): if dim in dim_sizes: if size != dim_sizes[dim]: raise ValueError( 'inconsistent size for core dimension %r: %r vs %r' % (dim, size, dim_sizes[dim])) else: dim_sizes[dim] = size
Example 27
Project: vnpy_crypto Author: birforce File: function_base.py License: MIT License | 5 votes |
def add_newdoc(place, obj, doc): """ Adds documentation to obj which is in module place. If doc is a string add it to obj as a docstring If doc is a tuple, then the first element is interpreted as an attribute of obj and the second as the docstring (method, docstring) If doc is a list, then each element of the list should be a sequence of length two --> [(method1, docstring1), (method2, docstring2), ...] This routine never raises an error. This routine cannot modify read-only docstrings, as appear in new-style classes or built-in functions. Because this routine never raises an error the caller must check manually that the docstrings were changed. """ try: new = getattr(__import__(place, globals(), {}, [obj]), obj) if isinstance(doc, str): add_docstring(new, doc.strip()) elif isinstance(doc, tuple): add_docstring(getattr(new, doc[0]), doc[1].strip()) elif isinstance(doc, list): for val in doc: add_docstring(getattr(new, val[0]), val[1].strip()) except Exception: pass # Based on scitools meshgrid
Example 28
Project: Global-Second-order-Pooling-Convolutional-Networks Author: ZilinGao File: svhn.py License: MIT License | 5 votes |
def __init__(self, root, split='train', transform=None, target_transform=None, download=False): self.root = os.path.expanduser(root) self.transform = transform self.target_transform = target_transform self.split = split # training set or test set or extra set if self.split not in self.split_list: raise ValueError('Wrong split entered! Please use split="train" ' 'or split="extra" or split="test"') self.url = self.split_list[split][0] self.filename = self.split_list[split][1] self.file_md5 = self.split_list[split][2] if download: self.download() if not self._check_integrity(): raise RuntimeError('Dataset not found or corrupted.' + ' You can use download=True to download it') # import here rather than at top of file because this is # an optional dependency for torchvision import scipy.io as sio # reading(loading) mat file as array loaded_mat = sio.loadmat(os.path.join(self.root, self.filename)) self.data = loaded_mat['X'] # loading from the .mat file gives an np array of type np.uint8 # converting to np.int64, so that we have a LongTensor after # the conversion from the numpy array # the squeeze is needed to obtain a 1D tensor self.labels = loaded_mat['y'].astype(np.int64).squeeze() # the svhn dataset assigns the class label "10" to the digit 0 # this makes it inconsistent with several loss functions # which expect the class labels to be in the range [0, C-1] np.place(self.labels, self.labels == 10, 0) self.data = np.transpose(self.data, (3, 2, 0, 1))
Example 29
Project: Computable Author: ktraunmueller File: function_base.py License: MIT License | 5 votes |
def place(arr, mask, vals): """ Change elements of an array based on conditional and input values. Similar to ``np.copyto(arr, vals, where=mask)``, the difference is that `place` uses the first N elements of `vals`, where N is the number of True values in `mask`, while `copyto` uses the elements where `mask` is True. Note that `extract` does the exact opposite of `place`. Parameters ---------- arr : array_like Array to put data into. mask : array_like Boolean mask array. Must have the same size as `a`. vals : 1-D sequence Values to put into `a`. Only the first N elements are used, where N is the number of True values in `mask`. If `vals` is smaller than N it will be repeated. See Also -------- copyto, put, take, extract Examples -------- >>> arr = np.arange(6).reshape(2, 3) >>> np.place(arr, arr>2, [44, 55]) >>> arr array([[ 0, 1, 2], [44, 55, 44]]) """ return _insert(arr, mask, vals)
Example 30
Project: Computable Author: ktraunmueller File: function_base.py License: MIT License | 5 votes |
def add_newdoc(place, obj, doc): """Adds documentation to obj which is in module place. If doc is a string add it to obj as a docstring If doc is a tuple, then the first element is interpreted as an attribute of obj and the second as the docstring (method, docstring) If doc is a list, then each element of the list should be a sequence of length two --> [(method1, docstring1), (method2, docstring2), ...] This routine never raises an error. This routine cannot modify read-only docstrings, as appear in new-style classes or built-in functions. Because this routine never raises an error the caller must check manually that the docstrings were changed. """ try: new = {} exec('from %s import %s' % (place, obj), new) if isinstance(doc, str): add_docstring(new[obj], doc.strip()) elif isinstance(doc, tuple): add_docstring(getattr(new[obj], doc[0]), doc[1].strip()) elif isinstance(doc, list): for val in doc: add_docstring(getattr(new[obj], val[0]), val[1].strip()) except: pass # Based on scitools meshgrid