Python numpy.int8() Examples
The following are 30
code examples of numpy.int8().
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 deep-learning-note with MIT License | 9 votes |
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
Example #2
Source File: test_utils.py From me-ica with GNU Lesser General Public License v2.1 | 6 votes |
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))
Example #3
Source File: numpy_helper.py From pyscf with Apache License 2.0 | 6 votes |
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)
Example #4
Source File: test_ftrl.py From Kaggler with MIT License | 6 votes |
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
Example #5
Source File: test_base.py From baseband with GNU General Public License v3.0 | 6 votes |
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)
Example #6
Source File: test_arraywriters.py From me-ica with GNU Lesser General Public License v2.1 | 6 votes |
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)
Example #7
Source File: test_quantization.py From dynamic-training-with-apache-mxnet-on-aws with Apache License 2.0 | 6 votes |
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)
Example #8
Source File: test_quantization.py From dynamic-training-with-apache-mxnet-on-aws with Apache License 2.0 | 6 votes |
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)
Example #9
Source File: test_vlbi_base.py From baseband with GNU General Public License v3.0 | 6 votes |
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)
Example #10
Source File: test_utils.py From me-ica with GNU Lesser General Public License v2.1 | 6 votes |
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])
Example #11
Source File: hdf5dtypeTest.py From hsds with Apache License 2.0 | 6 votes |
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)
Example #12
Source File: test_scaling.py From me-ica with GNU Lesser General Public License v2.1 | 6 votes |
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))
Example #13
Source File: metrics.py From DDPAE-video-prediction with MIT License | 6 votes |
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
Example #14
Source File: hdf5dtypeTest.py From hsds with Apache License 2.0 | 5 votes |
def testBaseEnumTypeItem(self): mapping = {'RED': 0, 'GREEN': 1, 'BLUE': 2} dt = special_dtype(enum=(np.int8, mapping)) typeItem = hdf5dtype.getTypeItem(dt) typeSize = hdf5dtype.getItemSize(typeItem) self.assertEqual(typeItem['class'], 'H5T_ENUM') baseItem = typeItem['base'] self.assertEqual(baseItem['class'], 'H5T_INTEGER') self.assertEqual(baseItem['base'], 'H5T_STD_I8LE') self.assertTrue('mapping' in typeItem) self.assertEqual(typeItem['mapping']['GREEN'], 1) self.assertEqual(typeSize, 1)
Example #15
Source File: test_unet.py From eye-in-the-sky with Apache License 2.0 | 5 votes |
def rgb_to_onehot(rgb_arr, color_dict): num_classes = len(color_dict) shape = rgb_arr.shape[:2]+(num_classes,) print(shape) arr = np.zeros( shape, dtype=np.int8 ) for i, cls in enumerate(color_dict): arr[:,:,i] = np.all(rgb_arr.reshape( (-1,3) ) == color_dict[i], axis=1).reshape(shape[:2]) return arr
Example #16
Source File: payload.py From baseband with GNU General Public License v3.0 | 5 votes |
def encode_8bit(values): return np.clip(np.rint(values), -128, 127).astype(np.int8)
Example #17
Source File: main_unet.py From eye-in-the-sky with Apache License 2.0 | 5 votes |
def rgb_to_onehot(rgb_arr, color_dict): num_classes = len(color_dict) shape = rgb_arr.shape[:2]+(num_classes,) #print(shape) arr = np.zeros( shape, dtype=np.int8 ) for i, cls in enumerate(color_dict): arr[:,:,i] = np.all(rgb_arr.reshape( (-1,3) ) == color_dict[i], axis=1).reshape(shape[:2]) return arr
Example #18
Source File: payload.py From baseband with GNU General Public License v3.0 | 5 votes |
def decode_8bit(words): return words.view(np.int8, np.ndarray).astype(np.float32)
Example #19
Source File: xferfcn_input_test.py From python-control with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_clean_part_tuple_tuples_arrays(self): """Tuple of tuples of numpy arrays for all valid types.""" for dtype in int, int8, int16, int32, int64, float, float16, float32, float64, longdouble: num = ((array([1, 1], dtype=dtype), array([2, 2], dtype=dtype)), (array([3, 4], dtype=dtype), array([4, 4], dtype=dtype))) num_ = _clean_part(num) assert isinstance(num_, list) assert np.all([isinstance(part, list) for part in num_]) np.testing.assert_array_equal(num_[0][0], array([1.0, 1.0], dtype=float)) np.testing.assert_array_equal(num_[0][1], array([2.0, 2.0], dtype=float))
Example #20
Source File: xferfcn_input_test.py From python-control with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_clean_part_list_tuple_array(self): """List of tuple of numpy array for all valid types.""" for dtype in int, int8, int16, int32, int64, float, float16, float32, float64, longdouble: num = [(array([1, 1], dtype=dtype), array([2, 2], dtype=dtype))] num_ = _clean_part(num) assert isinstance(num_, list) assert np.all([isinstance(part, list) for part in num_]) np.testing.assert_array_equal(num_[0][0], array([1.0, 1.0], dtype=float)) np.testing.assert_array_equal(num_[0][1], array([2.0, 2.0], dtype=float))
Example #21
Source File: test_base.py From baseband with GNU General Public License v3.0 | 5 votes |
def decode_8bit(values): return values.view(np.int8).astype(np.float32)
Example #22
Source File: xferfcn_input_test.py From python-control with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_clean_part_tuple_list_array(self): """Tuple of list of numpy arrays for all valid types.""" for dtype in int, int8, int16, int32, int64, float, float16, float32, float64, longdouble: num = ([array([1, 1], dtype=dtype), array([2, 2], dtype=dtype)],) num_ = _clean_part(num) assert isinstance(num_, list) assert np.all([isinstance(part, list) for part in num_]) np.testing.assert_array_equal(num_[0][0], array([1.0, 1.0], dtype=float)) np.testing.assert_array_equal(num_[0][1], array([2.0, 2.0], dtype=float))
Example #23
Source File: xferfcn_input_test.py From python-control with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_clean_part_list_list_array(self): """List of list of numpy arrays for all valid types.""" for dtype in int, int8, int16, int32, int64, float, float16, float32, float64, longdouble: num = [[array([1, 1], dtype=dtype), array([2, 2], dtype=dtype)]] num_ = _clean_part(num) assert isinstance(num_, list) assert np.all([isinstance(part, list) for part in num_]) np.testing.assert_array_equal(num_[0][0], array([1.0, 1.0], dtype=float)) np.testing.assert_array_equal(num_[0][1], array([2.0, 2.0], dtype=float))
Example #24
Source File: xferfcn_input_test.py From python-control with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_clean_part_tuple_all_types2(self): """Test tuple of a single value for all data types.""" for dtype in [int, int8, int16, int32, int64, float, float16, float32, float64, longdouble]: num = (dtype(1), dtype(2)) num_ = _clean_part(num) assert isinstance(num_, list) assert np.all([isinstance(part, list) for part in num_]) np.testing.assert_array_equal(num_[0][0], array([1, 2], dtype=float))
Example #25
Source File: xferfcn_input_test.py From python-control with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_clean_part_list_all_types2(self): """List of list of numbers of all data types.""" for dtype in [int, int8, int16, int32, int64, float, float16, float32, float64, longdouble]: num = [dtype(1), dtype(2)] num_ = _clean_part(num) assert isinstance(num_, list) assert np.all([isinstance(part, list) for part in num_]) np.testing.assert_array_equal(num_[0][0], array([1.0, 2.0], dtype=float))
Example #26
Source File: mesh_io.py From simnibs with GNU General Public License v3.0 | 5 votes |
def __init__(self, triangles=None, tetrahedra=None): # gmsh fields self.elm_type = np.zeros(0, 'int8') self.tag1 = np.zeros(0, dtype='int16') self.tag2 = np.zeros(0, dtype='int16') self.node_number_list = np.zeros((0, 4), dtype='int32') if triangles is not None: assert triangles.shape[1] == 3 assert np.all(triangles > 0), "Node count should start at 1" self.node_number_list = np.zeros( (triangles.shape[0], 4), dtype='int32') self.node_number_list[:, :3] = triangles.astype('int32') self.node_number_list[:, 3] = -1 self.elm_type = np.ones((self.nr,), dtype='int32') * 2 if tetrahedra is not None: assert tetrahedra.shape[1] == 4 assert np.all(tetrahedra > 0), "Node count should start at 1" if len(self.node_number_list) == 0: self.node_number_list = tetrahedra.astype('int32') self.elm_type = np.ones((self.nr,), dtype='int32') * 4 else: self.node_number_list = np.vstack( (self.node_number_list, tetrahedra.astype('int32'))) self.elm_type = np.append( self.elm_type, np.ones(len(tetrahedra), dtype='int32') * 4) if len(self.node_number_list) > 0: self.tag1 = np.ones((self.nr,), dtype='int32') self.tag2 = np.ones((self.nr,), dtype='int32')
Example #27
Source File: xferfcn_input_test.py From python-control with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_clean_part_list_all_types(self): """Test list of a single value for all data types.""" for dtype in [int, int8, int16, int32, int64, float, float16, float32, float64, longdouble]: num = [dtype(1)] num_ = _clean_part(num) assert isinstance(num_, list) assert np.all([isinstance(part, list) for part in num_]) np.testing.assert_array_equal(num_[0][0], array([1.0], dtype=float))
Example #28
Source File: xferfcn_input_test.py From python-control with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_clean_part_all_np_array_types2(self): """Test numpy array for all types.""" for dtype in [int, int8, int16, int32, int64, float, float16, float32, float64, longdouble]: num = np.array([1, 2], dtype=dtype) num_ = _clean_part(num) assert isinstance(num_, list) assert np.all([isinstance(part, list) for part in num_]) np.testing.assert_array_equal(num_[0][0], array([1.0, 2.0], dtype=float))
Example #29
Source File: xferfcn_input_test.py From python-control with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_clean_part_all_scalar_types(self): """Test single scalar value for all valid data types.""" for dtype in [int, int8, int16, int32, int64, float, float16, float32, float64, longdouble]: num = dtype(1) num_ = _clean_part(num) assert isinstance(num_, list) assert np.all([isinstance(part, list) for part in num_]) np.testing.assert_array_equal(num_[0][0], array([1.0], dtype=float))
Example #30
Source File: xferfcn_input_test.py From python-control with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_clean_part_all_np_array_types(self): """Test scalar value in numpy array of ndim=0 for all data types.""" for dtype in [int, int8, int16, int32, int64, float, float16, float32, float64, longdouble]: num = np.array(1, dtype=dtype) num_ = _clean_part(num) assert isinstance(num_, list) assert np.all([isinstance(part, list) for part in num_]) np.testing.assert_array_equal(num_[0][0], array([1.0], dtype=float))