numpy.

Here are the examples of the python api numpy. taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.

3 Examples 7

Example 1

Project: drmad Source File: exact_rep.py
Function: init
    def __init__(self, length):
        # Use an array of Python 'long' ints which conveniently grow
        # as large as necessary. It's about 50X slower though...
        self.store = np.array([0L] * length, dtype=object)

Example 2

Project: diogenes Source File: test_utils.py
    def test_convert_to_sa(self):
        # already a structured array
        sa = np.array([(1, 1.0, 'a', datetime(2015, 01, 01)),
                       (2, 2.0, 'b', datetime(2016, 01, 01))],
                      dtype=[('int', int), ('float', float), ('str', 'O'),
                             ('date', 'M8[s]')])
        self.assertTrue(np.array_equal(sa, utils.convert_to_sa(sa)))

        # humogeneous array no col names provided
        nd = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
        ctrl = np.array([(1, 2, 3), (4, 5, 6), (7, 8, 9)],
                        dtype=[('f0', int), ('f1', int), ('f2', int)])
        self.assertTrue(np.array_equal(ctrl, utils.convert_to_sa(nd)))

        # humogeneous array with col names provided
        nd = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
        ctrl = np.array([(1, 2, 3), (4, 5, 6), (7, 8, 9)],
                        dtype=[('i0', int), ('i1', int), ('i2', int)])
        self.assertTrue(np.array_equal(ctrl, utils.convert_to_sa(
            nd,
            col_names=['i0', 'i1', 'i2'])))

        # list of lists no col name provided
        lol = [[1, 1, None],
               ['abc', 2, 3.4]]
        ctrl = np.array([('1', 1, np.nan),
                         ('abc', 2, 3.4)],
                        dtype=[('f0', 'S3'), ('f1', int), ('f2', float)])
        res = utils.convert_to_sa(lol)
        self.assertTrue(uft.array_equal(ctrl, res))

        # list of lists with col name provided
        lol = [['hello', 1.2, datetime(2012, 1, 1), None],
               [1.3, np.nan, None, '2013-01-01'],
               [1.4, 1.5, '2014-01-01', 'NO_SUCH_RECORD']]
        ctrl = np.array([('hello', 1.2, datetime(2012, 1, 1), utils.NOT_A_TIME),
                         ('1.3', np.nan, utils.NOT_A_TIME, datetime(2013, 1, 1)),
                         ('1.4', 1.5, datetime(2014, 1, 1), utils.NOT_A_TIME)],
                        dtype=[('i0', 'S5'), ('i1', float), ('i2', 'M8[us]'),
                               ('i3', 'M8[us]')])
        res = utils.convert_to_sa(lol, col_names = ['i0', 'i1', 'i2', 'i3'])
        self.assertTrue(uft.array_equal(ctrl, res))

Example 3

Project: python-rl Source File: batch_model.py
    def predictSet(self, states):
        pred = []
        known = self.areKnown(states)
        states = self.normStates(states)
        for a in range(self.numActions):
            if self.has_fit[a]:
                predictions = numpy.array(map(lambda (m,p): m.predict(states[a]) if p is None else numpy.ones((len(states[a]),))*p,
                                  zip(self.model[a], self.predConst[a]))).T

                pState = predictions[:,2:]
                pTerminate = self.model_termination(predictions[:,1], known[a])
                pRewards = self.exploration_reward(states[a], known[a], predictions[:,0])

                ranges = self.getStateSpace()[0]
                if self.params['relative']:
                    pred += [(self.denormState((pState + states[a]).clip(min=0, max=1)), pRewards, pTerminate.clip(min=0, max=1))]
                else:
                    pred += [(self.denormState(pState.clip(min=0, max=1)), pRewards, pTerminate.clip(min=0, max=1))]
            else:
                pred += [([None]*len(states[a]), [None]*len(states[a]), [None]*len(states[a]))]
        return pred