def greater_equal(x1, x2):
"""
Return (x1 >= x2) element-wise.
Unlike `numpy.greater_equal`, this comparison is performed by
first stripping whitespace characters from the end of the string.
This behavior is provided for backward-compatibility with
numarray.
Parameters
----------
x1, x2 : array_like of str or unicode
Input arrays of the same shape.
Returns
-------
out : ndarray or bool
Output array of bools, or a single bool if x1 and x2 are scalars.
See Also
--------
equal, not_equal, less_equal, greater, less
"""
return compare_chararrays(x1, x2, '>=', True)
python类greater()的实例源码
defchararray.py 文件源码
项目:PyDataLondon29-EmbarrassinglyParallelDAWithAWSLambda
作者: SignalMedia
项目源码
文件源码
阅读 24
收藏 0
点赞 0
评论 0
defchararray.py 文件源码
项目:PyDataLondon29-EmbarrassinglyParallelDAWithAWSLambda
作者: SignalMedia
项目源码
文件源码
阅读 24
收藏 0
点赞 0
评论 0
def less_equal(x1, x2):
"""
Return (x1 <= x2) element-wise.
Unlike `numpy.less_equal`, this comparison is performed by first
stripping whitespace characters from the end of the string. This
behavior is provided for backward-compatibility with numarray.
Parameters
----------
x1, x2 : array_like of str or unicode
Input arrays of the same shape.
Returns
-------
out : ndarray or bool
Output array of bools, or a single bool if x1 and x2 are scalars.
See Also
--------
equal, not_equal, greater_equal, greater, less
"""
return compare_chararrays(x1, x2, '<=', True)
defchararray.py 文件源码
项目:PyDataLondon29-EmbarrassinglyParallelDAWithAWSLambda
作者: SignalMedia
项目源码
文件源码
阅读 23
收藏 0
点赞 0
评论 0
def less(x1, x2):
"""
Return (x1 < x2) element-wise.
Unlike `numpy.greater`, this comparison is performed by first
stripping whitespace characters from the end of the string. This
behavior is provided for backward-compatibility with numarray.
Parameters
----------
x1, x2 : array_like of str or unicode
Input arrays of the same shape.
Returns
-------
out : ndarray or bool
Output array of bools, or a single bool if x1 and x2 are scalars.
See Also
--------
equal, not_equal, greater_equal, less_equal, greater
"""
return compare_chararrays(x1, x2, '<', True)
test_ufunc.py 文件源码
项目:PyDataLondon29-EmbarrassinglyParallelDAWithAWSLambda
作者: SignalMedia
项目源码
文件源码
阅读 23
收藏 0
点赞 0
评论 0
def test_NotImplemented_not_returned(self):
# See gh-5964 and gh-2091. Some of these functions are not operator
# related and were fixed for other reasons in the past.
binary_funcs = [
np.power, np.add, np.subtract, np.multiply, np.divide,
np.true_divide, np.floor_divide, np.bitwise_and, np.bitwise_or,
np.bitwise_xor, np.left_shift, np.right_shift, np.fmax,
np.fmin, np.fmod, np.hypot, np.logaddexp, np.logaddexp2,
np.logical_and, np.logical_or, np.logical_xor, np.maximum,
np.minimum, np.mod
]
# These functions still return NotImplemented. Will be fixed in
# future.
# bad = [np.greater, np.greater_equal, np.less, np.less_equal, np.not_equal]
a = np.array('1')
b = 1
for f in binary_funcs:
assert_raises(TypeError, f, a, b)
test_deprecations.py 文件源码
项目:PyDataLondon29-EmbarrassinglyParallelDAWithAWSLambda
作者: SignalMedia
项目源码
文件源码
阅读 25
收藏 0
点赞 0
评论 0
def test_identity_equality_mismatch(self):
a = np.array([np.nan], dtype=object)
with warnings.catch_warnings():
warnings.filterwarnings('always', '', FutureWarning)
assert_warns(FutureWarning, np.equal, a, a)
assert_warns(FutureWarning, np.not_equal, a, a)
with warnings.catch_warnings():
warnings.filterwarnings('error', '', FutureWarning)
assert_raises(FutureWarning, np.equal, a, a)
assert_raises(FutureWarning, np.not_equal, a, a)
# And the other do not warn:
with np.errstate(invalid='ignore'):
np.less(a, a)
np.greater(a, a)
np.less_equal(a, a)
np.greater_equal(a, a)
test_core.py 文件源码
项目:PyDataLondon29-EmbarrassinglyParallelDAWithAWSLambda
作者: SignalMedia
项目源码
文件源码
阅读 22
收藏 0
点赞 0
评论 0
def test_minmax_func(self):
# Tests minimum and maximum.
(x, y, a10, m1, m2, xm, ym, z, zm, xf) = self.d
# max doesn't work if shaped
xr = np.ravel(x)
xmr = ravel(xm)
# following are true because of careful selection of data
assert_equal(max(xr), maximum(xmr))
assert_equal(min(xr), minimum(xmr))
assert_equal(minimum([1, 2, 3], [4, 0, 9]), [1, 0, 3])
assert_equal(maximum([1, 2, 3], [4, 0, 9]), [4, 2, 9])
x = arange(5)
y = arange(5) - 2
x[3] = masked
y[0] = masked
assert_equal(minimum(x, y), where(less(x, y), x, y))
assert_equal(maximum(x, y), where(greater(x, y), x, y))
assert_(minimum(x) == 0)
assert_(maximum(x) == 4)
x = arange(4).reshape(2, 2)
x[-1, -1] = masked
assert_equal(maximum(x), 2)
def validate(self, mb_inputs, mb_targets, mb_probs):
""""""
sents = []
mb_parse_probs, mb_rel_probs = mb_probs
for inputs, targets, parse_probs, rel_probs in zip(mb_inputs, mb_targets, mb_parse_probs, mb_rel_probs):
tokens_to_keep = np.greater(inputs[:,0], Vocab.ROOT)
length = np.sum(tokens_to_keep)
parse_preds, rel_preds = self.prob_argmax(parse_probs, rel_probs, tokens_to_keep)
sent = -np.ones( (length, 9), dtype=int)
tokens = np.arange(1, length+1)
sent[:,0] = tokens
sent[:,1:4] = inputs[tokens]
sent[:,4] = targets[tokens,0]
sent[:,5] = parse_preds[tokens]
sent[:,6] = rel_preds[tokens]
sent[:,7:] = targets[tokens, 1:]
sents.append(sent)
return sents
#=============================================================
def validate(self, mb_inputs, mb_targets, mb_probs):
""""""
sents = []
mb_parse_probs, mb_rel_probs = mb_probs
for inputs, targets, parse_probs, rel_probs in zip(mb_inputs, mb_targets, mb_parse_probs, mb_rel_probs):
tokens_to_keep = np.greater(inputs[:,0], Vocab.ROOT)
length = np.sum(tokens_to_keep)
parse_preds, rel_preds = self.prob_argmax(parse_probs, rel_probs, tokens_to_keep)
sent = -np.ones( (length, 9), dtype=int)
tokens = np.arange(1, length+1)
sent[:,0] = tokens
sent[:,1:4] = inputs[tokens]
sent[:,4] = targets[tokens,0]
sent[:,5] = parse_preds[tokens]
sent[:,6] = rel_preds[tokens]
sent[:,7:] = targets[tokens, 1:]
sents.append(sent)
return sents
#=============================================================
def equal(x1, x2):
"""
Return (x1 == x2) element-wise.
Unlike `numpy.equal`, this comparison is performed by first
stripping whitespace characters from the end of the string. This
behavior is provided for backward-compatibility with numarray.
Parameters
----------
x1, x2 : array_like of str or unicode
Input arrays of the same shape.
Returns
-------
out : ndarray or bool
Output array of bools, or a single bool if x1 and x2 are scalars.
See Also
--------
not_equal, greater_equal, less_equal, greater, less
"""
return compare_chararrays(x1, x2, '==', True)
def not_equal(x1, x2):
"""
Return (x1 != x2) element-wise.
Unlike `numpy.not_equal`, this comparison is performed by first
stripping whitespace characters from the end of the string. This
behavior is provided for backward-compatibility with numarray.
Parameters
----------
x1, x2 : array_like of str or unicode
Input arrays of the same shape.
Returns
-------
out : ndarray or bool
Output array of bools, or a single bool if x1 and x2 are scalars.
See Also
--------
equal, greater_equal, less_equal, greater, less
"""
return compare_chararrays(x1, x2, '!=', True)
def greater_equal(x1, x2):
"""
Return (x1 >= x2) element-wise.
Unlike `numpy.greater_equal`, this comparison is performed by
first stripping whitespace characters from the end of the string.
This behavior is provided for backward-compatibility with
numarray.
Parameters
----------
x1, x2 : array_like of str or unicode
Input arrays of the same shape.
Returns
-------
out : ndarray or bool
Output array of bools, or a single bool if x1 and x2 are scalars.
See Also
--------
equal, not_equal, less_equal, greater, less
"""
return compare_chararrays(x1, x2, '>=', True)
def less_equal(x1, x2):
"""
Return (x1 <= x2) element-wise.
Unlike `numpy.less_equal`, this comparison is performed by first
stripping whitespace characters from the end of the string. This
behavior is provided for backward-compatibility with numarray.
Parameters
----------
x1, x2 : array_like of str or unicode
Input arrays of the same shape.
Returns
-------
out : ndarray or bool
Output array of bools, or a single bool if x1 and x2 are scalars.
See Also
--------
equal, not_equal, greater_equal, greater, less
"""
return compare_chararrays(x1, x2, '<=', True)
def greater(x1, x2):
"""
Return (x1 > x2) element-wise.
Unlike `numpy.greater`, this comparison is performed by first
stripping whitespace characters from the end of the string. This
behavior is provided for backward-compatibility with numarray.
Parameters
----------
x1, x2 : array_like of str or unicode
Input arrays of the same shape.
Returns
-------
out : ndarray or bool
Output array of bools, or a single bool if x1 and x2 are scalars.
See Also
--------
equal, not_equal, greater_equal, less_equal, less
"""
return compare_chararrays(x1, x2, '>', True)
def test_NotImplemented_not_returned(self):
# See gh-5964 and gh-2091. Some of these functions are not operator
# related and were fixed for other reasons in the past.
binary_funcs = [
np.power, np.add, np.subtract, np.multiply, np.divide,
np.true_divide, np.floor_divide, np.bitwise_and, np.bitwise_or,
np.bitwise_xor, np.left_shift, np.right_shift, np.fmax,
np.fmin, np.fmod, np.hypot, np.logaddexp, np.logaddexp2,
np.logical_and, np.logical_or, np.logical_xor, np.maximum,
np.minimum, np.mod
]
# These functions still return NotImplemented. Will be fixed in
# future.
# bad = [np.greater, np.greater_equal, np.less, np.less_equal, np.not_equal]
a = np.array('1')
b = 1
for f in binary_funcs:
assert_raises(TypeError, f, a, b)
def test_minmax_func(self):
# Tests minimum and maximum.
(x, y, a10, m1, m2, xm, ym, z, zm, xf) = self.d
# max doesn't work if shaped
xr = np.ravel(x)
xmr = ravel(xm)
# following are true because of careful selection of data
assert_equal(max(xr), maximum(xmr))
assert_equal(min(xr), minimum(xmr))
assert_equal(minimum([1, 2, 3], [4, 0, 9]), [1, 0, 3])
assert_equal(maximum([1, 2, 3], [4, 0, 9]), [4, 2, 9])
x = arange(5)
y = arange(5) - 2
x[3] = masked
y[0] = masked
assert_equal(minimum(x, y), where(less(x, y), x, y))
assert_equal(maximum(x, y), where(greater(x, y), x, y))
assert_(minimum(x) == 0)
assert_(maximum(x) == 4)
x = arange(4).reshape(2, 2)
x[-1, -1] = masked
assert_equal(maximum(x), 2)
def preprocess_labels(label):
"""Preprocess the labels to adapt them to the loss computation requirements
Args:
Label corresponding to the input image (W,H) numpy array
Returns:
Label ready to compute the loss (1,W,H,1)
"""
if type(label) is not np.ndarray:
label = np.array(Image.open(label).split()[0], dtype=np.uint8)
max_mask = np.max(label) * 0.5
label = np.greater(label, max_mask)
label = np.expand_dims(np.expand_dims(label, axis=0), axis=3)
# label = tf.cast(np.array(label), tf.float32)
# max_mask = tf.multiply(tf.reduce_max(label), 0.5)
# label = tf.cast(tf.greater(label, max_mask), tf.float32)
# label = tf.expand_dims(tf.expand_dims(label, 0), 3)
return label
def class_balanced_cross_entropy_loss(output, label):
"""Define the class balanced cross entropy loss to train the network
Args:
output: Output of the network
label: Ground truth label
Returns:
Tensor that evaluates the loss
"""
labels = tf.cast(tf.greater(label, 0.5), tf.float32)
num_labels_pos = tf.reduce_sum(labels)
num_labels_neg = tf.reduce_sum(1.0 - labels)
num_total = num_labels_pos + num_labels_neg
output_gt_zero = tf.cast(tf.greater_equal(output, 0), tf.float32)
loss_val = tf.multiply(output, (labels - output_gt_zero)) - tf.log(
1 + tf.exp(output - 2 * tf.multiply(output, output_gt_zero)))
loss_pos = tf.reduce_sum(-tf.multiply(labels, loss_val))
loss_neg = tf.reduce_sum(-tf.multiply(1.0 - labels, loss_val))
final_loss = num_labels_neg / num_total * loss_pos + num_labels_pos / num_total * loss_neg
return final_loss
def class_balanced_cross_entropy_loss_theoretical(output, label):
"""Theoretical version of the class balanced cross entropy loss to train the network (Produces unstable results)
Args:
output: Output of the network
label: Ground truth label
Returns:
Tensor that evaluates the loss
"""
output = tf.nn.sigmoid(output)
labels_pos = tf.cast(tf.greater(label, 0), tf.float32)
labels_neg = tf.cast(tf.less(label, 1), tf.float32)
num_labels_pos = tf.reduce_sum(labels_pos)
num_labels_neg = tf.reduce_sum(labels_neg)
num_total = num_labels_pos + num_labels_neg
loss_pos = tf.reduce_sum(tf.multiply(labels_pos, tf.log(output + 0.00001)))
loss_neg = tf.reduce_sum(tf.multiply(labels_neg, tf.log(1 - output + 0.00001)))
final_loss = -num_labels_neg / num_total * loss_pos - num_labels_pos / num_total * loss_neg
return final_loss
def __init__(self, monitor='val_loss', patience=0, verbose=0, mode='auto'):
super(Callback, self).__init__()
self.monitor = monitor
self.patience = patience
self.verbose = verbose
self.wait = 0
self.best_epoch = 0
if mode == 'min':
self.monitor_op = np.less
self.best = np.Inf
elif mode == 'max':
self.monitor_op = np.greater
self.best = -np.Inf
else:
if 'acc' in self.monitor:
self.monitor_op = np.greater
self.best = -np.Inf
else:
self.monitor_op = np.less
self.best = np.Inf
def handle_rolling(agg, granularity, timestamps, values, is_aggregated,
references, window):
if window > len(values):
raise exceptions.UnAggregableTimeseries(
references,
"Rolling window '%d' is greater than serie length '%d'" %
(window, len(values))
)
timestamps = timestamps[window - 1:]
values = values.T
# rigtorp.se/2011/01/01/rolling-statistics-numpy.html
shape = values.shape[:-1] + (values.shape[-1] - window + 1, window)
strides = values.strides + (values.strides[-1],)
new_values = AGG_MAP[agg](as_strided(values, shape=shape, strides=strides),
axis=-1)
return granularity, timestamps, new_values.T, is_aggregated