本文整理匯總了Python中numpy.nanmean方法的典型用法代碼示例。如果您正苦於以下問題:Python numpy.nanmean方法的具體用法?Python numpy.nanmean怎麽用?Python numpy.nanmean使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類numpy
的用法示例。
在下文中一共展示了numpy.nanmean方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: test_select_confounds
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import nanmean [as 別名]
def test_select_confounds(confounds_file, selected_confounds, nan_confounds,
expanded_confounds):
import pandas as pd
import numpy as np
confounds_df = pd.read_csv(str(confounds_file), sep='\t', na_values='n/a')
res_df = _select_confounds(str(confounds_file), selected_confounds)
# check if the correct columns are selected
assert set(expanded_confounds) == set(res_df.columns)
# check if nans are being imputed when expected
if nan_confounds:
for nan_c in nan_confounds:
vals = confounds_df[nan_c].values
expected_result = np.nanmean(vals[vals != 0])
assert res_df[nan_c][0] == expected_result
示例2: eval_detection_voc
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import nanmean [as 別名]
def eval_detection_voc(pred_boxlists, gt_boxlists, iou_thresh=0.5, use_07_metric=False):
"""Evaluate on voc dataset.
Args:
pred_boxlists(list[BoxList]): pred boxlist, has labels and scores fields.
gt_boxlists(list[BoxList]): ground truth boxlist, has labels field.
iou_thresh: iou thresh
use_07_metric: boolean
Returns:
dict represents the results
"""
assert len(gt_boxlists) == len(
pred_boxlists
), "Length of gt and pred lists need to be same."
prec, rec, n_tp, n_fp, n_pos = calc_detection_voc_prec_rec(
pred_boxlists=pred_boxlists, gt_boxlists=gt_boxlists, iou_thresh=iou_thresh
)
ap = calc_detection_voc_ap(prec, rec, use_07_metric=use_07_metric)
prec = {k: v.tolist() for k, v in prec.items()}
rec = {k: v.tolist() for k, v in rec.items()}
res = [{"ap": ap[k], "class_id": k, "precisions": prec[k], "recalls": rec[k],
"n_tp": n_tp[k], "n_fp": n_fp[k], "n_positives": n_pos[k]} for k in ap.keys()]
return res, np.nanmean(ap.values())
示例3: _signal_synchrony_correlation
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import nanmean [as 別名]
def _signal_synchrony_correlation(signal1, signal2, window_size, center=False):
"""Calculates pairwise rolling correlation at each time. Grabs the upper triangle, at each timepoints.
- window: window size of rolling corr in samples
- center: whether to center result (Default: False, so correlation values are listed on the right.)
"""
data = pd.DataFrame({"y1": signal1, "y2": signal2})
rolled = data.rolling(window=window_size, center=center).corr()
synchrony = rolled["y1"].loc[rolled.index.get_level_values(1) == "y2"].values
# Realign
synchrony = np.append(synchrony[int(window_size / 2) :], np.full(int(window_size / 2), np.nan))
synchrony[np.isnan(synchrony)] = np.nanmean(synchrony)
return synchrony
示例4: compute_average_mAP_N_for_characteristic
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import nanmean [as 別名]
def compute_average_mAP_N_for_characteristic(sensitivity_analysis, characteristic_name):
gt_by_characteristic = sensitivity_analysis.ground_truth.groupby(characteristic_name)
average_mAP_n_by_characteristic_value = OrderedDict()
for characteristic_value, this_characteristic_gt in gt_by_characteristic:
ap = np.nan*np.zeros(len(sensitivity_analysis.activity_index))
gt_by_cls = this_characteristic_gt.groupby('label')
pred_by_cls = sensitivity_analysis.prediction.groupby('label')
for cls in sensitivity_analysis.activity_index.values():
this_cls_pred = pred_by_cls.get_group(cls).sort_values(by='score',ascending=False)
try:
this_cls_gt = gt_by_cls.get_group(cls)
except:
continue
gt_id_to_keep = np.append(this_cls_gt['gt-id'].values, [np.nan])
for tidx, tiou in enumerate(sensitivity_analysis.tiou_thresholds):
this_cls_pred = this_cls_pred[this_cls_pred[sensitivity_analysis.matched_gt_id_cols[tidx]].isin(gt_id_to_keep)]
ap[cls] = compute_mAP_N(sensitivity_analysis,this_cls_pred,this_cls_gt)
average_mAP_n_by_characteristic_value[characteristic_value] = np.nanmean(ap)
return average_mAP_n_by_characteristic_value
示例5: eval_detection_voc
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import nanmean [as 別名]
def eval_detection_voc(pred_boxlists, gt_boxlists, iou_thresh=0.5, use_07_metric=False):
"""Evaluate on voc dataset.
Args:
pred_boxlists(list[BoxList]): pred boxlist, has labels and scores fields.
gt_boxlists(list[BoxList]): ground truth boxlist, has labels field.
iou_thresh: iou thresh
use_07_metric: boolean
Returns:
dict represents the results
"""
assert len(gt_boxlists) == len(
pred_boxlists
), "Length of gt and pred lists need to be same."
prec, rec = calc_detection_voc_prec_rec(
pred_boxlists=pred_boxlists, gt_boxlists=gt_boxlists, iou_thresh=iou_thresh
)
ap = calc_detection_voc_ap(prec, rec, use_07_metric=use_07_metric)
return {"ap": ap, "map": np.nanmean(ap)}
示例6: segmentation_scores
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import nanmean [as 別名]
def segmentation_scores(label_trues, label_preds, n_class):
"""Returns accuracy score evaluation result.
- overall accuracy
- mean accuracy
- mean IU
- fwavacc
"""
hist = np.zeros((n_class, n_class))
for lt, lp in zip(label_trues, label_preds):
hist += _fast_hist(lt.flatten(), lp.flatten(), n_class)
acc = np.diag(hist).sum() / hist.sum()
acc_cls = np.diag(hist) / hist.sum(axis=1)
acc_cls = np.nanmean(acc_cls)
iu = np.diag(hist) / (hist.sum(axis=1) + hist.sum(axis=0) - np.diag(hist))
mean_iu = np.nanmean(iu)
freq = hist.sum(axis=1) / hist.sum()
fwavacc = (freq[freq > 0] * iu[freq > 0]).sum()
return {'overall_acc': acc,
'mean_acc': acc_cls,
'freq_w_acc': fwavacc,
'mean_iou': mean_iu}
示例7: get_scores
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import nanmean [as 別名]
def get_scores(self):
"""Returns accuracy score evaluation result.
- overall accuracy
- mean accuracy
- mean IU
- fwavacc
"""
hist = self.confusion_matrix
acc = np.diag(hist).sum() / hist.sum()
acc_cls = np.diag(hist) / hist.sum(axis=1)
acc_cls = np.nanmean(acc_cls)
iu = np.diag(hist) / (hist.sum(axis=1) + hist.sum(axis=0) - np.diag(hist))
mean_iu = np.nanmean(iu)
freq = hist.sum(axis=1) / hist.sum()
fwavacc = (freq[freq > 0] * iu[freq > 0]).sum()
cls_iu = dict(zip(range(self.n_classes), iu))
return {'Overall Acc: \t': acc,
'Mean Acc : \t': acc_cls,
'FreqW Acc : \t': fwavacc,
'Mean IoU : \t': mean_iu,}, cls_iu
示例8: _recmat_smooth
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import nanmean [as 別名]
def _recmat_smooth(presented, recalled, features, distance, match):
if match == 'best':
func = np.argmax
elif match == 'smooth':
func = np.nanmean
simmtx = _similarity_smooth(presented, recalled, features, distance)
if match == 'best':
recmat = np.atleast_3d([func(s, 1) for s in simmtx]).astype(np.float64)
recmat+=1
recmat[np.isnan(simmtx).any(2)]=np.nan
elif match == 'smooth':
recmat = np.atleast_3d([func(s, 0) for s in simmtx]).astype(np.float64)
return recmat
示例9: _similarity_smooth
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import nanmean [as 別名]
def _similarity_smooth(presented, recalled, features, distance):
lists = presented.index.get_values()
res = np.empty((len(lists), len(features), recalled.iloc[0].shape[0], presented.iloc[0].shape[0]))*np.nan
for li, l in enumerate(lists):
p_list = presented.loc[l]
r_list = recalled.loc[l]
for i, feature in enumerate(features):
get_feature = lambda x: np.array(x[feature]) if np.array(pd.notna(x['item'])).any() else np.nan
p = np.vstack(p_list.apply(get_feature).get_values())
r = r_list.dropna().apply(get_feature).get_values()
r = np.vstack(list(filter(lambda x: x is not np.nan, r)))
tmp = 1 - cdist(r, p, distance)
res[li, i, :tmp.shape[0], :] = tmp
if distance == 'correlation':
return np.nanmean(res, 1)
else:
return np.mean(res, 1)
示例10: _get_weight_best
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import nanmean [as 別名]
def _get_weight_best(egg, feature, distdict, permute, n_perms, distance):
if permute:
return _permute(egg, feature, distdict, _get_weight_best, n_perms)
rec = list(egg.get_rec_items().values[0])
if len(rec) <= 2:
warnings.warn('Not enough recalls to compute fingerprint, returning default'
'fingerprint.. (everything is .5)')
return np.nan
distmat = get_distmat(egg, feature, distdict)
matchmat = get_match(egg, feature, distdict)
ranks = []
for i in range(len(rec)-1):
cdx, ndx = np.argmin(matchmat[i, :]), np.argmin(matchmat[i+1, :])
dists = distmat[cdx, :]
di = dists[ndx]
dists_filt = np.array([dist for idx, dist in enumerate(dists)])
ranks.append(np.mean(np.where(np.sort(dists_filt)[::-1] == di)[0]+1) / len(dists_filt))
return np.nanmean(ranks)
示例11: _get_weight_smooth
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import nanmean [as 別名]
def _get_weight_smooth(egg, feature, distdict, permute, n_perms, distance):
if permute:
return _permute(egg, feature, distdict, _get_weight_smooth, n_perms)
rec = list(egg.get_rec_items().values[0])
if len(rec) <= 2:
warnings.warn('Not enough recalls to compute fingerprint, returning default'
'fingerprint.. (everything is .5)')
return np.nan
distmat = get_distmat(egg, feature, distdict)
matchmat = get_match(egg, feature, distdict)
ranks = []
for i in range(len(rec)-1):
cdx, ndx = np.argmin(matchmat[i, :]), np.argmin(matchmat[i+1, :])
dists = distmat[cdx, :]
di = dists[ndx]
dists_filt = np.array([dist for idx, dist in enumerate(dists)])
ranks.append(np.mean(np.where(np.sort(dists_filt)[::-1] == di)[0]+1) / len(dists_filt))
return np.nanmean(ranks)
示例12: _nanquantile_unchecked
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import nanmean [as 別名]
def _nanquantile_unchecked(a, q, axis=None, out=None, overwrite_input=False,
interpolation='linear', keepdims=np._NoValue):
"""Assumes that q is in [0, 1], and is an ndarray"""
# apply_along_axis in _nanpercentile doesn't handle empty arrays well,
# so deal them upfront
if a.size == 0:
return np.nanmean(a, axis, out=out, keepdims=keepdims)
r, k = function_base._ureduce(
a, func=_nanquantile_ureduce_func, q=q, axis=axis, out=out,
overwrite_input=overwrite_input, interpolation=interpolation
)
if keepdims and keepdims is not np._NoValue:
return r.reshape(q.shape + k)
else:
return r
示例13: cal_scores
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import nanmean [as 別名]
def cal_scores(hist):
n_class = settings.N_CLASSES
acc = np.diag(hist).sum() / hist.sum()
acc_cls = np.diag(hist) / hist.sum(axis=1)
acc_cls = np.nanmean(acc_cls)
iu = np.diag(hist) / (hist.sum(axis=1) + hist.sum(axis=0) - np.diag(hist))
mean_iu = np.nanmean(iu)
freq = hist.sum(axis=1) / hist.sum()
fwavacc = (freq[freq > 0] * iu[freq > 0]).sum()
cls_iu = dict(zip(label_names, iu))
return {
'pAcc': acc,
'mAcc': acc_cls,
'fIoU': fwavacc,
'mIoU': mean_iu,
}, cls_iu
示例14: cal_scores
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import nanmean [as 別名]
def cal_scores(hist):
n_class = settings.N_CLASSES
#acc = np.diag(hist).sum() / hist.sum()
#acc_cls = np.diag(hist) / hist.sum(axis=1)
#acc_cls = np.nanmean(acc_cls)
iu = np.diag(hist) / (hist.sum(axis=1) + hist.sum(axis=0) - np.diag(hist))
mean_iu = np.nanmean(iu)
freq = hist.sum(axis=1) / hist.sum()
fwavacc = (freq[freq > 0] * iu[freq > 0]).sum()
cls_iu = dict(zip(label_names, iu))
return {
#"pAcc": acc,
#"mAcc": acc_cls,
"fIoU": fwavacc,
"mIoU": mean_iu,
}#, cls_iu
示例15: mape
# 需要導入模塊: import numpy [as 別名]
# 或者: from numpy import nanmean [as 別名]
def mape(y_pred, y_test):
r"""
The Mean Absolute Percentage Error (MAPE)
The MAPE is computed as the mean of the absolute value of the relative
error in percent, i.e.:
.. math::
\text{MAPE}(\mathbf{y}, \mathbf{y}_{true}) =
\frac{100\%}{n}\sum_{i = 0}^n \frac{|y_{\text{pred},i} - y_{\text{true},i}|}
{|y_{\text{true},i}|}
Arguments:
y_pred(numpy.array): The predicted scalar values.
y_test(numpy.array): The true values.
Returns:
The MAPE for the given predictions.
"""
return np.nanmean(100.0 * np.abs(y_test - y_pred.ravel()) / np.abs(y_test).ravel())