Python numpy.histogram() Examples
The following are 30
code examples of numpy.histogram().
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 Semantic-Aware-Scene-Recognition with MIT License | 6 votes |
def intersectionAndUnion(imPred, imLab, numClass=150): """ Computes the intersection and Union for all the classes between two images :param imPred: Predictions image :param imLab: Ground-truth image :param numClass: Number of semantic classes. Default:150 :return: Intersection and union for all the classes """ # Remove classes from unlabeled pixels in gt image. # We should not penalize detections in unlabeled portions of the image. imPred = imPred * (imLab > 0).long() # Compute area intersection: intersection = imPred * (imPred == imLab).long() (area_intersection, _) = np.histogram(intersection, bins=numClass, range=(1, numClass)) # Compute area union: (area_pred, _) = np.histogram(imPred, bins=numClass, range=(1, numClass)) (area_lab, _) = np.histogram(imLab, bins=numClass, range=(1, numClass)) area_union = area_pred + area_lab - area_intersection IoU = area_intersection / (area_union + 1e-10) return IoU
Example #2
Source File: pytorch_ext.py From L3C-PyTorch with GNU General Public License v3.0 | 6 votes |
def histogram(t, L): """ A: If t is a list of tensors/np.ndarrays, B is executed for all, yielding len(ts) histograms, which are summed per bin B: convert t to numpy, count bins. :param t: tensor or list of tensor, each expected to be in [0, L) :param L: number of symbols :return: length-L array, containing at l the number of values mapping to to symbol l """ if isinstance(t, list): ts = t histograms = np.stack((histogram(t, L) for t in ts), axis=0) # get array (len(ts) x L) return np.sum(histograms, 0) assert 0 <= t.min() and t.max() < L, (t.min(), t.max()) a = tensor_to_np(t) counts, _ = np.histogram(a, np.arange(L+1)) # +1 because np.histogram takes bin edges, including rightmost edge return counts # Gradients --------------------------------------------------------------------
Example #3
Source File: histograms.py From recruit with Apache License 2.0 | 6 votes |
def _hist_bin_scott(x, range): """ Scott histogram bin estimator. The binwidth is proportional to the standard deviation of the data and inversely proportional to the cube root of data size (asymptotically optimal). Parameters ---------- x : array_like Input data that is to be histogrammed, trimmed to range. May not be empty. Returns ------- h : An estimate of the optimal bin width for the given data. """ del range # unused return (24.0 * np.pi**0.5 / x.size)**(1.0 / 3.0) * np.std(x)
Example #4
Source File: strhist.py From Depth-Map-Prediction with GNU General Public License v3.0 | 6 votes |
def hist_chars(x, m=None, M=None, width=50): ''' Prints a one-line histogram with one char per bin. The bin count is quantized into only a few values and scaled to create a visual representation. Min and max values are displayed on the ends. ''' (h, hbins, m, M) = _gethist(x, width, m, M) nchars = len(_strhist_chars) if np.any(h > 0): hmin = np.min(h) hmax = np.max(h) hchar = np.round((nchars-1)*(h - hmin)/(hmax - hmin)) hstr = ''.join([_strhist_chars[int(i)] for i in hchar]) else: hstr = ' ' * width return '% .5f |%s| %.5f' % (m, hstr, M)
Example #5
Source File: histograms.py From recruit with Apache License 2.0 | 6 votes |
def _hist_bin_rice(x, range): """ Rice histogram bin estimator. Another simple estimator with no normality assumption. It has better performance for large data than Sturges, but tends to overestimate the number of bins. The number of bins is proportional to the cube root of data size (asymptotically optimal). The estimate depends only on size of the data. Parameters ---------- x : array_like Input data that is to be histogrammed, trimmed to range. May not be empty. Returns ------- h : An estimate of the optimal bin width for the given data. """ del range # unused return x.ptp() / (2.0 * x.size ** (1.0 / 3))
Example #6
Source File: test_histograms.py From recruit with Apache License 2.0 | 6 votes |
def test_outliers(self): # Check that outliers are not tallied a = np.arange(10) + .5 # Lower outliers h, b = histogram(a, range=[0, 9]) assert_equal(h.sum(), 9) # Upper outliers h, b = histogram(a, range=[1, 10]) assert_equal(h.sum(), 9) # Normalization h, b = histogram(a, range=[1, 9], density=True) assert_almost_equal((h * np.diff(b)).sum(), 1, decimal=15) # Weights w = np.arange(10) + .5 h, b = histogram(a, range=[1, 9], weights=w, density=True) assert_equal((h * np.diff(b)).sum(), 1) h, b = histogram(a, bins=8, range=[1, 9], weights=w) assert_equal(h, w[1:-1])
Example #7
Source File: histograms.py From recruit with Apache License 2.0 | 6 votes |
def _hist_bin_sturges(x, range): """ Sturges histogram bin estimator. A very simplistic estimator based on the assumption of normality of the data. This estimator has poor performance for non-normal data, which becomes especially obvious for large data sets. The estimate depends only on size of the data. Parameters ---------- x : array_like Input data that is to be histogrammed, trimmed to range. May not be empty. Returns ------- h : An estimate of the optimal bin width for the given data. """ del range # unused return x.ptp() / (np.log2(x.size) + 1.0)
Example #8
Source File: constraint.py From scarlet with MIT License | 6 votes |
def threshold(self, morph): """Find the threshold value for a given morphology """ _morph = morph[morph > 0] _bins = 50 # Decrease the bin size for sources with a small number of pixels if _morph.size < 500: _bins = max(np.int(_morph.size / 10), 1) if _bins == 1: return 0, _bins hist, bins = np.histogram(np.log10(_morph).reshape(-1), _bins) cutoff = np.where(hist == 0)[0] # If all of the pixels are used there is no need to threshold if len(cutoff) == 0: return 0, _bins return 10 ** bins[cutoff[-1]], _bins
Example #9
Source File: test_histograms.py From recruit with Apache License 2.0 | 6 votes |
def test_bool_conversion(self): # gh-12107 # Reference integer histogram a = np.array([1, 1, 0], dtype=np.uint8) int_hist, int_edges = np.histogram(a) # Should raise an warning on booleans # Ensure that the histograms are equivalent, need to suppress # the warnings to get the actual outputs with suppress_warnings() as sup: rec = sup.record(RuntimeWarning, 'Converting input from .*') hist, edges = np.histogram([True, True, False]) # A warning should be issued assert_equal(len(rec), 1) assert_array_equal(hist, int_hist) assert_array_equal(edges, int_edges)
Example #10
Source File: histograms.py From recruit with Apache License 2.0 | 6 votes |
def _hist_bin_sqrt(x, range): """ Square root histogram bin estimator. Bin width is inversely proportional to the data size. Used by many programs for its simplicity. Parameters ---------- x : array_like Input data that is to be histogrammed, trimmed to range. May not be empty. Returns ------- h : An estimate of the optimal bin width for the given data. """ del range # unused return x.ptp() / np.sqrt(x.size)
Example #11
Source File: test_KeplerLike1.py From EXOSIMS with BSD 3-Clause "New" or "Revised" License | 6 votes |
def test_gen_radius(self): r"""Test gen_radius method. Approach: Ensures the output is set, of the correct type, length, and units. Check distributional agreement. """ plan_pop = self.fixture n = 10000 radii = plan_pop.gen_radius(n) # ensure the units are length self.assertEqual((radii/u.km).decompose().unit, u.dimensionless_unscaled) # radius > 0 self.assertTrue(np.all(radii.value > 0)) self.assertTrue(np.all(np.isfinite(radii.value))) h = np.histogram(radii.to('earthRad').value,bins=plan_pop.Rs) np.testing.assert_allclose(plan_pop.Rvals.sum()*h[0]/float(n),plan_pop.Rvals,rtol=0.05)
Example #12
Source File: test_KeplerLike1.py From EXOSIMS with BSD 3-Clause "New" or "Revised" License | 6 votes |
def test_gen_sma(self): r"""Test gen_sma method. Approach: Ensures the output is set, of the correct type, length, and units. Check that they are in the correct range and follow the distribution. """ plan_pop = self.fixture n = 10000 sma = plan_pop.gen_sma(n) # ensure the units are length self.assertEqual((sma/u.km).decompose().unit, u.dimensionless_unscaled) # sma > 0 self.assertTrue(np.all(sma.value >= 0)) # sma >= arange[0], sma <= arange[1] self.assertTrue(np.all(sma - plan_pop.arange[0] >= 0)) self.assertTrue(np.all(plan_pop.arange[1] - sma >= 0)) h = np.histogram(sma.to('AU').value,100,density=True) hx = np.diff(h[1])/2.+h[1][:-1] hp = plan_pop.dist_sma(hx) chi2 = scipy.stats.chisquare(h[0],hp) self.assertGreaterEqual(chi2[1],0.95)
Example #13
Source File: test_EarthTwinHabZone2.py From EXOSIMS with BSD 3-Clause "New" or "Revised" License | 6 votes |
def test_gen_plan_params(self): r"""Test generated planet parameters: Expected: all 1 R_E, all p = 0.67, e = 0, and uniform a,e in arange,erange """ obj = EarthTwinHabZone2(constrainOrbits=False,erange=[0.1,0.5],**self.spec) x = 10000 a, e, p, Rp = obj.gen_plan_params(x) assert(np.all(p == 0.367)) assert(np.all(Rp == 1.0*u.R_earth)) for param,param_range in zip([a.value,e],[obj.arange.value,obj.erange]): h = np.histogram(param,100,density=True) chi2 = scipy.stats.chisquare(h[0],[1.0/np.diff(param_range)[0]]*len(h[0])) self.assertGreater(chi2[1], 0.95)
Example #14
Source File: test_KeplerLike2.py From EXOSIMS with BSD 3-Clause "New" or "Revised" License | 6 votes |
def test_gen_sma(self): r"""Test gen_sma method. Approach: Ensures the output is set, of the correct type, length, and units. Check that they are in the correct range and follow the distribution. """ plan_pop = self.fixture n = 10000 sma = plan_pop.gen_sma(n) # ensure the units are length self.assertEqual((sma/u.km).decompose().unit, u.dimensionless_unscaled) # sma > 0 self.assertTrue(np.all(sma.value >= 0)) # sma >= arange[0], sma <= arange[1] self.assertTrue(np.all(sma - plan_pop.arange[0] >= 0)) self.assertTrue(np.all(plan_pop.arange[1] - sma >= 0)) h = np.histogram(sma.to('AU').value,100,density=True) hx = np.diff(h[1])/2.+h[1][:-1] hp = plan_pop.dist_sma(hx) chi2 = scipy.stats.chisquare(h[0],hp) self.assertGreaterEqual(chi2[1],0.95)
Example #15
Source File: test_EarthTwinHabZone1.py From EXOSIMS with BSD 3-Clause "New" or "Revised" License | 6 votes |
def test_gen_plan_params(self): r"""Test generated planet parameters: Expected: all 1 R_E, all p = 0.67, e = 0, and uniform a in arange """ obj = EarthTwinHabZone1(**self.spec) x = 10000 a, e, p, Rp = obj.gen_plan_params(x) assert(np.all(e == 0)) assert(np.all(p == 0.367)) assert(np.all(Rp == 1.0*u.R_earth)) h = np.histogram(a.to('AU').value,100,density=True) chi2 = scipy.stats.chisquare(h[0],[1.0/np.diff(obj.arange.to('AU').value)[0]]*len(h[0])) self.assertGreater(chi2[1], 0.95)
Example #16
Source File: groupImgGUI.py From groupImg with MIT License | 6 votes |
def read_image(self,im): if self.i >= self.k : self.i = 0 try: img = Image.open(im) osize = img.size img.thumbnail((self.resample,self.resample)) v = [float(p)/float(img.size[0]*img.size[1])*100 for p in np.histogram(np.asarray(img))[0]] if self.size : v += [osize[0], osize[1]] i = self.i self.i += 1 return [i, v, im] except Exception as e: print("Error reading ",im,e) return [None, None, None]
Example #17
Source File: graph_utils.py From DOTA_models with Apache License 2.0 | 6 votes |
def get_hardness_distribution(gtG, max_dist, min_dist, rng, trials, bins, nodes, n_ori, step_size): heuristic_fn = lambda node_ids, node_id: \ heuristic_fn_vec(nodes[node_ids, :], nodes[[node_id], :], n_ori, step_size) num_nodes = gtG.num_vertices() gt_dists = []; h_dists = []; for i in range(trials): end_node_id = rng.choice(num_nodes) gt_dist = gt.topology.shortest_distance(gt.GraphView(gtG, reversed=True), source=gtG.vertex(end_node_id), target=None, max_dist=max_dist) gt_dist = np.array(gt_dist.get_array()) ind = np.where(np.logical_and(gt_dist <= max_dist, gt_dist >= min_dist))[0] gt_dist = gt_dist[ind] h_dist = heuristic_fn(ind, end_node_id)[:,0] gt_dists.append(gt_dist) h_dists.append(h_dist) gt_dists = np.concatenate(gt_dists) h_dists = np.concatenate(h_dists) hardness = 1. - h_dists*1./gt_dists hist, _ = np.histogram(hardness, bins) hist = hist.astype(np.float64) hist = hist / np.sum(hist) return hist
Example #18
Source File: groupimg.py From groupImg with MIT License | 6 votes |
def read_image(self,im): if self.i >= self.k : self.i = 0 try: img = Image.open(im) osize = img.size img.thumbnail((self.resample,self.resample)) v = [float(p)/float(img.size[0]*img.size[1])*100 for p in np.histogram(np.asarray(img))[0]] if self.size : v += [osize[0], osize[1]] pbar.update(1) i = self.i self.i += 1 return [i, v, im] except Exception as e: print("Error reading ",im,e) return [None, None, None]
Example #19
Source File: tracking.py From pedestrian-haar-based-detector with GNU General Public License v2.0 | 6 votes |
def compareHistograms(image,x,y,w,h, ppl): #temporary crop detected target tempCrop = image[x:x+w, y:y+h] #generate temporary histogram to compare to existant ones tempHist = generateHistogram(tempCrop) if(len(ppl) > 0): b = checkSimilarity(tempHist, ppl, image) if(b): return (b.x, b.y, b.w, b.h, b.color, b.label) else: return None else: return None return None
Example #20
Source File: test_histograms.py From recruit with Apache License 2.0 | 5 votes |
def test_bin_edge_cases(self): # Ensure that floating-point computations correctly place edge cases. arr = np.array([337, 404, 739, 806, 1007, 1811, 2012]) hist, edges = np.histogram(arr, bins=8296, range=(2, 2280)) mask = hist > 0 left_edges = edges[:-1][mask] right_edges = edges[1:][mask] for x, left, right in zip(arr, left_edges, right_edges): assert_(x >= left) assert_(x < right)
Example #21
Source File: test_histograms.py From recruit with Apache License 2.0 | 5 votes |
def test_error_binnum_type (self): # Tests if right Error is raised if bins argument is float vals = np.linspace(0.0, 1.0, num=100) histogram(vals, 5) assert_raises(TypeError, histogram, vals, 2.4)
Example #22
Source File: histograms.py From recruit with Apache License 2.0 | 5 votes |
def _hist_bin_fd(x, range): """ The Freedman-Diaconis histogram bin estimator. The Freedman-Diaconis rule uses interquartile range (IQR) to estimate binwidth. It is considered a variation of the Scott rule with more robustness as the IQR is less affected by outliers than the standard deviation. However, the IQR depends on fewer points than the standard deviation, so it is less accurate, especially for long tailed distributions. If the IQR is 0, this function returns 1 for the number of bins. Binwidth is inversely proportional to the cube root of data size (asymptotically optimal). Parameters ---------- x : array_like Input data that is to be histogrammed, trimmed to range. May not be empty. Returns ------- h : An estimate of the optimal bin width for the given data. """ del range # unused iqr = np.subtract(*np.percentile(x, [75, 25])) return 2.0 * iqr * x.size ** (-1.0 / 3.0)
Example #23
Source File: test_histograms.py From recruit with Apache License 2.0 | 5 votes |
def test_density(self): # Check that the integral of the density equals 1. n = 100 v = np.random.rand(n) a, b = histogram(v, density=True) area = np.sum(a * np.diff(b)) assert_almost_equal(area, 1) # Check with non-constant bin widths v = np.arange(10) bins = [0, 1, 3, 6, 10] a, b = histogram(v, bins, density=True) assert_array_equal(a, .1) assert_equal(np.sum(a * np.diff(b)), 1) # Test that passing False works too a, b = histogram(v, bins, density=False) assert_array_equal(a, [1, 2, 3, 4]) # Variale bin widths are especially useful to deal with # infinities. v = np.arange(10) bins = [0, 1, 3, 6, np.inf] a, b = histogram(v, bins, density=True) assert_array_equal(a, [.1, .1, .1, 0.]) # Taken from a bug report from N. Becker on the numpy-discussion # mailing list Aug. 6, 2010. counts, dmy = np.histogram( [1, 2, 3, 4], [0.5, 1.5, np.inf], density=True) assert_equal(counts, [.25, 0])
Example #24
Source File: histograms.py From recruit with Apache License 2.0 | 5 votes |
def _hist_bin_stone(x, range): """ Histogram bin estimator based on minimizing the estimated integrated squared error (ISE). The number of bins is chosen by minimizing the estimated ISE against the unknown true distribution. The ISE is estimated using cross-validation and can be regarded as a generalization of Scott's rule. https://en.wikipedia.org/wiki/Histogram#Scott.27s_normal_reference_rule This paper by Stone appears to be the origination of this rule. http://digitalassets.lib.berkeley.edu/sdtr/ucb/text/34.pdf Parameters ---------- x : array_like Input data that is to be histogrammed, trimmed to range. May not be empty. range : (float, float) The lower and upper range of the bins. Returns ------- h : An estimate of the optimal bin width for the given data. """ n = x.size ptp_x = np.ptp(x) if n <= 1 or ptp_x == 0: return 0 def jhat(nbins): hh = ptp_x / nbins p_k = np.histogram(x, bins=nbins, range=range)[0] / n return (2 - (n + 1) * p_k.dot(p_k)) / hh nbins_upper_bound = max(100, int(np.sqrt(n))) nbins = min(_range(1, nbins_upper_bound + 1), key=jhat) if nbins == nbins_upper_bound: warnings.warn("The number of bins estimated may be suboptimal.", RuntimeWarning, stacklevel=2) return ptp_x / nbins
Example #25
Source File: test_utils.py From NTFLib with MIT License | 5 votes |
def test_top_sparse3(self): shape, rank, k, factors, x, x_indices, x_vals = \ utils.generate_dataset() model = utils.parafac(factors) for beta in [1.0, 2.0, 1.5]: for factor in range(3): # Generate the top numerator for the reference dense method einstr = utils.generate_dense(rank, factor) # Get all factors that aren't the current factor mode_factors = [a for j, a in enumerate(factors) if j != factor] mode_factors += [x * (model ** (beta - 2.)), ] top_d = np.einsum(einstr, *mode_factors) # Now get the top numerator for the sparse method top_s = np.zeros(factors[factor].shape, dtype=np.float32) utils.top_sparse3(x_indices, x_vals, top_s, beta, factor, *factors) # Now get the top numerator for the sparse method using numba top_n = np.zeros(factors[factor].shape, dtype=np.float32) utils.top_sparse3_numba(x_indices, x_vals, top_n, beta, factor, *factors) print(top_d, top_s, top_n) print(top_d - top_n) print(np.histogram(np.abs((top_d - top_n) / top_d))) result = np.allclose(top_d, top_s, rtol=1e-2, atol=1e-2) self.assertTrue(result) result = np.allclose(top_d, top_n, rtol=1e-2, atol=1e-2) self.assertTrue(result)
Example #26
Source File: logger.py From 3D-HourGlass-Network with MIT License | 5 votes |
def histo_summary(self, tag, values, step, bins=1000): """Log a histogram of the tensor of values.""" # Create a histogram using numpy counts, bin_edges = np.histogram(values, bins=bins) # Fill the fields of the histogram proto hist = tf.HistogramProto() hist.min = float(np.min(values)) hist.max = float(np.max(values)) hist.num = int(np.prod(values.shape)) hist.sum = float(np.sum(values)) hist.sum_squares = float(np.sum(values**2)) # Drop the start of the first bin bin_edges = bin_edges[1:] # Add bin edges and counts for edge in bin_edges: hist.bucket_limit.append(edge) for c in counts: hist.bucket.append(c) # Create and write Summary summary = tf.Summary(value=[tf.Summary.Value(tag=tag, histo=hist)]) self.writer.add_summary(summary, step) self.writer.flush()
Example #27
Source File: logger.py From 3D-HourGlass-Network with MIT License | 5 votes |
def histo_summary(self, tag, values, step, bins=1000): """Log a histogram of the tensor of values.""" # Create a histogram using numpy counts, bin_edges = np.histogram(values, bins=bins) # Fill the fields of the histogram proto hist = tf.HistogramProto() hist.min = float(np.min(values)) hist.max = float(np.max(values)) hist.num = int(np.prod(values.shape)) hist.sum = float(np.sum(values)) hist.sum_squares = float(np.sum(values**2)) # Drop the start of the first bin bin_edges = bin_edges[1:] # Add bin edges and counts for edge in bin_edges: hist.bucket_limit.append(edge) for c in counts: hist.bucket.append(c) # Create and write Summary summary = tf.Summary(value=[tf.Summary.Value(tag=tag, histo=hist)]) self.writer.add_summary(summary, step) self.writer.flush()
Example #28
Source File: visualization_utils.py From vehicle_counting_tensorflow with MIT License | 5 votes |
def add_hist_image_summary(values, bins, name): """Adds a tf.summary.image for a histogram plot of the values. Plots the histogram of values and creates a tf image summary. Args: values: a 1-D float32 tensor containing the values. bins: bin edges which will be directly passed to np.histogram. name: name for the image summary. """ def hist_plot(values, bins): """Numpy function to plot hist.""" fig = plt.figure(frameon=False) ax = fig.add_subplot('111') y, x = np.histogram(values, bins=bins) ax.plot(x[:-1], y) ax.set_ylabel('count') ax.set_xlabel('value') fig.canvas.draw() width, height = fig.get_size_inches() * fig.get_dpi() image = np.fromstring( fig.canvas.tostring_rgb(), dtype='uint8').reshape( 1, int(height), int(width), 3) return image hist_plot = tf.py_func(hist_plot, [values, bins], tf.uint8) tf.summary.image(name, hist_plot)
Example #29
Source File: logger.py From OpenChem with MIT License | 5 votes |
def histo_summary(self, tag, values, step, bins=1000): """Log a histogram of the tensor of values.""" # Create a histogram using numpy counts, bin_edges = np.histogram(values, bins=bins) # Fill the fields of the histogram proto hist = tf.HistogramProto() hist.min = float(np.min(values)) hist.max = float(np.max(values)) hist.num = int(np.prod(values.shape)) hist.sum = float(np.sum(values)) hist.sum_squares = float(np.sum(values ** 2)) # Drop the start of the first bin bin_edges = bin_edges[1:] # Add bin edges and counts for edge in bin_edges: hist.bucket_limit.append(edge) for c in counts: hist.bucket.append(c) # Create and write Summary summary = tf.Summary(value=[tf.Summary.Value(tag=tag, histo=hist)]) self.writer.add_summary(summary, step) self.writer.flush()
Example #30
Source File: loadData.py From ext_portrait_segmentation with MIT License | 5 votes |
def compute_class_weights(self, histogram): ''' Helper function to compute the class weights :param histogram: distribution of class samples :return: None, but updates the classWeights variable ''' normHist = histogram / np.sum(histogram) for i in range(self.classes): self.classWeights[i] = 1 / (np.log(self.normVal + normHist[i]))