def __getitem__(self, item): # TODO should be right for all the common use... But better write down a TestCase
if hasattr(self, 'process_all') and self.process_all: # keep attr check!
return self.data[item]
if isinstance(item, int):
return self.get_context(item=item)
if isinstance(item, tuple):
if len(item) == 2:
rows, columns = item
if isinstance(rows, int) and isinstance(columns, int): # TODO check here
# do you want the particular element?
return self.get_context(item=rows)[columns]
else:
raise TypeError('NOT IMPLEMENTED <|>')
if isinstance(rows, slice):
rows = range(*rows.indices(self.shape[0]))
return np.vstack([self.get_context(r) for r in rows])[:, columns]
else:
if isinstance(item, slice):
item = range(*item.indices(self.shape[0]))
return np.vstack([self.get_context(r) for r in item])
python类vstack()的实例源码
def stack(*datasets):
"""
Assuming that the datasets have same structure, stucks data and targets
:param datasets:
:return: stacked dataset
"""
return Dataset(data=vstack([d.data for d in datasets]),
target=stack_or_concat([d.target for d in datasets]),
sample_info=stack_or_concat([d.sample_info for d in datasets]),
info={k: [d.info.get(k, None) for d in datasets]
for k in merge_dicts(*[d.info for d in datasets])})
def stack_or_concat(list_of_arays):
func = np.concatenate if list_of_arays[0].ndim == 1 else np.vstack
return func(list_of_arays)
def vstack(lst):
"""
Vstack that considers sparse matrices
:param lst:
:return:
"""
return sp.vstack(lst) if sp and isinstance(lst[0], sp.sparse.csr.csr_matrix) else np.vstack(lst)
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 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 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 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 get_train_data(n_pos = 46443, n_neg = 206940,k=12):
'''
megre positive and negative examples
'''
suff = str(k)
X_name = 'train_data_'+ suff + '.npz'
y_name = 'labels_'+ suff + '.npz'
if not(os.path.exists(X_name) and os.path.exists(y_name)):
X_pos = []
# X_train_face,y_train_face = Datasets.get_train_face_wider_data(k = k)
# X_pos = X_train_face[y_train_face==1]
# X_pos = X_train_face
X_aflw,y_train_face_aflw = Datasets.get_aflw_face_data(k = k)
# if len(X_pos) > 0:
# X_pos = sp.vstack( [X_pos,X_aflw] )
# else:
# X_pos = X_aflw
X_pos = X_aflw
X_train_non_face,y_train_non_face = Datasets.get_train_non_face_data(k = k)
print('c1_pos:',len(X_pos))
#print((X_train_face[y_train_face==0].shape,X_train_non_face.shape))
# if len(X_train_face[y_train_face==0]) > 0:
# X_neg = sp.vstack( (X_train_face[y_train_face==0],X_train_non_face) )
# else:
# X_neg = X_train_non_face
X_neg = X_train_non_face
X_pos = shuffle(X_pos,random_state=42)
X_neg = shuffle(X_neg,random_state=42)
X_pos = X_pos[:n_pos]
X_neg = X_neg[:n_neg]
n_neg = len(X_neg)
n_pos = len(X_pos)
y_pos = sp.ones(n_pos,int)
y_neg = sp.zeros(n_neg,int)
X = sp.vstack((X_pos,X_neg))
y = sp.hstack( (y_pos,y_neg) )
X,y = shuffle(X,y,random_state=42)
sp.savez(X_name,X)
sp.savez(y_name,y)
def qzdiv(stake, A, B, Q, Z):
'''
Takes U.T. matrices A, B, orthonormal matrices Q,Z, rearranges them
so that all cases of abs(B(i,i)/A(i,i))>stake are in lower right
corner, while preserving U.T. and orthonormal properties and Q'AZ' and Q'BZ'.
Parameters
----------
stake : number, dtype=float
A : array_like, dtype=float
An upper triangular matrix
B : array_like, dtype=float
An upper triangular matrix
Q : array_like, dtype=float
An orthonormal matrix from the QZ decomposition
Z : array_like, dtype=float
An orthonormal matrix from the QZ decomposition
Returns
-------
A : array_like, dtype=float
Rearranged A matrix
B : array_like, dtype=float
Rearranged B matrix
Q : array_like, dtype=float
Rearranged Q matrix
Z : array_like, dtype=float
Rearranged Z matrix
Notes
-----
Copyright: C.A. Sims, 1996, Yale University.
'''
n, jnk = A.shape
root = abs(vstack((np.diag(A), np.diag(B))).T)
tmp = (root[:,0]<1.e-13).astype(int)
root[:,0] = root[:,0]- tmp *(root[:,0]+root[:,1])
root[:,1] = root[:,1]/root[:,0]
for i in xrange(n,0,-1):
m=0
for j in xrange(i,0,-1):
if (root[j-1,1] > stake or root[j-1,1] < -.1):
m=j
break
if m==0:
print "qzdiv(): Inputs unchanged!"
return A, B, Q, Z
for k in xrange(m,i,1):
A, B, Q, Z = qzswitch(k,A,B,Q,Z)
tmp = root[k-1,1]
root[k-1,1] = root[k,1]
root[k,1] = tmp
return A, B, Q, Z
def qzdiv(stake, A, B, Q, Z):
'''
Takes U.T. matrices A, B, orthonormal matrices Q,Z, rearranges them
so that all cases of abs(B(i,i)/A(i,i))>stake are in lower right
corner, while preserving U.T. and orthonormal properties and Q'AZ' and Q'BZ'.
Parameters
----------
stake : number, dtype=float
A : array_like, dtype=float
An upper triangular matrix
B : array_like, dtype=float
An upper triangular matrix
Q : array_like, dtype=float
An orthonormal matrix from the QZ decomposition
Z : array_like, dtype=float
An orthonormal matrix from the QZ decomposition
Returns
-------
A : array_like, dtype=float
Rearranged A matrix
B : array_like, dtype=float
Rearranged B matrix
Q : array_like, dtype=float
Rearranged Q matrix
Z : array_like, dtype=float
Rearranged Z matrix
Notes
-----
Copyright: C.A. Sims, 1996, Yale University.
'''
n, jnk = A.shape
root = abs(vstack((np.diag(A), np.diag(B))).T)
tmp = (root[:,0]<1.e-13).astype(int)
root[:,0] = root[:,0]- tmp *(root[:,0]+root[:,1])
root[:,1] = root[:,1]/root[:,0]
for i in xrange(n,0,-1):
m=0
for j in xrange(i,0,-1):
if (root[j-1,1] > stake or root[j-1,1] < -.1):
m=j
break
if m==0:
print "qzdiv(): Inputs unchanged!"
return A, B, Q, Z
for k in xrange(m,i,1):
A, B, Q, Z = qzswitch(k,A,B,Q,Z)
tmp = root[k-1,1]
root[k-1,1] = root[k,1]
root[k,1] = tmp
return A, B, Q, Z
def qzdiv(stake, A, B, Q, Z):
'''
Takes U.T. matrices A, B, orthonormal matrices Q,Z, rearranges them
so that all cases of abs(B(i,i)/A(i,i))>stake are in lower right
corner, while preserving U.T. and orthonormal properties and Q'AZ' and Q'BZ'.
Parameters
----------
stake : number, dtype=float
A : array_like, dtype=float
An upper triangular matrix
B : array_like, dtype=float
An upper triangular matrix
Q : array_like, dtype=float
An orthonormal matrix from the QZ decomposition
Z : array_like, dtype=float
An orthonormal matrix from the QZ decomposition
Returns
-------
A : array_like, dtype=float
Rearranged A matrix
B : array_like, dtype=float
Rearranged B matrix
Q : array_like, dtype=float
Rearranged Q matrix
Z : array_like, dtype=float
Rearranged Z matrix
Notes
-----
Copyright: C.A. Sims, 1996, Yale University.
'''
n, jnk = A.shape
root = abs(vstack((np.diag(A), np.diag(B))).T)
tmp = (root[:,0]<1.e-13).astype(int)
root[:,0] = root[:,0]- tmp *(root[:,0]+root[:,1])
root[:,1] = root[:,1]/root[:,0]
for i in range(n,0,-1):
m=0
for j in range(i,0,-1):
if (root[j-1,1] > stake or root[j-1,1] < -.1):
m=j
break
if m==0:
print ("qzdiv(): Inputs unchanged!")
return A, B, Q, Z
for k in range(m,i,1):
A, B, Q, Z = qzswitch(k,A,B,Q,Z)
tmp = root[k-1,1]
root[k-1,1] = root[k,1]
root[k,1] = tmp
return A, B, Q, Z
def qzdiv(stake, A, B, Q, Z):
'''
Takes U.T. matrices A, B, orthonormal matrices Q,Z, rearranges them
so that all cases of abs(B(i,i)/A(i,i))>stake are in lower right
corner, while preserving U.T. and orthonormal properties and Q'AZ' and Q'BZ'.
Parameters
----------
stake : number, dtype=float
A : array_like, dtype=float
An upper triangular matrix
B : array_like, dtype=float
An upper triangular matrix
Q : array_like, dtype=float
An orthonormal matrix from the QZ decomposition
Z : array_like, dtype=float
An orthonormal matrix from the QZ decomposition
Returns
-------
A : array_like, dtype=float
Rearranged A matrix
B : array_like, dtype=float
Rearranged B matrix
Q : array_like, dtype=float
Rearranged Q matrix
Z : array_like, dtype=float
Rearranged Z matrix
Notes
-----
Copyright: C.A. Sims, 1996, Yale University.
'''
n, jnk = A.shape
root = abs(vstack((np.diag(A), np.diag(B))).T)
tmp = (root[:,0]<1.e-13).astype(int)
root[:,0] = root[:,0]- tmp *(root[:,0]+root[:,1])
root[:,1] = root[:,1]/root[:,0]
for i in range(n,0,-1):
m=0
for j in range(i,0,-1):
if (root[j-1,1] > stake or root[j-1,1] < -.1):
m=j
break
if m==0:
print ("qzdiv(): Inputs unchanged!")
return A, B, Q, Z
for k in range(m,i,1):
A, B, Q, Z = qzswitch(k,A,B,Q,Z)
tmp = root[k-1,1]
root[k-1,1] = root[k,1]
root[k,1] = tmp
return A, B, Q, Z