本文整理汇总了Python中scipy.interp方法的典型用法代码示例。如果您正苦于以下问题:Python scipy.interp方法的具体用法?Python scipy.interp怎么用?Python scipy.interp使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类scipy
的用法示例。
在下文中一共展示了scipy.interp方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _plot_macro_roc
# 需要导入模块: import scipy [as 别名]
# 或者: from scipy import interp [as 别名]
def _plot_macro_roc(fpr, tpr, n, lw, fmt, ax):
all_fpr = np.unique(np.concatenate([fpr[i] for i in range(n)]))
mean_tpr = np.zeros_like(all_fpr)
for i in range(n):
mean_tpr += interp(all_fpr, fpr[i], tpr[i])
mean_tpr /= n
fpr_macro = all_fpr
tpr_macro = mean_tpr
auc_macro = auc(fpr_macro, tpr_macro)
label = 'ROC curve: macro (AUC = {auc:{fmt}})'.format(auc=auc_macro, fmt=fmt)
ax.plot(fpr_macro,
tpr_macro,
label=label,
color='navy',
ls=':',
lw=lw)
示例2: plot_ROC
# 需要导入模块: import scipy [as 别名]
# 或者: from scipy import interp [as 别名]
def plot_ROC(y_true, y_preds, num_classes, labels, micro=True, macro=True):
# Compute ROC curve and ROC area for each class
fpr = dict()
tpr = dict()
roc_auc = dict()
for i in range(num_classes):
fpr[i], tpr[i], _ = metrics.roc_curve(y_true[:, i], y_preds[:, i])
roc_auc[i] = metrics.auc(fpr[i], tpr[i])
if micro:
# Compute micro-average ROC curve and ROC area
fpr["micro"], tpr["micro"], _ = metrics.roc_curve(y_true.ravel(), y_preds.ravel())
roc_auc["micro"] = metrics.auc(fpr["micro"], tpr["micro"])
if macro:
# Compute macro-average ROC curve and ROC area
# First aggregate all false positive rates
all_fpr = np.unique(np.concatenate([fpr[i] for i in range(num_classes)]))
# Then interpolate all ROC curves at this points
mean_tpr = np.zeros_like(all_fpr)
for i in range(num_classes):
mean_tpr += interp(all_fpr, fpr[i], tpr[i])
# Finally average it and compute AUC
mean_tpr /= num_classes
fpr["macro"] = all_fpr
tpr["macro"] = mean_tpr
roc_auc["macro"] = metrics.auc(fpr["macro"], tpr["macro"])
for i in range(num_classes):
plot_class_ROC(fpr, tpr, roc_auc, i, labels)
plot_multi_ROC(fpr, tpr, roc_auc, num_classes, labels, micro, macro)
示例3: _plot_single_performance_curve
# 需要导入模块: import scipy [as 别名]
# 或者: from scipy import interp [as 别名]
def _plot_single_performance_curve(xs, ys, areas, areas_type, color="C0", curve_name="",
label_std=False, label_folds=False,
plot_folds=False, colored_folds=False, ax=None):
ax = ax or plt.gca()
assert len(xs) == len(ys) == len(areas)
n_folds = len(xs)
x_domain = np.linspace(0, 1, 100)
ys_interp = []
for i in range(n_folds):
if areas_type == "AP": # precision/recall need to be reversed for interpolation
ys_interp.append(scipy_interp(x_domain, xs[i][::-1], ys[i][::-1]))
else:
ys_interp.append(scipy_interp(x_domain, xs[i], ys[i]))
ys_interp[-1][0] = 0.0
area = areas[i]
folds_label = 'Fold {} ({} = {:.2f})'.format(i, areas_type, area) if label_folds else None
if plot_folds:
folds_color = None if colored_folds else color # use multiple colors if plotting only one stratum
ax.plot(xs[i], ys[i], lw=1, alpha=0.3, color=folds_color,
label=folds_label)
# Plot main (folds average) curve
mean_ys = np.nanmean(ys_interp, axis=0)
# if areas_type == "AUC":
# mean_ys[-1] = 1.0
mean_area = np.nanmean(areas)
std_area = np.nanstd(areas)
ax.plot(x_domain, mean_ys, color=color,
label=r'{} ({} = {:.2f} $\pm$ {:.2f})'.format(curve_name, areas_type, mean_area, std_area),
lw=2, alpha=.9)
# Plot uncertainty around main curve:
ys_std = np.std(ys_interp, axis=0)
upper_ys = np.minimum(mean_ys + ys_std, 1)
lower_ys = np.maximum(mean_ys - ys_std, 0)
std_label = r'$\pm$ 1 std. dev.' if label_std else None
ax.fill_between(x_domain, lower_ys, upper_ys, color=color, alpha=.2, label=std_label)
return ax
示例4: calc_macro_roc
# 需要导入模块: import scipy [as 别名]
# 或者: from scipy import interp [as 别名]
def calc_macro_roc(fpr, tpr):
"""Calcs macro ROC on log scale"""
# Create log scale domain
all_fpr = sorted(itertools.chain(*fpr))
# Then interpolate all ROC curves at this points
mean_tpr = np.zeros_like(all_fpr)
for i in range(len(tpr)):
mean_tpr += interp(all_fpr, fpr[i], tpr[i])
return all_fpr, mean_tpr / len(tpr), auc(all_fpr, mean_tpr) / len(tpr)
示例5: plot_allkfolds_ROC
# 需要导入模块: import scipy [as 别名]
# 或者: from scipy import interp [as 别名]
def plot_allkfolds_ROC(timestamp, cv, fpr_arr, tpr_arr):
sns.set(style="white", palette="muted", color_codes=True)
mean_tpr = 0.0
mean_fpr = 0.0
all_roc_auc = []
bins_roc = np.linspace(0, 1, 300)
with plt.style.context(('seaborn-muted')):
fig, ax = plt.subplots(figsize=(10, 8))
for i, (train, test) in enumerate(cv):
mean_tpr += interp(bins_roc, fpr_arr[i], tpr_arr[i])
mean_tpr[0] = 0.0
mean_fpr += interp(bins_roc, fpr_arr[i], tpr_arr[i])
mean_fpr[0] = 0.0
roc_auc = metrics.auc(fpr_arr[i], tpr_arr[i])
all_roc_auc.append(roc_auc)
ax.plot(fpr_arr[i], tpr_arr[i], lw=1, label='KFold %d (AUC = %0.2f)' % (i, roc_auc))
ax.plot([0, 1], [0, 1], '--', color=(0.6, 0.6, 0.6), label='Random')
mean_tpr /= len(cv)
mean_tpr[-1] = 1.0
mean_auc = np.mean(all_roc_auc)
ax.plot(bins_roc, mean_tpr, 'k--',
label='Mean ROC (AUC = %0.2f)' % mean_auc, lw=2)
ax.set_xlim([-0.05, 1.05])
ax.set_ylim([-0.05, 1.05])
ax.set_xlabel('False Positive Rate')
ax.set_ylabel('True Positive Rate')
ax.set_title('Receiver Operating Characteristic')
ax.legend(loc="lower right")
plt.savefig('{}_roc.png'.format(timestamp))
plt.close('all')
return mean_auc
示例6: interp_recall
# 需要导入模块: import scipy [as 别名]
# 或者: from scipy import interp [as 别名]
def interp_recall(ground_truth, scores, precision_sample):
precision, recall, thresholds = precision_recall_curve(ground_truth,
scores)
# precision_recall_curve do not return vectors of the same length.
# len(thresholds) = n, with len(precision) = len(recall) = n+1
thresholds = np.append(thresholds, 1)
# Add corner cases
thresholds = np.append(0, thresholds)
precision = np.append(sum(ground_truth) / len(ground_truth), precision)
recall = np.append(1, recall)
# Interpolation
recall = interp(precision_sample, precision, recall)
thresholds = interp(precision_sample, precision, thresholds)
return recall, thresholds
示例7: add_fold
# 需要导入模块: import scipy [as 别名]
# 或者: from scipy import interp [as 别名]
def add_fold(self, fold_id, predictions):
if (predictions.num_instances() == 0 or
sum(predictions.ground_truth) == 0):
return
if self.probabilist:
scores = predictions.probas
else:
scores = predictions.scores
fpr, tpr, thresholds = roc_curve(predictions.ground_truth, scores)
# Add corner cases
thresholds = np.append(1, thresholds)
fpr = np.append(0, fpr)
tpr = np.append(0, tpr)
thresholds = np.append(thresholds, 0)
fpr = np.append(fpr, 1)
tpr = np.append(tpr, 1)
if self.mean_tpr is None:
self.mean_tpr = interp(self.mean_fpr, fpr, tpr)
else:
self.mean_tpr += interp(self.mean_fpr, fpr, tpr)
self.thresholds = interp(self.mean_fpr, fpr, thresholds)
roc_auc = auc(fpr, tpr)
if self.num_folds > 1:
self.ax1.plot(fpr, tpr, lw=1,
label='ROC fold %d (area = %0.2f)' % (fold_id,
roc_auc))
else:
self.ax1.plot(fpr, tpr, lw=3, color=get_label_color('all'),
label='ROC (area = %0.2f)' % (roc_auc))
return fpr, tpr, roc_auc
示例8: plot_avg_roc
# 需要导入模块: import scipy [as 别名]
# 或者: from scipy import interp [as 别名]
def plot_avg_roc(path, f_row, t_row, tag = ''):
tprs = []
aucs = []
mean_fpr = np.linspace(0, 1, 100)
for i in range(10):
file = path + '_' +str(i) + '_roc_record.txt'
fpr = appoint_line(f_row, file)
tpr = appoint_line(t_row, file)
fpr = map(eval, fpr[5:-2].split(", "))
tpr = map(eval, tpr[5:-2].split(", "))
# print fpr
# print tpr
# raw_input()
# Compute ROC curve and area the curve
tprs.append(interp(mean_fpr, fpr, tpr))
tprs[-1][0] = 0.0
roc_auc = auc(fpr, tpr)
aucs.append(roc_auc)
# plt.plot(fpr, tpr, lw=1, alpha=0.3,
# label='ROC fold %d (AUC = %0.2f)' % (i, roc_auc))
plt.plot(fpr, tpr, lw=1, alpha=0.3)
plt.plot([0, 1], [0, 1], linestyle='--', lw=1, color='r',
label='Luck', alpha=.6)
mean_tpr = np.mean(tprs, axis=0)
mean_tpr[-1] = 1.0
mean_auc = auc(mean_fpr, mean_tpr)
std_auc = np.std(aucs)
plt.plot(mean_fpr, mean_tpr, color='b',
label=r'Mean ROC (AUC = %0.2f $\pm$ %0.2f)' % (mean_auc, std_auc),
lw=1.5, alpha=.8)
std_tpr = np.std(tprs, axis=0)
tprs_upper = np.minimum(mean_tpr + std_tpr, 1)
tprs_lower = np.maximum(mean_tpr - std_tpr, 0)
plt.fill_between(mean_fpr, tprs_lower, tprs_upper, color='grey', alpha=.2,
label=r'$\pm$ 1 std. dev.')
plt.xlim([-0.05, 1.05])
plt.ylim([-0.05, 1.05])
plt.xlabel('False Positive Rate')
plt.ylabel('True Positive Rate')
plt.title('Receiver operating characteristic example')
plt.legend(loc="lower right")
plt.savefig(path[:16]+tag + '_avg_roc.png')
plt.close('all') # 关闭图
print 'Avg ROC curve for %s saved at %s !'%(tag, path[:16])
示例9: _create_roc_plot
# 需要导入模块: import scipy [as 别名]
# 或者: from scipy import interp [as 别名]
def _create_roc_plot(roc_auc_dict, fpr_dict, tpr_dict, num_class, path):
"""Create and save a combined ROC plot to file.
Arguments:
roc_auc_dict: {int: float}
dictionary mapping classes to ROC AUC scores
fpr_dict: {string: np.ndarray}
dictionary mapping names of classes or an averaging method to
arrays of increasing false positive rates
tpr_dict: {string: float}
dictionary mapping names of classes or an averaging method to
arrays of increasing true positive rates
num_class: int
number of classes
path: string
filepath where to save the plot
"""
# aggregate all false positive rates
all_fpr = np.unique(np.concatenate(
[fpr_dict[i] for i in range(num_class)]))
# interpolate all ROC curves at this points
mean_tpr = np.zeros_like(all_fpr)
for i in range(num_class):
mean_tpr += interp(all_fpr, fpr_dict[i], tpr_dict[i])
# average and compute AUC
mean_tpr /= num_class
fpr_dict["macro"] = all_fpr
tpr_dict["macro"] = mean_tpr
roc_auc_dict["macro"] = auc(fpr_dict["macro"], tpr_dict["macro"])
# plot
plt.figure()
plt.plot(fpr_dict["micro"], tpr_dict["micro"],
label='micro-average ROC curve (area = {0:0.2f})'.format(
roc_auc_dict["micro"]),
linewidth=2)
plt.plot(fpr_dict["macro"], tpr_dict["macro"],
label='macro-average ROC curve (area = {0:0.2f})'.format(
roc_auc_dict["macro"]),
linewidth=2)
for i in range(num_class):
plt.plot(fpr_dict[i], tpr_dict[i],
label='ROC curve of class {0} (area = {1:0.2f})'.format(
i, roc_auc_dict[i]))
plt.plot([0, 1], [0, 1], 'k--')
plt.xlim([0.0, 1.0])
plt.ylim([0.0, 1.05])
plt.xlabel('False Positive Rate')
plt.ylabel('True Positive Rate')
plt.title('Some extension of Receiver operating characteristic to multi-class')
plt.legend(loc="lower right")
plt.savefig(path) # save plot
plt.close()
示例10: plot_roc_curve
# 需要导入模块: import scipy [as 别名]
# 或者: from scipy import interp [as 别名]
def plot_roc_curve(y_pred, y_true, n_classes, title='ROC_Curve'):
# Compute ROC curve and ROC area for each class
fpr = dict()
tpr = dict()
tresholds = dict()
roc_auc = dict()
for i in range(n_classes):
fpr[i], tpr[i], tresholds[i] = roc_curve(y_true, y_pred, pos_label=i, drop_intermediate=False)
roc_auc[i] = auc(fpr[i], tpr[i])
# Compute micro-average ROC curve and ROC area
# fpr["micro"], tpr["micro"], _ = roc_curve(np.asarray(y_true).ravel(), np.asarray(y_pred).ravel(), pos_label=0, drop_intermediate=True)
# roc_auc["micro"] = auc(fpr["micro"], tpr["micro"])
# Aggregate all false positive rates
all_fpr = np.unique(np.concatenate([fpr[i] for i in range(n_classes)]))
# print("Thresholds:")
# Interpolate all ROC curves at this points
mean_tpr = np.zeros_like(all_fpr)
for i in range(n_classes):
mean_tpr += interp(all_fpr, fpr[i], tpr[i])
# print("Class_{0}: {1}".format(i, tresholds[i]))
# Average it and compute AUC
mean_tpr /= n_classes
fpr["macro"] = all_fpr
tpr["macro"] = mean_tpr
roc_auc["macro"] = auc(fpr["macro"], tpr["macro"])
# Plot all ROC curves
fig = plt.figure()
ax = fig.add_subplot(111)
# plt.plot(fpr["micro"], tpr["micro"],
# label='micro-average ROC curve (area = {0:0.2f})'
# ''.format(roc_auc["micro"]),
# linewidth=3, ls='--', color='red')
plt.plot(fpr["macro"], tpr["macro"],
label='macro-average ROC curve (area = {0:0.2f})'
''.format(roc_auc["macro"]),
linewidth=3, ls='--', color='green')
for i in range(n_classes):
plt.plot(fpr[i], tpr[i], label='ROC curve of class {0} (area = {1:0.2f})'
''.format(i, roc_auc[i]))
plt.plot([0, 1], [0, 1], 'k--', linewidth=2)
plt.xlim([0.0, 1.0])
plt.ylim([0.0, 1.05])
plt.xlabel('False Positive Rate')
plt.ylabel('True Positive Rate')
plt.title('Multi-class Receiver Operating Characteristic')
lgd = ax.legend(bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.)
plt.savefig(pjoin(FLAGS.output_dir, title.replace(' ', '_') + '_ROC.png'), bbox_extra_artists=(lgd,), bbox_inches='tight')
plt.close()
示例11: plot_ROC
# 需要导入模块: import scipy [as 别名]
# 或者: from scipy import interp [as 别名]
def plot_ROC(X, y, classifier, cv):
from sklearn.metrics import roc_curve, auc
from sklearn.model_selection import StratifiedKFold
from scipy import interp
cv = StratifiedKFold(n_splits=cv)
tprs = []
aucs = []
mean_fpr = np.linspace(0, 1, 100)
i = 0
for train, test in cv.split(X, y):
probas_ = classifier.fit(X[train], y[train]).predict_proba(X[test])
# Compute ROC curve and area the curve
fpr, tpr, thresholds = roc_curve(y[test], probas_[:, 1])
tprs.append(interp(mean_fpr, fpr, tpr))
tprs[-1][0] = 0.0
roc_auc = auc(fpr, tpr)
aucs.append(roc_auc)
plt.plot(fpr, tpr, lw=1, alpha=0.3,
label='ROC fold %d (AUC = %0.2f)' % (i, roc_auc))
i += 1
#figure = plt.figure()
plt.gcf().clear()
plt.plot([0, 1], [0, 1], linestyle='--', lw=2, color='r',
label='Luck', alpha=.8)
mean_tpr = np.mean(tprs, axis=0)
mean_tpr[-1] = 1.0
mean_auc = auc(mean_fpr, mean_tpr)
std_auc = np.std(aucs)
plt.plot(mean_fpr, mean_tpr, color='b',
label=r'Mean ROC (AUC = %0.2f $\pm$ %0.2f)' % (mean_auc, std_auc),
lw=2, alpha=.8)
std_tpr = np.std(tprs, axis=0)
tprs_upper = np.minimum(mean_tpr + std_tpr, 1)
tprs_lower = np.maximum(mean_tpr - std_tpr, 0)
plt.fill_between(mean_fpr, tprs_lower, tprs_upper, color='grey', alpha=.2,
label=r'$\pm$ 1 std. dev.')
plt.xlim([0.0, 1.0])
plt.ylim([0.0, 1.0])
plt.xlabel('False Positive Rate')
plt.ylabel('True Positive Rate')
plt.title('ROC')
plt.legend(loc="lower right")
from io import BytesIO
figfile = BytesIO()
plt.savefig(figfile, format='png')
figfile.seek(0) # rewind to beginning of file
import base64
figdata_png = base64.b64encode(figfile.getvalue())
return figdata_png