What is the difference between predict and svmclassify? - matlab

I tried the following code
data = [27 9 0
11.6723281 28.93422177 0
25 9 0
23 8 0
5.896096039 23.97745722 1
21 6 0
21.16823369 5.292058423 0
4.242640687 13.43502884 1
22 6 0];
Attributes = data(:,1:2);
Classes = data(:,3);
train = [1 3 4 5 6 7];
test = [2 8 9];
%%# Train
SVMModel = fitcsvm(Classes(train),Attributes(train,:))
classOrder = SVMModel.ClassNames
sv = SVMModel.SupportVectors;
figure
gscatter(train(:,1),train(:,2),Classes)
hold on
plot(train(:,1),train(:,2),'ko','MarkerSize',10)
legend('good','bad','Support Vector')
hold off
I tried both predict and svmclassify; but it returns an error. What is the basic difference between these two functions?
[label,score] = predict(SVMModel,test);
label = svmclassify(SVMModel, test);

First off, there's quite a big note on top of the documentation page on svmclassify:
svmclassify will be removed in a future release. See fitcsvm, ClassificationSVM, and CompactClassificationSVM instead.
MATLAB is a bit vague in its naming of functions, as there's loads of functions named predict, using different schemes and algorithms. I suspect you'll want to use the one for SVMs. This should return the same result as svmclassify, but I think that either something went wrong in determining which predict MATLAB decided to use, or that predict has a newer algorithm than the unsupported svmclassify, hence a different output may result.
The conclusion is that you should use the newest functions to be able to run your code in future releases and get the newest algorithms. MATLAB will choose the correct version of predict based on what kind of input structure you feed it.

Related

How do I populate dynamic arrays in Julia similar to Matlab

My understanding of programming languages is limited, I have started 'coding' with matlab and wanted to transfer a simulation of mine from matlab to julia because there is no licensing fee. What I would like to know is in MATLAB I can auto populate array without ever initializing an array, while I know it is an inefficient way to it, I would like to know if there is similar way to do it on Julia.
Ex in MATLAB
for a in 1:10
x(a)=a;
end
will give me an array x = [ 1 2 3 4 5 6 7 8 9 10], is there a similar way to do that in julia? The way I have been doing it is declaring an empty array using Float64[] and append to it but it doesn't work the same way if the array is multidimensional.
my implementation in Julia for a 1D array
x = Float64[]
for a in 1:10
append!(x,a)
end
Any help regarding this will be greatly appreciated. Thank you!!!
MATLAB explicitly warns against the way you write this code and advises to preallocate for speed.
Julia, OTOH, cares more about performance and prohibits such pattern from the beginning. At the same time, it allows for more convenient and fast alternatives to do the same task.
julia> x = [a for a = 1:10] # OR x = [1:10;]
10-element Vector{Int64}:
1
2
3
4
5
6
7
8
9
10
and for 2D arrays,
julia> x = [i+j-1 for i = 1:5, j = 1:5] # OR x = (1:5) .+ (1:5)' .- 1
5×5 Matrix{Int64}:
1 2 3 4 5
2 3 4 5 6
3 4 5 6 7
4 5 6 7 8
5 6 7 8 9
And you have other convenience functions, like zeros(m,n), fill(val, dims), rand(Type, dims), etc., that you can use to initialize the arrays however you want.
Although #AboAmmar is correct that this generally isn't a good pattern, your code works if you use push! instead of append!. push! adds an element to a vector. append appends 2 vectors.

How to sum a series of values, ignoring any NaN values?

I use MATLAB version R2015a.
I get a series answer from solving the optimal problem several times, and I want to get their sum and average them. However, some of them are NaN. How do I write code to ignore those NaN and sum the others which are not NaN?
Option 1: toolbox free solution using sum and isnan from base MATLAB.
A = [1 2 3 4 5 6 7 8 9 NaN];
sum(A(~isnan(A))) % No toolbox required
Option 2: nansum (see this answer from OP)
Note: nansum requires the Statistics toolbox.
nansum(A) % Requires Statistics toolbox
Code tested using MATLAB R2018b.
Update from comments
Great suggestion from #Cris Luengo for those with more recent versions. Requires no toolbox.
sum(A,'omitnan') % No toolbox required
Another suggestion from #Ben Voigt for some applications. Also requires no toolbox.
sum(A(isfinite(A))) % No toolbox required
You can use inbuilt functions as suggested in the above answer. If you want to know the logic and use a loop..you can follow as shown below:
A = [NaN 1 2 NaN 3 4 7 -1 NaN] ;
count = 0 ;
thesum = 0 ;
for i = 1:length(A)
if ~isnan(A(i))
count = count+1 ;
thesum = thesum+A(i) ;
end
end
You can use the omitnan argument
A = [1 2 3 4 5 6 7 8 9 NaN];
s = sum( A, 'omitnan' )
Note, this is literally the same code as used by the nansum function from the Statistics toolbox, which was introduced before R2006a, so I would think compatibility is pretty good.

Function which no longer exists

I am interested in using the function here:
http://uk.mathworks.com/help/nnet/ref/removerows.html
However, when I try to use it in Matlab it says: "Undefined function or variable 'removerows'"
I typed: exist removerows and returned a value of 0, suggesting that it's been removed. Has this function just been renamed? or is it part of a toolbox I may not have, the information does not detail this.
Much appreciated
According to the link that you posted, this function is part of the Neural Network Toolbox. So my guess is that you don't have this toolbox installed.
You can remove rows in a matrix by assigning an empty array to them.
This way you don't have to use functions belonging to toolboxes that require extra licences.
Example
A = [1 2; 3 4; 5 6]
A =
1 2
3 4
5 6
A(2,:) = [] %remove row 2
A =
1 2
5 6
Similarly you can provide an index array with the rows to be deleted in case you want to remove several ones.

How to find values of the input data in plotsommaphits

I have used SOM tool box in MATLAB or iris data set. following example and using the plotsomhits i can see that how many data values are in each neuron of my neuron grid of the SOM . However I wish to know actual data values which are grouped in every neuron of the given SOM configuration .Is there any way to do it. this is the example I used.
net = selforgmap([8 8]);
view(net)
[net,tr] = train(net,x);
nntraintool
plotsomhits(net,x)
not that hard. plotsomhits just plots "fancily" the results of the simulation of the net.
so if you simulate it and add the "hits" you have the result!
basicaly:
hits=sum(sim(net,x)');
In your net case this was my results, that coincide with the numbers in the plotsomehits
hits= 6 5 0 7 15 7 4 0 8 20 3 3 9 3 0 8 6 3 11 4 5 5 7 10 1
PD: you can learn a lot in this amazing SO answer:
MATLAB: help needed with Self-Organizing Map (SOM) clustering
You need to convert vector to indices first and then you can see what input values a neuron correspond to.
>> input_neuron_mapping = vec2ind(net(x))';
Now, look into the neuron's inputs.
For example, you want to see neuron input values for neuron 2.
>> neuron_2_input_indices = find(input_neuron_mapping == 2)
>> neuron_2_input_values = x(neuron_2_input_indices)
It will display all the input values from your data.
Read here for more details:
https://bioinformaticsreview.com/20220603/how-to-get-input-values-from-som-sample-hits-plot-in-matlab/

Permutation vectors from the CLUSTERGRAM object (MATLAB)

I'm using the CLUSTERGRAM object from the Bioinformatics Toolbox (ver 3.7).
MATLAB version R2011a.
I'd like to get permutation vectors for row and columns for clustergram, as I can do with dendrogram function:
x = magic(10);
>> [~,~,permrows] = dendrogram(linkage(x,'average','euc'))
permrows =
9 10 6 7 8 1 2 4 5 3
>> [~,~,permcols] = dendrogram(linkage(x','average','euc'))
permcols =
6 7 8 9 2 1 3 4 5 10
I found that the clustering is not the same from clustergram and dendrogram, most probably due to optimal leaf ordering calculation (I don't want to disable it).
For example, for clustergram from:
clustergram(x)
('average' and 'eucledian' are default methods for clustergram)
the vectors (as on the figure attached) should be:
permrows = [1 2 4 5 3 10 9 6 7 8];
permcols = [1 2 8 9 6 7 10 5 4 3];
So, how to get those vectors programmatically? Anybody well familiar with this object?
Do anyone can suggest a good alternative? I know I can create a similar figure combining imagesc and dendrogram functions, but leaf ordering is much better (optimal) in clustergram, than in dendrogram.
From looking at the documentation, I guess that get(gco,'ColumnLabels') and get(gco,'RowLabels'), where gco is the clustergram object, should give you the reordered labels. Note that the corresponding set-methods take in the labels in original order and internally reorders them.
Consequently, if you have used custom labels (set(gco,'RowLabels',originalLabels))
[~,permrows] = ismember(get(gco,'RowLabels'),originalLabels)
should return the row permutation.