def test_minmax_funcs_with_output(self):
# Tests the min/max functions with explicit outputs
mask = np.random.rand(12).round()
xm = array(np.random.uniform(0, 10, 12), mask=mask)
xm.shape = (3, 4)
for funcname in ('min', 'max'):
# Initialize
npfunc = getattr(np, funcname)
mafunc = getattr(numpy.ma.core, funcname)
# Use the np version
nout = np.empty((4,), dtype=int)
try:
result = npfunc(xm, axis=0, out=nout)
except MaskError:
pass
nout = np.empty((4,), dtype=float)
result = npfunc(xm, axis=0, out=nout)
self.assertTrue(result is nout)
# Use the ma version
nout.fill(-999)
result = mafunc(xm, axis=0, out=nout)
self.assertTrue(result is nout)
python类round()的实例源码
def test_round(self):
a = array([1.23456, 2.34567, 3.45678, 4.56789, 5.67890],
mask=[0, 1, 0, 0, 0])
assert_equal(a.round(), [1., 2., 3., 5., 6.])
assert_equal(a.round(1), [1.2, 2.3, 3.5, 4.6, 5.7])
assert_equal(a.round(3), [1.235, 2.346, 3.457, 4.568, 5.679])
b = empty_like(a)
a.round(out=b)
assert_equal(b, [1., 2., 3., 5., 6.])
x = array([1., 2., 3., 4., 5.])
c = array([1, 1, 1, 0, 0])
x[2] = masked
z = where(c, x, -x)
assert_equal(z, [1., 2., 0., -4., -5])
c[0] = masked
z = where(c, x, -x)
assert_equal(z, [1., 2., 0., -4., -5])
assert_(z[0] is masked)
assert_(z[1] is not masked)
assert_(z[2] is masked)
def test_round_with_scalar(self):
# Testing round with scalar/zero dimension input
# GH issue 2244
a = array(1.1, mask=[False])
assert_equal(a.round(), 1)
a = array(1.1, mask=[True])
assert_(a.round() is masked)
a = array(1.1, mask=[False])
output = np.empty(1, dtype=float)
output.fill(-9999)
a.round(out=output)
assert_equal(output, 1)
a = array(1.1, mask=[False])
output = array(-9999., mask=[True])
a.round(out=output)
assert_equal(output[()], 1)
a = array(1.1, mask=[True])
output = array(-9999., mask=[False])
a.round(out=output)
assert_(output[()] is masked)
def dec_round(num, dprec=4, rnd='down', rto_zero=False):
"""
Round up/down numeric ``num`` at specified decimal ``dprec``.
Parameters
----------
num: float
dprec: int
Decimal position for truncation.
rnd: str (default: 'down')
Set as 'up' or 'down' to return a rounded-up or rounded-down value.
rto_zero: bool (default: False)
Use a *round-towards-zero* method, e.g., ``floor(-3.5) == -3``.
Returns
----------
float (default: rounded-up)
"""
dprec = 10**dprec
if rnd == 'up' or (rnd == 'down' and rto_zero and num < 0.):
return np.ceil(num*dprec)/dprec
elif rnd == 'down' or (rnd == 'up' and rto_zero and num < 0.):
return np.floor(num*dprec)/dprec
return np.round(num, dprec)
def sample_output(self, val):
vocabulary = self.get_vocabulary()
if self.one_hot:
vals = [ np.argmax(r) for r in val ]
ox_val = [vocabulary[obj] for obj in list(vals)]
string = "".join(ox_val)
return string
else:
val = np.reshape(val, [-1])
val *= len(vocabulary)/2.0
val += len(vocabulary)/2.0
val = np.round(val)
val = np.maximum(0, val)
val = np.minimum(len(vocabulary)-1, val)
ox_val = [self.get_character(obj) for obj in list(val)]
string = "".join(ox_val)
return string
def __init__(self, opt_engine, topK=16, grid_size=None, nps=320, model_name='tmp'):
QWidget.__init__(self)
self.topK = topK
if grid_size is None:
self.n_grid = int(np.ceil(np.sqrt(self.topK)))
self.grid_size = (self.n_grid, self.n_grid) # (width, height)
else:
self.grid_size = grid_size
self.select_id = 0
self.ims = None
self.vis_results = None
self.width = int(np.round(nps/ (4 * float(self.grid_size[1])))) * 4
self.winWidth = self.width * self.grid_size[0]
self.winHeight = self.width * self.grid_size[1]
self.setFixedSize(self.winWidth, self.winHeight)
self.opt_engine = opt_engine
self.frame_id = -1
self.sr = save_result.SaveResult(model_name=model_name)
def sresample(src, outshape):
""" Simple 3d array resampling
Inputs:
src -- a ndimensional array (dim>2)
outshape -- fixed output shape for the first 2 dimensions
Outputs:
hout -- resulting n-dimensional array
"""
inh, inw = src.shape[:2]
outh, outw = outshape
hslice = (np.arange(outh) * (inh-1.)/(outh-1.)).round().astype(int)
wslice = (np.arange(outw) * (inw-1.)/(outw-1.)).round().astype(int)
hout = src[hslice, :][:, wslice]
return hout.copy()
def stationInfo(stnds, varname, name, titlestr=None, alttitle=None, lflatten=False, lmon=False,):
''' helper to generate an axes title with station info '''
if stnds.hasAxis('station'): nstn = len(stnds.axes['station']) # number of stations
else: nstn = 1 # single station
if stnds.name[:3].lower() == 'obs' and varname in stnds:
ec = stnds[varname] # some variables are not present everywhere
if ec.hasAxis('time') and ec.time.units[:3].lower() == 'mon': units = 'mon.'
elif ec.hasAxis('year') and ec.year.units.lower() == 'year': units = 'yrs.'
else: units = 'mon.' if lmon else 'yrs.'
mask = ec.data_array.mask if isinstance(ec.data_array,np.ma.MaskedArray) else np.isnan(ec.data_array)
if lflatten: rec_len = (ec.data_array.size - mask.sum()) # valid years in obs/EC
else: rec_len = int(np.round(ec.data_array.shape[-1] - mask.sum(axis=-1).mean())) # valid years in obs/EC
if titlestr: axtitle = titlestr.format(name,nstn,rec_len) # axes label
else: axtitle = "{:s} (#{:d}, {:d} {:s})".format(name,nstn,rec_len,units) # axes label
else:
if alttitle: axtitle = alttitle.format(name,nstn) # axes label
elif titlestr: axtitle = titlestr.format(name,nstn) # axes label
else: axtitle = "{:s} (#{:d}, WRF only)".format(name,nstn) # axes label
return axtitle
# function to compute some statistics and print them
def get_fft_mel_mat(nfft, sr=8000, nfilts=None, width=1.0, minfrq=20, maxfrq=None, constamp=0):
if nfilts is None:
nfilts = nfft
if maxfrq is None:
maxfrq = sr // 2
wts = np.zeros((nfilts, nfft//2+1))
fftfrqs = np.arange(0, nfft//2+1) / (1. * nfft) * (sr)
minmel = hz2mel(minfrq)
maxmel = hz2mel(maxfrq)
binfrqs = mel2hz(minmel + np.arange(0, nfilts+2) / (nfilts+1.) * (maxmel - minmel))
# binbin = np.round(binfrqs / maxfrq * nfft)
for i in range(nfilts):
fs = binfrqs[[i+0, i+1, i+2]]
fs = fs[1] + width * (fs - fs[1])
loslope = (fftfrqs - fs[0]) / (fs[1] - fs[0])
hislope = (fs[2] - fftfrqs) / (fs[2] - fs[1])
wts[i, :] = np.maximum(0, np.minimum(loslope, hislope))
return wts
def get_fft_mel_mat(nfft, sr=8000, nfilts=None, width=1.0, minfrq=20, maxfrq=None, constamp=0):
if nfilts is None:
nfilts = nfft
if maxfrq is None:
maxfrq = sr // 2
wts = np.zeros((nfilts, nfft//2+1))
fftfrqs = np.arange(0, nfft//2+1) / (1. * nfft) * (sr)
minmel = hz2mel(minfrq)
maxmel = hz2mel(maxfrq)
binfrqs = mel2hz(minmel + np.arange(0, nfilts+2) / (nfilts+1.) * (maxmel - minmel))
# binbin = np.round(binfrqs / maxfrq * nfft)
for i in range(nfilts):
fs = binfrqs[[i+0, i+1, i+2]]
fs = fs[1] + width * (fs - fs[1])
loslope = (fftfrqs - fs[0]) / (fs[1] - fs[0])
hislope = (fs[2] - fftfrqs) / (fs[2] - fs[1])
wts[i, :] = np.maximum(0, np.minimum(loslope, hislope))
return wts
def CSMToBinary(D, Kappa):
"""
Turn a cross-similarity matrix into a binary cross-simlarity matrix
If Kappa = 0, take all neighbors
If Kappa < 1 it is the fraction of mutual neighbors to consider
Otherwise Kappa is the number of mutual neighbors to consider
"""
N = D.shape[0]
M = D.shape[1]
if Kappa == 0:
return np.ones((N, M))
elif Kappa < 1:
NNeighbs = int(np.round(Kappa*M))
else:
NNeighbs = Kappa
J = np.argpartition(D, NNeighbs, 1)[:, 0:NNeighbs]
I = np.tile(np.arange(N)[:, None], (1, NNeighbs))
V = np.ones(I.size)
[I, J] = [I.flatten(), J.flatten()]
ret = sparse.coo_matrix((V, (I, J)), shape=(N, M))
return ret.toarray()
def print_word_vectors(word_vectors, write_path):
"""
This function prints the collection of word vectors to file, in a plain textual format.
"""
f_write = codecs.open(write_path, 'w', 'utf-8')
for key in word_vectors:
print >>f_write, key, " ".join(map(unicode, numpy.round(word_vectors[key], decimals=6)))
print "Printed", len(word_vectors), "word vectors to:", write_path
def trataGroups(objeto):
current = list(filter(None.__ne__, objeto))
current = np.sort(current, axis=0)
for i in range(len(current[0])):
current_ = [j[i] for j in current]
mean_ = np.round(np.mean(current_, axis=0), 4)
deviation_ = np.round(np.std(current_, axis=0, ddof=1), 4)
return [mean_, deviation_]
def trataGroups(objeto):
current = list(filter(None.__ne__, objeto))
mean_ = np.round(np.mean(current, axis=0), 4)
deviation_ = np.round(np.std(current, axis=0, ddof=1), 4)
fivecent = np.round(np.percentile(current, 5.0, axis=0), 4)
# confidence intervals
lowci = np.round(np.percentile(current, 2.5, axis=0), 4)
highci = np.round(np.percentile(current, 97.5, axis=0), 4)
return [mean_, deviation_, fivecent, current, lowci, highci]
def PA(samples, variables):
datasets = 5000
eig_vals = []
for i in range(datasets):
data = np.random.standard_normal((variables, samples))
cor_ = np.corrcoef(data)
eig_vals.append(np.sort(np.linalg.eig(cor_)[0])[::-1])
quantile = (np.round(np.percentile(eig_vals, 95.0, axis=0), 4))
mean_ = (np.round(np.mean(eig_vals, axis=0), 4))
return quantile
def get_line_region(self, position, name=''):
"""Creates a line region at the given position (start_x, start_y, end_x, end_y),
inclusive.
Args:
position: Position of the line region (start_x, start_y, end_x, end_y).
name: Name of the region.
Returns:
Line region.
"""
start_idx = self.get_index(position[:2])
end_idx = self.get_index(position[2:])
x_diff = start_idx % self.x.samples - end_idx % self.x.samples
y_diff = int(start_idx / self.x.samples) - int(end_idx / self.x.samples)
num_points = max(np.abs([x_diff, y_diff]))
point_indices = []
for ii in range(num_points + 1):
x_position = start_idx % self.x.samples - np.round(ii / num_points * x_diff)
y_position = int(start_idx / self.x.samples) - np.round(ii / num_points * y_diff)
point_indices.append(int(x_position + self.x.samples * y_position))
return reg.LineRegion(point_indices, position, name=name)
def seq(start, stop, step=1):
n = int(round((stop - start)/float(step)))
if n > 1:
return([start + step*i for i in range(n+1)])
else:
return([])
def draw_circles(image,cands,origin,spacing):
#make empty matrix, which will be filled with the mask
image_mask = np.zeros(image.shape, dtype=np.int16)
#run over all the nodules in the lungs
for ca in cands.values:
#get middel x-,y-, and z-worldcoordinate of the nodule
#radius = np.ceil(ca[4])/2 ## original: replaced the ceil with a very minor increase of 1% ....
radius = (ca[4])/2 + 0.51 * spacing[0] # increasing by circa half of distance in z direction .... (trying to capture wider region/border for learning ... and adress the rough net .
coord_x = ca[1]
coord_y = ca[2]
coord_z = ca[3]
image_coord = np.array((coord_z,coord_y,coord_x))
#determine voxel coordinate given the worldcoordinate
image_coord = world_2_voxel(image_coord,origin,spacing)
#determine the range of the nodule
#noduleRange = seq(-radius, radius, RESIZE_SPACING[0]) # original, uniform spacing
noduleRange_z = seq(-radius, radius, spacing[0])
noduleRange_y = seq(-radius, radius, spacing[1])
noduleRange_x = seq(-radius, radius, spacing[2])
#x = y = z = -2
#create the mask
for x in noduleRange_x:
for y in noduleRange_y:
for z in noduleRange_z:
coords = world_2_voxel(np.array((coord_z+z,coord_y+y,coord_x+x)),origin,spacing)
#if (np.linalg.norm(image_coord-coords) * RESIZE_SPACING[0]) < radius: ### original (contrained to a uniofrm RESIZE)
if (np.linalg.norm((image_coord-coords) * spacing)) < radius:
image_mask[int(np.round(coords[0])),int(np.round(coords[1])),int(np.round(coords[2]))] = int(1)
return image_mask
def seq(start, stop, step=1):
n = int(round((stop - start)/float(step)))
if n > 1:
return([start + step*i for i in range(n+1)])
else:
return([])
def train_and_eval_sklearn_classifier( clf, data ):
x_train = data['x_train']
y_train = data['y_train']
x_test = data['x_test']
y_test = data['y_test']
clf.fit( x_train, y_train )
try:
p = clf.predict_proba( x_train )[:,1] # sklearn convention
except IndexError:
p = clf.predict_proba( x_train )
ll = log_loss( y_train, p )
auc = AUC( y_train, p )
acc = accuracy( y_train, np.round( p ))
print "\n# training | log loss: {:.2%}, AUC: {:.2%}, accuracy: {:.2%}".format( ll, auc, acc )
#
try:
p = clf.predict_proba( x_test )[:,1] # sklearn convention
except IndexError:
p = clf.predict_proba( x_test )
ll = log_loss( y_test, p )
auc = AUC( y_test, p )
acc = accuracy( y_test, np.round( p ))
print "# testing | log loss: {:.2%}, AUC: {:.2%}, accuracy: {:.2%}".format( ll, auc, acc )
#return { 'loss': 1 - auc, 'log_loss': ll, 'auc': auc }
return { 'loss': ll, 'log_loss': ll, 'auc': auc }
###
# "clf", even though it's a regressor