def test_empty_input_gives_all_zero_counts(self): # Bins will be: # (-inf, 1), [1, 2), [2, 3), [3, 4), [4, inf) value_range = [0.0, 5.0] values = [] expected_bins = [] with self.test_session(): bins = histogram_ops.histogram_fixed_width_bins(values, value_range, nbins=5) self.assertEqual(dtypes.int32, bins.dtype) self.assertAllClose(expected_bins, bins.eval())
def test_negative_nbins(self): value_range = [0.0, 5.0] values = [] with self.assertRaisesRegex((errors.InvalidArgumentError, ValueError), "must > 0"): with self.session(): bins = histogram_ops.histogram_fixed_width_bins(values, value_range, nbins=-1) self.evaluate(bins)
def test_range_overlap(self): # GitHub issue 29661 value_range = np.float32([0.0, 0.0]) values = np.float32([-1.0, 0.0, 1.5, 2.0, 5.0, 15]) expected_bins = [0, 0, 4, 4, 4, 4] with self.assertRaises(ValueError): with self.cached_session(): _ = histogram_ops.histogram_fixed_width_bins(values, value_range, nbins=5)
def test_1d_float64_values_int32_output(self): # Bins will be: # (-inf, 1), [1, 2), [2, 3), [3, 4), [4, inf) value_range = np.float64([0.0, 5.0]) values = np.float64([-1.0, 0.0, 1.5, 2.0, 5.0, 15]) expected_bins = [0, 0, 1, 2, 4, 4] with self.cached_session(): bins = histogram_ops.histogram_fixed_width_bins( values, value_range, nbins=5) self.assertEqual(dtypes.int32, bins.dtype) self.assertAllClose(expected_bins, self.evaluate(bins))
def test_empty_input_gives_all_zero_counts(self): # Bins will be: # (-inf, 1), [1, 2), [2, 3), [3, 4), [4, inf) value_range = [0.0, 5.0] values = [] expected_bins = [] with self.cached_session(): bins = histogram_ops.histogram_fixed_width_bins( values, value_range, nbins=5) self.assertEqual(dtypes.int32, bins.dtype) self.assertAllClose(expected_bins, self.evaluate(bins))
def test_1d_values_int32_output(self): # Bins will be: # (-inf, 1), [1, 2), [2, 3), [3, 4), [4, inf) value_range = [0.0, 5.0] values = [-1.0, 0.0, 1.5, 2.0, 5.0, 15] expected_bins = [0, 0, 1, 2, 4, 4] with self.test_session(): bins = histogram_ops.histogram_fixed_width_bins( values, value_range, nbins=5, dtype=dtypes.int64) self.assertEqual(dtypes.int32, bins.dtype) self.assertAllClose(expected_bins, bins.eval())
def test_2d_values(self): # Bins will be: # (-inf, 1), [1, 2), [2, 3), [3, 4), [4, inf) value_range = [0.0, 5.0] values = constant_op.constant( [[-1.0, 0.0, 1.5], [2.0, 5.0, 15]], shape=(2, 3)) expected_bins = [[0, 0, 1], [2, 4, 4]] with self.cached_session(): bins = histogram_ops.histogram_fixed_width_bins( values, value_range, nbins=5) self.assertEqual(dtypes.int32, bins.dtype) self.assertAllClose(expected_bins, self.evaluate(bins))