本文整理汇总了Python中numpy.floor_divide函数的典型用法代码示例。如果您正苦于以下问题:Python floor_divide函数的具体用法?Python floor_divide怎么用?Python floor_divide使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了floor_divide函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Python代码示例。
示例1: compute_offset
def compute_offset(transform, ds_x, ds_y):
"""
Compute the image offset based on the projected coordinates and the transformation.
The transformation performed is the invert transformation that is described on the transform object.
The resulting offsets are floored to int.
Results are valid as long as the transformation is linear (tranform[2] and tranform[4] are 0).
:param transform: the transformation obtained from Dataset::GetGeoTransform.
:param ds_x: the projected x-coordinate
:param ds_y: the projected y-coordinate
:return: the couple of offsets (x, y)
"""
# TODO is this exception really useful?
if transform is None:
raise Exception("Can only handle 'Affine GeoTransforms'")
# TODO tranform[2] and tranform[4] should be checked as equal to 0 (unless raise an error)
# http://www.gdal.org/classGDALDataset.html#af9593cc241e7d140f5f3c4798a43a668
origin_x = transform[0]
origin_y = transform[3]
pixel_width = transform[1]
pixel_height = transform[5]
# do the inverse geo transform, flooring to the int
# TODO better approximation than flooring to the int ?
offset_x = np.floor_divide(ds_x - origin_x, pixel_width).astype(int)
offset_y = np.floor_divide(ds_y - origin_y, pixel_height).astype(int)
return offset_x, offset_y
开发者ID:TheCapsLock,项目名称:YunoSeeMe,代码行数:32,代码来源:geods.py
示例2: get_color_cycle
def get_color_cycle(n, cmap="rainbow", rotations=3):
"""Get a list of RGBA colors following a colormap.
Useful for plotting lots of elements, keeping the color of each unique.
Parameters
----------
n : integer
The number of colors to return.
cmap : string (optional)
The colormap to use in the cycle. Default is rainbow.
rotations : integer (optional)
The number of times to repeat the colormap over the cycle. Default is 3.
Returns
-------
list
List of RGBA lists.
"""
cmap = colormaps[cmap]
if np.mod(n, rotations) == 0:
per = np.floor_divide(n, rotations)
else:
per = np.floor_divide(n, rotations) + 1
vals = list(np.linspace(0, 1, per))
vals = vals * rotations
vals = vals[:n]
out = cmap(vals)
return out
开发者ID:wright-group,项目名称:WrightTools,代码行数:29,代码来源:_colors.py
示例3: test_floor_division_complex
def test_floor_division_complex(self):
# check that implementation is correct
msg = "Complex floor division implementation check"
x = np.array([0.9 + 1j, -0.1 + 1j, 0.9 + 0.5 * 1j, 0.9 + 2.0 * 1j], dtype=np.complex128)
y = np.array([0.0, -1.0, 0.0, 0.0], dtype=np.complex128)
assert_equal(np.floor_divide(x ** 2, x), y, err_msg=msg)
# check overflow, underflow
msg = "Complex floor division overflow/underflow check"
x = np.array([1.0e110, 1.0e-110], dtype=np.complex128)
y = np.floor_divide(x ** 2, x)
assert_equal(y, [1.0e110, 0], err_msg=msg)
开发者ID:jarrodmillman,项目名称:numpy,代码行数:11,代码来源:test_umath.py
示例4: _scale
def _scale(a, n, m, copy=True):
# Scale unsigned/positive integers from n to m bits
# Numbers can be represented exactly only if m is a multiple of n
# Output array is of same kind as input.
kind = a.dtype.kind
if n > m and a.max() < 2 ** m:
mnew = int(np.ceil(m / 2) * 2)
if mnew > m:
dtype = "int%s" % mnew
else:
dtype = "uint%s" % mnew
n = int(np.ceil(n / 2) * 2)
msg = ("Downcasting %s to %s without scaling because max "
"value %s fits in %s" % (a.dtype, dtype, a.max(), dtype))
warn(msg)
return a.astype(_dtype2(kind, m))
elif n == m:
return a.copy() if copy else a
elif n > m:
# downscale with precision loss
prec_loss()
if copy:
b = np.empty(a.shape, _dtype2(kind, m))
np.floor_divide(a, 2**(n - m), out=b, dtype=a.dtype,
casting='unsafe')
return b
else:
a //= 2**(n - m)
return a
elif m % n == 0:
# exact upscale to a multiple of n bits
if copy:
b = np.empty(a.shape, _dtype2(kind, m))
np.multiply(a, (2**m - 1) // (2**n - 1), out=b, dtype=b.dtype)
return b
else:
a = np.array(a, _dtype2(kind, m, a.dtype.itemsize), copy=False)
a *= (2**m - 1) // (2**n - 1)
return a
else:
# upscale to a multiple of n bits,
# then downscale with precision loss
prec_loss()
o = (m // n + 1) * n
if copy:
b = np.empty(a.shape, _dtype2(kind, o))
np.multiply(a, (2**o - 1) // (2**n - 1), out=b, dtype=b.dtype)
b //= 2**(o - m)
return b
else:
a = np.array(a, _dtype2(kind, o, a.dtype.itemsize), copy=False)
a *= (2**o - 1) // (2**n - 1)
a //= 2**(o - m)
return a
开发者ID:benlongo,项目名称:scikit-image,代码行数:54,代码来源:dtype.py
示例5: test_floor_divide
def test_floor_divide():
a = afnumpy.random.random((2,3))
b = numpy.array(a)
fassert(afnumpy.floor_divide(a,a), numpy.floor_divide(b,b))
a = afnumpy.array(2)
b = numpy.array(a)
ao = afnumpy.array(0)
bo = numpy.array(0)
fassert(afnumpy.floor_divide(a,a), numpy.floor_divide(b,b))
fassert(afnumpy.floor_divide(a,a, out=ao), numpy.floor_divide(b,b, out = bo))
fassert(ao, bo)
开发者ID:FilipeMaia,项目名称:afnumpy,代码行数:11,代码来源:test_lib.py
示例6: main
def main():
"""
universal function(ufunc)
Function that returns the result of the operation of each element.
Arg is ndarray, returns a ndarray
"""
arr = np.arange(10)
print_headline('arg is ndarray')
print 'integer'
print np.sqrt(arr)
print np.exp(arr)
print np.square(arr)
print np.log10(arr)
print ''
print 'float'
arr = np.array([-2, -1.6, -1.4, 0, 1.4, 1.6, 2])
print np.sign(arr)
print np.ceil(arr)
print np.floor(arr)
print np.rint(arr) # round
print np.sin(arr) # cos, tan, arcxxx
print np.logical_not([arr >= 1]) # and, or, xor
print ''
print 'NaN, inf'
nan_inf = np.array([0, 1, np.NaN, np.inf, -np.inf])
print nan_inf
print np.isnan(nan_inf)
print np.isinf(nan_inf)
print_headline('args are ndarray')
x = np.arange(8)
y = np.arange(8)[::-1]
print x, y
print np.add(x, y)
print np.subtract(x, y)
print np.multiply(x, y)
print np.divide(x, y)
print np.floor_divide(x, y)
print np.power(x, y)
print np.mod(x, y)
print np.maximum(x, y)
print np.minimum(x, y)
print np.greater_equal(x, y)
print_headline('returns are ndarray')
arr = np.random.randn(10)
# divide integer ndarray and float ndarray
modf = np.modf(arr)
print arr
print modf[0]
print modf[1]
开发者ID:ksomemo,项目名称:pandas-study,代码行数:54,代码来源:universal_function.py
示例7: solve
def solve(groups, P):
groups = np.array(groups)
ms = np.sum(groups % P == np.arange(P)[:,None], axis=1)
if P == 2:
U = np.array([
[2]
])
elif P == 3:
U = np.array([
#1, 2
[3, 0],
[0, 3],
[1, 1]
])
elif P == 4:
U = np.array([
#1, 2, 3
[4, 0, 0],
[0, 2, 0],
[0, 0, 4],
[2, 1, 0],
[1, 0, 1],
[0, 1, 2]
])
else:
raise ValueError
m = ms[1:]
mtot = np.sum(m)
k_max = np.ones(U.shape, int) * mtot
np.floor_divide(m, U, out=k_max, where=U!=0)
k_max = k_max.min(axis=1)
ks = np.indices(k_max + 1).reshape(U.shape[0], -1)
n_groups = U.T @ ks #[size,ki]
invalid = np.any(n_groups > m[:,None], axis=0, keepdims=True)
happy = ks.sum(axis=0, keepdims=True)
happy[invalid] = 0
best_i = np.argmax(happy.ravel())
best_happy = happy[:,best_i].squeeze()
n = ms[0] + best_happy
if n_groups[:,best_i].sum() != mtot:
n += 1
return n
开发者ID:eric-wieser,项目名称:codejam,代码行数:54,代码来源:main.py
示例8: simscore
def simscore(trainMtx,testMtx,sparseMode=True,split=2,split2=2):
if sparseMode==True:
scorevec=np.zeros((testMtx.shape[0],trainMtx.shape[0]));
maxveclength=np.min([testMtx.shape[1],trainMtx.shape[1]]);
#setup an array for string the norm of each row
rownorm=np.zeros((trainMtx.shape[0],));
for i in range(trainMtx.shape[0]):
w=trainMtx[i,:];
rownorm[i]=np.sqrt(w.dot(w.T))[0,0];
for i in range(testMtx.shape[0]):
w=testMtx[i,:];
wnorm=np.sqrt(w.dot(w.T))[0,0];
print "calculating",i;
for j in range(trainMtx.shape[0]):
v=trainMtx[j,:];
vnorm=np.sqrt(v.dot(v.T))[0,0]
ip=w[0,0:maxveclength].multiply(v[0,0:maxveclength])/(wnorm*vnorm);
scorevec[i,j]=ip[0,0];
else:
#setup an array for storing the results
scorevec=np.zeros((testMtx.shape[0],trainMtx.shape[0]));
if split==0:
prd=sparsemtxproduct(trainMtx, testMtx);
scorevec=np.transpose(np.asarray(prd));
else:
#calcualte the split
bitesize=np.floor_divide(trainMtx.shape[0],split);
remem=np.mod(trainMtx.shape[0],split);
bitesize2=np.floor_divide(testMtx.shape[0],split2);
remem2=np.mod(testMtx.shape[0],split2);
#setup an array for string the norm of each row
for i in range(split+1):
for j in range(split2+1):
startidx,endidx=getindex(i,split,bitesize,remem);
startidx2,endidx2=getindex(j,split2,bitesize2,remem2);
prd=mtxproduct(trainMtx[startidx:endidx,:], testMtx[startidx2:endidx2,:])
scorevec[startidx2:endidx2,startidx:endidx]=np.transpose(np.asarray(prd));
del prd
return scorevec;
开发者ID:kanhua,项目名称:LSHTC,代码行数:52,代码来源:submitv1.py
示例9: test_floordiv_numbers
def test_floordiv_numbers(self):
"""Tensor // number is correct elementwise"""
constant, shape = 10., (3, 5)
with nn.variable_scope(self.get_scope()):
tensor1 = fixture.create_ones_tensor(shape, dtype='float32')
tensor2 = tensor1 // constant
tensor3 = constant // tensor1
session = nn.Session()
val1, val2, val3 = session.run(
outputs=[tensor1, tensor2, tensor3],
)
np.testing.assert_equal(np.floor_divide(val1, constant), val2)
np.testing.assert_equal(np.floor_divide(constant, val1), val3)
开发者ID:mthrok,项目名称:luchador,代码行数:15,代码来源:wrapper_test.py
示例10: shuffle_group
def shuffle_group(df, col, stage, k, npartitions):
""" Splits dataframe into groups
The group is determined by their final partition, and which stage we are in
in the shuffle
"""
if col == '_partitions':
ind = df[col]
else:
ind = hash_pandas_object(df[col], index=False)
c = ind._values
typ = np.min_scalar_type(npartitions * 2)
npartitions, k, stage = [np.array(x, dtype=np.min_scalar_type(x))[()]
for x in [npartitions, k, stage]]
c = np.mod(c, npartitions).astype(typ, copy=False)
c = np.floor_divide(c, k ** stage, out=c)
c = np.mod(c, k, out=c)
indexer, locations = groupsort_indexer(c.astype(np.int64), k)
df2 = df.take(indexer)
locations = locations.cumsum()
parts = [df2.iloc[a:b] for a, b in zip(locations[:-1], locations[1:])]
return dict(zip(range(k), parts))
开发者ID:floriango,项目名称:dask,代码行数:27,代码来源:shuffle.py
示例11: get_tick_prior
def get_tick_prior(self, val):
# {{{
''' get_tick_prior(val) - returns the date of the first tick prior to the
date represented by val. If a tick lies on val, it returns the date of the
previous tick.'''
def unpack(dt): return dt.get('year', 1), dt.get('month', 1), dt.get('day', 1)
dt = self._taxis.val_as_date(val, allfields=True)
yr, mn, dy = unpack(dt)
if val > self._taxis.date_as_val({'year':yr, 'month':mn, 'day':dy}):
yr, mn, dy = unpack(tu.wrapdate(self._taxis, {'year':yr, 'month':mn, 'day':dy+1}, allfields=True))
# Find first day on given multiple prior to the given day
from numpy import floor_divide
dy = floor_divide(dy - 2, self.mult) * self.mult + 1
# If we've wrapped, decrement the year
d = tu.wrapdate(self._taxis, {'year':yr, 'month':mn, 'day':dy}, allfields=True)
d1 = tu.wrapdate(self._taxis, {'year':yr, 'month':mn + 1, 'day':1}, allfields=True)
if tu.date_diff(self._taxis, d, d1, 'days') < self.mult / 2:
return d1
else:
return d
开发者ID:aerler,项目名称:pygeode,代码行数:26,代码来源:timeticker.py
示例12: step
def step(self, action):
""" Take one time step through the world """
self.action = action.copy().ravel()
self.timestep += 1
step_size = self.action[0] - self.action[1]
# An approximation of metabolic energy
energy = self.action[0] + self.action[1]
self.world_state = self.world_state + step_size
# At random intervals, jump to a random position in the world
if np.random.random_sample() < self.JUMP_FRACTION:
self.world_state = (self.num_real_sensors *
np.random.random_sample())
# Ensure that the world state falls between 0 and num_real_sensors
self.world_state -= (self.num_real_sensors *
np.floor_divide(self.world_state,
self.num_real_sensors))
self.simple_state = int(np.floor(self.world_state))
# Assign sensors as zeros or ones.
# Represent the presence or absence of the current position in the bin.
real_sensors = np.zeros(self.num_real_sensors)
real_sensors[self.simple_state] = 1
# Generate a set of noise sensors
noise_sensors = np.round(np.random.random_sample(
self.num_noise_sensors))
sensors = np.hstack((real_sensors, noise_sensors))
reward = -self.REWARD_MAGNITUDE
if self.simple_state == 1:
reward = self.REWARD_MAGNITUDE
reward -= energy * self.ENERGY_COST
return sensors, reward
开发者ID:alito,项目名称:becca,代码行数:30,代码来源:grid_1D_noise.py
示例13: step
def step(self, action):
""" Take one time step through the world """
self.action = action.ravel()
self.timestep += 1
energy = self.action[0] + self.action[1]
self.world_state += self.action[0] - self.action[1]
# Occasionally add a perturbation to the action to knock it
# into a different state
if np.random.random_sample() < self.JUMP_FRACTION:
self.world_state = self.num_sensors * np.random.random_sample()
# Ensure that the world state falls between 0 and 9
self.world_state -= self.num_sensors * np.floor_divide(
self.world_state, self.num_sensors)
self.simple_state = int(np.floor(self.world_state))
# Assign sensors as zeros or ones.
# Represent the presence or absence of the current position in the bin.
sensors = np.zeros(self.num_sensors)
sensors[self.simple_state] = 1
# Assign reward based on the current state
reward = sensors[8] * (-self.REWARD_MAGNITUDE)
reward += sensors[3] * (self.REWARD_MAGNITUDE)
# Punish actions just a little
reward -= energy * self.ENERGY_COST
reward = np.max(reward, -1)
return sensors, reward
开发者ID:eimirae,项目名称:becca,代码行数:25,代码来源:grid_1D_ms.py
示例14: get_submaps
def get_submaps(args, comm, data):
""" Get a list of locally hit pixels and submaps on every process.
"""
autotimer = timing.auto_timer()
if comm.comm_world.rank == 0:
print('Scanning local pixels', flush=args.flush)
start = MPI.Wtime()
# Prepare for using distpixels objects
nside = args.nside
subnside = 16
if subnside > nside:
subnside = nside
subnpix = 12 * subnside * subnside
# get locally hit pixels
lc = tm.OpLocalPixels()
localpix = lc.exec(data)
if localpix is None:
raise RuntimeError(
'Process {} has no hit pixels. Perhaps there are fewer '
'detectors than processes in the group?'.format(
comm.comm_world.rank))
# find the locally hit submaps.
localsm = np.unique(np.floor_divide(localpix, subnpix))
comm.comm_world.barrier()
stop = MPI.Wtime()
elapsed = stop - start
if comm.comm_world.rank == 0:
print('Local submaps identified in {:.3f} s'.format(elapsed),
flush=args.flush)
return localpix, localsm, subnpix
开发者ID:hpc4cmb,项目名称:toast,代码行数:35,代码来源:toast_satellite_sim.py
示例15: step
def step(self, action):
"""
Advance the world by one time step.
Parameters
----------
action : array of floats
The set of action commands to execute.
Returns
-------
reward : float
The amount of reward or punishment given by the world.
sensors : array of floats
The values of each of the sensors.
"""
self.action = action
self.action = np.round(self.action)
self.timestep += 1
self.energy = self.action[0] + self.action[1]
self.world_state += self.action[0] - self.action[1]
# Occasionally add a perturbation to the action to knock it
# into a different state.
if np.random.random_sample() < self.jump_fraction:
self.world_state = self.num_positions * np.random.random_sample()
# Ensure that the world state falls between 0 and 9
self.world_state -= self.num_positions * np.floor_divide(
self.world_state, self.num_positions)
self.simple_state = int(np.floor(self.world_state))
if self.simple_state == 9:
self.simple_state = 0
sensors = self.sense()
reward = self.assign_reward()
return sensors, reward
开发者ID:brohrer,项目名称:becca_test,代码行数:34,代码来源:grid_1D_ms.py
示例16: solve_rounds
def solve_rounds(pixels, max_pixels_per_can=100):
output = []
pixels = np.array(pixels)
## "Straining" pixels by value
picture_mod = pixels.shape[0]
pixels = pixels.reshape(pixels.size, 1)
unique = np.unique(pixels)
for unique_value in unique:
print("finding short path for label " + str(unique_value))
filtered_idx = np.where(pixels == unique_value)
x_coords = np.remainder(filtered_idx[0], picture_mod)
y_coords = np.floor_divide(filtered_idx[0], picture_mod)
coords = np.transpose([x_coords, y_coords])
## Ordering pixels
ordered = np.array(find_short_path(coords))
order_s = ordered.shape[0]
num_divisions = int(order_s/max_pixels_per_can) + 1
if num_divisions > 0:
split_points = [i*max_pixels_per_can for i in range(num_divisions)]
output.append([unique_value] + np.array_split(ordered, split_points)[1:])
else:
output.append([unique_value] + ordered)
return output
开发者ID:ut-ras,项目名称:venus,代码行数:34,代码来源:rounds.py
示例17: step
def step(self, action):
"""
Advance the world by one time step
"""
self.action = action.ravel()
self.action[np.nonzero(self.action)] = 1.
self.timestep += 1
energy = self.action[0] + self.action[1]
self.world_state += self.action[0] - self.action[1]
# Occasionally add a perturbation to the action to knock it
# into a different state
if np.random.random_sample() < self.JUMP_FRACTION:
self.world_state = self.num_sensors * np.random.random_sample()
# Ensure that the world state falls between 0 and 9
self.world_state -= self.num_sensors * np.floor_divide(
self.world_state, self.num_sensors)
self.simple_state = int(np.floor(self.world_state))
# TODO do this more elegantly
if self.simple_state == 9:
self.simple_state = 0
# Assign sensors as zeros or ones.
# Represent the presence or absence of the current position in the bin.
sensors = np.zeros(self.num_sensors)
sensors[self.simple_state] = 1
# Assign reward based on the current state
reward = sensors[8] * -1.
reward += sensors[3]
# Punish actions just a little
reward -= energy * self.ENERGY_COST
reward = np.max(reward, -1)
return sensors, reward
开发者ID:fdoperezi,项目名称:becca,代码行数:31,代码来源:grid_1D_ms.py
示例18: apply
def apply(self, array):
"""
Map the input array to its tile coding representation.
Essentially, this proceeds by first getting the integer coordinates of
the input array (subject to scaling), then by offsetting the
coordinates according to the displacement vector for each tiling.
Then, the displaced coordinates are hashed using `hfunc`, and the
resulting hashed values are summed modulo `n_tiles` to produce the
indices of the active tiles to be used as features.
Args:
array (np.ndarray): The array to be tiled.
Must be of length `n_input`, or else an exception is raised.
Returns:
ret (np.ndarray): An array of length `n_output`, whose entries
correspond to the indices of the active tiles.
"""
if len(array) != self.n_input:
raise ValueError("Incompatible array with length", len(array))
x = np.floor_divide(array, self.scale).astype(np.int)
v = x - ((x - self.dmat) % self.n_output)
a = np.apply_along_axis(self.hfunc, axis=0, arr=v)
ret = np.sum(a, axis=1) % self.n_tiles
return ret
开发者ID:rldotai,项目名称:flib,代码行数:26,代码来源:tile_coding.py
示例19: clean_data
def clean_data (game_data):
#This takes the section variable in the form <LL###>
#and breaks it into two sub-variables
#The letters extract informaion about the level
#The digits extraction about the section
sh = game_data.section.str.extract('(?P<letter>[A-Z]*)(?P<digit>\d*)')
#Merge the extracted data back into the dataframe
game_data= game_data.join(sh)
#Some data did not have section info, This data is ingnored the analysis
game_data= game_data[game_data.digit != ""]
#This changes the section into an intiger
game_data['digit'] = game_data['digit'].apply(int)
game_data['price'] = game_data['price'].apply(float)
#The section variable has two pieces of information
#This first digit of the section vairable is deck (1,2,3)
game_data['deck']= np.floor_divide(game_data['digit'],100)
#The second two digits are the section, describing the relative position of the stadium
game_data['block']= game_data['digit']-game_data['deck']*100
return game_data
开发者ID:evanbloom,项目名称:pricemyticket,代码行数:28,代码来源:allgame_data.py
示例20: __setitem__
def __setitem__(self, index, value):
#TODO update logic to match __getitem__
ts = self.ts
dt = (ts.tspan[-1] - ts.tspan[0]) / (len(ts) - 1)
if isinstance(index, numbers.Number):
newix = ts.tspan.searchsorted(index)
return ts.__setitem__(newix, value)
elif isinstance(index, _SliceType):
if index.step is None:
start, stop = ts.tspan.searchsorted(index.start, index.stop)
return ts.__setitem__(slice(start, stop, None), value)
else:
n = np.floor_divide(index.start - index.stop, index.step)
times = np.linspace(index.start, index.stop, n, endpoint=False)
indices = ts.tspan.searchsorted(times)
if indices[-1] == len(ts.tspan):
indices = indices[:-1]
return ts.__setitem__(indices, value)
elif isinstance(index, _EllipsisType) or index is None:
return ts.__setitem__(index, value)
elif isinstance(index, np.ndarray) and index.ndim is 1:
indices = ts.tspan.searchsorted(index)
if indices[-1] == len(ts.tspan):
indices = indices[:-1]
return ts.__setitem__(indices, value)
elif isinstance(index, _TupleType):
timeix = index[0]
ts = ts.t[timeix]
otherix = index[1:]
return ts.__setitem__(otherix, value)
else:
raise TypeError("Time slicing can't handle that type of index yet")
开发者ID:mattja,项目名称:nsim,代码行数:32,代码来源:timeseries.py
注:本文中的numpy.floor_divide函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论