Python numpy.arrays() Examples

The following are 30 code examples for showing how to use numpy.arrays(). These examples are extracted from open source projects. 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 check out the related API usage on the sidebar.

You may also want to check out all available functions/classes of the module numpy , or try the search function .

Example 1
Project: interpret-text   Author: interpretml   File: glove_preprocessor.py    License: MIT License 6 votes vote down vote up
def generate_tokens(self, text: str) -> List[Any]:
        """ Split text into padded lists of tokens that are part of the recognized vocabulary

        :param text: a piece of text (e.g. a sentence)
        :type text: str
        :return:
        indexed_text (np.array): the token/vocabulary indices of
                recognized words in text, padded to the maximum sentence length
        mask (np.array): a mask indicating which indices in indexed_text
                correspond to words (1s) and which correspond to pads (0s)
        :rtype: tuple (of np.arrays)
        """
        indexed_text = [
            self.word_vocab[word]
            if ((word in self.counts) and (self.counts[word]
                                           > self.count_threshold))
            else self.word_vocab[UNK]
            for word in text.split()
        ]
        pad_length = max((self.token_cutoff - len(indexed_text)), 0)
        mask = [1] * min(len(indexed_text), self.token_cutoff) + [0] * pad_length

        indexed_text = indexed_text[0:self.token_cutoff] + [self.word_vocab[PAD]] * pad_length

        return [np.array(indexed_text), np.array(mask)] 
Example 2
Project: spektral   Author: danielegrattarola   File: ogb.py    License: MIT License 6 votes vote down vote up
def graph_to_numpy(graph, dtype=None):
    """
    Converts a graph in OGB's library-agnostic format to a representation in
    Numpy/Scipy. See the [Open Graph Benchmark's website](https://ogb.stanford.edu)
    for more information.
    :param graph: OGB library-agnostic graph;
    :param dtype: if set, all output arrays will be cast to this dtype.
    :return:
        - X: np.array of shape (N, F) with the node features;
        - A: scipy.sparse adjacency matrix of shape (N, N) in COOrdinate format;
        - E: if edge features are available, np.array of shape (n_edges, S),
            `None` otherwise.
    """
    N = graph['num_nodes']
    X = graph['node_feat'].astype(dtype)
    row, col = graph['edge_index']
    A = sp.coo_matrix((np.ones_like(row), (row, col)), shape=(N, N)).astype(dtype)
    E = graph['edge_feat'].astype(dtype)

    return X, A, E 
Example 3
Project: vnpy_crypto   Author: birforce   File: test_testing.py    License: MIT License 6 votes vote down vote up
def test_numpy_array_equal_unicode_message(self):
        # Test ensures that `assert_numpy_array_equals` raises the right
        # exception when comparing np.arrays containing differing
        # unicode objects (#20503)

        expected = """numpy array are different

numpy array values are different \\(33\\.33333 %\\)
\\[left\\]:  \\[á, à, ä\\]
\\[right\\]: \\[á, à, å\\]"""

        with tm.assert_raises_regex(AssertionError, expected):
            assert_numpy_array_equal(np.array([u'á', u'à', u'ä']),
                                     np.array([u'á', u'à', u'å']))
        with tm.assert_raises_regex(AssertionError, expected):
            assert_almost_equal(np.array([u'á', u'à', u'ä']),
                                np.array([u'á', u'à', u'å'])) 
Example 4
Project: btgym   Author: Kismuz   File: utils.py    License: GNU Lesser General Public License v3.0 6 votes vote down vote up
def as_array(struct):
    """
    Given a dictionary of lists or tuples returns dictionary of np.arrays of same structure.

    Args:
        struct: dictionary of list, tuples etc.

    Returns:
        dict of np.arrays
    """
    if isinstance(struct,dict):
        out = {}
        for key, value in struct.items():
            out[key] = as_array(value)
        return out

    else:
        return np.asarray(struct) 
Example 5
Project: Computable   Author: ktraunmueller   File: triinterpolate.py    License: MIT License 6 votes vote down vote up
def _interpolate_single_key(self, return_key, tri_index, x, y):
        """
        Performs the interpolation at points belonging to the triangulation
        (inside an unmasked triangles).

        Parameters
        ----------
        return_index : string key from {'z', 'dzdx', 'dzdy'}
            Identifies the requested values (z or its derivatives)
        tri_index : 1d integer array
            Valid triangle index (-1 prohibited)
        x, y : 1d arrays, same shape as `tri_index`
            Valid locations where interpolation is requested.

        Returns
        -------
        ret : 1-d array
            Returned array of the same size as *tri_index*
        """
        raise NotImplementedError("TriInterpolator subclasses" +
                                  "should implement _interpolate_single_key!") 
Example 6
Project: Computable   Author: ktraunmueller   File: triinterpolate.py    License: MIT License 6 votes vote down vote up
def get_function_hessians(self, alpha, J, ecc, dofs):
        """
        Parameters
        ----------
        *alpha* is a (N x 3 x 1) array (array of column-matrices) of
        barycentric coordinates
        *J* is a (N x 2 x 2) array of jacobian matrices (jacobian matrix at
        triangle first apex)
        *ecc* is a (N x 3 x 1) array (array of column-matrices) of triangle
        eccentricities
        *dofs* is a (N x 1 x 9) arrays (arrays of row-matrices) of computed
        degrees of freedom.

        Returns
        -------
        Returns the values of interpolated function 2nd-derivatives
        [d2z/dx2, d2z/dy2, d2z/dxdy] in global coordinates at locations alpha,
        as a column-matrices of shape (N x 3 x 1).
        """
        d2sdksi2 = self.get_d2Sidksij2(alpha, ecc)
        d2fdksi2 = _prod_vectorized(dofs, d2sdksi2)
        H_rot = self.get_Hrot_from_J(J)
        d2fdx2 = _prod_vectorized(d2fdksi2, H_rot)
        return _transpose_vectorized(d2fdx2) 
Example 7
Project: Mastering-Elasticsearch-7.0   Author: PacktPublishing   File: triinterpolate.py    License: MIT License 6 votes vote down vote up
def _interpolate_single_key(self, return_key, tri_index, x, y):
        """
        Performs the interpolation at points belonging to the triangulation
        (inside an unmasked triangles).

        Parameters
        ----------
        return_index : string key from {'z', 'dzdx', 'dzdy'}
            Identifies the requested values (z or its derivatives)
        tri_index : 1d integer array
            Valid triangle index (-1 prohibited)
        x, y : 1d arrays, same shape as `tri_index`
            Valid locations where interpolation is requested.

        Returns
        -------
        ret : 1-d array
            Returned array of the same size as *tri_index*
        """
        raise NotImplementedError("TriInterpolator subclasses" +
                                  "should implement _interpolate_single_key!") 
Example 8
Project: Mastering-Elasticsearch-7.0   Author: PacktPublishing   File: triinterpolate.py    License: MIT License 6 votes vote down vote up
def get_function_hessians(self, alpha, J, ecc, dofs):
        """
        Parameters
        ----------
        *alpha* is a (N x 3 x 1) array (array of column-matrices) of
        barycentric coordinates
        *J* is a (N x 2 x 2) array of jacobian matrices (jacobian matrix at
        triangle first apex)
        *ecc* is a (N x 3 x 1) array (array of column-matrices) of triangle
        eccentricities
        *dofs* is a (N x 1 x 9) arrays (arrays of row-matrices) of computed
        degrees of freedom.

        Returns
        -------
        Returns the values of interpolated function 2nd-derivatives
        [d2z/dx2, d2z/dy2, d2z/dxdy] in global coordinates at locations alpha,
        as a column-matrices of shape (N x 3 x 1).
        """
        d2sdksi2 = self.get_d2Sidksij2(alpha, ecc)
        d2fdksi2 = _prod_vectorized(dofs, d2sdksi2)
        H_rot = self.get_Hrot_from_J(J)
        d2fdx2 = _prod_vectorized(d2fdksi2, H_rot)
        return _transpose_vectorized(d2fdx2) 
Example 9
Project: Mastering-Elasticsearch-7.0   Author: PacktPublishing   File: triinterpolate.py    License: MIT License 6 votes vote down vote up
def _to_matrix_vectorized(M):
    """
    Builds an array of matrices from individuals np.arrays of identical
    shapes.
    *M*: ncols-list of nrows-lists of shape sh.

    Returns M_res np.array of shape (sh, nrow, ncols) so that:
        M_res[...,i,j] = M[i][j]
    """
    assert isinstance(M, (tuple, list))
    assert all(isinstance(item, (tuple, list)) for item in M)
    c_vec = np.asarray([len(item) for item in M])
    assert np.all(c_vec-c_vec[0] == 0)
    r = len(M)
    c = c_vec[0]
    M00 = np.asarray(M[0][0])
    dt = M00.dtype
    sh = [M00.shape[0], r, c]
    M_ret = np.empty(sh, dtype=dt)
    for irow in range(r):
        for icol in range(c):
            M_ret[:, irow, icol] = np.asarray(M[irow][icol])
    return M_ret 
Example 10
Project: matplotlib-4-abaqus   Author: Solid-Mechanics   File: triinterpolate.py    License: MIT License 6 votes vote down vote up
def _interpolate_single_key(self, return_key, tri_index, x, y):
        """
        Performs the interpolation at points belonging to the triangulation
        (inside an unmasked triangles).

        Parameters
        ----------
        return_index : string key from {'z', 'dzdx', 'dzdy'}
            Identifies the requested values (z or its derivatives)
        tri_index : 1d integer array
            Valid triangle index (-1 prohibited)
        x, y : 1d arrays, same shape as `tri_index`
            Valid locations where interpolation is requested.

        Returns
        -------
        ret : 1-d array
            Returned array of the same size as *tri_index*
        """
        raise NotImplementedError("TriInterpolator subclasses" +
                                  "should implement _interpolate_single_key!") 
Example 11
Project: matplotlib-4-abaqus   Author: Solid-Mechanics   File: triinterpolate.py    License: MIT License 6 votes vote down vote up
def get_function_hessians(self, alpha, J, ecc, dofs):
        """
        Parameters
        ----------
        *alpha* is a (N x 3 x 1) array (array of column-matrices) of
        barycentric coordinates
        *J* is a (N x 2 x 2) array of jacobian matrices (jacobian matrix at
        triangle first apex)
        *ecc* is a (N x 3 x 1) array (array of column-matrices) of triangle
        eccentricities
        *dofs* is a (N x 1 x 9) arrays (arrays of row-matrices) of computed
        degrees of freedom.

        Returns
        -------
        Returns the values of interpolated function 2nd-derivatives
        [d2z/dx2, d2z/dy2, d2z/dxdy] in global coordinates at locations alpha,
        as a column-matrices of shape (N x 3 x 1).
        """
        d2sdksi2 = self.get_d2Sidksij2(alpha, ecc)
        d2fdksi2 = _prod_vectorized(dofs, d2sdksi2)
        H_rot = self.get_Hrot_from_J(J)
        d2fdx2 = _prod_vectorized(d2fdksi2, H_rot)
        return _transpose_vectorized(d2fdx2) 
Example 12
Project: scanpy   Author: theislab   File: __init__.py    License: BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def compute_neighbors_rapids(
    X: np.ndarray,
    n_neighbors: int
):
    """Compute nearest neighbors using RAPIDS cuml.

    Parameters
    ----------
    X: array of shape (n_samples, n_features)
        The data to compute nearest neighbors for.
    n_neighbors
        The number of neighbors to use.

        Returns
    -------
    **knn_indices**, **knn_dists** : np.arrays of shape (n_observations, n_neighbors)
    """
    from cuml.neighbors import NearestNeighbors
    nn = NearestNeighbors(n_neighbors=n_neighbors)
    X_contiguous = np.ascontiguousarray(X, dtype=np.float32)
    nn.fit(X_contiguous)
    knn_distsq, knn_indices = nn.kneighbors(X_contiguous)
    return knn_indices, np.sqrt(knn_distsq) # cuml uses sqeuclidean metric so take sqrt 
Example 13
Project: pyiron   Author: pyiron   File: atoms.py    License: BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def group_points_by_symmetry(self, points):
        """
            This function classifies the points into groups according to the box symmetry given by spglib.

            Args:
                points: (np.array/list) nx3 array which contains positions

            Returns: list of arrays containing geometrically equivalent positions

            It is possible that the original points are not found in the returned list, as the positions outsie
            the box will be projected back to the box.
        """
        struct_copy = self.copy()
        points = np.array(points).reshape(-1, 3)
        struct_copy += Atoms(elements=len(points) * ["Hs"], positions=points)
        struct_copy.center_coordinates_in_unit_cell()
        group_IDs = struct_copy.get_symmetry()["equivalent_atoms"][
            struct_copy.select_index("Hs")
        ]
        return [
            np.round(points[group_IDs == ID], decimals=8) for ID in np.unique(group_IDs)
        ] 
Example 14
Project: pyiron   Author: pyiron   File: atoms.py    License: BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def write(self, filename, format=None, **kwargs):
        """
        Write atoms object to a file.

        see ase.io.write for formats.
        kwargs are passed to ase.io.write.

        Args:
            filename:
            format:
            **kwargs:

        Returns:

        """
        from ase.io import write

        atoms = self.copy()
        atoms.arrays["positions"] = atoms.positions
        write(filename, atoms, format, **kwargs) 
Example 15
Project: neural-network-animation   Author: miloharper   File: triinterpolate.py    License: MIT License 6 votes vote down vote up
def _interpolate_single_key(self, return_key, tri_index, x, y):
        """
        Performs the interpolation at points belonging to the triangulation
        (inside an unmasked triangles).

        Parameters
        ----------
        return_index : string key from {'z', 'dzdx', 'dzdy'}
            Identifies the requested values (z or its derivatives)
        tri_index : 1d integer array
            Valid triangle index (-1 prohibited)
        x, y : 1d arrays, same shape as `tri_index`
            Valid locations where interpolation is requested.

        Returns
        -------
        ret : 1-d array
            Returned array of the same size as *tri_index*
        """
        raise NotImplementedError("TriInterpolator subclasses" +
                                  "should implement _interpolate_single_key!") 
Example 16
Project: neural-network-animation   Author: miloharper   File: triinterpolate.py    License: MIT License 6 votes vote down vote up
def get_function_hessians(self, alpha, J, ecc, dofs):
        """
        Parameters
        ----------
        *alpha* is a (N x 3 x 1) array (array of column-matrices) of
        barycentric coordinates
        *J* is a (N x 2 x 2) array of jacobian matrices (jacobian matrix at
        triangle first apex)
        *ecc* is a (N x 3 x 1) array (array of column-matrices) of triangle
        eccentricities
        *dofs* is a (N x 1 x 9) arrays (arrays of row-matrices) of computed
        degrees of freedom.

        Returns
        -------
        Returns the values of interpolated function 2nd-derivatives
        [d2z/dx2, d2z/dy2, d2z/dxdy] in global coordinates at locations alpha,
        as a column-matrices of shape (N x 3 x 1).
        """
        d2sdksi2 = self.get_d2Sidksij2(alpha, ecc)
        d2fdksi2 = _prod_vectorized(dofs, d2sdksi2)
        H_rot = self.get_Hrot_from_J(J)
        d2fdx2 = _prod_vectorized(d2fdksi2, H_rot)
        return _transpose_vectorized(d2fdx2) 
Example 17
Project: flare   Author: mir-group   File: otf_parser.py    License: MIT License 6 votes vote down vote up
def parse_frame_line(frame_line):
    """parse a line in otf output.
    :param frame_line: frame line to be parsed
    :type frame_line: string
    :return: species, position, force, uncertainty, and velocity of atom
    :rtype: list, np.arrays
    """

    frame_line = frame_line.split()

    spec = str(frame_line[0])
    position = np.array([float(n) for n in frame_line[1:4]])
    force = np.array([float(n) for n in frame_line[4:7]])
    uncertainty = np.array([float(n) for n in frame_line[7:10]])
    velocity = np.array([float(n) for n in frame_line[10:13]])

    return spec, position, force, uncertainty, velocity 
Example 18
Project: yolo_v2   Author: rky0930   File: automobile_data.py    License: Apache License 2.0 6 votes vote down vote up
def make_dataset(x, y=None):
    """Create a slice Dataset from a pandas DataFrame and labels"""
    # TODO(markdaooust): simplify this after the 1.4 cut.
    # Convert the DataFrame to a dict
    x = dict(x)

    # Convert the pd.Series to np.arrays
    for key in x:
        x[key] = np.array(x[key])

    items = [x]
    if y is not None:
        items.append(np.array(y, dtype=np.float32))

    # Create a Dataset of slices
    return tf.data.Dataset.from_tensor_slices(tuple(items)) 
Example 19
Project: recruit   Author: Frank-qlu   File: test_assert_numpy_array_equal.py    License: Apache License 2.0 5 votes vote down vote up
def test_numpy_array_equal_unicode():
    # see gh-20503
    #
    # Test ensures that `assert_numpy_array_equals` raises the right
    # exception when comparing np.arrays containing differing unicode objects.
    msg = """numpy array are different

numpy array values are different \\(33\\.33333 %\\)
\\[left\\]:  \\[á, à, ä\\]
\\[right\\]: \\[á, à, å\\]"""

    with pytest.raises(AssertionError, match=msg):
        assert_numpy_array_equal(np.array([u"á", u"à", u"ä"]),
                                 np.array([u"á", u"à", u"å"])) 
Example 20
Project: ibllib   Author: int-brain-lab   File: cca.py    License: MIT License 5 votes vote down vote up
def plot_correlations(corrs, errors=None, ax=None, **plot_kwargs):
    """
    Correlation vs CCA dimension

    :param corrs: correlation values for the CCA dimensions
    :type corrs: 1-D vector
    :param errors: error values
    :type shuffled: 1-D array of size len(corrs)
    :param ax: axis to plot on (default None)
    :type ax: matplotlib axis object
    :return: axis if specified, or plot if axis = None
    """
    # evaluate if np.arrays are passed
    assert type(corrs) is np.ndarray, "'corrs' is not a numpy array."
    if errors is not None:
        assert type(errors) is np.ndarray, "'errors' is not a numpy array."
    # create axis if no axis is passed
    if ax is None:
        ax = plt.gca()
    # get the data for the x and y axis
    y_data = corrs
    x_data = range(1, (len(corrs) + 1))
    # create the plot object
    ax.plot(x_data, y_data, **plot_kwargs)
    if errors is not None:
        ax.fill_between(x_data, y_data - errors, y_data + errors, **plot_kwargs, alpha=0.2)
    # change y and x labels and ticks
    ax.set_xticks(x_data)
    ax.set_ylabel("Correlation")
    ax.set_xlabel("CCA dimension")
    return ax 
Example 21
Project: wsdm19cup   Author: lampts   File: make_knn_feats.py    License: MIT License 5 votes vote down vote up
def get_features_for_one(self, x):	
        '''	
            Computes KNN features for a single object `x`	
        '''	

        NN_output = self.NN.kneighbors(x)	

        # Vector of size `n_neighbors`	
        # Stores indices of the neighbors	
        # NN_output = distances,indices	
        neighs = NN_output[1][0]	

        # Vector of size `n_neighbors`	
        # Stores distances to corresponding neighbors	
        neighs_dist = NN_output[0][0] 	

        # Vector of size `n_neighbors`	
        # Stores labels of corresponding neighbors	
        neighs_y = self.y_train[neighs] 	

        ## ========================================== ##	
        ##              YOUR CODE BELOW	
        ## ========================================== ##	

        # We will accumulate the computed features here	
        # Eventually it will be a list of lists or np.arrays	
        # and we will use np.hstack to concatenate those	
        return_list = [] 	
	
        return return_list 
Example 22
Project: spektral   Author: danielegrattarola   File: misc.py    License: MIT License 5 votes vote down vote up
def pad_jagged_array(x, target_shape, dtype=np.float):
    """
    Given a jagged array of arbitrary dimensions, zero-pads all elements in the
    array to match the provided `target_shape`.
    :param x: a list or np.array of dtype object, containing np.arrays of
    varying dimensions
    :param target_shape: a tuple or list s.t. target_shape[i] >= x.shape[i]
    for each x in X.
    If `target_shape[i] = -1`, it will be automatically converted to X.shape[i], 
    so that passing a target shape of e.g. (-1, n, m) will leave the first 
    dimension of each element untouched (note that the creation of the output
    array may fail if the result is again a jagged array). 
    :param dtype: the dtype of the returned np.array
    :return: a zero-padded np.array of shape `(X.shape[0], ) + target_shape`
    """
    if isinstance(x, list):
        x = np.array(x)
    for i in range(len(x)):
        shapes = []
        for j in range(len(target_shape)):
            ts = target_shape[j]
            cs = x[i].shape[j]
            shapes.append((cs if ts == -1 else ts, cs))
        if x.ndim == 1:
            x[i] = np.pad(x[i], [(0, ts - cs) for ts, cs in shapes], 'constant')
        else:
            x = np.pad(x, [(0, 0)] + [(0, ts - cs) for ts, cs in shapes], 'constant')

    try:
        return np.array(x, dtype=dtype)
    except ValueError:
        return np.array([_ for _ in x], dtype=dtype) 
Example 23
Project: spektral   Author: danielegrattarola   File: data.py    License: MIT License 5 votes vote down vote up
def numpy_to_disjoint(X_list, A_list, E_list=None):
    """
    Converts a batch of graphs stored in lists (X, A, and optionally E) to the
    [disjoint mode](https://danielegrattarola.github.io/spektral/data/#disjoint-mode).

    Each entry i of the lists should be associated to the same graph, i.e.,
    `X_list[i].shape[0] == A_list[i].shape[0] == E_list[i].shape[0]`.

    :param X_list: a list of np.arrays of shape `(N, F)`;
    :param A_list: a list of np.arrays or sparse matrices of shape `(N, N)`;
    :param E_list: a list of np.arrays of shape `(N, N, S)`;
    :return:
        -  `X_out`: a rank 2 array of shape `(n_nodes, F)`;
        -  `A_out`: a rank 2 array of shape `(n_nodes, n_nodes)`;
        -  `E_out`: (only if `E_list` is given) a rank 2 array of shape
        `(n_edges, S)`;
    """
    X_out = np.vstack(X_list)
    A_list = [sp.coo_matrix(a) for a in A_list]
    if E_list is not None:
        if E_list[0].ndim == 3:
            E_list = [e[a.row, a.col] for e, a in zip(E_list, A_list)]
        E_out = np.vstack(E_list)
    A_out = sp.block_diag(A_list)
    n_nodes = np.array([x.shape[0] for x in X_list])
    I_out = np.repeat(np.arange(len(n_nodes)), n_nodes)
    if E_list is not None:
        return X_out, A_out, E_out, I_out
    else:
        return X_out, A_out, I_out 
Example 24
Project: spektral   Author: danielegrattarola   File: data.py    License: MIT License 5 votes vote down vote up
def numpy_to_batch(X_list, A_list, E_list=None):
    """
    Converts a batch of graphs stored in lists (X, A, and optionally E) to the
    [batch mode](https://danielegrattarola.github.io/spektral/data/#batch-mode)
    by zero-padding all X, A and E matrices to have the same node dimensions
    (`N_max`).

    Each entry i of the lists should be associated to the same graph, i.e.,
    `X_list[i].shape[0] == A_list[i].shape[0] == E_list[i].shape[0]`.

    Note that if `A_list` contains sparse matrices, they will be converted to
    dense np.arrays, which can be expensice.

    :param X_list: a list of np.arrays of shape `(N, F)`;
    :param A_list: a list of np.arrays or sparse matrices of shape `(N, N)`;
    :param E_list: a list of np.arrays of shape `(N, N, S)`;
    :return:
        -  `X_out`: a rank 3 array of shape `(batch, N_max, F)`;
        -  `A_out`: a rank 2 array of shape `(batch, N_max, N_max)`;
        -  `E_out`: (only if `E_list` if given) a rank 2 array of shape
        `(batch, N_max, N_max, S)`;
    """
    N_max = max([a.shape[-1] for a in A_list])
    X_out = pad_jagged_array(X_list, (N_max, -1))
    # Convert sparse matrices to dense
    if hasattr(A_list[0], 'toarray'):
        A_list = [a.toarray() for a in A_list]
    A_out = pad_jagged_array(A_list, (N_max, N_max))
    if E_list is not None:
        E_out = pad_jagged_array(E_list, (N_max, N_max, -1))
        return X_out, A_out, E_out
    else:
        return X_out, A_out 
Example 25
Project: spektral   Author: danielegrattarola   File: data.py    License: MIT License 5 votes vote down vote up
def batch_iterator(data, batch_size=32, epochs=1, shuffle=True):
    """
    Iterates over the data for the given number of epochs, yielding batches of
    size `batch_size`.
    :param data: np.array or list of np.arrays with the same first dimension;
    :param batch_size: number of samples in a batch;
    :param epochs: number of times to iterate over the data;
    :param shuffle: whether to shuffle the data at the beginning of each epoch
    :return: batches of size `batch_size`.
    """
    if not isinstance(data, list):
        data = [data]
    if len(set([len(item) for item in data])) > 1:
        raise ValueError('All arrays must have the same length')

    len_data = len(data[0])
    batches_per_epoch = int(len_data / batch_size)
    if len_data % batch_size != 0:
        batches_per_epoch += 1
    for epochs in range(epochs):
        if shuffle:
            shuffle_idx = np.random.permutation(np.arange(len_data))
            data = [np.array(item)[shuffle_idx] for item in data]
        for batch in range(batches_per_epoch):
            start = batch * batch_size
            stop = min(start + batch_size, len_data)
            if len(data) > 1:
                yield [item[start:stop] for item in data]
            else:
                yield data[0][start:stop] 
Example 26
Project: btgym   Author: Kismuz   File: utils.py    License: GNU Lesser General Public License v3.0 5 votes vote down vote up
def batch_pad(batch, to_size, _one_hot=False):
    """
    Pads given `batch` with zeros along zero dimension

    Args:
        batch:      processed rollout as dictionary of np.arrays
        to_size:    desired batch size

    Returns:
        dictionary with all included np.arrays being zero-padded to size [to_size, own_depth].
    """
    if isinstance(batch, dict):
        padded_batch = {}
        for key, struct in batch.items():
            # Mind one-hot action encoding:
            if key in ['action', 'last_action_reward']:
                one_hot = True

            else:
                one_hot = False

            padded_batch[key] = batch_pad(struct, to_size, one_hot)

    elif isinstance(batch, np.ndarray):
        shape = batch.shape
        assert shape[0] < to_size, \
            'Padded batch size must be greater than initial, got: {}, {}'.format(to_size, shape[0])

        pad = np.zeros((to_size - shape[0],) + shape[1:])
        if _one_hot:
            pad[:, 0, ...] = 1
        padded_batch = np.concatenate([batch, pad], axis=0)

    else:
        # Hit tuple, scalar or something else:
        padded_batch = batch

    return padded_batch 
Example 27
Project: DRL_DeliveryDuel   Author: ArztSamuel   File: buffer.py    License: MIT License 5 votes vote down vote up
def extend(self, data):
                """
                Ads a list of np.arrays to the end of the list of np.arrays.
                :param data: The np.array list to append.
                """
                self += list(np.array(data)) 
Example 28
Project: Computable   Author: ktraunmueller   File: triinterpolate.py    License: MIT License 5 votes vote down vote up
def __init__(self, triangulation, z, trifinder=None):
        if not isinstance(triangulation, Triangulation):
            raise ValueError("Expected a Triangulation object")
        self._triangulation = triangulation

        self._z = np.asarray(z)
        if self._z.shape != self._triangulation.x.shape:
            raise ValueError("z array must have same length as triangulation x"
                             " and y arrays")

        if trifinder is not None and not isinstance(trifinder, TriFinder):
            raise ValueError("Expected a TriFinder object")
        self._trifinder = trifinder or self._triangulation.get_trifinder()

        # Default scaling factors : 1.0 (= no scaling)
        # Scaling may be used for interpolations for which the order of
        # magnitude of x, y has an impact on the interpolant definition.
        # Please refer to :meth:`_interpolate_multikeys` for details.
        self._unit_x = 1.0
        self._unit_y = 1.0

        # Default triangle renumbering: None (= no renumbering)
        # Renumbering may be used to avoid unecessary computations
        # if complex calculations are done inside the Interpolator.
        # Please refer to :meth:`_interpolate_multikeys` for details.
        self._tri_renum = None

    # __call__ and gradient docstrings are shared by all subclasses
    # (except, if needed, relevant additions).
    # However these methods are only implemented in subclasses to avoid
    # confusion in the documentation. 
Example 29
Project: Computable   Author: ktraunmueller   File: triinterpolate.py    License: MIT License 5 votes vote down vote up
def get_function_values(self, alpha, ecc, dofs):
        """
        Parameters
        ----------
        alpha : is a (N x 3 x 1) array (array of column-matrices) of
        barycentric coordinates,
        ecc : is a (N x 3 x 1) array (array of column-matrices) of triangle
        eccentricities,
        dofs : is a (N x 1 x 9) arrays (arrays of row-matrices) of computed
        degrees of freedom.

        Returns
        -------
        Returns the N-array of interpolated function values.
        """
        subtri = np.argmin(alpha, axis=1)[:, 0]
        ksi = _roll_vectorized(alpha, -subtri, axis=0)
        E = _roll_vectorized(ecc, -subtri, axis=0)
        x = ksi[:, 0, 0]
        y = ksi[:, 1, 0]
        z = ksi[:, 2, 0]
        x_sq = x*x
        y_sq = y*y
        z_sq = z*z
        V = _to_matrix_vectorized([
            [x_sq*x], [y_sq*y], [z_sq*z], [x_sq*z], [x_sq*y], [y_sq*x],
            [y_sq*z], [z_sq*y], [z_sq*x], [x*y*z]])
        prod = _prod_vectorized(self.M, V)
        prod += _scalar_vectorized(E[:, 0, 0],
                                   _prod_vectorized(self.M0, V))
        prod += _scalar_vectorized(E[:, 1, 0],
                                   _prod_vectorized(self.M1, V))
        prod += _scalar_vectorized(E[:, 2, 0],
                                   _prod_vectorized(self.M2, V))
        s = _roll_vectorized(prod, 3*subtri, axis=0)
        return _prod_vectorized(dofs, s)[:, 0, 0] 
Example 30
Project: Computable   Author: ktraunmueller   File: triinterpolate.py    License: MIT License 5 votes vote down vote up
def __init__(self, vals, rows, cols, shape):
        """
        Creates a sparse matrix in coo format
        *vals*: arrays of values of non-null entries of the matrix
        *rows*: int arrays of rows of non-null entries of the matrix
        *cols*: int arrays of cols of non-null entries of the matrix
        *shape*: 2-tuple (n,m) of matrix shape

        """
        self.n, self.m = shape
        self.vals = np.asarray(vals, dtype=np.float64)
        self.rows = np.asarray(rows, dtype=np.int32)
        self.cols = np.asarray(cols, dtype=np.int32)