Python numpy.put() Examples
The following are 30 code examples for showing how to use numpy.put(). 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: paramz Author: sods File: parameter_core.py License: BSD 3-Clause "New" or "Revised" License | 6 votes |
def _transform_gradients(self, g): """ Transform the gradients by multiplying the gradient factor for each constraint to it. """ #py3 fix #[np.put(g, i, c.gradfactor(self.param_array[i], g[i])) for c, i in self.constraints.iteritems() if c != __fixed__] [np.put(g, i, c.gradfactor(self.param_array[i], g[i])) for c, i in self.constraints.items() if c != __fixed__] if self._has_fixes(): return g[self._fixes_] return g #def _transform_gradients_non_natural(self, g): # """ # Transform the gradients by multiplying the gradient factor for each # constraint to it, using the theta transformed natural gradient. # """ # #py3 fix # #[np.put(g, i, c.gradfactor_non_natural(self.param_array[i], g[i])) for c, i in self.constraints.iteritems() if c != __fixed__] # [np.put(g, i, c.gradfactor_non_natural(self.param_array[i], g[i])) for c, i in self.constraints.items() if c != __fixed__] # if self._has_fixes(): return g[self._fixes_] # return g
Example 2
Project: cupy Author: cupy File: insert.py License: MIT License | 6 votes |
def put(a, ind, v, mode='wrap'): """Replaces specified elements of an array with given values. Args: a (cupy.ndarray): Target array. ind (array-like): Target indices, interpreted as integers. v (array-like): Values to place in `a` at target indices. If `v` is shorter than `ind` it will be repeated as necessary. mode (str): How out-of-bounds indices will behave. Its value must be either `'raise'`, `'wrap'` or `'clip'`. Otherwise, :class:`TypeError` is raised. .. note:: Default `mode` is set to `'wrap'` to avoid unintended performance drop. If you need NumPy's behavior, please pass `mode='raise'` manually. .. seealso:: :func:`numpy.put` """ a.put(ind, v, mode=mode)
Example 3
Project: fake-news-detection-pipeline Author: Johnny-Wish File: document_embedder.py License: Apache License 2.0 | 6 votes |
def get_onehot_arr(place, dim, put_value=1.): """ get a `dim` dimensional one-hot vector, with `place`-th entry being `put_value` and dtype being np.float32 e.g.: >>> get_onehot_arr(3, 5, 1.3) np.ndarray([0, 0, 0, 1.3, 0], dtype=np.float32) :param place: the place to put a non-zero value :param dim: the length of the vector :param put_value: the value to be put :return: a `dim` dimensional one-hot vector, with `place`-th entry being `put_value` and dtype being np.float32 """ if place >= dim or place < 0: print("Invalid input: place = {}, dim = {}".format(place, dim)) ans = np.zeros(dim, dtype=np.float32) np.put(ans, place, put_value) return ans
Example 4
Project: SDA Author: mrvollger File: MEC.py License: MIT License | 6 votes |
def readCuts(myfile): cuts = open(myfile).readlines() haps = np.zeros((len(cuts), m)) totalPSVs = 0 for idx, psvs in enumerate(cuts): psvs=psvs.strip().split() psvs = map(int, psvs) for psv in psvs: psvPositions.append(psv) np.put(haps[idx], psvs, [1]) totalPSVs += len(psvs) # get rid of positions where there are two PSVs at one stop # not sure why this happens, will Have to fix with mark # confirmed as a bug, a fix is in the works # hack to skip it for now toRm = list(np.where( (haps.sum(axis = 0) > 1) )[0]) for pos in toRm: print("mulitple PSVs in one spot!") psvPositions.remove(pos) return(haps)
Example 5
Project: actionable-recourse Author: ustunb File: builder.py License: BSD 3-Clause "New" or "Revised" License | 6 votes |
def __init__(self, action_set, x = None, **kwargs): self.built = False #setup the optimizer here: self._optimizer = SolverFactory('cbc') self._results = None #todo: Alex fill out these functions ## todo: check what each of these does in CPLEX. self._set_mip_time_limit = lambda mip, time_limit: True #_set_mip_time_limit(self, mip, time_limit) self._set_mip_node_limit = lambda mip, node_limit: True #_set_mip_node_limit(self, mip, node_limit) ## todo: not sure what to put for this. let's talk about what the cplex display flag does. self._set_mip_display = lambda mip, display_flag: True #_set_mip_display(self, mip, display) self._apriori_infeasible = False super().__init__(action_set = action_set, x = x, **kwargs) #### building MIP ####
Example 6
Project: biskit Author: graik File: reduce.py License: GNU General Public License v3.0 | 6 votes |
def merge( self, inmodel=None ): """ Rescue profiles and info records from input model into result model. """ r = self.result.clone() m = inmodel or self.model i1, i2 = r.compareAtoms( m ) mask1 = N.zeros( len( r ), N.int ) N.put( mask1, i1, 1 ) r.atoms.update( m.atoms, mask=mask1 ) r.fileName = m.fileName r.source = m.source return r
Example 7
Project: deep-learning-note Author: wdxtub File: 4_simulate_sin.py License: MIT License | 5 votes |
def train(): # 学习率 lr = 0.01 x = tf.placeholder(tf.float32) y = tf.placeholder(tf.float32) y_ = inference(x) # 损失函数 loss = tf.square(y_ - y) # 随机梯度下降 opt = tf.train.GradientDescentOptimizer(lr) train_op = opt.minimize(loss) init = tf.global_variables_initializer() with tf.Session() as sess: sess.run(init) print("start training") for i in range(1000000): train_x, train_y = get_train_data() sess.run(train_op, feed_dict={x: train_x, y: train_y}) if i % 10000 == 0: times = int(i / 10000) test_x_ndarray = np.arange(0, 2 * np.pi, 0.01) test_y_ndarray = np.zeros([len(test_x_ndarray)]) ind = 0 for test_x in test_x_ndarray: test_y = sess.run(y_, feed_dict={x: test_x, y: 1}) np.put(test_y_ndarray, ind, test_y) ind += 1 draw_correct_line() pylab.plot(test_x_ndarray, test_y_ndarray, '--', label= str(times)+'times') pylab.show()
Example 8
Project: simnibs Author: simnibs File: hmutils.py License: GNU General Public License v3.0 | 5 votes |
def row_wise_unique(arr, fill_value=0): """Return row-wise unique values of an array. The trick is to add a unique imaginary number to each row. That way, when calling np.unique, the floats in the original array will be recognized as different values if they occur in different rows, but be treated as the same value if they occur in the same row. PARAMETERS ---------- arr : ndarray Array in which to find unique values. RETURNS ---------- u_arr : ndarray Array in which unique values are retained whereas all non-unique elements are set to zero. Acknowledgements ---------------- From https://stackoverflow.com/questions/26958233/numpy-row-wise-unique-elements (by user "unutbu"). """ weight = 1j*np.linspace(0, arr.shape[1], arr.shape[0], endpoint=False) # row "weights" u_arr = arr + weight[:,np.newaxis] # add weights to rows u, ind = np.unique(u_arr, return_index=True) # now get unique values u_arr = np.ones_like(arr)*fill_value # initialize array np.put(u_arr, ind, arr.flat[ind]) # fill in unique values. Remaining values # are set to zero. return u_arr
Example 9
Project: tenpy Author: tenpy File: np_conserved.py License: GNU General Public License v3.0 | 5 votes |
def diag(s, leg, dtype=None, labels=None): """Returns a square, diagonal matrix of entries `s`. The resulting matrix has legs ``(leg, leg.conj())`` and charge 0. Parameters ---------- s : scalar | 1D array The entries to put on the diagonal. If scalar, all diagonal entries are the same. leg : :class:`LegCharge` The first leg of the resulting matrix. dtype : None | type The data type to be used for the result. By default, use dtype of `s`. labels : list of {str | None} Labels associated to each leg, ``None`` for non-named labels. Returns ------- diagonal : :class:`Array` A square matrix with diagonal entries `s`. See also -------- Array.scale_axis : similar as ``tensordot(diag(s), ...)``, but faster. """ s = np.asarray(s, dtype) scalar = (s.ndim == 0) if not scalar and len(s) != leg.ind_len: raise ValueError("len(s)={0:d} not equal to leg.ind_len={1:d}".format(len(s), leg.ind_len)) res = Array((leg, leg.conj()), s.dtype, labels=labels) # default charge is 0 # qdata = [[0, 0], [1, 1], ....] res._qdata = np.arange(leg.block_number, dtype=np.intp)[:, np.newaxis] * np.ones(2, np.intp) # ``res._qdata_sorted = True`` was already set if scalar: res._data = [np.diag(s * np.ones(size, dtype=s.dtype)) for size in leg.get_block_sizes()] else: res._data = [np.diag(s[leg.get_slice(qi)]) for qi in range(leg.block_number)] return res
Example 10
Project: paramz Author: sods File: parameter_core.py License: BSD 3-Clause "New" or "Revised" License | 5 votes |
def optimizer_array(self): """ Array for the optimizer to work on. This array always lives in the space for the optimizer. Thus, it is untransformed, going from Transformations. Setting this array, will make sure the transformed parameters for this model will be set accordingly. It has to be set with an array, retrieved from this method, as e.g. fixing will resize the array. The optimizer should only interfere with this array, such that transformations are secured. """ if self.__dict__.get('_optimizer_copy_', None) is None or self.size != self._optimizer_copy_.size: self._optimizer_copy_ = np.empty(self.size) if not self._optimizer_copy_transformed: self._optimizer_copy_.flat = self.param_array.flat #py3 fix #[np.put(self._optimizer_copy_, ind, c.finv(self.param_array[ind])) for c, ind in self.constraints.iteritems() if c != __fixed__] [np.put(self._optimizer_copy_, ind, c.finv(self.param_array[ind])) for c, ind in self.constraints.items() if c != __fixed__] self._optimizer_copy_transformed = True if self._has_fixes():# or self._has_ties()): self._ensure_fixes() return self._optimizer_copy_[self._fixes_] return self._optimizer_copy_
Example 11
Project: paramz Author: sods File: parameter_core.py License: BSD 3-Clause "New" or "Revised" License | 5 votes |
def optimizer_array(self, p): """ Make sure the optimizer copy does not get touched, thus, we only want to set the values *inside* not the array itself. Also we want to update param_array in here. """ f = None if self.has_parent() and self.constraints[__fixed__].size != 0: f = np.ones(self.size).astype(bool) f[self.constraints[__fixed__]] = FIXED elif self._has_fixes(): f = self._fixes_ if f is None: self.param_array.flat = p [np.put(self.param_array, ind, c.f(self.param_array.flat[ind])) #py3 fix #for c, ind in self.constraints.iteritems() if c != __fixed__] for c, ind in self.constraints.items() if c != __fixed__] else: self.param_array.flat[f] = p [np.put(self.param_array, ind[f[ind]], c.f(self.param_array.flat[ind[f[ind]]])) #py3 fix #for c, ind in self.constraints.iteritems() if c != __fixed__] for c, ind in self.constraints.items() if c != __fixed__] #self._highest_parent_.tie.propagate_val() self._optimizer_copy_transformed = False self.trigger_update()
Example 12
Project: pyod Author: yzhao062 File: lmdd.py License: BSD 2-Clause "Simplified" License | 5 votes |
def __sf(self, X): """Internal function to calculate for Smoothing Factors of data points Repeated n_iter_ of times in randomized mode. """ dis_ = np.zeros(shape=(X.shape[0],)) card_ = np.zeros(shape=(X.shape[0],)) # perform one process with the original input order itr_res = self.__dis(X) np.put(card_, X.shape[0] - sum([i > 0. for i in itr_res]), np.where(itr_res > 0.)) # create a copy of random state to preserve original state for # future fits (if any) random_state = np.random.RandomState( seed=self.random_state_.get_state()[1][0]) indices = np.arange(X.shape[0]) for _ in range(self.n_iter_): ind_ = indices random_state.shuffle(ind_) _x = X[indices] # get dissimilarity of this iteration and restore original order itr_res = self.__dis(_x)[np.argsort(ind_)] current_card = X.shape[0] - sum([i > 0. for i in itr_res]) # compare with previous iteration to get the maximal dissimilarity for i, j in enumerate(itr_res): if j > dis_[i]: dis_[i] = j card_[i] = current_card # Increase random state seed by one to reorder input next iteration random_state.seed(random_state.get_state()[1][0] + 1) return np.multiply(dis_, card_)
Example 13
Project: Mastering-Elasticsearch-7.0 Author: PacktPublishing File: rcv1.py License: MIT License | 5 votes |
def _inverse_permutation(p): """inverse permutation p""" n = p.size s = np.zeros(n, dtype=np.int32) i = np.arange(n, dtype=np.int32) np.put(s, p, i) # s[p] = i return s
Example 14
Project: FATE Author: FederatedAI File: hetero_stepwise.py License: Apache License 2.0 | 5 votes |
def _put_model(self, key, model): """ wrapper to put key, model dict pair into models dict """ model_dict = {'model': {'stepwise': model.export_model()}} self.models[key] = model_dict
Example 15
Project: FATE Author: FederatedAI File: hetero_stepwise.py License: Apache License 2.0 | 5 votes |
def predict(data_instances, model): if data_instances is None: return d_header = data_instances.schema.get("header") best_feature = [d_header.index(x) for x in model.header] best_mask = np.zeros(len(d_header), dtype=bool) np.put(best_mask, best_feature, 1) new_data = data_instances.mapValues(lambda v: Step.slice_data_instance(v, best_mask)) pred_result = model.predict(new_data) return pred_result
Example 16
Project: DeepSim Author: shijx12 File: util.py License: MIT License | 5 votes |
def __init__(self, imdb_name, resize=True): imdb = get_imdb(imdb_name) # Ignore the background class!!! So ['gt_classes'] must minus 1. self.classes = [ np.zeros(imdb.num_classes - 1) for i in range(imdb.num_images) ] for i, anno in enumerate(imdb.gt_roidb()): np.put(self.classes[i], map(lambda x: x-1, anno['gt_classes']), 1) # np.put(self.classes[i], random.choice(map(lambda x: x-1, anno['gt_classes'])), 1) self.images = [ imdb.image_path_at(i) for i in range(imdb.num_images) ] assert len(self.classes) == len(self.images) self._perm = np.random.permutation(np.arange(len(self.images))) self._cur = 0 self.resize = resize
Example 17
Project: LightNet Author: ansleliu File: loss.py License: MIT License | 5 votes |
def unique_encode(self, cls_targets): batch_size, _, _ = cls_targets.size() target_mask = (cls_targets >= 0) * (cls_targets != self.ignore_label) cls_targets = [cls_targets[idx].masked_select(target_mask[idx]) for idx in np.arange(batch_size)] # unique_cls = [np.unique(label.numpy(), return_counts=True) for label in cls_targets] unique_cls = [np.unique(label.numpy()) for label in cls_targets] encode = np.zeros((batch_size, self.num_classes), dtype=np.uint8) for idx in np.arange(batch_size): np.put(encode[idx], unique_cls[idx], 1) return torch.from_numpy(encode).float()
Example 18
Project: cupy Author: cupy File: insert.py License: MIT License | 5 votes |
def place(arr, mask, vals): """Change elements of an array based on conditional and input values. This function uses the first N elements of `vals`, where N is the number of true values in `mask`. Args: arr (cupy.ndarray): Array to put data into. mask (array-like): Boolean mask array. Must have the same size as `a`. vals (array-like): 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. Examples -------- >>> arr = np.arange(6).reshape(2, 3) >>> np.place(arr, arr>2, [44, 55]) >>> arr array([[ 0, 1, 2], [44, 55, 44]]) .. warning:: This function may synchronize the device. .. seealso:: :func:`numpy.place` """ # TODO(niboshi): Avoid nonzero which may synchronize the device. mask = cupy.asarray(mask) if arr.size != mask.size: raise ValueError('Mask and data must be the same size.') vals = cupy.asarray(vals) mask_indices = mask.ravel().nonzero()[0] # may synchronize if mask_indices.size == 0: return if vals.size == 0: raise ValueError('Cannot insert from an empty array.') arr.put(mask_indices, vals, mode='wrap')
Example 19
Project: emotion-classification Author: rishirdua File: setdiag0.py License: MIT License | 5 votes |
def setdiag0(K): """Set the diagonal entries of a square matrix to 0 """ n = K.shape[0] numpy.put(K, numpy.arange(n) * (n + 1), 0.0)
Example 20
Project: highway-env Author: eleurent File: lane_keeping_env.py License: MIT License | 5 votes |
def store_data(self) -> None: if self.lpv: state = self.vehicle.state.copy() interval = [] for x_t in self.lpv.change_coordinates(self.lpv.x_i_t, back=True, interval=True): # lateral state to full state np.put(state, [1, 2, 4, 5], x_t) # full state to absolute coordinates interval.append(state.squeeze(-1).copy()) self.interval_trajectory.append(interval) self.trajectory.append(copy.deepcopy(self.vehicle.state))
Example 21
Project: ClimateVegetationDynamics_GrangerCausality Author: h-cel File: GC_script.py License: GNU General Public License v3.0 | 5 votes |
def shift2(arr,num): arr=np.roll(arr,num) if num < 0: np.put(arr,range(len(arr)+num,len(arr)),np.nan) elif num > 0: np.put(arr,range(num),np.nan) return arr #parameters: string 'inpath' which is the path of the .csv dataset #returns: the dataset in a numpy array
Example 22
Project: bluesky Author: TUDelft-CNS-ATM File: ssd.py License: GNU General Public License v3.0 | 5 votes |
def qdrdist_matrix_indices(self, ntraf): """ This function gives the indices that can be used in the lon/lat-vectors """ # The indices will be n*(n-1)/2 long # Only works for n >= 2, which is logical... # This is faster than np.triu_indices :) tmp_range = np.arange(ntraf - 1, dtype=np.int32) ind1 = np.repeat(tmp_range, (tmp_range + 1)[::-1]) ind2 = np.ones(ind1.shape[0], dtype=np.int32) inds = np.cumsum(tmp_range[1:][::-1] + 1) np.put(ind2, inds, np.arange(ntraf * -1 + 3, 1)) ind2 = np.cumsum(ind2, out=ind2) return ind1, ind2
Example 23
Project: Splunking-Crime Author: nccgroup File: rcv1.py License: GNU Affero General Public License v3.0 | 5 votes |
def _inverse_permutation(p): """inverse permutation p""" n = p.size s = np.zeros(n, dtype=np.int32) i = np.arange(n, dtype=np.int32) np.put(s, p, i) # s[p] = i return s
Example 24
Project: alibi Author: SeldonIO File: test_cfproto.py License: Apache License 2.0 | 5 votes |
def test_tf_keras_iris_explainer(tf_keras_iris_explainer, use_kdtree, k): X_train, model, cf = tf_keras_iris_explainer # instance to be explained x = X_train[0].reshape(1, -1) pred_class = np.argmax(model.predict(x)) not_pred_class = np.argmin(model.predict(x)) # test fit cf.fit(X_train) if use_kdtree: # k-d trees assert len(cf.kdtrees) == cf.classes # each class has a k-d tree n_by_class = 0 for c in range(cf.classes): n_by_class += cf.X_by_class[c].shape[0] assert n_by_class == X_train.shape[0] # all training instances are stored in the trees assert cf.kdtrees[pred_class].query(x, k=1)[0] == 0. # nearest distance to own class equals 0 assert cf.score(x, not_pred_class, pred_class) == 0. # test score fn else: # encoder assert len(list(cf.class_proto.keys())) == cf.classes assert [True for _ in range(cf.classes)] == [v.shape == (1, 2) for _, v in cf.class_proto.items()] n_by_class = 0 for c in range(cf.classes): n_by_class += cf.class_enc[c].shape[0] assert n_by_class == X_train.shape[0] # all training instances encoded # test explanation explanation = cf.explain(x, k=k) assert cf.id_proto != pred_class assert np.argmax(model.predict(explanation.cf['X'])) == explanation.cf['class'] assert explanation.cf['grads_num'].shape == explanation.cf['grads_graph'].shape == x.shape assert explanation.meta.keys() == DEFAULT_META_CFP.keys() assert explanation.data.keys() == DEFAULT_DATA_CFP.keys() # test gradient shapes y = np.zeros((1, cf.classes)) np.put(y, pred_class, 1) cf.predict = cf.predict.predict # make model black box grads = cf.get_gradients(x, y, x.shape[1:]) assert grads.shape == x.shape
Example 25
Project: polara Author: evfro File: mmlwrapper.py License: MIT License | 5 votes |
def _remap_factors(entity_mapping, entity_factors, num_entities, num_factors): shape = (num_entities, num_factors) entity_id = np.repeat(entity_mapping.loc[:, 1].values, num_factors, axis=0).astype(np.int64) factor_id = entity_factors['col2'].values.astype(np.int64) entity_factors_idx = np.ravel_multi_index((entity_id, factor_id), dims=shape) entity_factors_new = np.zeros(shape) np.put(entity_factors_new, entity_factors_idx, entity_factors['col3'].values) return entity_factors_new
Example 26
Project: graspy Author: neurodata File: test_io.py License: Apache License 2.0 | 5 votes |
def setup_class(cls): # simple ERxN graph n = 15 p = 0.5 cls.A = np.zeros((n, n)) nedge = int(round(n * n * p)) np.put( cls.A, np.random.choice(np.arange(0, n * n), size=nedge, replace=False), np.random.normal(size=nedge), )
Example 27
Project: graspy Author: neurodata File: test_utils.py License: Apache License 2.0 | 5 votes |
def setUpClass(cls): # simple ERxN graph n = 15 p = 0.5 cls.A = np.zeros((n, n)) nedge = int(round(n * n * p)) np.put( cls.A, np.random.choice(np.arange(0, n * n), size=nedge, replace=False), np.random.normal(size=nedge), )
Example 28
Project: graspy Author: neurodata File: test_utils.py License: Apache License 2.0 | 5 votes |
def setUpClass(cls): # simple ERxN graph n = 15 p = 0.5 cls.A = np.zeros((n, n)) nedge = int(round(n * n * p)) np.put( cls.A, np.random.choice(np.arange(0, n * n), size=nedge, replace=False), np.random.normal(size=nedge), )
Example 29
Project: numdifftools Author: pbrod File: limits.py License: BSD 3-Clause "New" or "Revised" License | 5 votes |
def _call_lim(self, fz, z, f): err = np.zeros_like(fz) final_step = np.zeros_like(fz) index = np.zeros_like(fz, dtype=int) k = np.flatnonzero(np.isnan(fz)) if k.size > 0: fz = np.where(np.isnan(fz), 0, fz) lim_fz, info1 = self._lim(f, z.flat[k]) np.put(fz, k, lim_fz) if self.full_output: np.put(final_step, k, info1.final_step) np.put(index, k, info1.index) np.put(err, k, info1.error_estimate) return fz, self.info(err, final_step, index)
Example 30
Project: Stock-Trading-Visualization Author: notadamking File: StockTradingEnv.py License: MIT License | 5 votes |
def _next_observation(self): frame = np.zeros((5, LOOKBACK_WINDOW_SIZE + 1)) # Get the stock data points for the last 5 days and scale to between 0-1 np.put(frame, [0, 4], [ self.df.loc[self.current_step: self.current_step + LOOKBACK_WINDOW_SIZE, 'Open'].values / MAX_SHARE_PRICE, self.df.loc[self.current_step: self.current_step + LOOKBACK_WINDOW_SIZE, 'High'].values / MAX_SHARE_PRICE, self.df.loc[self.current_step: self.current_step + LOOKBACK_WINDOW_SIZE, 'Low'].values / MAX_SHARE_PRICE, self.df.loc[self.current_step: self.current_step + LOOKBACK_WINDOW_SIZE, 'Close'].values / MAX_SHARE_PRICE, self.df.loc[self.current_step: self.current_step + LOOKBACK_WINDOW_SIZE, 'Volume'].values / MAX_NUM_SHARES, ]) # Append additional data and scale each value to between 0-1 obs = np.append(frame, [ [self.balance / MAX_ACCOUNT_BALANCE], [self.max_net_worth / MAX_ACCOUNT_BALANCE], [self.shares_held / MAX_NUM_SHARES], [self.cost_basis / MAX_SHARE_PRICE], [self.total_sales_value / (MAX_NUM_SHARES * MAX_SHARE_PRICE)], ], axis=1) return obs