def data(self, size=20):
"""Create some fake data in a dataframe"""
numpy.random.seed(0)
random.seed(0)
x = scipy.rand(size)
M = scipy.zeros([size,size])
for i in range(size):
for j in range(size): M[i,j] = abs(x[i] - x[j])
df = pandas.DataFrame(M, index=[names.get_last_name() for _ in range(size)],
columns=[names.get_first_name() for _ in range(size)])
df['Mary']['Day'] = 1.5
df['Issac']['Day'] = 1.0
return df
python类zeros()的实例源码
def predict(tau,model,xT,yT):
err = sp.zeros(tau.size)
for j,t in enumerate(tau):
yp = model.predict(xT,tau=t)[0]
eq = sp.where(yp.ravel()==yT.ravel())[0]
err[j] = eq.size*100.0/yT.size
return err
def cross_validation(self,x,y,tau,v=5):
'''
Function that computes the cross validation accuracy for the value tau of the regularization
Input:
x : the training samples
y : the labels
tau : a range of values to be tested
v : the number of fold
Output:
err : the estimated error with cross validation for all tau's value
'''
## Initialization
ns = x.shape[0] # Number of samples
np = tau.size # Number of parameters to test
cv = CV() # Initialization of the indices for the cross validation
cv.split_data_class(y)
err = sp.zeros(np) # Initialization of the errors
## Create GMM model for each fold
model_cv = []
for i in range(v):
model_cv.append(GMMR())
model_cv[i].learn(x[cv.it[i],:], y[cv.it[i]])
## Initialization of the pool of processes
pool = mp.Pool()
processes = [pool.apply_async(predict,args=(tau,model_cv[i],x[cv.iT[i],:],y[cv.iT[i]])) for i in range(v)]
pool.close()
pool.join()
for p in processes:
err += p.get()
err /= v
## Free memory
for model in model_cv:
del model
elf
del processes,pool,model_cv
return tau[err.argmax()],err
def asStackBW(self, size=None):
'''
Outputs an image buffer as a 3D numpy array ("stack") of grayscale images.
@param size: A tuple (w,h) indicating the output size of each frame.
If None, then the size of the first image in the buffer will be used.
@return: a 3D array (stack) of the gray scale version of the images
in the buffer. The dimensions of the stack are (N,w,h), where N is
the number of images (buffer size), w and h are the width and height
of each image.
'''
if size==None:
img0 = self[0]
(w,h) = img0.size
else:
(w,h) = size
f = self.getCount()
stack = sp.zeros((f,w,h))
for i,img in enumerate(self._data):
#if img is not (w,h) in size, then resize first
sz = img.size
if (w,h) != sz:
img2 = img.resize((w,h))
mat = img2.asMatrix2D()
else:
mat = img.asMatrix2D()
stack[i,:,:] = mat
return stack
def auc_mat(y, prob, weights = None):
if weights == None or len(weights) == 0:
weights = scipy.ones([y.shape[0], 1])
wweights = weights*y
wweights = wweights.flatten()
wweights = scipy.reshape(wweights, [1, wweights.size])
ny= y.shape[0]
a = scipy.zeros([ny, 1])
b = scipy.ones([ny, 1])
yy = scipy.vstack((a, b))
pprob = scipy.vstack((prob,prob))
result = auc(yy, pprob, wweights)
return(result)
#=========================
def lambda_interp(lambdau, s):
# lambda is the index sequence that is produced by the model
# s is the new vector at which evaluations are required.
# the value is a vector of left and right indices, and a vector of fractions.
# the new values are interpolated bewteen the two using the fraction
# Note: lambda decreases. you take:
# sfrac*left+(1-sfrac*right)
if len(lambdau) == 1:
nums = len(s)
left = scipy.zeros([nums, 1], dtype = scipy.integer)
right = left
sfrac = scipy.zeros([nums, 1], dtype = scipy.float64)
else:
s[s > scipy.amax(lambdau)] = scipy.amax(lambdau)
s[s < scipy.amin(lambdau)] = scipy.amin(lambdau)
k = len(lambdau)
sfrac = (lambdau[0] - s)/(lambdau[0] - lambdau[k - 1])
lambdau = (lambdau[0] - lambdau)/(lambdau[0] - lambdau[k - 1])
coord = scipy.interpolate.interp1d(lambdau, range(k))(sfrac)
left = scipy.floor(coord).astype(scipy.integer, copy = False)
right = scipy.ceil(coord).astype(scipy.integer, copy = False)
#
tf = left != right
sfrac[tf] = (sfrac[tf] - lambdau[right[tf]])/(lambdau[left[tf]] - lambdau[right[tf]])
sfrac[~tf] = 1.0
#if left != right:
# sfrac = (sfrac - lambdau[right])/(lambdau[left] - lambdau[right])
#else:
# sfrac[left == right] = 1.0
result = dict()
result['left'] = left
result['right'] = right
result['frac'] = sfrac
return(result)
# end of lambda_interp
# =========================================
def auc_mat(y, prob, weights = None):
if weights == None or len(weights) == 0:
weights = scipy.ones([y.shape[0], 1])
wweights = weights*y
wweights = wweights.flatten()
wweights = scipy.reshape(wweights, [1, wweights.size])
ny= y.shape[0]
a = scipy.zeros([ny, 1])
b = scipy.ones([ny, 1])
yy = scipy.vstack((a, b))
pprob = scipy.vstack((prob,prob))
result = auc(yy, pprob, wweights)
return(result)
#=========================
def lambda_interp(lambdau, s):
# lambda is the index sequence that is produced by the model
# s is the new vector at which evaluations are required.
# the value is a vector of left and right indices, and a vector of fractions.
# the new values are interpolated bewteen the two using the fraction
# Note: lambda decreases. you take:
# sfrac*left+(1-sfrac*right)
if len(lambdau) == 1:
nums = len(s)
left = scipy.zeros([nums, 1], dtype = scipy.integer)
right = left
sfrac = scipy.zeros([nums, 1], dtype = scipy.float64)
else:
s[s > scipy.amax(lambdau)] = scipy.amax(lambdau)
s[s < scipy.amin(lambdau)] = scipy.amin(lambdau)
k = len(lambdau)
sfrac = (lambdau[0] - s)/(lambdau[0] - lambdau[k - 1])
lambdau = (lambdau[0] - lambdau)/(lambdau[0] - lambdau[k - 1])
coord = scipy.interpolate.interp1d(lambdau, range(k))(sfrac)
left = scipy.floor(coord).astype(scipy.integer, copy = False)
right = scipy.ceil(coord).astype(scipy.integer, copy = False)
#
tf = left != right
sfrac[tf] = (sfrac[tf] - lambdau[right[tf]])/(lambdau[left[tf]] - lambdau[right[tf]])
sfrac[~tf] = 1.0
#if left != right:
# sfrac = (sfrac - lambdau[right])/(lambdau[left] - lambdau[right])
#else:
# sfrac[left == right] = 1.0
result = dict()
result['left'] = left
result['right'] = right
result['frac'] = sfrac
return(result)
# end of lambda_interp
# =========================================
def lambda_interp(lambdau, s):
# lambda is the index sequence that is produced by the model
# s is the new vector at which evaluations are required.
# the value is a vector of left and right indices, and a vector of fractions.
# the new values are interpolated bewteen the two using the fraction
# Note: lambda decreases. you take:
# sfrac*left+(1-sfrac*right)
if len(lambdau) == 1:
nums = len(s)
left = scipy.zeros([nums, 1], dtype = scipy.integer)
right = left
sfrac = scipy.zeros([nums, 1], dtype = scipy.float64)
else:
s[s > scipy.amax(lambdau)] = scipy.amax(lambdau)
s[s < scipy.amin(lambdau)] = scipy.amin(lambdau)
k = len(lambdau)
sfrac = (lambdau[0] - s)/(lambdau[0] - lambdau[k - 1])
lambdau = (lambdau[0] - lambdau)/(lambdau[0] - lambdau[k - 1])
coord = scipy.interpolate.interp1d(lambdau, range(k))(sfrac)
left = scipy.floor(coord).astype(scipy.integer, copy = False)
right = scipy.ceil(coord).astype(scipy.integer, copy = False)
#
tf = left != right
sfrac[tf] = (sfrac[tf] - lambdau[right[tf]])/(lambdau[left[tf]] - lambdau[right[tf]])
sfrac[~tf] = 1.0
#if left != right:
# sfrac = (sfrac - lambdau[right])/(lambdau[left] - lambdau[right])
#else:
# sfrac[left == right] = 1.0
result = dict()
result['left'] = left
result['right'] = right
result['frac'] = sfrac
return(result)
# end of lambda_interp
# =========================================
def auc_mat(y, prob, weights = None):
if weights == None or len(weights) == 0:
weights = scipy.ones([y.shape[0], 1])
wweights = weights*y
wweights = wweights.flatten()
wweights = scipy.reshape(wweights, [1, wweights.size])
ny= y.shape[0]
a = scipy.zeros([ny, 1])
b = scipy.ones([ny, 1])
yy = scipy.vstack((a, b))
pprob = scipy.vstack((prob,prob))
result = auc(yy, pprob, wweights)
return(result)
#=========================
imdb_success_predictor.py 文件源码
项目:Movie-Success-Predictor
作者: Blueteak
项目源码
文件源码
阅读 21
收藏 0
点赞 0
评论 0
def create_input(movie_info):
# don't want to cinlude movie_id, title, country in predicition
SKIP = 3
WIDTH = len(movie_info[0]) - SKIP
X = scipy.zeros((len(movie_info), WIDTH))
for i in range(0, len(movie_info)):
for j in range(SKIP, WIDTH):
try:
X[i, j-SKIP] = movie_info[i][j] if movie_info[i][j] != '' else 0
except Exception:
pass
return X
imdb_success_predictor.py 文件源码
项目:Movie-Success-Predictor
作者: Blueteak
项目源码
文件源码
阅读 20
收藏 0
点赞 0
评论 0
def create_output(movie_info):
Y = scipy.zeros(len(movie_info))
for i in range(0, len(movie_info)):
gross = movie_info[i][15]
if gross > 1000000:
Y[i] = 1
print 'Number of successful movies', sum(Y)
return Y
imdb_success_predictor.py 文件源码
项目:Movie-Success-Predictor
作者: Blueteak
项目源码
文件源码
阅读 43
收藏 0
点赞 0
评论 0
def create_output_before_release(movie_info):
Y = scipy.zeros(len(movie_info))
for i in range(0, len(movie_info)):
gross = movie_info[i][14]
if gross > 1000000:
Y[i] = 1
print 'Number of successful movies', sum(Y)
return Y
def subtruction(ssignal,ksignal,window,winsize,method):
nf = len(ssignal)/(winsize/2) - 1
out=sp.zeros(len(ssignal),sp.float32)
for no in xrange(nf):
s = get_frame(ssignal, winsize, no)
k = get_frame(ksignal, winsize, no)
add_signal(out, method.compute(s,k), winsize, no)
return out
def fin(size,signal):
fil = sp.zeros(size,sp.float32)
for i in xrange(size):
ratio=sp.log10((i+1)/float(size)*10+1.0)
if ratio>1.0:
ratio=1.0
fil[i] = ratio
return fil*signal
def fout(size,signal):
fil = sp.zeros(size,sp.float32)
for i in xrange(size):
ratio = sp.log10((size-i)/float(size)*10+1.0)
if ratio>1.0:
ratio = 1.0
fil[i] = ratio
return fil*signal
def vad(vas,signal,winsize,window):
out=sp.zeros(len(signal),sp.float32)
for va in vas:
for i in range(va[0],va[1]):
add_signal(out,get_frame(signal, winsize, i)*window,winsize,i)
for va in vas:
out[(va[0])*winsize/2:(va[0]+4)*winsize/2] = fin(winsize*2,out[(va[0])*winsize/2:(va[0]+4)*winsize/2])
out[(va[1]-4)*winsize/2:(va[1])*winsize/2] = fout(winsize*2,out[(va[1]-4)*winsize/2:(va[1])*winsize/2])
return out
def read(fname,winsize):
if fname =="-":
wf=wave.open(sys.stdin,'rb')
n=wf.getnframes()
str=wf.readframes(n)
params = ((wf.getnchannels(), wf.getsampwidth(),
wf.getframerate(), wf.getnframes(),
wf.getcomptype(), wf.getcompname()))
siglen=((int )(len(str)/2/winsize) + 1) * winsize
signal=sp.zeros(siglen, sp.float32)
signal[0:len(str)/2] = sp.float32(sp.fromstring(str,sp.int16))/32767.0
return signal,params
else:
return read_signal(fname,winsize)
def noise_reduction(signal,params,winsize,window,ss,ntime):
out=sp.zeros(len(signal),sp.float32)
n_pow = compute_avgpowerspectrum(signal[0:winsize*int(params[2] /float(winsize)/(1000.0/ntime))],winsize,window)#maybe 300ms
nf = len(signal)/(winsize/2) - 1
for no in xrange(nf):
s = get_frame(signal, winsize, no)
add_signal(out, ss.compute_by_noise_pow(s,n_pow), winsize, no)
return out
def read(fname,winsize):
if fname =="-":
wf=wave.open(sys.stdin,'rb')
n=wf.getnframes()
str=wf.readframes(n)
params = ((wf.getnchannels(), wf.getsampwidth(),
wf.getframerate(), wf.getnframes(),
wf.getcomptype(), wf.getcompname()))
siglen=((int )(len(str)/2/winsize) + 1) * winsize
signal=sp.zeros(siglen, sp.float32)
signal[0:len(str)/2] = sp.float32(sp.fromstring(str,sp.int16))/32767.0
return signal,params
else:
return read_signal(fname,winsize)