Plot ROC curve in Matlab - matlab

I need to plot a ROC curve in matlab. I have two arrays, one containing the true positive rate and one containing the false positive rate. I've tried both plotroc and perfcurve with the two arrays as input, but it doesn't seems to work. There is another way for plotting ROC curve with the data I have?
EDIT
I'm posting an image in order to answer to Tasos Papastylianou:
simply plotting the two array it does not really seems a ROC curve :S
EDIT2
Upload the image with the array inverted, still not looking like a ROC!
EDIT3
Image showing the plot of my ROC curve, normalized in [0,1]
[]3

I thought I'd post a proper answer with a nice ROC curve example, demonstrating the stuff we discussed in the comments:
%% Create datasets
[X,Y] = ndgrid(1:100,1:100);
% Ground Truth image
Circle= zeros(100); Circle((X-50).^2 + (Y-50).^2 - 500 <= 0) = 1;
% Test image (parameterised by threshold)
pkg load statistics % if using octave - needed for 'mvnpdf'
pkg load image % if using octave - needed for 'mat2gray'
Gaussian = mvnpdf([X(:), Y(:)], [45, 45], [500,0;0,500]);
Gaussian = reshape(Gaussian, size(X));
Gaussian = mat2gray(Gaussian);
%% Generate ROC curve for a range of thresholds
ThresholdRange = 0 : 0.025 : 1;
TPs = zeros(size(ThresholdRange));
FPs = zeros(size(ThresholdRange));
Ind = 0;
for Threshold = ThresholdRange
Ind = Ind + 1;
TP = Circle .* (Gaussian > Threshold);
T = Circle;
TPR = sum(TP(:)) / sum(T(:));
TPs(Ind) = TPR;
FP = (1 - Circle) .* (Gaussian > Threshold);
N = (1 - Circle);
FPR = sum(FP(:)) / sum(N(:));
FPs(Ind) = FPR;
end
%% Plotski curvski
plot(FPs, TPs, 'linewidth', 3, 'marker', 'o', 'markersize',10,'markeredgecolor', 'k', 'markerfacecolor', 'g');
hold on;
plot(ThresholdRange, ThresholdRange, 'r-.', 'linewidth', 3);
axis([0,1,0,1]);
title('Les Curves du ROC! Ooh-la-la!', 'fontsize', 16);
xlabel('Le Rate des Positifs Falses! Oh mon dieu!', 'fontsize', 14);
ylabel('Le Rate des Positifs Vrais! Magnifique!', 'fontsize', 14);
grid on;

I resolved thanks to the comments provided by #TasosPapastylianou. For getting better results in plotting, I sort the two array on the x-axis (or the False Positive Rates). Thanks again to #TasosPapastylianou !!!

Related

Image Transformation - Cartesian to Polar, and back again (MATLAB)

I have been using Peter Kovesi's MatLab functions for machine vision (which are outstanding if you aren't aware of them).
I have been transforming images to polar co-ordinates using the polar transform. The function from Peter Kovesi is named 'PolarTrans' and can be found here -
http://www.peterkovesi.com/matlabfns/#syntheticimages
The function beautifully transforms an images into polar co-ordinates. However, I would like the reverse to happen also. Peter Kovesi uses interp2 to transform images, but I can't seem to figure out how to reverse this transform. A requirement of interp2 is that it needs a meshgrid as input.
In short - can you help me reverse the transformation: polar to cartesian. I would like it be accordance with Peter's function - i.e. using the same parameters for coherence.
Dear Swjm,
I am posting my reply here because I do not have space in the comments section.
Firstly, thank you very much indeed for your reply. You have shown me how to invert interp2 - something I thought was impossible. This is a huge step forwards. However your code only maps a small segment of the image. Please see the demo code below to understand what I mean.
clc; clear all; close all;
gauss = fspecial('gauss',64,15);
gauss = uint8(mat2gray(gauss).*255);
[H,W] = size(gauss);
pim = polartrans(gauss,64,360);
cim = carttrans(pim,64,64);
subplot(2,2,1);
imagesc(gauss); colormap(jet);
axis off;
title('Image to be Transformed');
subplot(2,2,2);
imagesc(pim); colormap(jet);
axis off;
title('Polar Representation');
subplot(2,2,3);
imagesc(cim); colormap(jet);
axis off;
title('Back to Cartesian');
subplot(2,2,4);
diff = uint8(gauss) - uint8(cim);
imagesc(diff); colormap(jet);
axis off;
title('Difference Image');
I've had a look at Kovesi's code and this code should perform the reverse transformation. It assumes you used the 'full' shape and 'linear' map parameters in polartrans. Note that polar transforms generally lose resolution at low radial values (and gain resolution at high values), so it won't be lossless even if your polar image has the same dimensions as your original image.
function im = carttrans(pim, nrows, ncols, cx, cy)
[rad, theta] = size(pim); % Dimensions of polar image.
if nargin==3
cx = ncols/2 + .5; % Polar coordinate center, should match
cy = nrows/2 + .5; % polartrans. Defaults to same.
end
[X,Y] = meshgrid(1:ncols, 1:nrows);
[TH,R] = cart2pol(X-cx,Y-cy); % Polar coordinate arrays.
TH(TH<0) = TH(TH<0)+2*pi; % Put angles in range [0, 2*pi].
rmax = max(R(:)); % Max radius.
xi = TH * (theta+1) / 2*pi; % Query array for angles.
yi = R * rad / (rmax-1) + 1; % Query array for radius.
pim = [pim pim(:,1)]; % Add first col to end of polar image.
[pX,pY] = meshgrid(1:theta+1, 1:rad);
im = interp2(pX, pY, pim, xi, yi);

Speed up coloring segments of a spline curve?

I am trying to color segments of a spline curve with different RGB values. Many thanks to #Suever, I have a working version:
x = [0.16;0.15;0.25;0.48;0.67];
y = [0.77;0.55;0.39;0.22;0.21];
spcv = cscvn([x, y].'); % spline curve
N = size(x, 1);
figure;
hold on;
for idx = 1:N-2
before = get(gca, 'children'); % before plotting this segment
fnplt(spcv, spcv.breaks([idx, idx+1]), 2);
after = get(gca, 'children'); % after plotting this segment
new = setdiff(after, before);
set(new, 'Color', [idx/N, 1-idx/N, 0, idx/N]); % set new segment to a specific RGBA color
end
hold off;
Now I am looking to speed it up. Is it possible?
No explicit benchmarks per se, but you can vectorise this easily by
a. collecting the plotted points and dividing them into 'segments' (e.g. using the buffer function)
b. setting the 'color' property of the Children (thanks to #Suever for pointing out this can be done on an array of object handles directly)
%% Get spline curve
x = [0.16; 0.15; 0.25; 0.48; 0.67];
y = [0.77; 0.55; 0.39; 0.22; 0.21];
spcv = cscvn ([x, y].');
%% Split into segments
pts = fnplt (spcv); xpts = pts(1,:).'; ypts = pts(2,:).';
idx = buffer ([1 : length(xpts)]', 10, 1, 'nodelay'); % 10pt segments
lastidx=idx(:,end); lastidx(lastidx==0)=[]; idx(:,end)=[]; % correct last segment
% Plot segments
plot (xpts(idx), ypts(idx), xpts(lastidx), ypts(lastidx), 'linewidth', 10);
% Adjust colour and transparency
Children = flipud (get (gca, 'children'));
Colours = hsv (size (Children, 1)); % generate from colourmap
Alphas = linspace (0, 1, length (Children)).'; % for example
set (Children, {'color'}, num2cell([Colours, Alphas],2));
Note: As also pointed out in the comments section (thanks #Dev-iL), setting the colour to an RGBA quadruplet the way you ask (i.e. as opposed to a simple RGB triplet) is a newer (also, for now, undocumented) Matlab feature. This code, e.g. will not work in 2013b.

Bad contour lines when plotting gridded data

I have heavily edited data from an underwater glider that measures temperature, conductivity, and pressure. From that we get salinity, density and sigma (sigma = density - 1000). The instrument did not sample very well, hence the heavy editing. I need to make "pretty" contour plots for a poster. This is not for a publication so I am not worried about the amount of filtering or smoothing.
I am having trouble with the contour lines of sigma (density), please see below.
The black contour lines should trace the filled contours of sigma but they look very bad. The data was binned by 1 m before any gridding is done. here is the code used to generate the plots.
Here is the code used to generate this image
% load data
load Matlab_data
maxy = 50;
t = 1;
Tstep = t./24./60; % Grid data on t minute time intervals
X1 = time(1)-Tstep:Tstep:time(end)+Tstep;
Y1 = 0:1:maxy; % Grid data on 1 meter depth intervals
ygrid = [' Depth grid: ' num2str(diff(Y1(1:2)))];
xgrid = [' Time grid: ' num2str(diff(X1(1:2)))];
[X,Y]= meshgrid(X1,Y1);
bad_vals = isnan(sal) .* ~isfinite(sal) .* isnan(press); % don't include NaNs or funky imaginary salinities in the contours
vals = find(bad_vals == 0);
Zd = griddata(time(vals),depth(vals),density(vals),X,Y);
Zt = griddata(time(vals),depth(vals),temp(vals),X,Y);
Zs = griddata(time(vals),depth(vals),sal(vals),X,Y);
Zst = griddata(time(vals),depth(vals),sigmat(vals),X,Y);
% Interpolate over gaps
vst = interp1gap(sigmat);
vs = interp1gap(sal);
% Grid interpolated salinity and sigma data
Zst_interp = griddata(time(vals),depth(vals),vst(vals),X,Y);
Zs_interp = griddata(time(vals),depth(vals),vs(vals),X,Y);
%% Contour Plot
% Set up the figure
figure3=figure('Position', [2000, 50, 1500, 500]);
clf
colormap(jet);
% Temperature
ax1 = subplot(2, 1,1);
[c,h] = contourf(X,Y,Zst,50,'linestyle','none'); %,[4:.5:9],'linestyle','none');
cRange = caxis;
hold on
[c2,h2] = contour(X,Y,Zst,[24 24.5 25],'color','k'); %,[22:.5:26.5],'linewidth',1.5,'color','k');
clabel(c2,h2,'fontsize',10,'labelspacing',150);
set(h2,'linewidth',1)
hc = colorbar;
colormap(jet);
datetick('x','mm/dd','keeplimits','keepticks');
grid on;
box on
pos = get(gca,'position');
set(gca,'YDir','reverse')%,'position',[pos(1) pos(2) pos(3)-.06 pos(4)]);
set(gca,'xlim',[time(1)+.5./24 time(end)-.5./24],...
'ylim',[0 maxy],'fontsize',8,'xminortick','on','yminortick','on');
set(get(hc,'ylabel'),'string','Sigma-Theta (kg m^-^3)','rotation',270,'verticalalignment','bottom');
ylabel('Ocean Depth (m)');
xlabel('Date');
%title(['Sigma Theta (kg m^-^3) -' strrep(tag,'_','\_')], 'fontweight', 'bold','FontSize',12)
title(['Sigma Theta (kg m^-^3) :' ygrid xgrid ], 'fontweight', 'bold','FontSize',12)
ax2 = subplot(2, 1,2);
%h=pcolor(X,Y,Zst);
[c,h] = contourf(X,Y,Zst_interp,50,'linestyle','none'); %,[4:.5:9],'linestyle','none');
shading interp
hc = colorbar;
cRange = caxis;
hold on
[c2,h2] = contour(X,Y,Zst_interp,[24 24.5 25],'color','k'); %,[22:.5:26.5],'linewidth',1.5,'color','k');
clabel(c2,h2,'fontsize',10,'labelspacing',150);
set(h2,'linewidth',1)
hc = colorbar;
colormap(jet);
caxis(cRange);
datetick('x','mm/dd','keeplimits','keepticks');
grid on;
box on
pos = get(gca,'position');
set(gca,'YDir','reverse')%,'position',[pos(1) pos(2) pos(3)-.06 pos(4)]);
set(gca,'xlim',[time(1)+.5./24 time(end)-.5./24],...
'ylim',[0 maxy],'fontsize',8,'xminortick','on','yminortick','on');
set(get(hc,'ylabel'),'string','Sigma-Theta (kg m^-^3)','rotation',270,'verticalalignment','bottom');
ylabel('Ocean Depth (m)');
xlabel('Date');
% title(['Sigma Theta (kg m^-^3) -' strrep(tag,'_','\_')], 'fontweight', 'bold','FontSize',12)
title(['Sigma Theta interp1gap (kg m^-^3) :' ygrid xgrid ], 'fontweight', 'bold','FontSize',12)
Please help! I have TWO issues... the first and most obvious is the ugly black contour lines, these should "flow" around the filled contours nicely. If anyone has experience or suggestions of how to smooth these please pass it along, with code if possible.
The second issue lies in the bottom plot, I need to fill the gaps in data (due to editing original bad data). I used the function interp1gap, available on the File exchange but it also interpolates the data to deeper depths, which I do not want. I just want the gaps to be filled in, such as by choosing their horizontal neighbors and filling in.
Please let me know if you have any suggestions for fixing this! I have attached the data (Matlab_data.mat) and it includes time, depth, sal (salinity), sigmat, press, temp, and density.
Is this an issue of gridding? Please be as specific as possible and any code and figures would be greatly appreciated if you have time to look into this.
The data is available drop box https://www.dropbox.com/s/mjd8f9bzdvwddk5/Matlab_data.mat?dl=0
Thank you very much in advance!

How to plot two 1-dimensional Gaussian distributions together with the classification boundary [Matlab]?

I have two classes(normally distributed), C1 and C2, each defined by their mean and standard deviation. I want to be able to visualize the pdf plot of a normal distributions and the classification boundary between the two. Currently I have the code to plot the distributions but I'm not sure how to go about plotting the decision boundary. Any ideas would be appreciated. I have included a sample of what I want to plot. 1
Many thanks!
This is what I came up with:
% Generate some example data
mu1 = -0.5; sigma1 = 0.7; mu2 = 0.8; sigma2 = 0.5;
x = linspace(-8, 8, 500);
y1 = normpdf(x, mu1, sigma1);
y2 = normpdf(x, mu2, sigma2);
% Plot it
figure; plot(x, [y1; y2])
hold on
% Detect intersection between curves; choose threshold so you get the whole
% intersection (0.0001 should do unless your sigmas are very large)
ind = y1 .* y2 > 0.0001;
% Find the minimum values in range
minVals = min([y1(ind); y2(ind)]);
if ~isempty(minVals)
area(x(ind), minVals)
end
I don't know if this is the best way to do what you want, but it seems to work.

How do I plot confidence intervals in MATLAB?

I want to plot some confidence interval graphs in MATLAB but I don't have any idea at all how to do it. I have the data in a .xls file.
Can someone give me a hint, or does anyone know commands for plotting CIs?
After reading numerous threads, here's my attempt.
% Get some random data
x = linspace(0.3, pi-0.3, 10);
Data = sin(x) + randn(1, 10)/10;
Data_sd = 0.1+randn(1,10)/30;
% prepare it for the fill function
x_ax = 1:10;
X_plot = [x_ax, fliplr(x_ax)];
Y_plot = [Data-1.96.*Data_sd, fliplr(Data+1.96.*Data_sd)];
% plot a line + confidence bands
hold on
plot(x_ax, Data, 'blue', 'LineWidth', 1.2)
fill(X_plot, Y_plot , 1,....
'facecolor','blue', ...
'edgecolor','none', ...
'facealpha', 0.3);
hold off
Mostly based on this question: Plotting with transparency
I'm not sure what you meant by confidence intervals graph, but this is an example of how to plot a two-sided 95% CI of a normal distribution:
alpha = 0.05; % significance level
mu = 10; % mean
sigma = 2; % std
cutoff1 = norminv(alpha, mu, sigma);
cutoff2 = norminv(1-alpha, mu, sigma);
x = [linspace(mu-4*sigma,cutoff1), ...
linspace(cutoff1,cutoff2), ...
linspace(cutoff2,mu+4*sigma)];
y = normpdf(x, mu, sigma);
plot(x,y)
xlo = [x(x<=cutoff1) cutoff1];
ylo = [y(x<=cutoff1) 0];
patch(xlo, ylo, 'b')
xhi = [cutoff2 x(x>=cutoff2)];
yhi = [0 y(x>=cutoff2)];
patch(xhi, yhi, 'b')
See e.g. these m-files on Matlab File Exchange:
plot confidence intervals
confplot