本文整理汇总了Python中numpy.int8方法的典型用法代码示例。如果您正苦于以下问题:Python numpy.int8方法的具体用法?Python numpy.int8怎么用?Python numpy.int8使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类numpy
的用法示例。
在下文中一共展示了numpy.int8方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: find_match
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import int8 [as 别名]
def find_match(self, pred, gt):
'''
Match component to balls.
'''
batch_size, n_frames_input, n_components, _ = pred.shape
diff = pred.reshape(batch_size, n_frames_input, n_components, 1, 2) - \
gt.reshape(batch_size, n_frames_input, 1, n_components, 2)
diff = np.sum(np.sum(diff ** 2, axis=-1), axis=1)
# Direct indices
indices = np.argmin(diff, axis=2)
ambiguous = np.zeros(batch_size, dtype=np.int8)
for i in range(batch_size):
_, counts = np.unique(indices[i], return_counts=True)
if not np.all(counts == 1):
ambiguous[i] = 1
return indices, ambiguous
示例2: parse_data
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import int8 [as 别名]
def parse_data(path, dataset, flatten):
if dataset != 'train' and dataset != 't10k':
raise NameError('dataset must be train or t10k')
label_file = os.path.join(path, dataset + '-labels-idx1-ubyte')
with open(label_file, 'rb') as file:
_, num = struct.unpack(">II", file.read(8))
labels = np.fromfile(file, dtype=np.int8) # int8
new_labels = np.zeros((num, 10))
new_labels[np.arange(num), labels] = 1
img_file = os.path.join(path, dataset + '-images-idx3-ubyte')
with open(img_file, 'rb') as file:
_, num, rows, cols = struct.unpack(">IIII", file.read(16))
imgs = np.fromfile(file, dtype=np.uint8).reshape(num, rows, cols) # uint8
imgs = imgs.astype(np.float32) / 255.0
if flatten:
imgs = imgs.reshape([num, -1])
return imgs, new_labels
示例3: test_quantize_float32_to_int8
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import int8 [as 别名]
def test_quantize_float32_to_int8():
shape = rand_shape_nd(4)
data = rand_ndarray(shape, 'default', dtype='float32')
min_range = mx.nd.min(data)
max_range = mx.nd.max(data)
qdata, min_val, max_val = mx.nd.contrib.quantize(data, min_range, max_range, out_type='int8')
data_np = data.asnumpy()
min_range = min_range.asscalar()
max_range = max_range.asscalar()
real_range = np.maximum(np.abs(min_range), np.abs(max_range))
quantized_range = 127.0
scale = quantized_range / real_range
assert qdata.dtype == np.int8
assert min_val.dtype == np.float32
assert max_val.dtype == np.float32
assert same(min_val.asscalar(), -real_range)
assert same(max_val.asscalar(), real_range)
qdata_np = (np.sign(data_np) * np.minimum(np.abs(data_np) * scale + 0.5, quantized_range)).astype(np.int8)
assert_almost_equal(qdata.asnumpy(), qdata_np, atol = 1)
示例4: test_quantized_flatten
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import int8 [as 别名]
def test_quantized_flatten():
def check_quantized_flatten(shape, qdtype):
if qdtype == 'uint8':
data_low = 0.0
data_high = 127.0
else:
data_low = -127.0
data_high = 127.0
qdata = mx.nd.random.uniform(low=data_low, high=data_high, shape=shape).astype(qdtype)
min_data = mx.nd.array([-1023.343], dtype='float32')
max_data = mx.nd.array([2343.324275], dtype='float32')
qoutput, min_output, max_output = mx.nd.contrib.quantized_flatten(qdata, min_data, max_data)
assert qoutput.ndim == 2
assert qoutput.shape[0] == qdata.shape[0]
assert qoutput.shape[1] == np.prod(qdata.shape[1:])
assert same(qdata.asnumpy().flatten(), qoutput.asnumpy().flatten())
assert same(min_data.asnumpy(), min_output.asnumpy())
assert same(max_data.asnumpy(), max_output.asnumpy())
for qdtype in ['int8', 'uint8']:
check_quantized_flatten((10,), qdtype)
check_quantized_flatten((10, 15), qdtype)
check_quantized_flatten((10, 15, 18), qdtype)
check_quantized_flatten((3, 4, 23, 23), qdtype)
示例5: main
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import int8 [as 别名]
def main():
print('create y...')
y = np.random.randint(2, size=N_OBS)
print('create X...')
row = np.random.randint(N_OBS, size=N_VALUE)
col = np.random.randint(N_FEATURE, size=N_VALUE)
data = np.ones(N_VALUE)
X = sparse.csr_matrix((data, (row, col)), dtype=np.int8)
print('train...')
profiler = cProfile.Profile(subcalls=True, builtins=True, timeunit=0.001,)
clf = FTRL(interaction=False)
profiler.enable()
clf.fit(X, y)
profiler.disable()
profiler.print_stats()
p = clf.predict(X)
print('AUC: {:.4f}'.format(auc(y, p)))
assert auc(y, p) > .5
示例6: frompointer
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import int8 [as 别名]
def frompointer(pointer, count, dtype=float):
'''Interpret a buffer that the pointer refers to as a 1-dimensional array.
Args:
pointer : int or ctypes pointer
address of a buffer
count : int
Number of items to read.
dtype : data-type, optional
Data-type of the returned array; default: float.
Examples:
>>> s = numpy.ones(3, dtype=numpy.int32)
>>> ptr = s.ctypes.data
>>> frompointer(ptr, count=6, dtype=numpy.int16)
[1, 0, 1, 0, 1, 0]
'''
dtype = numpy.dtype(dtype)
count *= dtype.itemsize
buf = (ctypes.c_char * count).from_address(pointer)
a = numpy.ndarray(count, dtype=numpy.int8, buffer=buf)
return a.view(dtype)
示例7: test_no_offset_scale
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import int8 [as 别名]
def test_no_offset_scale():
# Specific tests of no-offset scaling
SAW = SlopeArrayWriter
# Floating point
for data in ((-128, 127),
(-128, 126),
(-128, -127),
(-128, 0),
(-128, -1),
(126, 127),
(-127, 127)):
aw = SAW(np.array(data, dtype=np.float32), np.int8)
assert_equal(aw.slope, 1.0)
aw = SAW(np.array([-126, 127 * 2.0], dtype=np.float32), np.int8)
assert_equal(aw.slope, 2)
aw = SAW(np.array([-128 * 2.0, 127], dtype=np.float32), np.int8)
assert_equal(aw.slope, 2)
# Test that nasty abs behavior does not upset us
n = -2**15
aw = SAW(np.array([n, n], dtype=np.int16), np.uint8)
assert_array_almost_equal(aw.slope, n / 255.0, 5)
示例8: test_calculate_scale
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import int8 [as 别名]
def test_calculate_scale():
# Test for special cases in scale calculation
npa = np.array
# Here the offset handles it
res = calculate_scale(npa([-2, -1], dtype=np.int8), np.uint8, True)
assert_equal(res, (1.0, -2.0, None, None))
# Not having offset not a problem obviously
res = calculate_scale(npa([-2, -1], dtype=np.int8), np.uint8, 0)
assert_equal(res, (-1.0, 0.0, None, None))
# Case where offset handles scaling
res = calculate_scale(npa([-1, 1], dtype=np.int8), np.uint8, 1)
assert_equal(res, (1.0, -1.0, None, None))
# Can't work for no offset case
assert_raises(ValueError,
calculate_scale, npa([-1, 1], dtype=np.int8), np.uint8, 0)
# Offset trick can't work when max is out of range
res = calculate_scale(npa([-1, 255], dtype=np.int16), np.uint8, 1)
assert_not_equal(res, (1.0, -1.0, None, None))
示例9: test_a2f_min_max
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import int8 [as 别名]
def test_a2f_min_max():
str_io = BytesIO()
for in_dt in (np.float32, np.int8):
for out_dt in (np.float32, np.int8):
arr = np.arange(4, dtype=in_dt)
# min thresholding
data_back = write_return(arr, str_io, out_dt, 0, 0, 1, 1)
assert_array_equal(data_back, [1, 1, 2, 3])
# max thresholding
data_back = write_return(arr, str_io, out_dt, 0, 0, 1, None, 2)
assert_array_equal(data_back, [0, 1, 2, 2])
# min max thresholding
data_back = write_return(arr, str_io, out_dt, 0, 0, 1, 1, 2)
assert_array_equal(data_back, [1, 1, 2, 2])
# Check that works OK with scaling and intercept
arr = np.arange(4, dtype=np.float32)
data_back = write_return(arr, str_io, np.int, 0, -1, 0.5, 1, 2)
assert_array_equal(data_back * 0.5 - 1, [1, 1, 2, 2])
# Even when scaling is negative
data_back = write_return(arr, str_io, np.int, 0, 1, -0.5, 1, 2)
assert_array_equal(data_back * -0.5 + 1, [1, 1, 2, 2])
示例10: test_can_cast
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import int8 [as 别名]
def test_can_cast():
tests = ((np.float32, np.float32, True, True, True),
(np.float64, np.float32, True, True, True),
(np.complex128, np.float32, False, False, False),
(np.float32, np.complex128, True, True, True),
(np.float32, np.uint8, False, True, True),
(np.uint32, np.complex128, True, True, True),
(np.int64, np.float32, True, True, True),
(np.complex128, np.int16, False, False, False),
(np.float32, np.int16, False, True, True),
(np.uint8, np.int16, True, True, True),
(np.uint16, np.int16, False, True, True),
(np.int16, np.uint16, False, False, True),
(np.int8, np.uint16, False, False, True),
(np.uint16, np.uint8, False, True, True),
)
for intype, outtype, def_res, scale_res, all_res in tests:
assert_equal(def_res, can_cast(intype, outtype))
assert_equal(scale_res, can_cast(intype, outtype, False, True))
assert_equal(all_res, can_cast(intype, outtype, True, True))
示例11: testEnumArrayTypeItem
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import int8 [as 别名]
def testEnumArrayTypeItem(self):
mapping = {'RED': 0, 'GREEN': 1, 'BLUE': 2}
dt_enum = special_dtype(enum=(np.int8, mapping))
typeItem = hdf5dtype.getTypeItem(dt_enum)
dt_array = np.dtype('(2,3)'+dt_enum.str, metadata=dict(dt_enum.metadata))
typeItem = hdf5dtype.getTypeItem(dt_array)
self.assertEqual(typeItem['class'], 'H5T_ARRAY')
self.assertTrue("dims" in typeItem)
self.assertEqual(typeItem["dims"], (2,3))
baseItem = typeItem['base']
self.assertEqual(baseItem['class'], 'H5T_ENUM')
self.assertTrue('mapping' in baseItem)
self.assertEqual(baseItem['mapping']['GREEN'], 1)
self.assertTrue("base" in baseItem)
basePrim = baseItem["base"]
self.assertEqual(basePrim["class"], 'H5T_INTEGER')
self.assertEqual(basePrim['base'], 'H5T_STD_I8LE')
typeSize = hdf5dtype.getItemSize(typeItem)
self.assertEqual(typeSize, 6) # one-byte for base enum type * shape of (2,3)
示例12: test_payload_getitem_setitem
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import int8 [as 别名]
def test_payload_getitem_setitem(self, item):
data = self.payload.data
sel_data = data[item]
assert np.all(self.payload[item] == sel_data)
payload = self.Payload(self.payload.words.copy(), sample_shape=(2,),
bps=8, complex_data=False)
assert payload == self.payload
payload[item] = 1 - sel_data
check = self.payload.data
check[item] = 1 - sel_data
assert np.all(payload[item] == 1 - sel_data)
assert np.all(payload.data == check)
assert np.all(payload[:]
== payload.words.view(np.int8).reshape(-1, 2))
assert payload != self.payload
payload[item] = sel_data
assert np.all(payload[item] == sel_data)
assert payload == self.payload
payload = self.Payload.fromdata(data + 1j * data, bps=8)
sel_data = payload.data[item]
assert np.all(payload[item] == sel_data)
payload[item] = 1 - sel_data
check = payload.data
check[item] = 1 - sel_data
assert np.all(payload.data == check)
示例13: _convert
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import int8 [as 别名]
def _convert(self, vals):
res = {}
for k, v in vals.items():
if isinstance(v, (np.int, np.int8, np.int16, np.int32, np.int64)):
v = int(v)
elif isinstance(v, (np.float, np.float16, np.float32, np.float64)):
v = float(v)
elif isinstance(v, Labels):
v = list(v)
elif isinstance(v, np.ndarray):
v = v.tolist()
elif isinstance(v, dict):
v = self._convert(v)
res[k] = v
return res
示例14: _toscalar
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import int8 [as 别名]
def _toscalar(v):
if isinstance(v, (np.float16, np.float32, np.float64,
np.uint8, np.uint16, np.uint32, np.uint64,
np.int8, np.int16, np.int32, np.int64)):
return np.asscalar(v)
else:
return v
示例15: convert
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import int8 [as 别名]
def convert(self, complex_iq):
intlv = self._interleave(complex_iq)
clipped = self._clip(intlv)
converted = 127. * clipped
hackrf_out = converted.astype(np.int8)
return hackrf_out