本文整理汇总了Python中numpy.ndarrays方法的典型用法代码示例。如果您正苦于以下问题:Python numpy.ndarrays方法的具体用法?Python numpy.ndarrays怎么用?Python numpy.ndarrays使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类numpy
的用法示例。
在下文中一共展示了numpy.ndarrays方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: histogram
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import ndarrays [as 别名]
def histogram(t, L):
"""
A: If t is a list of tensors/np.ndarrays, B is executed for all, yielding len(ts) histograms, which are summed
per bin
B: convert t to numpy, count bins.
:param t: tensor or list of tensor, each expected to be in [0, L)
:param L: number of symbols
:return: length-L array, containing at l the number of values mapping to to symbol l
"""
if isinstance(t, list):
ts = t
histograms = np.stack((histogram(t, L) for t in ts), axis=0) # get array (len(ts) x L)
return np.sum(histograms, 0)
assert 0 <= t.min() and t.max() < L, (t.min(), t.max())
a = tensor_to_np(t)
counts, _ = np.histogram(a, np.arange(L+1)) # +1 because np.histogram takes bin edges, including rightmost edge
return counts
# Gradients --------------------------------------------------------------------
示例2: nan_dot
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import ndarrays [as 别名]
def nan_dot(A, B):
"""
Returns np.dot(left_matrix, right_matrix) with the convention that
nan * 0 = 0 and nan * x = nan if x != 0.
Parameters
----------
A, B : np.ndarrays
"""
# Find out who should be nan due to nan * nonzero
should_be_nan_1 = np.dot(np.isnan(A), (B != 0))
should_be_nan_2 = np.dot((A != 0), np.isnan(B))
should_be_nan = should_be_nan_1 + should_be_nan_2
# Multiply after setting all nan to 0
# This is what happens if there were no nan * nonzero conflicts
C = np.dot(np.nan_to_num(A), np.nan_to_num(B))
C[should_be_nan] = np.nan
return C
示例3: _max_error
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import ndarrays [as 别名]
def _max_error(arrays1, arrays2):
"""Computes maximum elementwise gap between two lists of ndarrays.
Computes the maximum elementwise gap between two lists with the same length,
of arrays with the same shape.
Args:
arrays1: a lists of np.ndarrays.
arrays2: a lists of np.ndarrays of the same shape as arrays1.
Returns:
The maximum elementwise absolute difference between the two lists of arrays.
"""
error = 0
for array1, array2 in zip(arrays1, arrays2):
if array1.size or array2.size: # Handle zero size ndarrays correctly
error = np.maximum(error, np.fabs(array1 - array2).max())
return error
示例4: _nested_ndarrays_to_tensors
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import ndarrays [as 别名]
def _nested_ndarrays_to_tensors(data):
"""Converts possibly nested lists of np.ndarrays and Tensors to Tensors.
This is necessary in case some data in the Structure is already computed. We
just pass everything to tf.Session.run, and some data may be a tf.constant
which is just spit back out.
Args:
data: An np.ndarray, Tensor, or list recursively containing the same types.
Returns:
data with all np.ndarrays converted to Tensors.
Raises:
ValueError: If unexpected data was given.
"""
if isinstance(data, list):
return [_nested_ndarrays_to_tensors(element) for element in data]
elif isinstance(data, np.ndarray):
return tf.constant(data)
elif isinstance(data, tf.Tensor):
return data
else:
raise ValueError('Unexpected data: %s' % data)
示例5: __init__
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import ndarrays [as 别名]
def __init__(self, sizes, num_sequences=1, state_variables=None):
"""Constructs a list of recurrent layer states and initializes them.
This will create state vectors for each layer for each of the sequences
that will be processed in parallel. Unless ``state_variables`` is given,
the vectors will be initialized to zeros.
:type sizes: list of ints
:param sizes: size of each recurrent layer state
:type num_sequences: int
:param num_sequences: number of sequences to be processed in parallel
:type state_variables: list of numpy.ndarrays
:param state_variables: if set to other than ``None``, sets the initial
recurrent layer states to this instead of zeros
"""
self.sizes = sizes
self.num_sequences = num_sequences
if state_variables is None:
self.reset()
else:
self.set(state_variables)
示例6: set
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import ndarrays [as 别名]
def set(self, state_variables):
"""Sets the state vector of every recurrent layer.
:type state_variables: list of numpy.ndarrays
:param state_variables: a matrix for each recurrent layer that contains
the state vector for each sequence at one time
step
"""
if len(state_variables) != len(self.sizes):
raise ValueError("Recurrent state should contain as many arrays "
"as there are recurrent layers.")
for state_variable, size in zip(state_variables, self.sizes):
if state_variable.shape[0] != 1:
raise ValueError("Recurrent state should contain only one time "
"step.")
if state_variable.shape[1] != self.num_sequences:
raise ValueError("Recurrent state contains incorrect number of "
"sequences.")
if state_variable.shape[2] != size:
raise ValueError("Recurrent state contains a layer with "
"incorrect size.")
self._state_variables = state_variables
示例7: get
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import ndarrays [as 别名]
def get(self, index=None):
"""Returns the state matrix of a given layer, or a list of the matrices
of all layers.
:type index: int
:param index: index of a recurrent layer in the state object; if
set to other than ``None``, returns only the matrix
for the corresponding layer
:rtype: numpy.ndarray or list of numpy.ndarrays
:returns: a matrix for each recurrent layer that contains the state
vector for each sequence at one time step
"""
if index is not None:
return self._state_variables[index]
else:
return self._state_variables
示例8: safe_vstack
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import ndarrays [as 别名]
def safe_vstack(a, b):
"""Stack two arrays on top of one another.
Safely handle vertical stacking of arrays. This works for
either np.ndarrays or pd.DataFrames. The types of both inputs must
match!
Parameters
----------
a : array-like, shape=(n_samples, n_features)
The array that will be stacked on the top vertically.
b : array-like, shape=(n_samples, n_features)
The array that will be stacked below the other vertically.
"""
# we can only pd.concat if they BOTH are DataFrames
if all(isinstance(x, pd.DataFrame) for x in (a, b)):
return pd.concat([a, b], axis=0)
# otherwise, at least one of them is a numpy array (we think)
return np.vstack([a, b])
示例9: mv2g
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import ndarrays [as 别名]
def mv2g(**kwargs):
'''Converts all `numpy.ndarrays` given as the keyword arguments
(`**kwargs`) from a vector grid of `shape=(..., Nx*Ny*Nz, ...,)` to a regular
grid of `shape=(..., Nx, Ny, Nz, ...,)`, and, if more than one `**kwargs` is
given, returns it as a dictionary.
Hint: The global values for the grid dimensionality, i.e., :mod:`grid.N_`,
are used for reshaping.
'''
import itertools
return_val = {}
for i,j in kwargs.items():
j = numpy.asarray(j,dtype=float)
shape = numpy.shape(j)
where = numpy.argwhere(shape==numpy.product(N_))[0,0]
return_val[i] = numpy.zeros(shape[:where]+tuple(N_)+shape[where+1:])
for key in itertools.product(*[range(k) for k in (shape[:where] + shape[where+1:])]):
obj = [slice(k,k+1) for k in key]
for r in range(3):
obj.insert(where,slice(None,None))
return_val[i][obj] = matrix_vector2grid(j[obj[:where]+obj[where+2:]].reshape((-1,)),
**dict(zip(['Nx','Ny','Nz'],N_)))
return list(return_val.values())[0] if len(return_val.values()) == 1 else return_val
示例10: get_newsnr
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import ndarrays [as 别名]
def get_newsnr(trigs):
"""
Calculate newsnr ('reweighted SNR') for a trigs/dictionary object
Parameters
----------
trigs: dict of numpy.ndarrays, h5py group (or similar dict-like object)
Dictionary-like object holding single detector trigger information.
'chisq_dof', 'snr', and 'chisq' are required keys
Returns
-------
numpy.ndarray
Array of newsnr values
"""
dof = 2. * trigs['chisq_dof'][:] - 2.
nsnr = newsnr(trigs['snr'][:], trigs['chisq'][:] / dof)
return numpy.array(nsnr, ndmin=1, dtype=numpy.float32)
示例11: get_newsnr_sgveto
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import ndarrays [as 别名]
def get_newsnr_sgveto(trigs):
"""
Calculate newsnr re-weigthed by the sine-gaussian veto
Parameters
----------
trigs: dict of numpy.ndarrays, h5py group (or similar dict-like object)
Dictionary-like object holding single detector trigger information.
'chisq_dof', 'snr', 'sg_chisq' and 'chisq' are required keys
Returns
-------
numpy.ndarray
Array of newsnr values
"""
dof = 2. * trigs['chisq_dof'][:] - 2.
nsnr_sg = newsnr_sgveto(trigs['snr'][:],
trigs['chisq'][:] / dof,
trigs['sg_chisq'][:])
return numpy.array(nsnr_sg, ndmin=1, dtype=numpy.float32)
示例12: get_newsnr_sgveto_psdvar
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import ndarrays [as 别名]
def get_newsnr_sgveto_psdvar(trigs):
"""
Calculate snr re-weighted by Allen chisq, sine-gaussian veto and
psd variation statistic
Parameters
----------
trigs: dict of numpy.ndarrays
Dictionary holding single detector trigger information.
'chisq_dof', 'snr', 'chisq' and 'psd_var_val' are required keys
Returns
-------
numpy.ndarray
Array of newsnr values
"""
dof = 2. * trigs['chisq_dof'][:] - 2.
nsnr_sg_psd = \
newsnr_sgveto_psdvar(trigs['snr'][:], trigs['chisq'][:] / dof,
trigs['sg_chisq'][:],
trigs['psd_var_val'][:])
return numpy.array(nsnr_sg_psd, ndmin=1, dtype=numpy.float32)
示例13: get_newsnr_sgveto_psdvar_scaled
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import ndarrays [as 别名]
def get_newsnr_sgveto_psdvar_scaled(trigs):
"""
Calculate newsnr re-weighted by the sine-gaussian veto and scaled
psd variation statistic
Parameters
----------
trigs: dict of numpy.ndarrays
Dictionary holding single detector trigger information.
'chisq_dof', 'snr', 'chisq' and 'psd_var_val' are required keys
Returns
-------
numpy.ndarray
Array of newsnr values
"""
dof = 2. * trigs['chisq_dof'][:] - 2.
nsnr_sg_psdscale = \
newsnr_sgveto_psdvar_scaled(
trigs['snr'][:], trigs['chisq'][:] / dof,
trigs['sg_chisq'][:],
trigs['psd_var_val'][:])
return numpy.array(nsnr_sg_psdscale, ndmin=1, dtype=numpy.float32)
示例14: get_newsnr_sgveto_psdvar_scaled_threshold
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import ndarrays [as 别名]
def get_newsnr_sgveto_psdvar_scaled_threshold(trigs):
"""
Calculate newsnr re-weighted by the sine-gaussian veto and scaled
psd variation statistic. A further threshold is applied to the
reduced chisq.
Parameters
----------
trigs: dict of numpy.ndarrays
Dictionary holding single detector trigger information.
'chisq_dof', 'snr', 'chisq' and 'psd_var_val' are required keys
Returns
-------
numpy.ndarray
Array of newsnr values
"""
dof = 2. * trigs['chisq_dof'][:] - 2.
nsnr_sg_psdt = \
newsnr_sgveto_psdvar_scaled_threshold(
trigs['snr'][:], trigs['chisq'][:] / dof,
trigs['sg_chisq'][:],
trigs['psd_var_val'][:])
return numpy.array(nsnr_sg_psdt, ndmin=1, dtype=numpy.float32)
示例15: single
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import ndarrays [as 别名]
def single(self, trigs):
"""Calculate the single detector statistic.
Parameters
----------
trigs: dict of numpy.ndarrays, h5py group (or similar dict-like object)
Dictionary-like object holding single detector trigger information.
Returns
-------
newsnr: numpy.ndarray
Array of single detector values
"""
newsnr = ranking.get_newsnr(trigs)
rchisq = trigs['chisq'][:] / (2. * trigs['chisq_dof'][:] - 2.)
newsnr[numpy.logical_and(newsnr < 10, rchisq > 2)] = -1
return newsnr