Skip to content

Instantly share code, notes, and snippets.

View sadimanna's full-sized avatar
🎯
Focusing

Siladittya Manna sadimanna

🎯
Focusing
View GitHub Profile
def prcurve(y,pred):
P = []
R = []
thresholds = np.arange(0.0,1.0+0.01,0.01)
for th in thresholds:
P.append(precision(y,pred,th))
R.append(recall(y,pred,th))
plt.figure(figsize=(10,10))
plt.plot(R,P,linewidth=2)
plt.xlim(0.0, 1.0)
def bootstrap_metric(y, pred, classes, metric='auc',bootstraps = 100, fold_size = 1000):
statistics = np.zeros((len(classes), bootstraps))
if metric=='AUC':
metric_func = roc_auc_score
if metric=='Sensitivity':
metric_func = sensitivity
if metric=='Specificity':
metric_func = specificity
if metric=='Accuracy':
metric_func = get_accuracy
# class_labels should contain names of both the classes
def plot_confusion_matrix(y_true,y_pred,class_labels):
cm = np.zeros((2,2))
cm[0,0] = np.sum((y_true==1) & (y_pred>0.5)) #TP
cm[0,1] = np.sum((y_true==0) & (y_pred>0.5)) #FP
cm[1,0] = np.sum((y_true==1) & (y_pred<0.5)) #FN
cm[1,1] = np.sum((y_true==0) & (y_pred<0.5)) #TN
cm_sum = np.sum(cm, axis=1, keepdims=True)
cm_perc = cm / cm_sum.astype(float) * 100
annot = np.empty_like(cm).astype(str)
def plot_perf_metrics_errbars(y,pred,class_labels):
metric_dfs = get_confidence_intervals(y,pred,class_labels)
metrics = list(metric_dfs.keys())
fig,axs = plt.subplots(len(metrics),1,sharey=True)
for i in range(len(metrics)):
ci = metric_dfs[metrics[i]][['Mean '+metrics[i]+' (CI 5%-95%)']].values[0]
ci_mean,ci_ints = ci[0].split(' ')
ci_mean = float(ci_mean)
ci_min,ci_max = list(map(float,ci_ints.strip('()').split('-')))
ci_err = (ci_max-ci_min)/2
def get_performance_metrics(y, pred, class_labels, tp=TP, tn=TN, fp=FP,fn=FN,
acc=accuracy, spec=specificity, sens=sensitivity,
ppv=ppv, npv=npv, auc=auc_score, f1=fscore,
prevalence=prevalence, thresholds=[]):
if len(thresholds) != len(class_labels):
thresholds = [.5] * len(class_labels)
func_dict = locals()
func_keys = list(func_dict.keys())
columns = [""]
y = np.array([]).reshape((0,NUM_CLASSES))
pred = np.array([]).reshape((0,NUM_CLASSES))
for i in range(NUM_ITERS):
X,y_true = DATAGEN.__getitem__(i)
if y_true.shape[1]!=NUM_CLASSES:
y = np.append(y,to_categorical(y_true,num_classes=NUM_CLASSES),axis=0)
y_pred = model(X)
pred = np.append(pred,y_pred,axis=0)
def get_roc_curve(gt, pred, target_names):
for i in range(len(target_names)):
curve_function = plot_roc_curve
auc_roc = auc_score(gt[:, i], pred[:, i])
label = str(target_names[i]) + " AUC: %.3f " % auc_roc
xlabel = "False positive rate"
ylabel = "True positive rate"
a, b, _ = curve_function(gt[:, i], pred[:, i])
plt.figure(1, figsize=(7, 7))
plt.plot([0, 1], [0, 1], 'k--')
int i, j;
char filename[] = "Input_Image.bmp";
int data = 0, offset, bpp = 0, width, height;
long bmpsize = 0, bmpdataoff = 0;
int** image;
int temp = 0;
// Reading the BMP File
FILE* image_file;
image_file = fopen(filename, "rb");
// Creating the Histogram
int hist[256];
for (i = 0; i < 256; i++)
hist[i] = 0;
for (i = 0; i < height; i++)
for (j = 0; j < width; j++)
hist[image[i][j]] += 1;
// Finding number of
// non-zero occurrences
int nodes = 0;
for (i = 0; i < 256; i++)
{
if (hist[i] != 0)
nodes += 1;
}