Python numpy.mean() Examples
The following are 30
code examples of numpy.mean().
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: utils.py From nmp_qc with MIT License | 8 votes |
def get_graph_stats(graph_obj_handle, prop='degrees'): # if prop == 'degrees': num_cores = multiprocessing.cpu_count() inputs = [int(i*len(graph_obj_handle)/num_cores) for i in range(num_cores)] + [len(graph_obj_handle)] res = Parallel(n_jobs=num_cores)(delayed(get_values)(graph_obj_handle, inputs[i], inputs[i+1], prop) for i in range(num_cores)) stat_dict = {} if 'degrees' in prop: stat_dict['degrees'] = list(set([d for core_res in res for file_res in core_res for d in file_res['degrees']])) if 'edge_labels' in prop: stat_dict['edge_labels'] = list(set([d for core_res in res for file_res in core_res for d in file_res['edge_labels']])) if 'target_mean' in prop or 'target_std' in prop: param = np.array([file_res['params'] for core_res in res for file_res in core_res]) if 'target_mean' in prop: stat_dict['target_mean'] = np.mean(param, axis=0) if 'target_std' in prop: stat_dict['target_std'] = np.std(param, axis=0) return stat_dict
Example #2
Source File: insertsizes.py From svviz with MIT License | 6 votes |
def __init__(self, bam, keepReads=False): self.insertSizes = [] self.readLengths = [] self.orientations = [] self._insertSizeKDE = None self.singleEnded = False self._insertSizeScores = {} # cache try: self.insertSizes, self.reads, self.orientations, self.readLengths = sampleInsertSizes(bam, keepReads=keepReads) if len(self.insertSizes) > 1: logging.info(" insert size mean: {:.2f} std: {:.2f}".format(numpy.mean(self.insertSizes), numpy.std(self.insertSizes))) except ValueError as e: print("*"*100, "here") print("ERROR:", e)
Example #3
Source File: iterated_maximum_subarray.py From EDeN with MIT License | 6 votes |
def extract_sequence_and_score(graph=None): # make dict with positions as keys and lists of ids as values pos_to_ids = defaultdict(list) for u in graph.nodes(): if 'position' not in graph.node[u]: # no position attributes in graph, use the vertex id instead raise Exception('Missing "position" attribute in node:%s %s' % (u, graph.node[u])) else: pos = graph.node[u]['position'] # accumulate all node ids pos_to_ids[pos] += [u] # extract sequence of labels and importances seq = [None] * len(pos_to_ids) score = [0] * len(pos_to_ids) for pos in sorted(pos_to_ids): ids = pos_to_ids[pos] labels = [graph.node[u].get('label', 'N/A') for u in ids] # check that all labels for the same position are identical assert(sum([1 for label in labels if label == labels[0]]) == len(labels) ), 'ERROR: non identical labels referring to same position: %s %s' % (pos, labels) seq[pos] = labels[0] # average all importance score for the same position importances = [graph.node[u].get('importance', 0) for u in ids] score[pos] = np.mean(importances) return seq, score
Example #4
Source File: experiment.py From Neural-LP with MIT License | 6 votes |
def train(self): while (self.epoch < self.option.max_epoch and not self.early_stopped): self.one_epoch_train() self.one_epoch_valid() self.one_epoch_test() self.epoch += 1 model_path = self.saver.save(self.sess, self.option.model_path, global_step=self.epoch) print("Model saved at %s" % model_path) if self.early_stop(): self.early_stopped = True print("Early stopped at epoch %d" % (self.epoch)) all_test_in_top = [np.mean(x[1]) for x in self.test_stats] best_test_epoch = np.argmax(all_test_in_top) best_test = all_test_in_top[best_test_epoch] msg = "Best test in top: %0.4f at epoch %d." % (best_test, best_test_epoch + 1) print(msg) self.log_file.write(msg + "\n") pickle.dump([self.train_stats, self.valid_stats, self.test_stats], open(os.path.join(self.option.this_expsdir, "results.pckl"), "w"))
Example #5
Source File: bayestar.py From dustmaps with GNU General Public License v2.0 | 6 votes |
def _raise_on_mode(self, mode): """ Checks that the provided query mode is one of the accepted values. If not, raises a :obj:`ValueError`. """ valid_modes = [ 'random_sample', 'random_sample_per_pix', 'samples', 'median', 'mean', 'best', 'percentile'] if mode not in valid_modes: raise ValueError( '"{}" is not a valid `mode`. Valid modes are:\n' ' {}'.format(mode, valid_modes) )
Example #6
Source File: element.py From StructEngPy with MIT License | 6 votes |
def __init__(self,node_i, node_j, node_k, node_l,t, E, mu, rho, name=None): #8-nodes self.__nodes.append(node_i) self.__nodes.append(node_j) self.__nodes.append(node_k) self.__nodes.append(node_l) self.__t=t center=np.mean([node_i,node_j,node_k,node_l]) # self.local_csys = CoordinateSystem.cartisian(center,nodes[4],nodes[5]) self.__alpha=[]#the angle between edge and local-x, to be added self.__alpha.append(self.angle(node_i,node_j,self.local_csys.x)) self.__alpha.append(self.angle(node_j,node_k,self.local_csys.x)) self.__alpha.append(self.angle(node_k,node_l,self.local_csys.x)) self.__alpha.append(self.angle(node_l,node_i,self.local_csys.x)) self.__K=np.zeros((24,24))
Example #7
Source File: dynamic_roi_head.py From mmdetection with Apache License 2.0 | 6 votes |
def _bbox_forward_train(self, x, sampling_results, gt_bboxes, gt_labels, img_metas): num_imgs = len(img_metas) rois = bbox2roi([res.bboxes for res in sampling_results]) bbox_results = self._bbox_forward(x, rois) bbox_targets = self.bbox_head.get_targets(sampling_results, gt_bboxes, gt_labels, self.train_cfg) # record the `beta_topk`-th smallest target # `bbox_targets[2]` and `bbox_targets[3]` stand for bbox_targets # and bbox_weights, respectively pos_inds = bbox_targets[3][:, 0].nonzero().squeeze(1) num_pos = len(pos_inds) cur_target = bbox_targets[2][pos_inds, :2].abs().mean(dim=1) beta_topk = min(self.train_cfg.dynamic_rcnn.beta_topk * num_imgs, num_pos) cur_target = torch.kthvalue(cur_target, beta_topk)[0].item() self.beta_history.append(cur_target) loss_bbox = self.bbox_head.loss(bbox_results['cls_score'], bbox_results['bbox_pred'], rois, *bbox_targets) bbox_results.update(loss_bbox=loss_bbox) return bbox_results
Example #8
Source File: dynamic_roi_head.py From mmdetection with Apache License 2.0 | 6 votes |
def update_hyperparameters(self): """Update hyperparameters like IoU thresholds for assigner and beta for SmoothL1 loss based on the training statistics. Returns: tuple[float]: the updated ``iou_thr`` and ``beta``. """ new_iou_thr = max(self.train_cfg.dynamic_rcnn.initial_iou, np.mean(self.iou_history)) self.iou_history = [] self.bbox_assigner.pos_iou_thr = new_iou_thr self.bbox_assigner.neg_iou_thr = new_iou_thr self.bbox_assigner.min_pos_iou = new_iou_thr new_beta = min(self.train_cfg.dynamic_rcnn.initial_beta, np.median(self.beta_history)) self.beta_history = [] self.bbox_head.loss_bbox.beta = new_beta return new_iou_thr, new_beta
Example #9
Source File: coco.py From mmdetection with Apache License 2.0 | 6 votes |
def fast_eval_recall(self, results, proposal_nums, iou_thrs, logger=None): gt_bboxes = [] for i in range(len(self.img_ids)): ann_ids = self.coco.get_ann_ids(img_ids=self.img_ids[i]) ann_info = self.coco.load_anns(ann_ids) if len(ann_info) == 0: gt_bboxes.append(np.zeros((0, 4))) continue bboxes = [] for ann in ann_info: if ann.get('ignore', False) or ann['iscrowd']: continue x1, y1, w, h = ann['bbox'] bboxes.append([x1, y1, x1 + w, y1 + h]) bboxes = np.array(bboxes, dtype=np.float32) if bboxes.shape[0] == 0: bboxes = np.zeros((0, 4)) gt_bboxes.append(bboxes) recalls = eval_recalls( gt_bboxes, results, proposal_nums, iou_thrs, logger=logger) ar = recalls.mean(axis=1) return ar
Example #10
Source File: analyze_logs.py From mmdetection with Apache License 2.0 | 6 votes |
def cal_train_time(log_dicts, args): for i, log_dict in enumerate(log_dicts): print(f'{"-" * 5}Analyze train time of {args.json_logs[i]}{"-" * 5}') all_times = [] for epoch in log_dict.keys(): if args.include_outliers: all_times.append(log_dict[epoch]['time']) else: all_times.append(log_dict[epoch]['time'][1:]) all_times = np.array(all_times) epoch_ave_time = all_times.mean(-1) slowest_epoch = epoch_ave_time.argmax() fastest_epoch = epoch_ave_time.argmin() std_over_epoch = epoch_ave_time.std() print(f'slowest epoch {slowest_epoch + 1}, ' f'average time is {epoch_ave_time[slowest_epoch]:.4f}') print(f'fastest epoch {fastest_epoch + 1}, ' f'average time is {epoch_ave_time[fastest_epoch]:.4f}') print(f'time std over epochs is {std_over_epoch:.4f}') print(f'average iter time: {np.mean(all_times):.4f} s/iter') print()
Example #11
Source File: test_attacks.py From neural-fingerprinting with BSD 3-Clause "New" or "Revised" License | 6 votes |
def test_generate_np_targeted_gives_adversarial_example(self): x_val = np.random.rand(100, 2) x_val = np.array(x_val, dtype=np.float32) feed_labs = np.zeros((100, 2)) feed_labs[np.arange(100), np.random.randint(0, 1, 100)] = 1 x_adv = self.attack.generate_np(x_val, max_iterations=100, binary_search_steps=3, initial_const=1, clip_min=-5, clip_max=5, batch_size=100, y_target=feed_labs) new_labs = np.argmax(self.sess.run(self.model(x_adv)), axis=1) self.assertTrue(np.mean(np.argmax(feed_labs, axis=1) == new_labs) > 0.9)
Example #12
Source File: test_attacks.py From neural-fingerprinting with BSD 3-Clause "New" or "Revised" License | 6 votes |
def test_generate_gives_adversarial_example(self): x_val = np.random.rand(100, 2) x_val = np.array(x_val, dtype=np.float32) orig_labs = np.argmax(self.sess.run(self.model(x_val)), axis=1) feed_labs = np.zeros((100, 2)) feed_labs[np.arange(100), orig_labs] = 1 x = tf.placeholder(tf.float32, x_val.shape) y = tf.placeholder(tf.float32, feed_labs.shape) x_adv_p = self.attack.generate(x, max_iterations=100, binary_search_steps=3, initial_const=1, clip_min=-5, clip_max=5, batch_size=100, y=y) self.assertEqual(x_val.shape, x_adv_p.shape) x_adv = self.sess.run(x_adv_p, {x: x_val, y: feed_labs}) new_labs = np.argmax(self.sess.run(self.model(x_adv)), axis=1) self.assertTrue(np.mean(orig_labs == new_labs) < 0.1)
Example #13
Source File: test_attacks.py From neural-fingerprinting with BSD 3-Clause "New" or "Revised" License | 6 votes |
def test_generate_np_targeted_gives_adversarial_example(self): x_val = np.random.rand(100, 2) x_val = np.array(x_val, dtype=np.float32) feed_labs = np.zeros((100, 2)) feed_labs[np.arange(100), np.random.randint(0, 1, 100)] = 1 x_adv = self.attack.generate_np(x_val, max_iterations=100, binary_search_steps=3, initial_const=1, clip_min=-5, clip_max=5, batch_size=100, y_target=feed_labs) new_labs = np.argmax(self.sess.run(self.model(x_adv)), axis=1) self.assertTrue(np.mean(np.argmax(feed_labs, axis=1) == new_labs) > 0.9)
Example #14
Source File: test_attacks.py From neural-fingerprinting with BSD 3-Clause "New" or "Revised" License | 6 votes |
def test_generate_gives_adversarial_example(self): x_val = np.random.rand(100, 2) x_val = np.array(x_val, dtype=np.float32) orig_labs = np.argmax(self.sess.run(self.model(x_val)), axis=1) feed_labs = np.zeros((100, 2)) feed_labs[np.arange(100), orig_labs] = 1 x = tf.placeholder(tf.float32, x_val.shape) y = tf.placeholder(tf.float32, feed_labs.shape) x_adv_p = self.attack.generate(x, max_iterations=100, binary_search_steps=3, initial_const=1, clip_min=-5, clip_max=5, batch_size=100, y=y) self.assertEqual(x_val.shape, x_adv_p.shape) x_adv = self.sess.run(x_adv_p, {x: x_val, y: feed_labs}) new_labs = np.argmax(self.sess.run(self.model(x_adv)), axis=1) self.assertTrue(np.mean(orig_labs == new_labs) < 0.1)
Example #15
Source File: test_attacks.py From neural-fingerprinting with BSD 3-Clause "New" or "Revised" License | 6 votes |
def test_generate_gives_adversarial_example(self): x_val = np.random.rand(100, 2) x_val = np.array(x_val, dtype=np.float32) orig_labs = np.argmax(self.sess.run(self.model(x_val)), axis=1) x = tf.placeholder(tf.float32, x_val.shape) x_adv_p = self.attack.generate(x, over_shoot=0.02, max_iter=50, nb_candidate=2, clip_min=-5, clip_max=5) self.assertEqual(x_val.shape, x_adv_p.shape) x_adv = self.sess.run(x_adv_p, {x: x_val}) new_labs = np.argmax(self.sess.run(self.model(x_adv)), axis=1) self.assertTrue(np.mean(orig_labs == new_labs) < 0.1)
Example #16
Source File: test_attacks.py From neural-fingerprinting with BSD 3-Clause "New" or "Revised" License | 6 votes |
def test_attack_strength(self): """ If clipping is not done at each iteration (not using clip_min and clip_max), this attack fails by np.mean(orig_labels == new_labels) == .5 """ x_val = np.random.rand(100, 2) x_val = np.array(x_val, dtype=np.float32) x_adv = self.attack.generate_np(x_val, eps=1.0, eps_iter=0.05, clip_min=0.5, clip_max=0.7, nb_iter=5) orig_labs = np.argmax(self.sess.run(self.model(x_val)), axis=1) new_labs = np.argmax(self.sess.run(self.model(x_adv)), axis=1) self.assertTrue(np.mean(orig_labs == new_labs) < 0.1)
Example #17
Source File: test_attacks.py From neural-fingerprinting with BSD 3-Clause "New" or "Revised" License | 6 votes |
def test_generate_np_targeted_gives_adversarial_example(self): x_val = np.random.rand(100, 2) x_val = np.array(x_val, dtype=np.float32) feed_labs = np.zeros((100, 2)) feed_labs[np.arange(100), np.random.randint(0, 1, 100)] = 1 x_adv = self.attack.generate_np(x_val, max_iterations=100, binary_search_steps=3, initial_const=1, clip_min=-5, clip_max=5, batch_size=100, y_target=feed_labs) new_labs = np.argmax(self.sess.run(self.model(x_adv)), axis=1) self.assertTrue(np.mean(np.argmax(feed_labs, axis=1) == new_labs) > 0.9)
Example #18
Source File: layers.py From deep-learning-note with MIT License | 6 votes |
def __forward(self, x, train_flg): if self.running_mean is None: N, D = x.shape self.running_mean = np.zeros(D) self.running_var = np.zeros(D) if train_flg: mu = x.mean(axis=0) xc = x - mu var = np.mean(xc ** 2, axis=0) std = np.sqrt(var + 10e-7) xn = xc / std self.batch_size = x.shape[0] self.xc = xc self.xn = xn self.std = std self.running_mean = self.momentum * self.running_mean + (1 - self.momentum) * mu self.running_var = self.momentum * self.running_var + (1 - self.momentum) * var else: xc = x - self.running_mean xn = xc / ((np.sqrt(self.running_var + 10e-7))) out = self.gamma * xn + self.beta return out
Example #19
Source File: forest-fire.py From unicorn-hat-hd with MIT License | 5 votes |
def average_forest(forest): avg_forest = [[space for x in range(width)] for y in range(height)] for i, x in enumerate(range(1, forest_width, scale)): for j, y in enumerate(range(1, forest_height, scale)): neighbours = get_neighbours(x, y, avg_size) red = int(numpy.mean([forest[n[0]][n[1]][0] for n in neighbours])) green = int(numpy.mean([forest[n[0]][n[1]][1] for n in neighbours])) blue = int(numpy.mean([forest[n[0]][n[1]][2] for n in neighbours])) avg_forest[i][j] = [red, green, blue] return avg_forest
Example #20
Source File: summarystats.py From svviz with MIT License | 5 votes |
def addVariantResults(self, dataHub): variant = str(dataHub.variant) for sampleName, sample in dataHub.samples.items(): counts = collections.Counter() reasons = {} alnScores = collections.defaultdict(list) insertSizes = collections.defaultdict(list) # collect stats for alnCollection in sample.alnCollections: allele = alnCollection.choice counts[allele] += 1 if not allele in reasons: reasons[allele] = collections.Counter() reasons[allele][alnCollection.why] += 1 alnScores[allele].append(sum(aln.score for aln in alnCollection.chosenSet().getAlignments())) insertSizes[allele].append(len(alnCollection.chosenSet())) # record stats for allele, count in counts.items(): self.stats.append([variant, sampleName, allele, "count", count]) for allele in reasons: for reason in reasons[allele]: self.stats.append([variant, sampleName, allele, "reason_{}".format(reason), reasons[allele][reason]]) for allele in alnScores: self.stats.append([variant, sampleName, allele, "alnScore_mean", numpy.mean(alnScores[allele])]) self.stats.append([variant, sampleName, allele, "alnScore_std", numpy.std(alnScores[allele])]) for allele in insertSizes: self.stats.append([variant, sampleName, allele, "insertSize_mean", numpy.mean(insertSizes[allele])]) self.stats.append([variant, sampleName, allele, "insertSize_std", numpy.std(insertSizes[allele])])
Example #21
Source File: insertsizes.py From svviz with MIT License | 5 votes |
def meanInsertSize(self): if self.hasInsertSizeDistribution(): return mean(self.insertSizes) return None
Example #22
Source File: insertsizes.py From svviz with MIT License | 5 votes |
def meanReadLength(self): if self.hasReadLengthDistribution(): return mean(self.readLengths) return None
Example #23
Source File: tcpr.py From libTLDA with MIT License | 5 votes |
def risk(self, Z, theta, q): """ Compute target contrastive pessimistic risk. Parameters ---------- Z : array target samples (M samples by D features) theta : array classifier parameters (D features by K classes) q : array soft labels (M samples by K classes) Returns ------- float Value of risk function. """ # Number of classes K = q.shape[1] # Compute negative log-likelihood L = self.neg_log_likelihood(Z, theta) # Weight loss by soft labels for k in range(K): L[:, k] *= q[:, k] # Sum over weighted losses L = np.sum(L, axis=1) # Risk is average loss return np.mean(L, axis=0)
Example #24
Source File: tcpr.py From libTLDA with MIT License | 5 votes |
def error_rate(self, preds, u_): """Compute classification error rate.""" return np.mean(preds != u_, axis=0)
Example #25
Source File: suba.py From libTLDA with MIT License | 5 votes |
def reg_cov(self, X): """ Regularize covariance matrix until non-singular. Parameters ---------- C : array square symmetric covariance matrix. Returns ------- C : array regularized covariance matrix. """ # Compute mean of data muX = np.mean(X, axis=0, keepdims=1) # Compute covariance matrix without regularization SX = np.cov((X - muX).T) # Initialize regularization parameter reg = 1e-6 # Keep going until non-singular while not self.is_pos_def(SX): # Compute covariance matrix with regularization SX = np.cov((X - muX).T) + reg*np.eye(X.shape[1]) # Increment reg reg *= 10 # Report regularization print('Final regularization parameter = {}'.format(reg)) return SX
Example #26
Source File: suba.py From libTLDA with MIT License | 5 votes |
def score(self, Z, U, zscore=False): """ Compute classification error on test set. Parameters ---------- Z : array new data set (M samples x D features) zscore : boolean whether to transform the data using z-scoring (def: false) Returns ------- preds : array label predictions (M samples x 1) """ # If classifier is trained, check for same dimensionality if self.is_trained: if not self.train_data_dim == Z.shape[1]: raise ValueError("""Test data is of different dimensionality than training data.""") # Make predictions preds = self.predict(Z, zscore=zscore) # Compute error return np.mean(preds != U)
Example #27
Source File: suba.py From libTLDA with MIT License | 5 votes |
def reg_cov(self, X): """ Regularize covariance matrix until non-singular. Parameters ---------- C : array square symmetric covariance matrix. Returns ------- C : array regularized covariance matrix. """ # Number of data points N = X.shape[0] # Compute mean of data muX = np.mean(X, axis=0, keepdims=1) # Compute covariance matrix without regularization SX = np.dot((X - muX).T, (X - muX)) / N # Initialize regularization parameter reg = 1e-6 # Keep going until non-singular while not self.is_pos_def(SX): # Compute covariance matrix with regularization SX = np.dot((X - muX).T, (X - muX)) / N + reg*np.eye(X.shape[1]) # Increment reg reg *= 10 # Report regularization print('Final regularization parameter = {}'.format(reg)) return SX
Example #28
Source File: __init__.py From EDeN with MIT License | 5 votes |
def heatmap(values, xlabel, ylabel, xticklabels, yticklabels, cmap=None, vmin=None, vmax=None, ax=None, fmt="%0.2f"): """heatmap.""" if ax is None: ax = plt.gca() # plot the mean cross-validation scores img = ax.pcolor(values, cmap=cmap, vmin=vmin, vmax=vmax) img.update_scalarmappable() ax.set_xlabel(xlabel) ax.set_ylabel(ylabel) ax.set_xticks(np.arange(len(xticklabels)) + .5) ax.set_yticks(np.arange(len(yticklabels)) + .5) ax.set_xticklabels(xticklabels) ax.set_yticklabels(yticklabels) ax.set_aspect(1) for p, color, value in zip(img.get_paths(), img.get_facecolors(), img.get_array()): x, y = p.vertices[:-2, :].mean(0) if np.mean(color[:3]) > 0.5: c = 'k' else: c = 'w' ax.text(x, y, fmt % value, color=c, ha="center", va="center") return img
Example #29
Source File: estimator.py From EDeN with MIT License | 5 votes |
def bias_variance_decomposition(self, graphs, targets, cv=5, n_bootstraps=10): """bias_variance_decomposition.""" x = self.transform(graphs) score_list = [] for i in range(n_bootstraps): scores = cross_val_score( self.model, x, targets, cv=cv) score_list.append(scores) score_list = np.array(score_list) mean_scores = np.mean(score_list, axis=1) std_scores = np.std(score_list, axis=1) return mean_scores, std_scores
Example #30
Source File: estimator.py From EDeN with MIT License | 5 votes |
def _eval_params(graphs, targets, param_distr): # sample parameters params = _sample_params(param_distr) # create model with those parameters est = EdenEstimator(**params) # run a cross_val_score scores = est.cross_val_score(graphs, targets) # return average return np.mean(scores), params