本文整理汇总了Python中numpy.fromiter方法的典型用法代码示例。如果您正苦于以下问题:Python numpy.fromiter方法的具体用法?Python numpy.fromiter怎么用?Python numpy.fromiter使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类numpy
的用法示例。
在下文中一共展示了numpy.fromiter方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: make2d
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import fromiter [as 别名]
def make2d(array, cols=None, dtype=None):
'''
Make a 2D array from an array of arrays. The `cols' and `dtype'
arguments can be omitted if the array is not empty.
'''
if (cols is None or dtype is None) and not len(array):
raise RuntimeError("cols and dtype must be specified for empty "
"array")
if cols is None:
cols = len(array[0])
if dtype is None:
dtype = array[0].dtype
return _np.fromiter(array, [('_', dtype, (cols,))],
count=len(array))['_']
示例2: test_constructor_object_dtype
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import fromiter [as 别名]
def test_constructor_object_dtype(self):
# GH 11856
arr = SparseArray(['A', 'A', np.nan, 'B'], dtype=np.object)
assert arr.dtype == SparseDtype(np.object)
assert np.isnan(arr.fill_value)
arr = SparseArray(['A', 'A', np.nan, 'B'], dtype=np.object,
fill_value='A')
assert arr.dtype == SparseDtype(np.object, 'A')
assert arr.fill_value == 'A'
# GH 17574
data = [False, 0, 100.0, 0.0]
arr = SparseArray(data, dtype=np.object, fill_value=False)
assert arr.dtype == SparseDtype(np.object, False)
assert arr.fill_value is False
arr_expected = np.array(data, dtype=np.object)
it = (type(x) == type(y) and x == y for x, y in zip(arr, arr_expected))
assert np.fromiter(it, dtype=np.bool).all()
示例3: decons_obs_group_ids
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import fromiter [as 别名]
def decons_obs_group_ids(comp_ids, obs_ids, shape, labels, xnull):
"""
reconstruct labels from observed group ids
Parameters
----------
xnull: boolean,
if nulls are excluded; i.e. -1 labels are passed through
"""
if not xnull:
lift = np.fromiter(((a == -1).any() for a in labels), dtype='i8')
shape = np.asarray(shape, dtype='i8') + lift
if not is_int64_overflow_possible(shape):
# obs ids are deconstructable! take the fast route!
out = decons_group_index(obs_ids, shape)
return out if xnull or not lift.any() \
else [x - y for x, y in zip(out, lift)]
i = unique_label_indices(comp_ids)
i8copy = lambda a: a.astype('i8', subok=False, copy=True)
return [i8copy(lab[i]) for lab in labels]
示例4: divide_qualities_into_bins
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import fromiter [as 别名]
def divide_qualities_into_bins(qualities, n_bins=4):
"""Divides the raw quality scores in bins according to the mean phred
quality of the sequence they come from
Args:
qualities (list): raw count of all the phred scores and mean sequence
quality
n_bins (int): number of bins to create (default: 4)
Returns:
list: a list of lists containing the binned quality scores
"""
logger = logging.getLogger(__name__)
logger.debug('Dividing qualities into mean clusters')
bin_lists = [[] for _ in range(n_bins)] # create list of `n_bins` list
ranges = np.split(np.array(range(40)), n_bins)
for quality in qualities:
mean = int(quality[0][1]) # mean is at 1 and same regardless of b pos
which_array = 0
for array in ranges:
if mean in array:
read = np.fromiter((q[0] for q in quality), dtype=np.float)
bin_lists[which_array].append(read)
which_array += 1
return bin_lists
示例5: make2d
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import fromiter [as 别名]
def make2d(array, cols=None, dtype=None):
'''
Make a 2D array from an array of arrays. The `cols' and `dtype'
arguments can be omitted if the array is not empty.
'''
if (cols is None or dtype is None) and not len(array):
raise RuntimeError("cols and dtype must be specified for empty "
"array")
if cols is None:
cols = len(array[0])
if dtype is None:
dtype = array[0].dtype
return _np.fromiter(array, [('_', dtype, (cols,))],
count=len(array))['_']
示例6: create_array
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import fromiter [as 别名]
def create_array(width, height, background=None):
"""returns an array.array or numpy.array of the correct size and
with the given background color"""
if numpy is not None:
if background is None:
return numpy.zeros(width * height, dtype=numpy.uint32)
else:
iterable = (background for _ in range(width * height))
return numpy.fromiter(iterable, numpy.uint32)
else:
# Use the smallest typecode that can store a 32-bit unsigned integer
typecode = "I" if array.array("I").itemsize >= 4 else "L"
background = (background if background is not None else
ColorForName["transparent"])
return array.array(typecode, [background] * width * height)
# Taken from rgb.txt and converted to ARGB (with the addition of
# transparent). Default is solid black.
示例7: test_constructor_object_dtype
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import fromiter [as 别名]
def test_constructor_object_dtype(self):
# GH 11856
arr = SparseArray(['A', 'A', np.nan, 'B'], dtype=np.object)
assert arr.dtype == np.object
assert np.isnan(arr.fill_value)
arr = SparseArray(['A', 'A', np.nan, 'B'], dtype=np.object,
fill_value='A')
assert arr.dtype == np.object
assert arr.fill_value == 'A'
# GH 17574
data = [False, 0, 100.0, 0.0]
arr = SparseArray(data, dtype=np.object, fill_value=False)
assert arr.dtype == np.object
assert arr.fill_value is False
arr_expected = np.array(data, dtype=np.object)
it = (type(x) == type(y) and x == y for x, y in zip(arr, arr_expected))
assert np.fromiter(it, dtype=np.bool).all()
示例8: load_word2vec
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import fromiter [as 别名]
def load_word2vec(word2vec_model_path,embed_size):
"""
load pretrained word2vec in txt format
:param word2vec_model_path:
:return: word2vec_dict. word2vec_dict[word]=vector
"""
#word2vec_object = codecs.open(word2vec_model_path,'r','utf-8') #open(word2vec_model_path,'r')
#lines=word2vec_object.readlines()
#word2vec_dict={}
#for i,line in enumerate(lines):
# if i==0: continue
# string_list=line.strip().split(" ")
# word=string_list[0]
# vector=string_list[1:][0:embed_size]
# word2vec_dict[word]=vector
######################
word2vec_dict = {}
with open(word2vec_model_path, errors='ignore') as f:
meta = f.readline()
for line in f.readlines():
items = line.split(' ')
#if len(items[0]) > 1 and items[0] in vocab:
word2vec_dict[items[0]] = np.fromiter(items[1:][0:embed_size], dtype=float)
return word2vec_dict
示例9: cartesian_product
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import fromiter [as 别名]
def cartesian_product(X):
'''
Numpy version of itertools.product or pandas.compat.product.
Sometimes faster (for large inputs)...
Examples
--------
>>> cartesian_product([list('ABC'), [1, 2]])
[array(['A', 'A', 'B', 'B', 'C', 'C'], dtype='|S1'),
array([1, 2, 1, 2, 1, 2])]
'''
lenX = np.fromiter((len(x) for x in X), dtype=int)
cumprodX = np.cumproduct(lenX)
a = np.roll(cumprodX, 1)
a[0] = 1
b = cumprodX[-1] / cumprodX
return [np.tile(np.repeat(x, b[i]),
np.product(a[i]))
for i, x in enumerate(X)]
示例10: __load
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import fromiter [as 别名]
def __load(fh):
return numpy.fromiter(Obj.__read(fh), dtype=Obj.obj_dtype)
示例11: __load_ascii
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import fromiter [as 别名]
def __load_ascii(fh, header):
return numpy.fromiter(Stl.__ascii_reader(fh, header), dtype=Stl.stl_dtype)
示例12: to_timestamps
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import fromiter [as 别名]
def to_timestamps(values):
try:
if len(values) == 0:
return []
if isinstance(values[0], (numpy.datetime64, datetime.datetime)):
times = numpy.array(values)
else:
try:
# Try to convert to float. If it works, then we consider
# timestamps to be number of seconds since Epoch
# e.g. 123456 or 129491.1293
float(values[0])
except ValueError:
try:
# Try to parse the value as a string of ISO timestamp
# e.g. 2017-10-09T23:23:12.123
numpy.datetime64(values[0])
except ValueError:
# Last chance: it can be relative timestamp, so convert
# to timedelta relative to now()
# e.g. "-10 seconds" or "5 minutes"
times = numpy.fromiter(
numpy.add(numpy.datetime64(utcnow()),
[to_timespan(v, True) for v in values]),
dtype='datetime64[ns]', count=len(values))
else:
times = numpy.array(values, dtype='datetime64[ns]')
else:
times = numpy.array(values, dtype='float') * 10e8
except ValueError:
raise ValueError("Unable to convert timestamps")
times = times.astype('datetime64[ns]')
if (times < unix_universal_start64).any():
raise ValueError('Timestamp must be after Epoch')
return times
示例13: _encode_measures
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import fromiter [as 别名]
def _encode_measures(self, measures):
return numpy.fromiter(measures,
dtype=TIMESERIES_ARRAY_DTYPE).tobytes()
示例14: _transitions_matrix
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import fromiter [as 别名]
def _transitions_matrix(self):
""" Return a matrix of transition log probabilities. """
trans_iter = (self._transitions[sj].logprob(si)
for sj in self._states
for si in self._states)
transitions_logprob = np.fromiter(trans_iter, dtype=np.float64)
N = len(self._states)
return transitions_logprob.reshape((N, N)).T
示例15: _outputs_vector
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import fromiter [as 别名]
def _outputs_vector(self, symbol):
"""
Return a vector with log probabilities of emitting a symbol
when entering states.
"""
out_iter = (self._output_logprob(sj, symbol) for sj in self._states)
return np.fromiter(out_iter, dtype=np.float64)