本文整理匯總了Python中numpy.empty方法的典型用法代碼示例。如果您正苦於以下問題:Python numpy.empty方法的具體用法?Python numpy.empty怎麽用?Python numpy.empty使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類numpy
的用法示例。
在下文中一共展示了numpy.empty方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: wordbag2mat
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import empty [as 別名]
def wordbag2mat(self, wordbag): #testing
if self.model==None:
raise Exception("no model")
matrix=np.empty((len(wordbag),self.len_vector))
#如果詞典中不存在該詞,拋出異常,但暫時還沒有自定義詞典的辦法,所以暫時不那麽嚴格
#try:
# for i in range(len(wordbag)):
# matrix[i,:]=self.model[wordbag[i]]
#except:
# raise Exception("'%s' can not be found in dictionary." % wordbag[i])
#如果詞典中不存在該詞,則push進一列零向量
for i in range(len(wordbag)):
try:
matrix[i,:]=self.model.wv.__getitem__(wordbag[i])#[wordbag[i]]
except:
matrix[i,:]=np.zeros((1,self.len_vector))
return matrix
################################ problem #####################################
示例2: get_cls_results
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import empty [as 別名]
def get_cls_results(det_results, annotations, class_id):
"""Get det results and gt information of a certain class.
Args:
det_results (list[list]): Same as `eval_map()`.
annotations (list[dict]): Same as `eval_map()`.
class_id (int): ID of a specific class.
Returns:
tuple[list[np.ndarray]]: detected bboxes, gt bboxes, ignored gt bboxes
"""
cls_dets = [img_res[class_id] for img_res in det_results]
cls_gts = []
cls_gts_ignore = []
for ann in annotations:
gt_inds = ann['labels'] == class_id
cls_gts.append(ann['bboxes'][gt_inds, :])
if ann.get('labels_ignore', None) is not None:
ignore_inds = ann['labels_ignore'] == class_id
cls_gts_ignore.append(ann['bboxes_ignore'][ignore_inds, :])
else:
cls_gts_ignore.append(np.empty((0, 4), dtype=np.float32))
return cls_dets, cls_gts, cls_gts_ignore
示例3: __init__
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import empty [as 別名]
def __init__(self, masks, height, width):
self.height = height
self.width = width
if len(masks) == 0:
self.masks = np.empty((0, self.height, self.width), dtype=np.uint8)
else:
assert isinstance(masks, (list, np.ndarray))
if isinstance(masks, list):
assert isinstance(masks[0], np.ndarray)
assert masks[0].ndim == 2 # (H, W)
else:
assert masks.ndim == 3 # (N, H, W)
self.masks = np.stack(masks).reshape(-1, height, width)
assert self.masks.shape[1] == self.height
assert self.masks.shape[2] == self.width
示例4: test_bitmap_mask_resize
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import empty [as 別名]
def test_bitmap_mask_resize():
# resize with empty bitmap masks
raw_masks = dummy_raw_bitmap_masks((0, 28, 28))
bitmap_masks = BitmapMasks(raw_masks, 28, 28)
resized_masks = bitmap_masks.resize((56, 72))
assert len(resized_masks) == 0
assert resized_masks.height == 56
assert resized_masks.width == 72
# resize with bitmap masks contain 1 instances
raw_masks = np.diag(np.ones(4, dtype=np.uint8))[np.newaxis, ...]
bitmap_masks = BitmapMasks(raw_masks, 4, 4)
resized_masks = bitmap_masks.resize((8, 8))
assert len(resized_masks) == 1
assert resized_masks.height == 8
assert resized_masks.width == 8
truth = np.array([[[1, 1, 0, 0, 0, 0, 0, 0], [1, 1, 0, 0, 0, 0, 0, 0],
[0, 0, 1, 1, 0, 0, 0, 0], [0, 0, 1, 1, 0, 0, 0, 0],
[0, 0, 0, 0, 1, 1, 0, 0], [0, 0, 0, 0, 1, 1, 0, 0],
[0, 0, 0, 0, 0, 0, 1, 1], [0, 0, 0, 0, 0, 0, 1, 1]]])
assert (resized_masks.masks == truth).all()
示例5: test_bitmap_mask_pad
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import empty [as 別名]
def test_bitmap_mask_pad():
# pad with empty bitmap masks
raw_masks = dummy_raw_bitmap_masks((0, 28, 28))
bitmap_masks = BitmapMasks(raw_masks, 28, 28)
padded_masks = bitmap_masks.pad((56, 56))
assert len(padded_masks) == 0
assert padded_masks.height == 56
assert padded_masks.width == 56
# pad with bitmap masks contain 3 instances
raw_masks = dummy_raw_bitmap_masks((3, 28, 28))
bitmap_masks = BitmapMasks(raw_masks, 28, 28)
padded_masks = bitmap_masks.pad((56, 56))
assert len(padded_masks) == 3
assert padded_masks.height == 56
assert padded_masks.width == 56
assert (padded_masks.masks[:, 28:, 28:] == 0).all()
示例6: test_bitmap_mask_crop
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import empty [as 別名]
def test_bitmap_mask_crop():
# crop with empty bitmap masks
dummy_bbox = np.array([0, 10, 10, 27], dtype=np.int)
raw_masks = dummy_raw_bitmap_masks((0, 28, 28))
bitmap_masks = BitmapMasks(raw_masks, 28, 28)
cropped_masks = bitmap_masks.crop(dummy_bbox)
assert len(cropped_masks) == 0
assert cropped_masks.height == 17
assert cropped_masks.width == 10
# crop with bitmap masks contain 3 instances
raw_masks = dummy_raw_bitmap_masks((3, 28, 28))
bitmap_masks = BitmapMasks(raw_masks, 28, 28)
cropped_masks = bitmap_masks.crop(dummy_bbox)
assert len(cropped_masks) == 3
assert cropped_masks.height == 17
assert cropped_masks.width == 10
x1, y1, x2, y2 = dummy_bbox
assert (cropped_masks.masks == raw_masks[:, y1:y2, x1:x2]).all()
# crop with invalid bbox
with pytest.raises(AssertionError):
dummy_bbox = dummy_bboxes(2, 28, 28)
bitmap_masks.crop(dummy_bbox)
示例7: test_bitmap_mask_crop_and_resize
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import empty [as 別名]
def test_bitmap_mask_crop_and_resize():
dummy_bbox = dummy_bboxes(5, 28, 28)
inds = np.random.randint(0, 3, (5, ))
# crop and resize with empty bitmap masks
raw_masks = dummy_raw_bitmap_masks((0, 28, 28))
bitmap_masks = BitmapMasks(raw_masks, 28, 28)
cropped_resized_masks = bitmap_masks.crop_and_resize(
dummy_bbox, (56, 56), inds)
assert len(cropped_resized_masks) == 0
assert cropped_resized_masks.height == 56
assert cropped_resized_masks.width == 56
# crop and resize with bitmap masks contain 3 instances
raw_masks = dummy_raw_bitmap_masks((3, 28, 28))
bitmap_masks = BitmapMasks(raw_masks, 28, 28)
cropped_resized_masks = bitmap_masks.crop_and_resize(
dummy_bbox, (56, 56), inds)
assert len(cropped_resized_masks) == 5
assert cropped_resized_masks.height == 56
assert cropped_resized_masks.width == 56
示例8: test_polygon_mask_pad
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import empty [as 別名]
def test_polygon_mask_pad():
# pad with empty polygon masks
raw_masks = dummy_raw_polygon_masks((0, 28, 28))
polygon_masks = PolygonMasks(raw_masks, 28, 28)
padded_masks = polygon_masks.pad((56, 56))
assert len(padded_masks) == 0
assert padded_masks.height == 56
assert padded_masks.width == 56
assert padded_masks.to_ndarray().shape == (0, 56, 56)
# pad with polygon masks contain 3 instances
raw_masks = dummy_raw_polygon_masks((3, 28, 28))
polygon_masks = PolygonMasks(raw_masks, 28, 28)
padded_masks = polygon_masks.pad((56, 56))
assert len(padded_masks) == 3
assert padded_masks.height == 56
assert padded_masks.width == 56
assert padded_masks.to_ndarray().shape == (3, 56, 56)
assert (padded_masks.to_ndarray()[:, 28:, 28:] == 0).all()
示例9: test_polygon_mask_crop_and_resize
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import empty [as 別名]
def test_polygon_mask_crop_and_resize():
dummy_bbox = dummy_bboxes(5, 28, 28)
inds = np.random.randint(0, 3, (5, ))
# crop and resize with empty polygon masks
raw_masks = dummy_raw_polygon_masks((0, 28, 28))
polygon_masks = PolygonMasks(raw_masks, 28, 28)
cropped_resized_masks = polygon_masks.crop_and_resize(
dummy_bbox, (56, 56), inds)
assert len(cropped_resized_masks) == 0
assert cropped_resized_masks.height == 56
assert cropped_resized_masks.width == 56
assert cropped_resized_masks.to_ndarray().shape == (0, 56, 56)
# crop and resize with polygon masks contain 3 instances
raw_masks = dummy_raw_polygon_masks((3, 28, 28))
polygon_masks = PolygonMasks(raw_masks, 28, 28)
cropped_resized_masks = polygon_masks.crop_and_resize(
dummy_bbox, (56, 56), inds)
assert len(cropped_resized_masks) == 5
assert cropped_resized_masks.height == 56
assert cropped_resized_masks.width == 56
assert cropped_resized_masks.to_ndarray().shape == (5, 56, 56)
示例10: map_values
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import empty [as 別名]
def map_values(values, pos, target_pos, dtype=None, nan=dat.CPG_NAN):
"""Maps `values` array at positions `pos` to `target_pos`.
Inserts `nan` for uncovered positions.
"""
assert len(values) == len(pos)
assert np.all(pos == np.sort(pos))
assert np.all(target_pos == np.sort(target_pos))
values = values.ravel()
pos = pos.ravel()
target_pos = target_pos.ravel()
idx = np.in1d(pos, target_pos)
pos = pos[idx]
values = values[idx]
if not dtype:
dtype = values.dtype
target_values = np.empty(len(target_pos), dtype=dtype)
target_values.fill(nan)
idx = np.in1d(target_pos, pos).nonzero()[0]
assert len(idx) == len(values)
assert np.all(target_pos[idx] == pos)
target_values[idx] = values
return target_values
示例11: random_projection
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import empty [as 別名]
def random_projection(X):
data_demension = X.shape[1]
new_data_demension = random.randint(2, data_demension)
new_X = np.empty((data_demension, new_data_demension))
minus_one = 0.1
positive_one = 0.9
for i in range(len(new_X)):
for j in range(len(new_X[i])):
rand = random.random()
if rand < minus_one:
new_X[i][j] = -1.0
elif rand >= positive_one:
new_X[i][j] = 1.0
else:
new_X[i][j] = 0.0
new_X = np.inner(X, new_X.T)
return new_X
示例12: sample_categorical
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import empty [as 別名]
def sample_categorical(prob, rng):
"""Sample from independent categorical distributions
Each batch is an independent categorical distribution.
Parameters
----------
prob : numpy.ndarray
Probability of the categorical distribution. Shape --> (batch_num, category_num)
rng : numpy.random.RandomState
Returns
-------
ret : numpy.ndarray
Sampling result. Shape --> (batch_num,)
"""
ret = numpy.empty(prob.shape[0], dtype=numpy.float32)
for ind in range(prob.shape[0]):
ret[ind] = numpy.searchsorted(numpy.cumsum(prob[ind]), rng.rand()).clip(min=0.0,
max=prob.shape[
1] - 0.5)
return ret
示例13: _new_alloc_handle
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import empty [as 別名]
def _new_alloc_handle(shape, ctx, delay_alloc, dtype=mx_real_t):
"""Return a new handle with specified shape and context.
Empty handle is only used to hold results.
Returns
-------
handle
A new empty `NDArray` handle.
"""
hdl = NDArrayHandle()
check_call(_LIB.MXNDArrayCreateEx(
c_array_buf(mx_uint, native_array('I', shape)),
mx_uint(len(shape)),
ctypes.c_int(ctx.device_typeid),
ctypes.c_int(ctx.device_id),
ctypes.c_int(int(delay_alloc)),
ctypes.c_int(int(_DTYPE_NP_TO_MX[np.dtype(dtype).type])),
ctypes.byref(hdl)))
return hdl
示例14: asnumpy
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import empty [as 別名]
def asnumpy(self):
"""Returns a ``numpy.ndarray`` object with value copied from this array.
Examples
--------
>>> x = mx.nd.ones((2,3))
>>> y = x.asnumpy()
>>> type(y)
<type 'numpy.ndarray'>
>>> y
array([[ 1., 1., 1.],
[ 1., 1., 1.]], dtype=float32)
>>> z = mx.nd.ones((2,3), dtype='int32')
>>> z.asnumpy()
array([[1, 1, 1],
[1, 1, 1]], dtype=int32)
"""
data = np.empty(self.shape, dtype=self.dtype)
check_call(_LIB.MXNDArraySyncCopyToCPU(
self.handle,
data.ctypes.data_as(ctypes.c_void_p),
ctypes.c_size_t(data.size)))
return data
示例15: predict_2d_space
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import empty [as 別名]
def predict_2d_space(net, delta=0.05):
"""
Iterate predictions over a 2d space
:param net: (object) A NumpyNet model object
:param delta: space between predictions
:return: prediction_matrix: the actual predictions
axis_x and axis_y: the axes (useful for plotting)
"""
axis_x = np.arange(net.predict_space[0], net.predict_space[1] + delta, delta)
axis_y = np.arange(net.predict_space[2], net.predict_space[3] + delta, delta)
prediction_matrix = np.empty((len(axis_x), len(axis_y)))
for i, x in enumerate(axis_x):
for j, y in enumerate(axis_y):
test_prediction = np.array([x, y])
test_prediction = net.predict(test_prediction)
prediction_matrix[i, j] = test_prediction
return prediction_matrix, axis_x, axis_y