本文整理汇总了Python中numpy.where函数的典型用法代码示例。如果您正苦于以下问题:Python where函数的具体用法?Python where怎么用?Python where使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了where函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_float_modulus_exact
def test_float_modulus_exact(self):
# test that float results are exact for small integers. This also
# holds for the same integers scaled by powers of two.
nlst = list(range(-127, 0))
plst = list(range(1, 128))
dividend = nlst + [0] + plst
divisor = nlst + plst
arg = list(itertools.product(dividend, divisor))
tgt = list(divmod(*t) for t in arg)
a, b = np.array(arg, dtype=int).T
# convert exact integer results from Python to float so that
# signed zero can be used, it is checked.
tgtdiv, tgtrem = np.array(tgt, dtype=float).T
tgtdiv = np.where((tgtdiv == 0.0) & ((b < 0) ^ (a < 0)), -0.0, tgtdiv)
tgtrem = np.where((tgtrem == 0.0) & (b < 0), -0.0, tgtrem)
for dt in np.typecodes['Float']:
msg = 'dtype: %s' % (dt,)
fa = a.astype(dt)
fb = b.astype(dt)
# use list comprehension so a_ and b_ are scalars
div = [self.floordiv(a_, b_) for a_, b_ in zip(fa, fb)]
rem = [self.mod(a_, b_) for a_, b_ in zip(fa, fb)]
assert_equal(div, tgtdiv, err_msg=msg)
assert_equal(rem, tgtrem, err_msg=msg)
示例2: soft_threshold
def soft_threshold(lamda,b):
th = float(lamda)/2.0
print ("(lamda,Threshold)",lamda,th)
print("The type of b is ..., its len is ",type(b),b.shape,len(b[0]))
if(lamda == 0):
return b
m,n = b.shape
x = np.zeros((m,n))
k = np.where(b > th)
# print("(b > th)",k)
#print("Number of elements -->(b > th) ",type(k))
x[k] = b[k] - th
k = np.where(np.absolute(b) <= th)
# print("abs(b) <= th",k)
# print("Number of elements -->abs(b) <= th ",len(k))
x[k] = 0
k = np.where(b < -th )
# print("(b < -th )",k)
# print("Number of elements -->(b < -th ) <= th",len(k))
x[k] = b[k] + th
x = x[:]
return x
示例3: plotResult
def plotResult(self, nn):
cmask = np.where(self.y==1);
plot(self.X[cmask,0], self.X[cmask,1], 'or', markersize=4)
cmask = np.where(self.y==2);
plot(self.X[cmask,0], self.X[cmask,1], 'ob', markersize=4)
cmask = np.where(self.y==3);
plot(self.X[cmask,0], self.X[cmask,1], 'og', markersize=4)
minX = min(self.X[:,0])
minY = min(self.X[:,1])
maxX = max(self.X[:,0])
maxY = max(self.X[:,1])
grid_range = [minX, maxX, minY, maxY];
delta = 0.05; levels = 100
a = arange(grid_range[0],grid_range[1],delta)
b = arange(grid_range[2],grid_range[3],delta)
A, B = meshgrid(a, b)
values = np.zeros(A.shape)
for i in range(len(a)):
for j in range(len(b)):
values[j,i] = nn.getNetworkOutput( [ a[i], b[j] ] )
contour(A, B, values, levels=[1], colors=['k'], linestyles='dashed')
contourf(A, B, values, levels=linspace(values.min(),values.max(),levels), cmap=cm.RdBu)
示例4: infos
def infos() :
'''Some information about the neuronal populations for each structures'''
print "Striatal populations: %d" % len(STR)
print "Pallidal populations: %d" % len(GP)
print
C = (W_STR_STR > 0).sum(axis=1) + (W_STR_STR < 0).sum(axis=1)
L = W_STR_STR[np.where(W_STR_STR != 0)]
print "Collateral striatal connections"
print "Mean number: %g (+/- %g)" % (C.mean(), C.std())
print "Mean length: %g (+/- %g)" % (L.mean(), L.std())
print
C = (W_GP_GP > 0).sum(axis=1) + (W_GP_GP < 0).sum(axis=1)
L = W_GP_GP[np.where(W_GP_GP != 0)]
print "Collateral pallidal connections"
print "Mean number: %g (+/- %g)" % (C.mean(), C.std())
print "Mean length: %g (+/- %g)" % (L.mean(), L.std())
print
C = (W_STR_GP > 0).sum(axis=1) + (W_STR_GP < 0).sum(axis=1)
L = W_STR_GP[np.where(W_STR_GP != 0)]
print "Striato-pallidal connections"
print "Mean number: %g (+/- %g)" % (C.mean(), C.std())
print "Mean length: %g (+/- %g)" % (L.mean(), L.std())
print
print "Mean # collateral striato-pallidal connections: %g (+/- %g)" % (C.mean(), C.std())
C = (W_GP_STR > 0).sum(axis=1) + (W_GP_STR < 0).sum(axis=1)
L = W_GP_STR[np.where(W_GP_STR != 0)]
print "Pallido-striatal connections"
print "Mean number: %g (+/- %g)" % (C.mean(), C.std())
print "Mean length: %g (+/- %g)" % (L.mean(), L.std())
print
示例5: compute_cost
def compute_cost( X, y, theta, lam ):
'''Compute cost for logistic regression.'''
# Number of training examples
m = y.shape[0]
# Compute the prediction based on theta and X
predictions = X.dot( theta )
# Preprocessing values before sending to sigmoid function.
# If the argument to sigmoid function >= 0, we know that the
# sigmoid value is 1. Similarly for the negative values.
predictions[ where( predictions >= 20 ) ] = 20
predictions[ where( predictions <= -500 ) ] = -500
hypothesis = sigmoid( predictions )
hypothesis[ where( hypothesis == 1.0 ) ] = 0.99999
# Part of the cost function without regularization
J1 = ( -1.0 / m ) * sum( ( y * np.log( hypothesis ) ) +
( ( 1.0 - y ) * np.log( 1.0 - hypothesis ) ) )
# Computing the regularization term
J2 = lam / ( 2.0 * m ) * sum( theta[ 1:, ] * theta[ 1:, ] )
error = hypothesis - y
return J1 + J2
示例6: pixel_to_prime
def pixel_to_prime(self, x, y, color=0):
# Secret decoder ring:
# http://www.sdss.org/dr7/products/general/astrometry.html
# (color)0 is called riCut;
# g0, g1, g2, and g3 are called
# dRow0, dRow1, dRow2, and dRow3, respectively;
# h0, h1, h2, and h3 are called
# dCol0, dCol1, dCol2, and dCol3, respectively;
# px and py are called csRow and csCol, respectively;
# and qx and qy are called ccRow and ccCol, respectively.
color0 = self._get_ricut()
g0, g1, g2, g3 = self._get_drow()
h0, h1, h2, h3 = self._get_dcol()
px, py, qx, qy = self._get_cscc()
# #$(%*&^(%$%*& bad documentation.
(px,py) = (py,px)
(qx,qy) = (qy,qx)
yprime = y + g0 + g1 * x + g2 * x**2 + g3 * x**3
xprime = x + h0 + h1 * x + h2 * x**2 + h3 * x**3
# The code below implements this, vectorized:
# if color < color0:
# xprime += px * color
# yprime += py * color
# else:
# xprime += qx
# yprime += qy
qx = qx * np.ones_like(x)
qy = qy * np.ones_like(y)
xprime += np.where(color < color0, px * color, qx)
yprime += np.where(color < color0, py * color, qy)
return (xprime, yprime)
示例7: processTrafficData
def processTrafficData(self):
for index, row in self.traffic_data.iterrows():
adjacent_list = []
if index > 1 and index < 18467:
if self.traffic_data.ix[index - 1]['inter1'] == row[0]:
key1 = self.traffic_data.ix[index - 1]['inter1'] + ', ' + self.traffic_data.ix[index - 1]['inter2']
adjacent_list.append(key1)
if self.traffic_data.ix[index + 1]['inter1'] == row[0]:
key2 = self.traffic_data.ix[index + 1]['inter1'] + ', ' + self.traffic_data.ix[index + 1]['inter2']
adjacent_list.append(key2)
keylist = np.where(self.traffic_data['inter1'] == row[1])[0]
keylist1 = np.where(self.traffic_data['inter2'] == row[0])[0]
ind_list = np.intersect1d(keylist, keylist1)
if len(ind_list) >= 1:
ind = ind_list[0]
if ind > 1 and ind < 18467:
if self.traffic_data.ix[ind - 1]['inter1'] == row[1]:
key3 = self.traffic_data.ix[ind - 1]['inter1'] + ', ' + self.traffic_data.ix[ind - 1]['inter2']
adjacent_list.append(key3)
if self.traffic_data.ix[ind + 1]['inter1'] == row[1]:
key4 = self.traffic_data.ix[ind + 1]['inter1'] + ', ' + self.traffic_data.ix[ind + 1]['inter2']
adjacent_list.append(key4)
node = row['node']
node.setAdjacents(adjacent_list)
示例8: word2ind
def word2ind(word, vocab, utok_ind=None):
ind = np.where(vocab == unicode(word))
if len(ind[0]) == 0:
if not utok_ind:
utok_ind = np.where(vocab == UTOK)
ind = utok_ind
return ind[0][0]
示例9: test_background_model
def test_background_model(tmpdir):
data_store = DataStore.from_dir('$GAMMAPY_EXTRA/datasets/hess-crab4-hd-hap-prod2/')
bgmaker = OffDataBackgroundMaker(data_store, outdir=str(tmpdir))
bgmaker.select_observations(selection='all')
table = Table.read('run.lis', format='ascii.csv')
assert table['OBS_ID'][1] == 23526
bgmaker.group_observations()
table = ObservationTable.read(str(tmpdir / 'obs.fits'))
assert list(table['GROUP_ID']) == [0, 0, 0, 1]
table = ObservationTable.read(str(tmpdir / 'group-def.fits'))
assert list(table['ZEN_PNT_MAX']) == [49, 90]
# TODO: Fix 3D code
# bgmaker.make_model("3D")
# bgmaker.save_models("3D")
# model = CubeBackgroundModel.read(str(tmpdir / 'background_3D_group_001_table.fits.gz'))
# assert model.counts_cube.data.sum() == 1527
bgmaker.make_model("2D")
bgmaker.save_models("2D")
model = EnergyOffsetBackgroundModel.read(str(tmpdir / 'background_2D_group_001_table.fits.gz'))
assert model.counts.data.value.sum() == 1398
index_table_new = bgmaker.make_total_index_table(data_store, "2D", None, None)
table_bkg = index_table_new[np.where(index_table_new["HDU_NAME"] == "bkg_2d")]
name_bkg_run023523 = table_bkg[np.where(table_bkg["OBS_ID"] == 23523)]["FILE_NAME"]
assert str(tmpdir) + "/" + name_bkg_run023523[0] == str(tmpdir) + '/background_2D_group_001_table.fits.gz'
name_bkg_run023526 = table_bkg[np.where(table_bkg["OBS_ID"] == 23526)]["FILE_NAME"]
assert str(tmpdir) + "/" + name_bkg_run023526[0] == str(tmpdir) + '/background_2D_group_000_table.fits.gz'
示例10: _lininterp
def _lininterp(self,x,X,Y):
if hasattr(x,'__len__'):
xtype = 'array'
xx=np.asarray(x).astype(np.float)
else:
xtype = 'scalar'
xx=np.asarray([x]).astype(np.float)
idx = X.searchsorted(xx)
yy = xx*0
yy[idx>len(X)-1] = Y[-1] # over
yy[idx<=0] = Y[0] # under
wok = np.where((idx>0) & (idx<len(X))) # the good ones
iok=idx[wok]
yywok = Y[iok-1] + ( (Y[iok]-Y[iok-1])/(X[iok]-X[iok-1])
* (xx[wok]-X[iok-1]) )
w = np.where( ((X[iok]-X[iok-1]) == 0) ) # where are the nan ?
yywok[w] = Y[iok[w]-1] # replace by previous value
wl = np.where(xx[wok] == X[0])
yywok[wl] = Y[0]
wh = np.where(xx[wok] == X[-1])
yywok[wh] = Y[-1]
yy[wok] = yywok
if xtype == 'scalar':
yy = yy[0]
return yy
示例11: corrtag_image
def corrtag_image(in_data,xtype='XCORR',ytype='YCORR',pha=(2,30),bins=(1024,16384),times=None,ranges=((0,1023),(0,16384)),binning=(1,1),NUV=False):
try: histogram2d
except NameError: from numpy import histogram2d,where,zeros
try: getdata
except NameError: from pyfits import getdata
try: events=getdata(in_data,1)
except: events=in_data
xlength = (ranges[1][1]+1)
ylength = (ranges[0][1]+1)
xbinning = binning[1]
ybinning = binning[0]
if NUV:
bins = (1024,1024)
pha = (-1,1)
ranges = ( (0,1023), (0,1023) )
if times != None:
index = where( (events['TIME']>=times[0]) & (events['TIME'] <= times[1]) )
events= events[index]
index = where((events['PHA']>=pha[0])&(events['PHA']<=pha[1]))
if len(index[0]):
image,y_r,x_r = histogram2d(events[ytype][index],events[xtype][index],bins=bins,range=ranges)
else:
image = zeros( (bins[0]//binning[0],bins[1]//binning[1]) )
return image
示例12: __init__
def __init__(self,turn,elem,single,name,s,x,xp,y,yp,pc,de,tau,**args):
apc=float(pc[0])*1e9
ade=float(de[0])
self.m0=self.pmass
en=np.sqrt(apc**2+self.pmass**2)
self.e0=en-ade
self.p0c=np.sqrt(self.e0**2-self.m0**2)
# structure
self.elem=np.array(elem,dtype=int)
self.turn=np.array(turn,dtype=int)
d0=np.where(np.diff(self.elem)!=0)[0][0]+1
d1=(np.where(np.diff(self.turn)!=0)[0][0]+1)/d0
d2=len(self.turn)/d1/d0
self.single=np.array(single,dtype=int)
self.name=np.array(name,dtype=str)
self.s =np.array(s ,dtype=float)
self.x =np.array(x ,dtype=float)
self.y =np.array(y ,dtype=float)
self.tau=-np.array(tau,dtype=float)*self.clight
opd=np.array(pc,dtype=float)*(1e9/self.p0c)
self.delta=opd-1
self.pt=np.array(de,dtype=float)/self.p0c
self.px=np.array(xp,dtype=float)*opd
self.py=np.array(yp,dtype=float)*opd
for nn,vv in self.__dict__.items():
if hasattr(vv,'__len__') and len(vv)==d0*d1*d2:
setattr(self,nn,vv.reshape(d2,d1,d0))
示例13: mutual_information
def mutual_information(self, x_index, y_index, log_base, debug=False):
"""
Calculate and return Mutual information between two random variables
"""
# Check if index are into the bounds
assert (0 <= x_index <= self.n_rows)
assert (0 <= y_index <= self.n_rows)
# Variable to return MI
summation = 0.0
# Get uniques values of random variables
values_x = set(self.data[x_index])
values_y = set(self.data[y_index])
# Print debug info
if debug:
print 'MI between'
print self.data[x_index]
print self.data[y_index]
# For each random
for value_x in values_x:
for value_y in values_y:
px = shape(where(self.data[x_index] == value_x))[1] / self.n_cols
py = shape(where(self.data[y_index] == value_y))[1] / self.n_cols
pxy = len(where(in1d(where(self.data[x_index] == value_x)[0],
where(self.data[y_index] == value_y)[0]) == True)[0]) / self.n_cols
if pxy > 0.0:
summation += pxy * math.log((pxy / (px * py)), log_base)
if debug:
print '(%d,%d) px:%f py:%f pxy:%f' % (value_x, value_y, px, py, pxy)
return summation
示例14: entropy
def entropy(self, x_index, y_index, log_base, debug=False):
"""
Calculate the entropy between two random variable
"""
assert (0 <= x_index <= self.n_rows)
assert (0 <= y_index <= self.n_rows)
# Variable to return MI
summation = 0.0
# Get uniques values of random variables
values_x = set(self.data[x_index])
values_y = set(self.data[y_index])
# Print debug info
if debug:
print 'Entropy between'
print self.data[x_index]
print self.data[y_index]
# For each random
for value_x in values_x:
for value_y in values_y:
pxy = len(where(in1d(where(self.data[x_index] == value_x)[0],
where(self.data[y_index] == value_y)[0]) == True)[0]) / self.n_cols
if pxy > 0.0:
summation += pxy * math.log(pxy, log_base)
if debug:
print '(%d,%d) pxy:%f' % (value_x, value_y, pxy)
if summation == 0.0:
return summation
else:
return - summation
示例15: myTradingSystem
def myTradingSystem(DATE, CLOSE, settings):
''' This system uses mean reversion techniques to allocate capital into the desired equities '''
# This strategy evaluates two averages over time of the close over a long/short
# scale and builds the ratio. For each day, "smaQuot" is an array of "nMarkets"
# size.
nMarkets = numpy.shape(CLOSE)[1]
periodLong = 200
periodShort = 40
smaLong = numpy.sum(CLOSE[-periodLong:, :], axis=0)/periodLong
smaRecent = numpy.sum(CLOSE[-periodShort:, :], axis=0)/periodShort
smaQuot = smaRecent / smaLong
# For each day, scan the ratio of moving averages over the markets and find the
# market with the maximum ratio and the market with the minimum ratio:
longEquity = numpy.where(smaQuot == numpy.nanmin(smaQuot))
shortEquity = numpy.where(smaQuot == numpy.nanmax(smaQuot))
# Take a contrarian view, going long the market with the minimum ratio and
# going short the market with the maximum ratio. The array "pos" will contain
# all zero entries except for those cases where we go long (1) and short (-1):
pos = numpy.zeros((1, nMarkets))
pos[0, longEquity[0][0]] = 1
pos[0, shortEquity[0][0]] = -1
# For the position sizing, we supply a vector of weights defining our
# exposure to the markets in settings['markets']. This vector should be
# normalized.
pos = pos/numpy.nansum(abs(pos))
return pos, settings