Skip to content

Instantly share code, notes, and snippets.

@dvreed77
Created February 18, 2013 20:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dvreed77/4980426 to your computer and use it in GitHub Desktop.
Save dvreed77/4980426 to your computer and use it in GitHub Desktop.
Difference between Matlab's classify and ClassificationDiscriminant functions.
%% Balanced Classes, easily seperable
data = [randn(2500, 1) ; 1 + randn(2500, 1)];
labels = [zeros(2500, 1) ; ones(2500, 1)];
y_pred = classify(data, data, labels);
TP1 = sum(y_pred == 1 & labels == 1);
temp = ClassificationDiscriminant.fit(data, labels);
y_pred2 = temp.predict(data);
TP2 = sum(y_pred2 == 1 & labels == 1);
fprintf('Data Easily Seperable (Balanced Classes): classify = %i, ClassificationDiscriminant = %i\n', TP1, TP2)
%% Unbalanced Classes, easily seperable
N1 = 3500; N2 = 1500;
data = [randn(N1, 1) ; 1 + randn(N2, 1)];
labels = [zeros(N1, 1) ; ones(N2, 1)];
y_pred = classify(data, data, labels);
TP1 = sum(y_pred == 1 & labels == 1);
temp = ClassificationDiscriminant.fit(data, labels);
y_pred2 = temp.predict(data);
TP2 = sum(y_pred2 == 1 & labels == 1);
fprintf('Data Easily Seperable (Unbalanced Classes): classify = %i, ClassificationDiscriminant = %i\n', TP1, TP2)
%% Same Distribution
data = randn(5000, 1);
labels = [zeros(N1, 1) ; ones(N2, 1)];
y_pred = classify(data, data, labels);
TP1 = sum(y_pred == 1 & labels == 1);
temp = ClassificationDiscriminant.fit(data, labels);
y_pred2 = temp.predict(data);
TP2 = sum(y_pred2 == 1 & labels == 1);
fprintf('Data drawn from same distribution (Unbalanced Classes): classify = %i, ClassificationDiscriminant = %i\n', TP1, TP2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment