本文整理汇总了Python中numpy.s_方法的典型用法代码示例。如果您正苦于以下问题:Python numpy.s_方法的具体用法?Python numpy.s_怎么用?Python numpy.s_使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类numpy
的用法示例。
在下文中一共展示了numpy.s_方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: update
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import s_ [as 别名]
def update(self, actions, board, layers, backdrop, things, the_plot):
# Move up or down as directed if there is room.
action = Actions.STAY if actions is None else actions[self.character]
if action == Actions.UP:
if self._paddle_top > 1: self._paddle_top -= 1
elif action == Actions.DOWN:
if self._paddle_top < 7: self._paddle_top += 1
# Repaint the paddle. Note "blinking" effect if the ball slips past us.
self.curtain[:, self._paddle_col] = False
blink = (things['@'].position.col <= self._paddle_col # "past" us depends
if self.character == '1' else # on which paddle
things['@'].position.col >= self._paddle_col) # we are.
if not blink or (the_plot.frame % 2 == 0):
paddle_rows = np.s_[self._paddle_top:(self._paddle_top + 2)]
self.curtain[paddle_rows, self._paddle_col] = True
示例2: test_prepend_not_one
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import s_ [as 别名]
def test_prepend_not_one(self):
assign = self.assign
s_ = np.s_
a = np.zeros(5)
# Too large and not only ones.
assert_raises(ValueError, assign, a, s_[...], np.ones((2, 1)))
with warnings.catch_warnings():
# Will be a ValueError as well.
warnings.simplefilter("error", DeprecationWarning)
assert_raises(DeprecationWarning, assign, a, s_[[1, 2, 3],],
np.ones((2, 1)))
assert_raises(DeprecationWarning, assign, a, s_[[[1], [2]],],
np.ones((2,2,1)))
示例3: _initialize_factor_transition
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import s_ [as 别名]
def _initialize_factor_transition(self):
order = self.factor_order * self.k_factors
k_factors = self.k_factors
# Initialize the parameters
self.parameters['factor_transition'] = (
self.factor_order * self.k_factors**2)
# Setup fixed components of state space matrices
# VAR(p) for factor transition
if self.k_factors > 0:
if self.factor_order > 0:
self.ssm['transition', k_factors:order, :order - k_factors] = (
np.eye(order - k_factors))
self.ssm['selection', :k_factors, :k_factors] = np.eye(k_factors)
# Identification requires constraining the state covariance to an
# identity matrix
self.ssm['state_cov', :k_factors, :k_factors] = np.eye(k_factors)
# Setup indices of state space matrices
self._idx_factor_transition = np.s_['transition', :k_factors, :order]
示例4: _initialize_error_transition_var
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import s_ [as 别名]
def _initialize_error_transition_var(self):
k_endog = self.k_endog
_factor_order = self._factor_order
_error_order = self._error_order
# Initialize the parameters
self.parameters['error_transition'] = _error_order * k_endog
# Fixed components already setup above
# Setup indices of state space matrices
# Here we want to set all of the elements of the coefficient matrices,
# the same as in a VAR specification
self._idx_error_transition = np.s_[
'transition',
_factor_order:_factor_order + k_endog,
_factor_order:_factor_order + _error_order]
示例5: readPredMap
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import s_ [as 别名]
def readPredMap(mapFile):
try:
with open(mapFile, 'r') as f:
En = np.array(f.readline().split(), dtype=np.dtype(float))
A = np.loadtxt(f, unpack =False)
except:
print('\033[1m' + ' Map data file not found \n' + '\033[0m')
return
X = A[:,0]
Y = A[:,1]
A = np.delete(A, np.s_[0:2], 1)
print(' Shape map: ' + str(A.shape))
return X, Y, A, En
####################################################################
示例6: readLearnFile
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import s_ [as 别名]
def readLearnFile(learnFile):
try:
with open(learnFile, 'r') as f:
M = np.loadtxt(f, unpack =False)
except:
print('\033[1m' + ' Learn data file not found \n' + '\033[0m')
return
learnFileRoot = os.path.splitext(learnFile)[0]
#En = np.delete(np.array(M[0,:]),np.s_[0:1],0)
#M = np.delete(np.array(M[:,1:]),np.s_[0:1],0)
En = np.delete(np.array(M[0,:]),np.s_[0:1],0)
M = np.delete(M,np.s_[0:1],0)
Cl = np.asarray(['{:.2f}'.format(x) for x in M[:,0]]).reshape(-1,1)
M = np.delete(M,np.s_[0:1],1)
print("En:",En.shape)
print("M:",M.shape)
return En, M, Cl, learnFileRoot
####################################################################
示例7: test_e_i
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import s_ [as 别名]
def test_e_i():
assert_almost_equal(e_i(7, 5, output='r'),
array([[0., 0., 0., 0., 0., 1., 0.]])
)
assert_almost_equal(e_i(5, [0, 4, 4, 4, 1]),
array([[1., 0., 0., 0., 0.],
[0., 0., 0., 0., 1.],
[0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0.],
[0., 1., 1., 1., 0.]])
)
assert_almost_equal(e_i(5, s_[1:3]),
array([[0., 0.],
[1., 0.],
[0., 1.],
[0., 0.],
[0., 0.]])
)
assert_almost_equal(e_i(5, slice(1, 5, 2), output='r'),
array([[0., 1., 0., 0., 0.],
[0., 0., 0., 1., 0.]])
)
示例8: test_read_direct
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import s_ [as 别名]
def test_read_direct():
with pyfive.File(EARLIEST_HDF5_FILE) as hfile:
dset1 = hfile['dataset1']
arr = np.zeros(4)
dset1.read_direct(arr)
assert_array_equal(arr, [0, 1, 2, 3])
arr = np.zeros(4)
dset1.read_direct(arr, np.s_[:2], np.s_[:2])
assert_array_equal(arr, [0, 1, 0, 0])
arr = np.zeros(4)
dset1.read_direct(arr, np.s_[1:3], np.s_[2:])
assert_array_equal(arr, [0, 0, 1, 2])
示例9: initialize_from
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import s_ [as 别名]
def initialize_from(self, filename, ob_stat=None):
"""
Initializes weights from another policy, which must have the same architecture (variable names),
but the weight arrays can be smaller than the current policy.
"""
with h5py.File(filename, 'r') as f:
f_var_names = []
f.visititems(lambda name, obj: f_var_names.append(name) if isinstance(obj, h5py.Dataset) else None)
assert set(v.name for v in self.all_variables) == set(f_var_names), 'Variable names do not match'
init_vals = []
for v in self.all_variables:
shp = v.get_shape().as_list()
f_shp = f[v.name].shape
assert len(shp) == len(f_shp) and all(a >= b for a, b in zip(shp, f_shp)), \
'This policy must have more weights than the policy to load'
init_val = v.eval()
# ob_mean and ob_std are initialized with nan, so set them manually
if 'ob_mean' in v.name:
init_val[:] = 0
init_mean = init_val
elif 'ob_std' in v.name:
init_val[:] = 0.001
init_std = init_val
# Fill in subarray from the loaded policy
init_val[tuple([np.s_[:s] for s in f_shp])] = f[v.name]
init_vals.append(init_val)
self.set_all_vars(*init_vals)
if ob_stat is not None:
ob_stat.set_from_init(init_mean, init_std, init_count=1e5)
示例10: plot_mcontour
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import s_ [as 别名]
def plot_mcontour(self, ndim0, ndim1, z, show_mode):
"use mayavi.mlab to plot contour."
if not mayavi_installed:
self.__logger.info("Mayavi is not installed on your device.")
return
#do 2d interpolation
#get slice object
s = np.s_[0:ndim0:1, 0:ndim1:1]
x, y = np.ogrid[s]
mx, my = np.mgrid[s]
#use cubic 2d interpolation
interpfunc = interp2d(x, y, z, kind='cubic')
newx = np.linspace(0, ndim0, 600)
newy = np.linspace(0, ndim1, 600)
newz = interpfunc(newx, newy)
#mlab
face = mlab.surf(newx, newy, newz, warp_scale=2)
mlab.axes(xlabel='x', ylabel='y', zlabel='z')
mlab.outline(face)
#save or show
if show_mode == 'show':
mlab.show()
elif show_mode == 'save':
mlab.savefig('mlab_contour3d.png')
else:
raise ValueError('Unrecognized show mode parameter : ' +
show_mode)
return
示例11: test_prepend_not_one
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import s_ [as 别名]
def test_prepend_not_one(self):
assign = self.assign
s_ = np.s_
a = np.zeros(5)
# Too large and not only ones.
assert_raises(ValueError, assign, a, s_[...], np.ones((2, 1)))
assert_raises(ValueError, assign, a, s_[[1, 2, 3],], np.ones((2, 1)))
assert_raises(ValueError, assign, a, s_[[[1], [2]],], np.ones((2,2,1)))
示例12: test_simple_broadcasting_errors
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import s_ [as 别名]
def test_simple_broadcasting_errors(self):
assign = self.assign
s_ = np.s_
a = np.zeros((5, 1))
assert_raises(ValueError, assign, a, s_[...], np.zeros((5, 2)))
assert_raises(ValueError, assign, a, s_[...], np.zeros((5, 0)))
assert_raises(ValueError, assign, a, s_[:, [0]], np.zeros((5, 2)))
assert_raises(ValueError, assign, a, s_[:, [0]], np.zeros((5, 0)))
assert_raises(ValueError, assign, a, s_[[0], :], np.zeros((2, 1)))
示例13: get_segment_data_slice
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import s_ [as 别名]
def get_segment_data_slice(self, datafile, dsname, n_iter, seg_id, slice_=None, index_data=None,
iter_prec=None):
'''Return the data from the dataset named ``dsname`` within the given ``datafile`` (an open
h5py.File object) for the given iteration and segment. By default, it is assumed that the
dataset is stored in the iteration group for iteration ``n_iter``, but if ``index_data``
is provided, it must be an iterable (preferably a simple array) of (n_iter,seg_id) pairs,
and the index in the ``index_data`` iterable of the matching n_iter/seg_id pair is used as
the index of the data to retrieve.
If an optional ``slice_`` is provided, then the given slicing tuple is appended to that
used to retrieve the segment-specific data (i.e. it can be used to pluck a subset of the
data that would otherwise be returned).
'''
if slice_ is None:
slice_ = numpy.s_[...]
if index_data is not None:
dataset = datafile[dsname]
for i, (i_n_iter,i_seg_id) in enumerate(index_data):
if (i_n_iter,i_seg_id) == (n_iter,seg_id):
break
else:
raise KeyError((n_iter,seg_id))
itpl = (i,) + slice_
return dataset[itpl]
else:
if not iter_prec:
iter_prec = datafile.attrs.get('west_iter_prec', self.data_manager.default_iter_prec)
igname_tail = 'iter_{:0{iter_prec:d}d}'.format(int(n_iter),iter_prec=int(iter_prec))
try:
iter_group = datafile['/iterations/' + igname_tail]
except KeyError:
iter_group = datafile[igname_tail]
dataset = iter_group[dsname]
itpl = (seg_id,) + slice_
return dataset[itpl]
示例14: test_simple_broadcasting_errors
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import s_ [as 别名]
def test_simple_broadcasting_errors(self):
assign = self.assign
s_ = np.s_
a = np.zeros((5, 1))
assert_raises(ValueError, assign, a, s_[...], np.zeros((5, 2)))
assert_raises(ValueError, assign, a, s_[...], np.zeros((5, 0)))
assert_raises(ValueError, assign, a, s_[:, [0]], np.zeros((5, 2)))
assert_raises(ValueError, assign, a, s_[:, [0]], np.zeros((5, 0)))
assert_raises(ValueError, assign, a, s_[[0], :], np.zeros((2, 1)))
示例15: _initialize_loadings
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import s_ [as 别名]
def _initialize_loadings(self):
# Initialize the parameters
self.parameters['factor_loadings'] = self.k_endog * self.k_factors
# Setup fixed components of state space matrices
if self.error_order > 0:
start = self._factor_order
end = self._factor_order + self.k_endog
self.ssm['design', :, start:end] = np.eye(self.k_endog)
# Setup indices of state space matrices
self._idx_loadings = np.s_['design', :, :self.k_factors]