本文整理汇总了Python中numpy.argmin函数的典型用法代码示例。如果您正苦于以下问题:Python argmin函数的具体用法?Python argmin怎么用?Python argmin使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了argmin函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: find_surface
def find_surface(cube):
"""
Return the `cube` index for the surface layer of for any model grid
(rgrid, ugrid, sgrid), and any non-dimensional coordinate.
TODO: Fold this into `find_layer()`
"""
z = z_coord(cube)
if not z:
msg = "Cannot find the surface for cube {!r}".format
raise ValueError(msg(cube))
else:
if np.argmin(z.shape) == 0 and z.ndim == 2:
points = z[:, 0].points
elif np.argmin(z.shape) == 1 and z.ndim == 2:
points = z[0, :].points
else:
points = z.points
positive = z.attributes.get('positive', None)
if positive == 'up':
idx = np.unique(points.argmax(axis=0))[0]
else:
idx = np.unique(points.argmin(axis=0))[0]
return idx
示例2: main
def main():
try:
prog = sys.argv[0]
# a = float(sys.argv[1])
# b = float(sys.argv[2])
except IndexError:
# print '\nusage: '+prog+' a b (where a & b are numbers)\n'
sys.exit(0)
unoptFile = open('i.dat','r')
gamma_data = []
i_data = []
for line in unoptFile:
gamma_data.append(float(line.split()[0]))
i_data.append(float(line.split()[1]))
gamma_data, i_data = np.array(gamma_data), np.array(i_data)
unopt_gamma = gamma_data[np.argmin(i_data)]
unoptFile.close()
optFile = open('omega_opt_ideriv/i.dat','r')
gamma_data = []
i_data = []
for line in optFile:
gamma_data.append(float(line.split()[0]))
i_data.append(float(line.split()[1]))
gamma_data, i_data = np.array(gamma_data), np.array(i_data)
opt_gamma = gamma_data[np.argmin(i_data)]
optFile.close()
print unopt_gamma - opt_gamma, ' '
示例3: fitChiSq
def fitChiSq(self, sampleSpec, returnChiSq=False):
if np.all(sampleSpec.wave == self.wave):
newSampleSpec = sampleSpec
grid = self.values
else:
print "error in function"
newSampleSpec = sampleSpec.interpolate(self.wave)
minIDx = self.wave.searchsorted(sampleSpec.wave[0])
maxIDx = self.wave.searchsorted(sampleSpec.wave[-1])
grid = self.values[:,minIDx:maxIDx]
if newSampleSpec.var != None:
var = newSampleSpec.var
else:
var = 1.
if newSampleSpec.dq != None:
dqMask = newSampleSpec.dq
else:
dqMask = np.ones(grid.shape[1]).astype(bool)
chiSq = ((grid[:,dqMask]-newSampleSpec.flux[dqMask])/var[dqMask])**2
nu = (np.ones(grid.shape[0])*grid.shape[1]) - len(self.params) - 1
redChiSq = np.sum(chiSq, axis=1) / nu
if returnChiSq:
return np.min(redChiSq), self.points[np.argmin(redChiSq)]
else:
return self.points[np.argmin(redChiSq)]
示例4: click
def click(self,event):
"""
What to do, if a click on the figure happens:
1. Check which axis
2. Get data coord's.
3. Plot resulting data.
4. Update Figure
"""
if event.inaxes==self.overview:
#Get nearest data
xpos=np.argmin(np.abs(event.xdata-self.x))
ypos=np.argmin(np.abs(event.ydata-self.y))
#Check which mouse button:
if event.button==1:
#Plot it
c,=self.y_subplot.plot(self.y, self.z[:,xpos],label=str(self.x[xpos]))
self.overview.axvline(self.x[xpos],color=c.get_color(),lw=2)
elif event.button==3:
#Plot it
c,=self.x_subplot.plot(self.x, self.z[ypos,:],label=str(self.y[ypos]))
self.overview.axhline(self.y[ypos],color=c.get_color(),lw=2)
if event.inaxes==self.y_subplot:
ypos=np.argmin(np.abs(event.xdata-self.y))
c=self.x_subplot.plot(self.x, self.z[ypos,:],label=str(self.y[ypos]))
self.overview.axhline(self.y[ypos],color=c.get_color(),lw=2)
if event.inaxes==self.x_subplot:
xpos=np.argmin(np.abs(event.xdata-self.x))
c,=self.y_subplot.plot(self.y, self.z[:,xpos],label=str(self.x[xpos]))
self.overview.axvline(self.x[xpos],color=c.get_color(),lw=2)
#Show it
plt.draw()
示例5: get_rectangular_subset
def get_rectangular_subset(lon,lat,data,view,xmin,xmax,ymin,ymax,buffer=0):
imin = np.argmin(np.abs(lon-xmin))
imax = np.argmin(np.abs(lon-xmax))
jmin = np.argmin(np.abs(lat-ymin))
jmax = np.argmin(np.abs(lat-ymax))
return lon[imin-buffer:imax+1+buffer], lat[jmin-buffer:jmax+1+buffer], grid_convert(grid_convert(data,view,'x+y+')[imin-buffer:imax+1+buffer,jmin-buffer:jmax+1+buffer],'x+y+',view)
示例6: kmeans
def kmeans(xx, centroids, maxIters = 20, minclust=30, maxDiff = 2):
# Cluster Assignment step
ca = np.array([np.argmin([np.dot(x_i-y_k, x_i-y_k) for y_k in centroids]) for x_i in xx])
# all clusters have at least minclust?
(unique, counts) = np.unique(ca, return_counts=True)
for cc in counts:
if cc < minclust:
return("error: too few", np.array(centroids), ca)
# Move centroids step
centroids = np.array([xx[ca == k].mean(axis = 0) for k in range(centroids.shape[0])])
iter=1
while (iter<maxIters):
# Cluster Assignment step
canew = np.array([np.argmin([np.dot(x_i-y_k, x_i-y_k) for y_k in centroids]) for x_i in xx])
# all clusters have at least minclust?
(unique, counts) = np.unique(canew, return_counts=True)
for cc in counts:
if cc < minclust:
return("error: too few", np.array(centroids), canew)
numdiff = sum(ca != canew)
if numdiff < maxDiff:
return("converged", np.array(centroids), canew)
ca = canew
# Move centroids step
centroids = np.array([xx[ca == k].mean(axis = 0) for k in range(centroids.shape[0])])
iter += 1
return("error: not converged", np.array(centroids), ca)
示例7: draw
def draw(self):
self.spec_widg.on_draw(no_draw=True)
# Add text?
for kk,lls in enumerate(self.abssys_widg.all_abssys):
# Label
ipos = self.abssys_widg.all_items[kk].rfind('_')
ilbl = self.abssys_widg.all_items[kk][ipos+1:]
# Add text
for wv,lbl in self.plt_wv:
idx = np.argmin(np.abs(self.continuum.dispersion-wv*(1+lls.zabs)))
self.spec_widg.ax.text(wv.value*(1+lls.zabs),
self.continuum.flux[idx],
'{:s}_{:s}'.format(ilbl,lbl), ha='center',
color='blue', size='small', rotation=90.)
# Ticks for selected LLS
idxl = self.get_sngl_sel_sys()
if idxl is not None:
lls = self.abssys_widg.all_abssys[idxl]
# Label
ipos = self.abssys_widg.all_items[idxl].rfind('_')
ilbl = self.abssys_widg.all_items[idxl][ipos+1:]
for line in lls.lls_lines:
if line.wrest < 915.*u.AA:
continue
idx = np.argmin(np.abs(self.continuum.dispersion-
line.wrest*(1+lls.zabs)))
self.spec_widg.ax.text(line.wrest.value*(1+lls.zabs),
self.continuum.flux[idx],
'-{:s}'.format(ilbl), ha='center',
color='red', size='small', rotation=90.)
# Draw
self.spec_widg.canvas.draw()
示例8: get_cmd_shape
def get_cmd_shape(color, mag):
'''
gets the outline of a cmd. Guesses at a large polygon, and then add points
that are outside of the polygon, ignores points within.
then polar sorts the result.
returns: N,2 array.
'''
# make a guess at the polygon.
left = (np.min(color), mag[np.argmin(color)])
right = (np.max(color), mag[np.argmax(color)])
up = (color[np.argmin(mag)], np.min(mag))
down = (color[np.argmax(mag)], np.max(mag))
verts = np.vstack((left, right, up, down))
points = np.column_stack((color, mag))
for point in points:
if nxutils.pnpoly(point[0], point[1], verts) == 0.:
# add point to verts
col = verts[:, 0]
m = verts[:, 1]
col = np.append(col, point[0])
m = np.append(m, point[1])
# order the new points in a circle
verts = polar_sort(zip(col, m))
verts = np.append(verts, [verts[0]], axis=0)
# plt.plot(verts[:, 0], verts[:, 1], lw = 2)
return verts
示例9: _augmenting_row_reduction
def _augmenting_row_reduction(self):
"""
Augmenting row reduction step from LAPJV algorithm
"""
unassigned = np.where(self._x == -1)[0]
for i in unassigned:
while True:
#find smallest 2 values and indices
temp = self.c[i] - self._v
j1 = np.argmin(temp)
u1 = temp[j1]
temp[j1] = np.max(temp) + 1
j2 = np.argmin(temp)
u2 = temp[j2]
if u1 < u2:
self._v[j1] -= u2 - u1
elif self._y[j1] != -1:
j1 = j2
k = self._y[j1]
if k != -1:
self._x[k] = -1
self._x[i] = j1
self._y[j1] = i
i = k
if np.allclose(u1, u2) or k == -1:
break
示例10: _make_tuples
def _make_tuples(self, key):
print('Populating', key)
spikes = np.vstack([s.squeeze() for s in (preprocess.Spikes.RateTrace() & key).fetch('rate_trace')])
s = spikes.sum(axis=0)
nans = np.isnan(s)
key['leading_nans'] = int(nans[0])
key['trailing_nans'] = int(nans[1])
t = (preprocess.Sync() & key).fetch1('frame_times') # does not need to be unique
flip_first = (vis.Trial() * preprocess.Sync().proj('psy_id', trial_idx='first_trial') & key).fetch1('flip_times')
flip_last = (vis.Trial() * preprocess.Sync().proj('psy_id', trial_idx='last_trial') & key).fetch1('flip_times')
# (vis.Trial() * preprocess.Sync() & 'trial_idx between first_trial and last_trial')
fro = np.atleast_1d(flip_first.squeeze())[0]
to = np.atleast_1d(flip_last.squeeze())[
-1] # not necessarily where the stimulus stopped, just last presentation
idx_fro = np.argmin(np.abs(t - fro))
idx_to = np.argmin(np.abs(t - to)) + 1
key['stimulus_nans'] = int(np.any(nans[idx_fro:idx_to]))
if np.any(nans):
key['nan_idx'] = nans
key['stimulus_start'] = idx_fro + 1
key['stimulus_end'] = idx_to
self.insert1(key)
示例11: find_boundaries
def find_boundaries(self, all=False):
"""Find the local minima on either side of each peak
Arguments:
(none)
"""
try:
prevb = np.argmin(self.y[0:self._idx[0]])
except IndexError:
prevb = 0
bounds = []
if not all:
pos = self._idx[self._keep]
else:
pos = self._idx
npks = len(pos)
for i in range(npks):
thismax = pos[i]
if i < npks-1:
nextmax = pos[i + 1]
relb = np.argmin(self.y[thismax:nextmax])
nextb = relb + thismax
else:
nextmax = len(self.y)-1
nextb = len(self.y)-1
bounds.append([prevb, nextb])
prevb = nextb
self._bounds = np.array(bounds)
示例12: _transform_indices
def _transform_indices(self, key):
"""
Transforms indices by snapping to the closest value if
values are numeric, otherwise applies no transformation.
"""
ndims = self.ndims
if all(not isinstance(el, slice) for el in key):
dim_inds = []
for dim in self._cached_index_names:
dim_type = self.get_dimension_type(dim)
if isinstance(dim_type, type) and issubclass(dim_type, Number):
dim_inds.append(self.get_dimension_index(dim))
str_keys = iter(key[i] for i in range(self.ndims)
if i not in dim_inds)
num_keys = []
if len(dim_inds):
keys = list({tuple(k[i] if ndims > 1 else k for i in dim_inds)
for k in self.keys()})
q = np.array([tuple(key[i] if ndims > 1 else key for i in dim_inds)])
idx = np.argmin([np.inner(q - np.array(x), q - np.array(x))
if len(dim_inds) == 2 else np.abs(q-x)
for x in keys])
num_keys = iter(keys[idx])
key = tuple(next(num_keys) if i in dim_inds else next(str_keys)
for i in range(self.ndims))
elif any(not isinstance(el, slice) for el in key):
index_ind = [idx for idx, el in enumerate(key)
if not isinstance(el, (slice, str))][0]
dim_keys = np.array([k[index_ind] for k in self.keys()])
snapped_val = dim_keys[np.argmin(np.abs(dim_keys-key[index_ind]))]
key = list(key)
key[index_ind] = snapped_val
key = tuple(key)
return key
示例13: estimate_pk_parms_1d
def estimate_pk_parms_1d(x,f,pktype):
"""
Gives initial guess of parameters for analytic fit of one dimensional peak
data.
Required Arguments:
x -- (n) ndarray of coordinate positions
f -- (n) ndarray of intensity measurements at coordinate positions x
pktype -- string, type of analytic function that will be used to fit the data,
current options are "gaussian","lorentzian","pvoigt" (psuedo voigt), and
"split_pvoigt" (split psuedo voigt)
Outputs:
p -- (m) ndarray containing initial guesses for parameters for the input peaktype
(see peak function help for what each parameters corresponds to)
"""
data_max=np.max(f)
# lbg=np.mean(f[:2])
# rbg=np.mean(f[:2])
if((f[0]> (0.25*data_max)) and (f[-1]> (0.25*data_max))):#heuristic for wide peaks
bg0=0.
elif (f[0]> (0.25*data_max)): #peak cut off on the left
bg0=f[-1]
elif (f[-1]> (0.25*data_max)): #peak cut off on the right
bg0=f[0]
else:
bg0=(f[0]+f[-1])/2.
#bg1=(rbg-lbg)/(x[-1]-x[0])
cen_index=np.argmax(f)
x0=x[cen_index]
A=data_max-bg0#-(bg0+bg1*x0)
num_pts=len(f)
#checks for peaks that are cut off
if cen_index == (num_pts-1):
FWHM=x[cen_index]-x[np.argmin(np.abs(f[:cen_index]-A/2.))]#peak cut off on the left
elif cen_index == 0:
FWHM=x[cen_index+np.argmin(np.abs(f[cen_index+1:]-A/2.))]-x[0] #peak cut off on the right
else:
FWHM=x[cen_index+np.argmin(np.abs(f[cen_index+1:]-A/2.))]-x[np.argmin(np.abs(f[:cen_index]-A/2.))]
if FWHM <=0:##uh,oh something went bad
FWHM=(x[-1]-x[0])/4. #completely arbitrary, set peak width to 1/4 window size
if pktype=='gaussian' or pktype=='lorentzian':
p=[A,x0,FWHM,bg0,0.]
elif pktype=='pvoigt':
p=[A,x0,FWHM,0.5,bg0,0.]
elif pktype=='split_pvoigt':
p=[A,x0,FWHM,FWHM,0.5,0.5,bg0,0.]
p=np.array(p)
return p
示例14: gpu_nnc_predict
def gpu_nnc_predict(trX, trY, teX, metric='cosine', batch_size=4096):
if metric == 'cosine':
metric_fn = cosine_dist
else:
metric_fn = euclid_dist
idxs = []
for i in range(0, len(teX), batch_size):
mb_dists = []
mb_idxs = []
for j in range(0, len(trX), batch_size):
dist = metric_fn(floatX(teX[i:i+batch_size]), floatX(trX[j:j+batch_size]))
if metric == 'cosine':
mb_dists.append(np.max(dist, axis=1))
mb_idxs.append(j+np.argmax(dist, axis=1))
else:
mb_dists.append(np.min(dist, axis=1))
mb_idxs.append(j+np.argmin(dist, axis=1))
mb_idxs = np.asarray(mb_idxs)
mb_dists = np.asarray(mb_dists)
if metric == 'cosine':
i = mb_idxs[np.argmax(mb_dists, axis=0), np.arange(mb_idxs.shape[1])]
else:
i = mb_idxs[np.argmin(mb_dists, axis=0), np.arange(mb_idxs.shape[1])]
idxs.append(i)
idxs = np.concatenate(idxs, axis=0)
nearest = trY[idxs]
return nearest
示例15: optimize_pa
def optimize_pa(self,fixed_pa=False,step=10,pa_init=0,pa_max=180):
self.fixed_pa_best=False
if fixed_pa:
self.fixed_pa_best=True
self.pa_best=fixed_pa
else:
self.pas=[]
self.chi2=[]
pa=pa_init
while True:
##
## this could be made more efficient by only calling the sub-routine rotate_and_fft
## but then need to watch out for differential vs. absolute rotations
i=img2vis(self.f_model, self.pxscale, self.lam, oifits=self.oifits, pa=pa, phot=self.phot)
self.chi2.append(i.vis_chi2())
self.pas.append(pa)
pa+=step
if pa > pa_max:
break
## chose global chi2 minimum here for the moment, and plot PA vs. chi2
## so that we see if global minimum is bad.
self.pa_best = self.pas[np.argmin(self.chi2)]
##
## write out best chi**2 and name of model
with open("chi2_min.txt","a") as f:
txt="{0:06.0f} -- {1:5.3f} -- {2:5.2f} -- {3}\n".format(self.chi2[np.argmin(self.chi2)], self.pxscale, self.f_p, self.f_model)
f.write(txt)
self.rotate_and_fft(self.pa_best)