本文整理匯總了Python中scipy.unique方法的典型用法代碼示例。如果您正苦於以下問題:Python scipy.unique方法的具體用法?Python scipy.unique怎麽用?Python scipy.unique使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類scipy
的用法示例。
在下文中一共展示了scipy.unique方法的6個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: __MR_boundary_indictor
# 需要導入模塊: import scipy [as 別名]
# 或者: from scipy import unique [as 別名]
def __MR_boundary_indictor(self,labels):
s = sp.amax(labels)+1
up_indictor = (sp.ones((s,1))).astype(float)
right_indictor = (sp.ones((s,1))).astype(float)
low_indictor = (sp.ones((s,1))).astype(float)
left_indictor = (sp.ones((s,1))).astype(float)
upper_ids = sp.unique(labels[0,:]).astype(int)
right_ids = sp.unique(labels[:,labels.shape[1]-1]).astype(int)
low_ids = sp.unique(labels[labels.shape[0]-1,:]).astype(int)
left_ids = sp.unique(labels[:,0]).astype(int)
up_indictor[upper_ids] = 0.0
right_indictor[right_ids] = 0.0
low_indictor[low_ids] = 0.0
left_indictor[left_ids] = 0.0
return up_indictor,right_indictor,low_indictor,left_indictor
示例2: get_phenotypes
# 需要導入模塊: import scipy [as 別名]
# 或者: from scipy import unique [as 別名]
def get_phenotypes(plinkf, debug=False):
samples = plinkf.get_samples()
num_individs = len(samples)
Y = [s.phenotype for s in samples]
fids = [s.fid for s in samples]
iids = [s.iid for s in samples]
unique_phens = sp.unique(Y)
if len(unique_phens) == 1:
print('Unable to find phenotype values.')
has_phenotype = False
elif len(unique_phens) == 2:
cc_bins = sp.bincount(Y)
assert len(cc_bins) == 2, 'Problems with loading phenotype'
if debug:
print('Loaded %d controls and %d cases' % (cc_bins[0], cc_bins[1]))
has_phenotype = True
else:
if debug:
print('Found quantitative phenotype values')
has_phenotype = True
return {'has_phenotype':has_phenotype, 'fids':fids, 'iids':iids, 'phenotypes':Y, 'num_individs':num_individs}
示例3: __MR_second_stage_indictor
# 需要導入模塊: import scipy [as 別名]
# 或者: from scipy import unique [as 別名]
def __MR_second_stage_indictor(self,saliency_img_mask,labels):
s = sp.amax(labels)+1
# get ids from labels image
ids = sp.unique(labels[saliency_img_mask]).astype(int)
# indictor
indictor = sp.zeros((s,1)).astype(float)
indictor[ids] = 1.0
return indictor
示例4: __MR_get_adj_loop
# 需要導入模塊: import scipy [as 別名]
# 或者: from scipy import unique [as 別名]
def __MR_get_adj_loop(self, labels):
s = sp.amax(labels) + 1
adj = np.ones((s, s), np.bool)
for i in range(labels.shape[0] - 1):
for j in range(labels.shape[1] - 1):
if labels[i, j] != labels[i+1, j]:
adj[labels[i, j], labels[i+1, j]] = False
adj[labels[i+1, j], labels[i, j]] = False
if labels[i, j] != labels[i, j + 1]:
adj[labels[i, j], labels[i, j+1]] = False
adj[labels[i, j+1], labels[i, j]] = False
if labels[i, j] != labels[i + 1, j + 1]:
adj[labels[i, j] , labels[i+1, j+1]] = False
adj[labels[i+1, j+1], labels[i, j]] = False
if labels[i + 1, j] != labels[i, j + 1]:
adj[labels[i+1, j], labels[i, j+1]] = False
adj[labels[i, j+1], labels[i+1, j]] = False
upper_ids = sp.unique(labels[0,:]).astype(int)
right_ids = sp.unique(labels[:,labels.shape[1]-1]).astype(int)
low_ids = sp.unique(labels[labels.shape[0]-1,:]).astype(int)
left_ids = sp.unique(labels[:,0]).astype(int)
bd = np.append(upper_ids, right_ids)
bd = np.append(bd, low_ids)
bd = sp.unique(np.append(bd, left_ids))
for i in range(len(bd)):
for j in range(i + 1, len(bd)):
adj[bd[i], bd[j]] = False
adj[bd[j], bd[i]] = False
return adj
示例5: read_face_data
# 需要導入模塊: import scipy [as 別名]
# 或者: from scipy import unique [as 別名]
def read_face_data(h5fn):
f = h5py.File(h5fn, "r")
keys = ["test", "train", "val"]
Y = {}
Rid = {}
Did = {}
for key in keys:
Y[key] = f["Y_" + key][:]
for key in keys:
Rid[key] = f["Rid_" + key][:]
for key in keys:
Did[key] = f["Did_" + key][:]
f.close()
# exclude test and validation not in trian
uDid = sp.unique(Did["train"])
for key in ["test", "val"]:
Iok = sp.in1d(Did[key], uDid)
Y[key] = Y[key][Iok]
Rid[key] = Rid[key][Iok]
Did[key] = Did[key][Iok]
# one hot encode donors
table = {}
for _i, _id in enumerate(uDid):
table[_id] = _i
D = {}
for key in keys:
D[key] = sp.array([table[_id] for _id in Did[key]])[:, sp.newaxis]
# one hot encode views
uRid = sp.unique(sp.concatenate([Rid[key] for key in keys]))
table_w = {}
for _i, _id in enumerate(uRid):
table_w[_id] = _i
W = {}
for key in keys:
W[key] = sp.array([table_w[_id] for _id in Rid[key]])[:, sp.newaxis]
for key in keys:
Y[key] = Y[key].astype(float) / 255.0
Y[key] = torch.tensor(Y[key].transpose((0, 3, 1, 2)).astype(sp.float32))
D[key] = torch.tensor(D[key].astype(sp.float32))
W[key] = torch.tensor(W[key].astype(sp.float32))
return Y, D, W
示例6: calc_auc
# 需要導入模塊: import scipy [as 別名]
# 或者: from scipy import unique [as 別名]
def calc_auc(y_true, y_hat, show_plot=False):
"""
Calculate the Area Under the Curve (AUC) for a predicted and observed case-control phenotype.
"""
y_true = sp.copy(y_true)
if len(sp.unique(y_true)) == 2:
y_min = y_true.min()
y_max = y_true.max()
if y_min != 0 or y_max != 1:
print('Transforming back to a dichotomous trait')
y_true[y_true == y_min] = 0
y_true[y_true == y_max] = 1
else:
print('Warning: Calculating AUC for a quantitative phenotype.')
y_mean = sp.mean(y_true)
zero_filter = y_true <= y_mean
one_filter = y_true > y_mean
y_true[zero_filter] = 0
y_true[one_filter] = 1
num_cases = sp.sum(y_true == 1)
num_controls = sp.sum(y_true == 0)
assert num_cases + num_controls == len(y_true), 'The phenotype is not defined as expected. It is not binary (0 1 case-control status).'
print('%d cases, %d controls' % (num_cases, num_controls))
num_indivs = float(len(y_true))
tot_num_pos = float(sp.sum(y_true))
tot_num_neg = float(num_indivs - tot_num_pos)
l = y_hat.tolist()
l.sort(reverse=True)
roc_x = []
roc_y = []
auc = 0.0
prev_fpr = 0.0
for thres in l:
thres_filter = y_hat >= thres
y_t = y_true[thres_filter]
n = len(y_t)
tp = sp.sum(y_t)
fp = n - tp
fpr = fp / tot_num_neg
tpr = tp / tot_num_pos
roc_x.append(fpr)
roc_y.append(tpr)
delta_fpr = fpr - prev_fpr
auc += tpr * delta_fpr
prev_fpr = fpr
print('AUC: %0.4f' % auc)
if show_plot:
import pylab
pylab.plot(roc_x, roc_y)
pylab.show()
return auc