當前位置: 首頁>>代碼示例>>Python>>正文


Python array.array方法代碼示例

本文整理匯總了Python中dask.array.array方法的典型用法代碼示例。如果您正苦於以下問題:Python array.array方法的具體用法?Python array.array怎麽用?Python array.array使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在dask.array的用法示例。


在下文中一共展示了array.array方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: norm

# 需要導入模塊: from dask import array [as 別名]
# 或者: from dask.array import array [as 別名]
def norm(x: Union[np.ndarray, da.array], p: int) -> Union[np.ndarray, da.array]:
    """
    Compute p-norm across the features of a batch of instances.

    Parameters
    ----------
    x
        Batch of instances of shape [N, features].
    p
        Power of the norm.

    Returns
    -------
    Array where p-norm is applied to the features.
    """
    return (x ** p).sum(axis=1) ** (1 / p) 
開發者ID:SeldonIO,項目名稱:alibi-detect,代碼行數:18,代碼來源:distance.py

示例2: pairwise_distance

# 需要導入模塊: from dask import array [as 別名]
# 或者: from dask.array import array [as 別名]
def pairwise_distance(x: Union[np.ndarray, da.array],
                      y: Union[np.ndarray, da.array],
                      p: int = 2
                      ) -> Union[np.ndarray, da.array]:
    """
    Compute pairwise distance between 2 samples.

    Parameters
    ----------
    x
        Batch of instances of shape [Nx, features].
    y
        Batch of instances of shape [Ny, features].
    p
        Power of the norm used to compute the distance.

    Returns
    -------
    [Nx, Ny] matrix with pairwise distances.
    """
    assert len(x.shape) == len(y.shape) and len(x.shape) == 2 and x.shape[-1] == y.shape[-1]
    diff = x.reshape(x.shape + (1,)) - y.T.reshape((1,) + y.T.shape)  # [Nx,F,1]-[1,F,Ny]=[Nx,F,Ny]
    dist = norm(diff, p)  # [Nx,Ny]
    return dist 
開發者ID:SeldonIO,項目名稱:alibi-detect,代碼行數:26,代碼來源:distance.py

示例3: infer_sigma

# 需要導入模塊: from dask import array [as 別名]
# 或者: from dask.array import array [as 別名]
def infer_sigma(x: Union[np.ndarray, da.array],
                y: Union[np.ndarray, da.array],
                p: int = 2
                ) -> float:
    """
    Infer sigma used in the kernel by setting it to the median distance
    between each of the pairwise instances in x and y.

    Parameters
    ----------
    x
        Batch of instances of shape [Nx, features].
    y
        Batch of instances of shape [Ny, features].
    p
        Power used in the distance calculation, default equals 2 (Euclidean distance).
    chunks
        Chunk sizes for x and y when using dask to compute the pairwise distances.

    Returns
    -------
    Sigma used in the kernel.
    """
    dist = distance.pairwise_distance(x, y, p=p)
    return np.median(dist) if isinstance(dist, np.ndarray) else da.median(dist.reshape(-1,), axis=0).compute() 
開發者ID:SeldonIO,項目名稱:alibi-detect,代碼行數:27,代碼來源:kernels.py

示例4: _get_slice

# 需要導入模塊: from dask import array [as 別名]
# 或者: from dask.array import array [as 別名]
def _get_slice(segments, shape):
    """Segment a 1D or 2D array."""
    if not (1 <= len(shape) <= 2):
        raise ValueError('Cannot segment array of shape: %s' % str(shape))
    else:
        size = shape[0]
        slice_length = int(np.ceil(float(size) / segments))
        start_idx = 0
        end_idx = slice_length
        while start_idx < size:
            if len(shape) == 1:
                yield slice(start_idx, end_idx)
            else:
                yield (slice(start_idx, end_idx), slice(None))
            start_idx = end_idx
            end_idx = min(start_idx + slice_length, size) 
開發者ID:pytroll,項目名稱:pyresample,代碼行數:18,代碼來源:geometry.py

示例5: test_round_to_resolution

# 需要導入模塊: from dask import array [as 別名]
# 或者: from dask.array import array [as 別名]
def test_round_to_resolution(self):
        """Test rounding to given resolution"""
        # Scalar, integer resolution
        self.assertEqual(bucket.round_to_resolution(5.5, 2.), 6)
        # Scalar, non-integer resolution
        self.assertEqual(bucket.round_to_resolution(5.5, 1.7), 5.1)
        # List
        self.assertTrue(np.all(bucket.round_to_resolution([4.2, 5.6], 2) ==
                               np.array([4., 6.])))
        # Numpy array
        self.assertTrue(np.all(bucket.round_to_resolution(np.array([4.2, 5.6]), 2) ==
                               np.array([4., 6.])))
        # Dask array
        self.assertTrue(
            np.all(bucket.round_to_resolution(da.array([4.2, 5.6]), 2) ==
                   np.array([4., 6.]))) 
開發者ID:pytroll,項目名稱:pyresample,代碼行數:18,代碼來源:test_bucket.py

示例6: test_call

# 需要導入模塊: from dask import array [as 別名]
# 或者: from dask.array import array [as 別名]
def test_call(self):
        """Test palette compositing."""
        from satpy.composites import PaletteCompositor
        cmap_comp = PaletteCompositor('test_cmap_compositor')
        palette = xr.DataArray(np.array([[0, 0, 0], [127, 127, 127], [255, 255, 255]]),
                               dims=['value', 'band'])
        palette.attrs['palette_meanings'] = [2, 3, 4]

        data = xr.DataArray(np.array([[4, 3, 2], [2, 3, 4]], dtype=np.uint8), dims=['y', 'x'])
        res = cmap_comp([data, palette])
        exp = np.array([[[1., 0.498039, 0.],
                         [0., 0.498039, 1.]],
                        [[1., 0.498039, 0.],
                         [0., 0.498039, 1.]],
                        [[1., 0.498039, 0.],
                         [0., 0.498039, 1.]]])
        self.assertTrue(np.allclose(res, exp)) 
開發者ID:pytroll,項目名稱:satpy,代碼行數:19,代碼來源:test_composites.py

示例7: setUp

# 需要導入模塊: from dask import array [as 別名]
# 或者: from dask.array import array [as 別名]
def setUp(self):
        """Create test data."""
        from satpy.composites import GenericCompositor
        self.comp = GenericCompositor(name='test')
        self.comp2 = GenericCompositor(name='test2', common_channel_mask=False)

        all_valid = np.ones((1, 2, 2))
        self.all_valid = xr.DataArray(all_valid, dims=['bands', 'y', 'x'])
        first_invalid = np.reshape(np.array([np.nan, 1., 1., 1.]), (1, 2, 2))
        self.first_invalid = xr.DataArray(first_invalid,
                                          dims=['bands', 'y', 'x'])
        second_invalid = np.reshape(np.array([1., np.nan, 1., 1.]), (1, 2, 2))
        self.second_invalid = xr.DataArray(second_invalid,
                                           dims=['bands', 'y', 'x'])
        wrong_shape = np.reshape(np.array([1., 1., 1.]), (1, 3, 1))
        self.wrong_shape = xr.DataArray(wrong_shape, dims=['bands', 'y', 'x']) 
開發者ID:pytroll,項目名稱:satpy,代碼行數:18,代碼來源:test_composites.py

示例8: test_multiple_sensors

# 需要導入模塊: from dask import array [as 別名]
# 或者: from dask.array import array [as 別名]
def test_multiple_sensors(self):
        """Test the background compositing from multiple sensor data."""
        from satpy.composites import BackgroundCompositor
        import numpy as np
        comp = BackgroundCompositor("name")

        # L mode images
        attrs = {'mode': 'L', 'area': 'foo'}
        foreground = xr.DataArray(np.array([[[1., 0.5],
                                             [0., np.nan]]]),
                                  dims=('bands', 'y', 'x'),
                                  coords={'bands': [c for c in attrs['mode']]},
                                  attrs=attrs.copy())
        foreground.attrs['sensor'] = 'abi'
        background = xr.DataArray(np.ones((1, 2, 2)), dims=('bands', 'y', 'x'),
                                  coords={'bands': [c for c in attrs['mode']]},
                                  attrs=attrs.copy())
        background.attrs['sensor'] = 'glm'
        res = comp([foreground, background])
        self.assertEqual(res.attrs['area'], 'foo')
        self.assertTrue(np.all(res == np.array([[1., 0.5], [0., 1.]])))
        self.assertEqual(res.attrs['mode'], 'L')
        self.assertEqual(res.attrs['sensor'], {'abi', 'glm'}) 
開發者ID:pytroll,項目名稱:satpy,代碼行數:25,代碼來源:test_composites.py

示例9: test_get_flag_value

# 需要導入模塊: from dask import array [as 別名]
# 或者: from dask.array import array [as 別名]
def test_get_flag_value(self):
        """Test reading flag value from attributes based on a name."""
        from satpy.composites import _get_flag_value

        flag_values = da.array([1, 2])
        mask = da.array([[1, 2, 2],
                         [2, 1, 2],
                         [2, 2, 1]])
        mask = xr.DataArray(mask, dims=['y', 'x'])
        flag_meanings = ['Cloud-free_land', 'Cloud-free_sea']
        mask.attrs['flag_meanings'] = flag_meanings
        mask.attrs['flag_values'] = flag_values

        assert _get_flag_value(mask, 'Cloud-free_land') == 1
        assert _get_flag_value(mask, 'Cloud-free_sea') == 2

        flag_meanings_str = 'Cloud-free_land Cloud-free_sea'
        mask.attrs['flag_meanings'] = flag_meanings_str
        assert _get_flag_value(mask, 'Cloud-free_land') == 1
        assert _get_flag_value(mask, 'Cloud-free_sea') == 2 
開發者ID:pytroll,項目名稱:satpy,代碼行數:22,代碼來源:test_composites.py

示例10: predict

# 需要導入模塊: from dask import array [as 別名]
# 或者: from dask.array import array [as 別名]
def predict(self, X):
        """Predict the closest cluster each sample in X belongs to.
        In the vector quantization literature, `cluster_centers_` is called
        the code book and each value returned by `predict` is the index of
        the closest code in the code book.

        Parameters
        ----------
        X : array-like, shape = [n_samples, n_features]
            New data to predict.

        Returns
        -------
        labels : array, shape [n_samples,]
            Index of the cluster each sample belongs to.
        """
        check_is_fitted(self, "cluster_centers_")
        X = self._check_array(X)
        labels = pairwise_distances_argmin_min(X, self.cluster_centers_)[0].astype(
            np.int32
        )
        return labels 
開發者ID:dask,項目名稱:dask-ml,代碼行數:24,代碼來源:k_means.py

示例11: _array_numpy_to_dask

# 需要導入模塊: from dask import array [as 別名]
# 或者: from dask.array import array [as 別名]
def _array_numpy_to_dask(data, chunk_pixels, n_chunks_min=4):
    """
    Convert an array (e.g. XRF map) from numpy array to chunked Dask array. Select chunk
    size based on the desired number of pixels `chunk_pixels`. The array is considered
    as an image with pixels along axes 0 and 1. The array is chunked only along axes 0 and 1.

    Parameters
    ----------
    data: ndarray(float), 3D
        Numpy array of the shape `(ny, nx, ...)` with at least 2 dimensions. If `data` is
        an image, then `ny` and `nx` represent the image dimensions.
    chunk_pixels: int
        Desired number of pixels in a chunk. The actual number of pixels may differ from
        the desired number to accommodate minimum requirements on the number of chunks or
        limited size of the dataset.
    n_chunks_min: int
        minimum number of chunks, which should be selected based on the minimum number of
        workers that should be used to process the map. Each chunk will contain at least
        one pixel: if there is not enough pixels, then the number of chunks will be reduced.

    Results
    -------
    Dask array of the same shape as `data` with chunks selected based on the desired number
    of pixels `chunk_pixels`.
    """

    if not isinstance(data, np.ndarray) or (data.ndim < 2):
        raise ValueError(f"Parameter 'data' must numpy array with at least 2 dimensions: "
                         f"type(data)={type(data)}")

    ny, nx = data.shape[0:2]
    # Since numpy array is not chunked by default, set the original chunk size to (1,1)
    #   because here we are performing 'original' chunking
    chunk_y, chunk_x = _compute_optimal_chunk_size(chunk_pixels=chunk_pixels,
                                                   data_chunksize=(1, 1),
                                                   data_shape=(ny, nx),
                                                   n_chunks_min=n_chunks_min)

    return _chunk_numpy_array(data, (chunk_y, chunk_x)) 
開發者ID:NSLS-II,項目名稱:PyXRF,代碼行數:41,代碼來源:map_processing.py

示例12: maximum_mean_discrepancy

# 需要導入模塊: from dask import array [as 別名]
# 或者: from dask.array import array [as 別名]
def maximum_mean_discrepancy(x: Union[np.ndarray, da.array],
                             y: Union[np.ndarray, da.array],
                             kernel: Callable = gaussian_kernel,
                             **kwargs) -> float:
    """
    Compute maximum mean discrepancy between 2 samples.

    Parameters
    ----------
    x
        Batch of instances of shape [Nx, features].
    y
        Batch of instances of shape [Ny, features].
    kernel
        Kernel function. Defaults to a Gaussian kernel.
    kwargs
        Kwargs for the kernel function. For instance the kernel width `sigma` for the Gaussian kernel.

    Returns
    -------
    MMD^2 between the samples x and y.
    """
    k = partial(kernel, **kwargs) if kwargs else kernel
    nx, ny = x.shape[0], y.shape[0]
    cxx, cyy, cxy = 1 / (nx * (nx - 1)), 1 / (ny * (ny - 1)), 2 / (nx * ny)
    kxx, kyy, kxy = k(x, x), k(y, y), k(x, y)  # type: ignore
    mmd2 = cxx * (kxx.sum() - kxx.trace()) + cyy * (kyy.sum() - kyy.trace()) - cxy * kxy.sum()
    return mmd2 
開發者ID:SeldonIO,項目名稱:alibi-detect,代碼行數:30,代碼來源:distance.py

示例13: cityblock_batch

# 需要導入模塊: from dask import array [as 別名]
# 或者: from dask.array import array [as 別名]
def cityblock_batch(X: np.ndarray,
                    y: np.ndarray) -> np.ndarray:
    """
    Calculate the L1 distances between a batch of arrays X and an array of the same shape y.

    Parameters
    ----------
    X
        Batch of arrays to calculate the distances from
    y
        Array to calculate the distance to

    Returns
    -------
    Array of distances from each array in X to y

    """
    X_dim = len(X.shape)
    y_dim = len(y.shape)

    if X_dim == y_dim:
        assert y.shape[0] == 1, 'y must have batch size equal to 1'
    else:
        assert X.shape[1:] == y.shape, 'X and y must have matching shapes'

    return np.abs(X - y).sum(axis=tuple(np.arange(1, X_dim))).reshape(X.shape[0], -1) 
開發者ID:SeldonIO,項目名稱:alibi-detect,代碼行數:28,代碼來源:distance.py

示例14: permutation_test

# 需要導入模塊: from dask import array [as 別名]
# 或者: from dask.array import array [as 別名]
def permutation_test(x: Union[np.ndarray, da.array],
                     y: Union[np.ndarray, da.array],
                     n_permutations: int = 1000,
                     metric: Callable = maximum_mean_discrepancy,
                     **kwargs) -> np.float:
    """
    Apply a permutation test to samples x and y.

    Parameters
    ----------
    x
        Batch of instances of shape [Nx, features].
    y
        Batch of instances of shape [Ny, features].
    n_permutations
        Number of permutations used in the test.
    metric
        Distance metric used for the test. Defaults to Maximum Mean Discrepancy.
    kwargs
        Kwargs for the metric. For the default this includes for instance the kernel used.

    Returns
    -------
    p-value obtained from the test.
    """
    is_np = True if isinstance(x, np.ndarray) and isinstance(y, np.ndarray) else False
    n, k = x.shape[0], 0
    dist = metric(x, y, **kwargs)
    x_y = np.concatenate([x, y])
    if not is_np:  # dask array
        dist = dist.compute()
        xchunks, ychunks = x.chunksize, y.chunksize
        x_y = np.array(x_y)
    for _ in range(n_permutations):
        np.random.shuffle(x_y)
        x, y = x_y[:n], x_y[n:]
        if not is_np:
            x, y = da.from_array(x, chunks=xchunks), da.from_array(y, chunks=ychunks)
        dist_permutation = metric(x, y, **kwargs)
        k += dist <= (dist_permutation if is_np else dist_permutation.compute())
    return k / n_permutations 
開發者ID:SeldonIO,項目名稱:alibi-detect,代碼行數:43,代碼來源:statstest.py

示例15: __eq__

# 需要導入模塊: from dask import array [as 別名]
# 或者: from dask.array import array [as 別名]
def __eq__(self, other):
        """Test for approximate equality."""
        if self is other:
            return True
        if other.lons is None or other.lats is None:
            other_lons, other_lats = other.get_lonlats()
        else:
            other_lons = other.lons
            other_lats = other.lats

        if self.lons is None or self.lats is None:
            self_lons, self_lats = self.get_lonlats()
        else:
            self_lons = self.lons
            self_lats = self.lats

        if self_lons is other_lons and self_lats is other_lats:
            return True
        if isinstance(self_lons, DataArray) and np.ndarray is not DataArray:
            self_lons = self_lons.data
            self_lats = self_lats.data
        if isinstance(other_lons, DataArray) and np.ndarray is not DataArray:
            other_lons = other_lons.data
            other_lats = other_lats.data
        try:
            from dask.array import allclose
        except ImportError:
            from numpy import allclose
        try:
            return (allclose(self_lons, other_lons, atol=1e-6, rtol=5e-9, equal_nan=True) and
                    allclose(self_lats, other_lats, atol=1e-6, rtol=5e-9, equal_nan=True))
        except (AttributeError, ValueError):
            return False 
開發者ID:pytroll,項目名稱:pyresample,代碼行數:35,代碼來源:geometry.py


注:本文中的dask.array.array方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。