本文整理汇总了Python中numpy.empty函数的典型用法代码示例。如果您正苦于以下问题:Python empty函数的具体用法?Python empty怎么用?Python empty使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了empty函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _binopt
def _binopt(self, other, op):
"""apply the binary operation fn to two sparse matrices."""
other = self.__class__(other)
# e.g. csr_plus_csr, csr_minus_csr, etc.
fn = getattr(_sparsetools, self.format + op + self.format)
maxnnz = self.nnz + other.nnz
idx_dtype = get_index_dtype((self.indptr, self.indices,
other.indptr, other.indices),
maxval=maxnnz)
indptr = np.empty(self.indptr.shape, dtype=idx_dtype)
indices = np.empty(maxnnz, dtype=idx_dtype)
bool_ops = ['_ne_', '_lt_', '_gt_', '_le_', '_ge_']
if op in bool_ops:
data = np.empty(maxnnz, dtype=np.bool_)
else:
data = np.empty(maxnnz, dtype=upcast(self.dtype, other.dtype))
fn(self.shape[0], self.shape[1],
np.asarray(self.indptr, dtype=idx_dtype),
np.asarray(self.indices, dtype=idx_dtype),
self.data,
np.asarray(other.indptr, dtype=idx_dtype),
np.asarray(other.indices, dtype=idx_dtype),
other.data,
indptr, indices, data)
A = self.__class__((data, indices, indptr), shape=self.shape)
A.prune()
return A
示例2: __init__
def __init__(self, V, rank, win=1, circular=False, **kwargs):
"""
Parameters
----------
V : array, shape (`F`, `T`)
Matrix to analyze.
rank : int
Rank of the decomposition (i.e. number of columns of `W`
and rows of `H`).
win : int
Length of each of the convolutive bases. Defaults to 1,
i.e. the model is identical to PLCA.
circular : boolean
If True, data shifted past `T` will wrap around to
0. Defaults to False.
alphaW, alphaZ, alphaH : float or appropriately shaped array
Sparsity prior parameters for `W`, `Z`, and `H`. Negative
values lead to sparser distributions, positive values
makes the distributions more uniform. Defaults to 0 (no
prior).
**Note** that the prior is not parametrized in the
standard way where the uninformative prior has alpha=1.
"""
PLCA.__init__(self, V, rank, **kwargs)
self.win = win
self.circular = circular
self.VRW = np.empty((self.F, self.rank, self.win))
self.VRH = np.empty((self.T, self.rank))
示例3: theta_limiter
def theta_limiter(r,cfl,theta=0.95):
r"""
Theta limiter
Additional Input:
- *theta* =
"""
a = np.empty((2,len(r)))
b = np.empty((3,len(r)))
a[0,:] = 0.001
a[1,:] = cfl
cfmod1 = np.max(a,axis=0)
a[0,:] = 0.999
cfmod2 = np.min(a,axis=0)
s1 = 2.0 / cfmod1
s2 = (1.0 + cfl) / 3.0
phimax = 2.0 / (1.0 - cfmod2)
a[0,:] = (1.0 - theta) * s1
a[1,:] = 1.0 + s2 * (r - 1.0)
left = np.max(a,axis=0)
a[0,:] = (1.0 - theta) * phimax * r
a[1,:] = theta * s1 * r
middle = np.max(a,axis=0)
b[0,:] = left
b[1,:] = middle
b[2,:] = theta*phimax
return np.min(b,axis=0)
示例4: test_uniform_targets
def test_uniform_targets():
enet = ElasticNetCV(fit_intercept=True, n_alphas=3)
m_enet = MultiTaskElasticNetCV(fit_intercept=True, n_alphas=3)
lasso = LassoCV(fit_intercept=True, n_alphas=3)
m_lasso = MultiTaskLassoCV(fit_intercept=True, n_alphas=3)
models_single_task = (enet, lasso)
models_multi_task = (m_enet, m_lasso)
rng = np.random.RandomState(0)
X_train = rng.random_sample(size=(10, 3))
X_test = rng.random_sample(size=(10, 3))
y1 = np.empty(10)
y2 = np.empty((10, 2))
for model in models_single_task:
for y_values in (0, 5):
y1.fill(y_values)
assert_array_equal(model.fit(X_train, y1).predict(X_test), y1)
assert_array_equal(model.alphas_, [np.finfo(float).resolution]*3)
for model in models_multi_task:
for y_values in (0, 5):
y2[:, 0].fill(y_values)
y2[:, 1].fill(2 * y_values)
assert_array_equal(model.fit(X_train, y2).predict(X_test), y2)
assert_array_equal(model.alphas_, [np.finfo(float).resolution]*3)
示例5: block2d_to_blocknd
def block2d_to_blocknd(values, items, shape, labels, ref_items=None):
""" pivot to the labels shape """
from pandas.core.internals import make_block
panel_shape = (len(items),) + shape
# TODO: lexsort depth needs to be 2!!
# Create observation selection vector using major and minor
# labels, for converting to panel format.
selector = factor_indexer(shape[1:], labels)
mask = np.zeros(np.prod(shape), dtype=bool)
mask.put(selector, True)
if mask.all():
pvalues = np.empty(panel_shape, dtype=values.dtype)
else:
dtype, fill_value = _maybe_promote(values.dtype)
pvalues = np.empty(panel_shape, dtype=dtype)
pvalues.fill(fill_value)
values = values
for i in xrange(len(items)):
pvalues[i].flat[mask] = values[:, i]
if ref_items is None:
ref_items = items
return make_block(pvalues, items, ref_items)
示例6: __init__
def __init__(self, ale, agent, resized_width, resized_height,
resize_method, num_epochs, epoch_length, test_length,
frame_skip, death_ends_episode, max_start_nullops):
self.ale = ale
self.agent = agent
self.num_epochs = num_epochs
self.epoch_length = epoch_length
self.test_length = test_length
self.frame_skip = frame_skip
self.death_ends_episode = death_ends_episode
self.min_action_set = ale.getMinimalActionSet()
self.resized_width = resized_width
self.resized_height = resized_height
self.resize_method = resize_method
self.width, self.height = ale.getScreenDims()
self.buffer_length = 2
self.buffer_count = 0
self.screen_rgb = np.empty((self.height, self.width, 3),
dtype=np.uint8)
self.screen_buffer = np.empty((self.buffer_length,
self.height, self.width),
dtype=np.uint8)
self.terminal_lol = False # Most recent episode ended on a loss of life
self.max_start_nullops = max_start_nullops
示例7: _test_dtype
def _test_dtype(dtype, can_hold_na):
data = np.random.randint(0, 2, (5, 3)).astype(dtype)
indexer = [2, 1, 0, 1]
out0 = np.empty((4, 3), dtype=dtype)
out1 = np.empty((5, 4), dtype=dtype)
com.take_nd(data, indexer, out=out0, axis=0)
com.take_nd(data, indexer, out=out1, axis=1)
expected0 = data.take(indexer, axis=0)
expected1 = data.take(indexer, axis=1)
tm.assert_almost_equal(out0, expected0)
tm.assert_almost_equal(out1, expected1)
indexer = [2, 1, 0, -1]
out0 = np.empty((4, 3), dtype=dtype)
out1 = np.empty((5, 4), dtype=dtype)
if can_hold_na:
com.take_nd(data, indexer, out=out0, axis=0)
com.take_nd(data, indexer, out=out1, axis=1)
expected0 = data.take(indexer, axis=0)
expected1 = data.take(indexer, axis=1)
expected0[3, :] = np.nan
expected1[:, 3] = np.nan
tm.assert_almost_equal(out0, expected0)
tm.assert_almost_equal(out1, expected1)
else:
for i, out in enumerate([out0, out1]):
with tm.assertRaisesRegexp(TypeError, self.fill_error):
com.take_nd(data, indexer, out=out, axis=i)
# no exception o/w
data.take(indexer, out=out, axis=i)
示例8: __init__
def __init__(self, parent=None, width=5, height=4, dpi=60):
"""
Descript. :
"""
self.mouse_position = [0, 0]
self.max_plot_points = None
self.fig = Figure(figsize=(width, height), dpi=dpi)
self.axes = self.fig.add_subplot(111)
FigureCanvas.__init__(self, self.fig)
self.setParent(parent)
FigureCanvas.setSizePolicy(
self, QtImport.QSizePolicy.Expanding, QtImport.QSizePolicy.Expanding
)
FigureCanvas.updateGeometry(self)
self.single_curve = None
self.real_time = None
self._axis_x_array = np.empty(0)
self._axis_y_array = np.empty(0)
self._axis_x_limits = [None, None]
self._axis_y_limits = [None, None]
self._curves_dict = {}
self.setMaximumSize(2000, 2000)
示例9: test_empty_pass_dtype
def test_empty_pass_dtype(self):
data = 'one,two'
result = self.read_csv(StringIO(data), dtype={'one': 'u1'})
expected = DataFrame({'one': np.empty(0, dtype='u1'),
'two': np.empty(0, dtype=np.object)})
tm.assert_frame_equal(result, expected, check_index_type=False)
示例10: sweep_lo_dirfile
def sweep_lo_dirfile(self, Npackets_per = 10, channels = None,center_freq = 750.0e6, span = 2.0e6, bb_freqs=None,save_path = '/mnt/iqstream/lo_sweeps'):
N = Npackets_per
start = center_freq - (span/2.)
stop = center_freq + (span/2.)
step = 2.5e3
sweep_freqs = np.arange(start, stop, step)
sweep_freqs = np.round(sweep_freqs/step)*step
print 'Sweep freqs =', sweep_freqs/1.0e6
f=np.empty((len(channels),sweep_freqs.size))
i=np.empty((len(channels),sweep_freqs.size))
q=np.empty((len(channels),sweep_freqs.size))
for count,freq in enumerate(sweep_freqs):
print 'Sweep freq =', freq/1.0e6
if self.v1.set_frequency(0, freq/1.0e6, 0.01):
time.sleep(0.1)
i_buffer,q_buffer = self.get_UDP(N, freq, skip_packets=2, channels = channels)
f[:,count]=freq+bb_freqs
i[:,count]=np.mean(i_buffer,axis=0)
q[:,count]=np.mean(q_buffer,axis=0)
else:
time.sleep(0.1)
f[:,count]=freq+bb_freqs
i[:,count]=np.nan
q[:,count]=np.nan
self.v1.set_frequency(0,center_freq / (1.0e6), 0.01) # LO
return f, i, q
示例11: get_UDP
def get_UDP(self, Npackets, LO_freq, skip_packets=2, channels = None):
#Npackets = np.int(time_interval * self.accum_freq)
I_buffer = np.empty((Npackets + skip_packets, len(channels)))
Q_buffer = np.empty((Npackets + skip_packets, len(channels)))
self.fpga.write_int('pps_start', 1)
count = 0
while count < Npackets + skip_packets:
packet = self.s.recv(8192)
data = np.fromstring(packet,dtype = '<i').astype('float')
data /= 2.0**17
data /= (self.accum_len/512.)
ts = (np.fromstring(packet[-4:],dtype = '<i').astype('float')/ self.fpga_samp_freq)*1.0e3 # ts in ms
odd_chan = channels[1::2]
even_chan = channels[0::2]
I_odd = data[1024 + ((odd_chan - 1) / 2)]
Q_odd = data[1536 + ((odd_chan - 1) /2)]
I_even = data[0 + (even_chan/2)]
Q_even = data[512 + (even_chan/2)]
even_phase = np.arctan2(Q_even,I_even)
odd_phase = np.arctan2(Q_odd,I_odd)
if len(channels) % 2 > 0:
I = np.hstack(zip(I_even[:len(I_odd)], I_odd))
Q = np.hstack(zip(Q_even[:len(Q_odd)], Q_odd))
I = np.hstack((I, I_even[-1]))
Q = np.hstack((Q, Q_even[-1]))
I_buffer[count] = I
Q_buffer[count] = Q
else:
I = np.hstack(zip(I_even, I_odd))
Q = np.hstack(zip(Q_even, Q_odd))
I_buffer[count] = I
Q_buffer[count] = Q
count += 1
return I_buffer[skip_packets:],Q_buffer[skip_packets:]
示例12: get_stream
def get_stream(self, chan, time_interval):
self.fpga.write_int('pps_start', 1)
#self.phases = np.empty((len(self.freqs),Npackets))
Npackets = np.int(time_interval * self.accum_freq)
Is = np.empty(Npackets)
Qs = np.empty(Npackets)
phases = np.empty(Npackets)
count = 0
while count < Npackets:
packet = self.s.recv(8192 + 42) # total number of bytes including 42 byte header
header = np.fromstring(packet[:42],dtype = '<B')
roach_mac = header[6:12]
filter_on = np.array([2, 68, 1, 2, 13, 33])
if np.array_equal(roach_mac,filter_on):
data = np.fromstring(packet[42:],dtype = '<i').astype('float')
data /= 2.0**17
data /= (self.accum_len/512.)
ts = (np.fromstring(packet[-4:],dtype = '<i').astype('float')/ self.fpga_samp_freq)*1.0e3 # ts in ms
# To stream one channel, make chan an argument
if (chan % 2) > 0:
I = data[1024 + ((chan - 1) / 2)]
Q = data[1536 + ((chan - 1) /2)]
else:
I = data[0 + (chan/2)]
Q = data[512 + (chan/2)]
phase = np.arctan2([Q],[I])
Is[count]=I
Qs[count]=Q
phases[count]=phase
else:
continue
count += 1
return Is, Qs, phases
示例13: _TO_DELETE_initialize_drifters
def _TO_DELETE_initialize_drifters(self, driftersPerOceanModel):
"""
Initialize drifters and attach them for each particle.
"""
self.driftersPerOceanModel = np.int32(driftersPerOceanModel)
# Define mid-points for the different drifters
# Decompose the domain, so that we spread the drifters as much as possible
sub_domains_y = np.int(np.round(np.sqrt(self.driftersPerOceanModel)))
sub_domains_x = np.int(np.ceil(1.0*self.driftersPerOceanModel/sub_domains_y))
self.midPoints = np.empty((driftersPerOceanModel, 2))
for sub_y in range(sub_domains_y):
for sub_x in range(sub_domains_x):
drifter_id = sub_y*sub_domains_x + sub_x
if drifter_id >= self.driftersPerOceanModel:
break
self.midPoints[drifter_id, 0] = (sub_x + 0.5)*self.nx*self.dx/sub_domains_x
self.midPoints[drifter_id, 1] = (sub_y + 0.5)*self.ny*self.dy/sub_domains_y
# Loop over particles, sample drifters, and attach them
for i in range(self.numParticles+1):
drifters = GPUDrifterCollection.GPUDrifterCollection(self.gpu_ctx, self.driftersPerOceanModel,
observation_variance=self.observation_variance,
boundaryConditions=self.boundaryConditions,
domain_size_x=self.nx*self.dx, domain_size_y=self.ny*self.dy)
initPos = np.empty((self.driftersPerOceanModel, 2))
for d in range(self.driftersPerOceanModel):
initPos[d,:] = np.random.multivariate_normal(self.midPoints[d,:], self.initialization_cov_drifters)
drifters.setDrifterPositions(initPos)
self.particles[i].attachDrifters(drifters)
示例14: test_buffer_mode
def test_buffer_mode(self) :
# allocate something manifestly too short, should raise a value error
buff = np.empty(0, dtype=np.int64)
self.assertRaises(ValueError,
query_disc,
self.NSIDE, self.vec, self.radius, inclusive=True, buff=buff)
# allocate something of wrong type, should raise a value error
buff = np.empty(nside2npix(self.NSIDE), dtype=np.float64)
self.assertRaises(ValueError,
query_disc,
self.NSIDE, self.vec, self.radius, inclusive=True, buff=buff)
# allocate something acceptable, should succeed and return a subview
buff = np.empty(nside2npix(self.NSIDE), dtype=np.int64)
result = query_disc(self.NSIDE, self.vec, self.radius, inclusive=True, buff=buff)
assert result.base is buff
np.testing.assert_array_equal(
result,
np.array([ 0, 3, 4, 5, 11, 12, 13, 23 ])
)
示例15: get_rpn_batch
def get_rpn_batch(roidb):
"""
prototype for rpn batch: data, im_info, gt_boxes
:param roidb: ['image', 'flipped'] + ['gt_boxes', 'boxes', 'gt_classes']
:return: data, label
"""
assert len(roidb) == 1, 'Single batch only'
imgs, roidb = get_image(roidb)
im_array = imgs[0]
im_info = np.array([roidb[0]['im_info']], dtype=np.float32)
# gt boxes: (x1, y1, x2, y2, cls)
if roidb[0]['gt_classes'].size > 0:
gt_inds = np.where(roidb[0]['gt_classes'] != 0)[0]
gt_boxes = np.empty((roidb[0]['boxes'].shape[0], 5), dtype=np.float32)
gt_boxes[:, 0:4] = roidb[0]['boxes'][gt_inds, :]
gt_boxes[:, 4] = roidb[0]['gt_classes'][gt_inds]
else:
gt_boxes = np.empty((0, 5), dtype=np.float32)
data = {'data': im_array,
'im_info': im_info}
label = {'gt_boxes': gt_boxes}
return data, label