python类fromiter()的实例源码

program.py 文件源码 项目:codecad 作者: bluecube 项目源码 文件源码 阅读 33 收藏 0 点赞 0 评论 0
def make_program(shape):
    """ Returns numpy array containing the eval instructions for eval """
    return numpy.fromiter(_make_program_pieces(shape), pyopencl.cltypes.float)
program.py 文件源码 项目:codecad 作者: bluecube 项目源码 文件源码 阅读 37 收藏 0 点赞 0 评论 0
def make_program(shape):
    """ Returns numpy array containing the eval instructions for eval """
    return numpy.fromiter(_make_program_pieces(shape), pyopencl.cltypes.float)
util.py 文件源码 项目:fold 作者: tensorflow 项目源码 文件源码 阅读 23 收藏 0 点赞 0 评论 0
def __array__(self, dtype=None):
    """NumPy array protocol; returns iterator values as an ndarray."""
    if self._value is None:
      # Call fromiter if we can; it is faster and avoids the extra
      # copy, but doesn't support object types and requires a dtype.
      if dtype is None or dtype.hasobject:
        self._value = np.array(list(self._iterator), dtype)
      else:
        self._value = np.fromiter(self._iterator, dtype)
    return self._value
test_regression.py 文件源码 项目:radar 作者: amoose136 项目源码 文件源码 阅读 26 收藏 0 点赞 0 评论 0
def test_mem_on_invalid_dtype(self):
        "Ticket #583"
        self.assertRaises(ValueError, np.fromiter, [['12', ''], ['13', '']], str)
test_regression.py 文件源码 项目:radar 作者: amoose136 项目源码 文件源码 阅读 36 收藏 0 点赞 0 评论 0
def test_mem_fromiter_invalid_dtype_string(self, level=rlevel):
        x = [1, 2, 3]
        self.assertRaises(ValueError,
                              np.fromiter, [xi for xi in x], dtype='S')
test_regression.py 文件源码 项目:radar 作者: amoose136 项目源码 文件源码 阅读 27 收藏 0 点赞 0 评论 0
def test_fromiter_bytes(self):
        # Ticket #1058
        a = np.fromiter(list(range(10)), dtype='b')
        b = np.fromiter(list(range(10)), dtype='B')
        assert_(np.alltrue(a == np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])))
        assert_(np.alltrue(b == np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])))
test_regression.py 文件源码 项目:radar 作者: amoose136 项目源码 文件源码 阅读 22 收藏 0 点赞 0 评论 0
def test_fromiter_comparison(self, level=rlevel):
        a = np.fromiter(list(range(10)), dtype='b')
        b = np.fromiter(list(range(10)), dtype='B')
        assert_(np.alltrue(a == np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])))
        assert_(np.alltrue(b == np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])))
test_regression.py 文件源码 项目:radar 作者: amoose136 项目源码 文件源码 阅读 34 收藏 0 点赞 0 评论 0
def test_duplicate_field_names_assign(self):
        ra = np.fromiter(((i*3, i*2) for i in range(10)), dtype='i8,f8')
        ra.dtype.names = ('f1', 'f2')
        repr(ra)  # should not cause a segmentation fault
        assert_raises(ValueError, setattr, ra.dtype, 'names', ('f1', 'f1'))
test_numeric.py 文件源码 项目:radar 作者: amoose136 项目源码 文件源码 阅读 27 收藏 0 点赞 0 评论 0
def test_lengths(self):
        expected = np.array(list(self.makegen()))
        a = np.fromiter(self.makegen(), int)
        a20 = np.fromiter(self.makegen(), int, 20)
        self.assertTrue(len(a) == len(expected))
        self.assertTrue(len(a20) == 20)
        self.assertRaises(ValueError, np.fromiter,
                          self.makegen(), int, len(expected) + 10)
test_numeric.py 文件源码 项目:radar 作者: amoose136 项目源码 文件源码 阅读 28 收藏 0 点赞 0 评论 0
def test_values(self):
        expected = np.array(list(self.makegen()))
        a = np.fromiter(self.makegen(), int)
        a20 = np.fromiter(self.makegen(), int, 20)
        self.assertTrue(np.alltrue(a == expected, axis=0))
        self.assertTrue(np.alltrue(a20 == expected[:20], axis=0))
test_numeric.py 文件源码 项目:radar 作者: amoose136 项目源码 文件源码 阅读 27 收藏 0 点赞 0 评论 0
def test_2592(self):
        # Test iteration exceptions are correctly raised.
        count, eindex = 10, 5
        self.assertRaises(NIterError, np.fromiter,
                          self.load_data(count, eindex), dtype=int, count=count)
test_numeric.py 文件源码 项目:radar 作者: amoose136 项目源码 文件源码 阅读 25 收藏 0 点赞 0 评论 0
def test_2592_edge(self):
        # Test iter. exceptions, edge case (exception at end of iterator).
        count = 10
        eindex = count-1
        self.assertRaises(NIterError, np.fromiter,
                          self.load_data(count, eindex), dtype=int, count=count)
smesh.py 文件源码 项目:Sverchok 作者: Sverchok 项目源码 文件源码 阅读 25 收藏 0 点赞 0 评论 0
def from_pydata(cls, faces):

        loop_total = np.empty(len(faces), dtype=np.uint32)
        loop_start = np.zeros(len(faces), dtype=np.uint32)
        loop_total[:] = tuple(map(len, faces))
        loop_start[1:] = loop_total[:-1].cumsum()
        vertex_indices = np.fromiter(chain.from_iterable(faces),
                                     dtype=np.uint32,)
                                     #count=loop_start.sum())
        return cls(loop_start, loop_total, vertex_indices)
simple.py 文件源码 项目:luckyhorse 作者: alexmbird 项目源码 文件源码 阅读 37 收藏 0 点赞 0 评论 0
def _calculate(self, period):
        data = list(self.loadTradesForPeriod(period))
        if len(data) == 0:
            raise InsufficientDataError()
        values      = np.fromiter(map(attrgetter('price'), data), np.float, len(data))
        weights     = np.fromiter(map(attrgetter('volume'), data), np.float, len(data))
        mean, std = weighted_avg_and_std(values, weights)
        return (mean,)
common.py 文件源码 项目:medaka 作者: nanoporetech 项目源码 文件源码 阅读 21 收藏 0 点赞 0 评论 0
def rle(array, low_mem=False):
    """Calculate a run length encoding (rle), of an input vector.

    :param array: 1D input array.
    :param low_mem: use a lower memory implementation

    returns: structured array with fields `start`, `length`, and `value`.
    """
    if len(array.shape) != 1:
        raise TypeError("Input array must be one dimensional.")
    dtype = [('length', int), ('start', int), ('value', array.dtype)]

    if not low_mem:
        pos = np.where(np.diff(array) != 0)[0]
        pos = np.concatenate(([0], pos+1, [len(array)]))
        return np.fromiter(
            ((length, start, array[start]) for (length, start) in zip(pos[1:], pos[:-1])),
            dtype, count=len(pos) - 1,
        )
    else:
        def _gen():
            start = 0
            for key, group in itertools.groupby(array):
                length = sum(1 for x in group)
                yield length, start, key
                start += length
        return np.fromiter(_gen(), dtype=dtype)
utils.py 文件源码 项目:char-rnn-text-generation 作者: yxtay 项目源码 文件源码 阅读 23 收藏 0 点赞 0 评论 0
def encode_text(text, char2id=CHAR2ID):
    """
    encode text to array of integers with CHAR2ID
    """
    return np.fromiter((char2id.get(ch, 0) for ch in text), int)
isp_data_pollution.py 文件源码 项目:isp-data-pollution 作者: essandess 项目源码 文件源码 阅读 23 收藏 0 点赞 0 评论 0
def entropy(self,p):
        return -np.fromiter((self.xlgx(x) for x in p.flatten()),dtype=p.dtype).sum()
isp_data_pollution.py 文件源码 项目:isp-data-pollution 作者: essandess 项目源码 文件源码 阅读 37 收藏 0 点赞 0 评论 0
def draw_domain(self,log_sampling=False):
        """ Draw a single, random domain. """
        domain = None
        domain_array = np.array([dmn for dmn in self.domain_links])
        domain_count = np.array([len(self.domain_links[domain_array[k]]) for k in range(domain_array.shape[0])])
        p = np.array([np.float(c) for c in domain_count])
        count_total = p.sum()
        if log_sampling:  # log-sampling [log(x+1)] to bias lower count domains
            p = np.fromiter((np.log1p(x) for x in p), dtype=p.dtype)
        if count_total > 0:
            p = p/p.sum()
            cnts = npr.multinomial(1, pvals=p)
            k = int(np.nonzero(cnts)[0])
            domain = domain_array[k]
        return domain
test_regression.py 文件源码 项目:krpcScripts 作者: jwvanderbeck 项目源码 文件源码 阅读 21 收藏 0 点赞 0 评论 0
def test_mem_on_invalid_dtype(self):
        "Ticket #583"
        self.assertRaises(ValueError, np.fromiter, [['12', ''], ['13', '']], str)
test_regression.py 文件源码 项目:krpcScripts 作者: jwvanderbeck 项目源码 文件源码 阅读 22 收藏 0 点赞 0 评论 0
def test_mem_fromiter_invalid_dtype_string(self, level=rlevel):
        x = [1, 2, 3]
        self.assertRaises(ValueError,
                              np.fromiter, [xi for xi in x], dtype='S')


问题


面经


文章

微信
公众号

扫码关注公众号