Klasyfikacja Support Vector Machines LABORKA Piotr Ciskowski
przykład 1 KLASYFIKACJA KWIATKÓW IRYSA
przykład 1. klasyfikacja kwiatków irysa (versicolor-virginica) żródło: pomoc MATLABa: http://www.mathworks.com/help/stats/svmclassify.html załaduj zbiór uczący: load fisheriris ; wybierz dwie ostatnie kolumny (cechy) i dwa ostatnie gatunki (klasy): - versicolor - virginica xdata = meas ( 51:end, 3:4 ) ; group = species ( 51:end ) ; skonstruuj klasyfikator SVM - liniowy: czas = clock ;... svmstruct = svmtrain ( xdata, group, 'ShowPlot', true ),... czas = etime ( clock, czas )
przykład 1. klasyfikacja kwiatków irysa (versicolor-virginica) żródło: pomoc MATLABa: http://www.mathworks.com/help/stats/svmclassify.html sprawdź, jak SVM klasyfikuje nowy punkt: newflower = [ 5, 2 ] ; species = svmclassify ( svmstruct, newflower, 'ShowPlot', true ) zaznacz nowy punkt: plot ( newflower(1), newflower(2), 'ro', 'MarkerSize', 12 )
przykład 1 KLASYFIKACJA KWIATKÓW IRYSA - RÓŻNE KERNELE
przykład 1. klasyfikacja kwiatków irysa (versicolor-virginica) żródło: pomoc MATLABa: http://www.mathworks.com/help/stats/svmtrain.html skonstruuj klasyfikator SVM - liniowy: svmstruct = svmtrain ( xdata, group, 'ShowPlot', true ) svmstruct = svmtrain ( xdata, group, 'ShowPlot', true,... 'kernel_function', 'linear' ) 2.5 2 versicolor (training) versicolor (classified) virginica (training) virginica (classified) Support Vectors 1.5 1 3 3.5 4 4.5 5 5.5 6 6.5 7
przykład 1. klasyfikacja kwiatków irysa (versicolor-virginica) żródło: pomoc MATLABa: http://www.mathworks.com/help/stats/svmtrain.html skonstruuj klasyfikator SVM z jądrem kwadratowym: svmstruct = svmtrain ( xdata, group, 'ShowPlot', true ) svmstruct = svmtrain ( xdata, group, 'ShowPlot', true,... 'kernel_function', 'quadratic' )
przykład 1. klasyfikacja kwiatków irysa (versicolor-virginica) żródło: pomoc MATLABa: http://www.mathworks.com/help/stats/svmtrain.html skonstruuj klasyfikator SVM z jądrem wielomianowym (stopnia 3): svmstruct = svmtrain ( xdata, group, 'ShowPlot', true ) svmstruct = svmtrain ( xdata, group, 'ShowPlot', true,... 'kernel_function', polynomial' )
przykład 1. klasyfikacja kwiatków irysa (versicolor-virginica) żródło: pomoc MATLABa: http://www.mathworks.com/help/stats/svmtrain.html skonstruuj klasyfikator SVM z jądrem wielomianowym (stopni 5-10-15): svmstruct = svmtrain ( xdata, group, 'ShowPlot', true ) svmstruct = svmtrain ( xdata, group, 'ShowPlot', true, 'kernel_function', polynomial', 'polyorder', 5 ) svmstruct = svmtrain ( xdata, group, 'ShowPlot', true, 'kernel_function', polynomial', 'polyorder', 10 ) svmstruct = svmtrain ( xdata, group, 'ShowPlot', true, 'kernel_function', polynomial', 'polyorder', 15 )
przykład 1. klasyfikacja kwiatków irysa (versicolor-virginica) żródło: pomoc MATLABa: http://www.mathworks.com/help/stats/svmtrain.html skonstruuj klasyfikator SVM z jądrem RBF: svmstruct = svmtrain ( xdata, group, 'ShowPlot', true,... 'kernel_function', 'rbf ) skonstruuj klasyfikator SVM z jądrem MLP: svmstruct = svmtrain ( xdata, group, 'ShowPlot', true,... 'kernel_function', 'mlp' )
przykład 1 KLASYFIKACJA KWIATKÓW IRYSA - PRAWDOPODOBIEŃSTWA A POSTERIORI
przykład 1. klasyfikacja kwiatków irysa (setosa-versicolor) żródło: pomoc MATLABa: http://www.mathworks.com/help/stats/compactclassificationsvm.predict.html Score - the SVM score for classifying observation x is the numerical predicted response f(x) computed by the trained SVM classification function: - - estimated SVM parameters - - dot product in the predictor space between x and the support vectors - the sum includes the training set observations
przykład 1. klasyfikacja kwiatków irysa (setosa-versicolor) żródło: pomoc MATLABa: http://www.mathworks.com/help/stats/compactclassificationsvm.predict.html Posterior Probability - The probability that an observation belongs in a particular class, given the data. - the posterior probability is a function of the score: P(s) - classes: k = { -1, 1 } - For separable classes, the posterior probability is the step function: where: s j is the score of observation j +1 and 1 denote the positive and negative classes, respectively π is the prior probability that an observation is in the positive class
przykład 1. klasyfikacja kwiatków irysa (setosa-versicolor) żródło: pomoc MATLABa: http://www.mathworks.com/help/stats/compactclassificationsvm.predict.html Posterior Probability - The probability that an observation belongs in a particular class, given the data. - the posterior probability is a function of the score: P(s) - classes: k = { -1, 1 } - For separable classes, the posterior probability is the step function: where: s j is the score of observation j +1 and 1 denote the positive and negative classes, respectively π is the prior probability that an observation is in the positive class - For inseparable classes, the posterior probability is the sigmoid function: where the parameters A and B are the slope and intercept parameters
przykład 1. klasyfikacja kwiatków irysa (setosa-versicolor) żródło: pomoc MATLABa: http://www.mathworks.com/help/stats/compactclassificationsvm.predict.html Prior Probability - believed relative frequency that observations from a class occur in the population, for each class
przykład 1. klasyfikacja kwiatków irysa (setosa-versicolor) żródło: pomoc MATLABa: http://www.mathworks.com/help/stats/compactclassificationsvm.predict.html zrób porządek załaduj jeszcze raz dane: clear all close all clc load fisheriris pozbądź się z danych gatunku virginica, zostaw setosa-versicolor: classkeep = ~strcmp ( species, 'virginica' ) ; x = meas ( classkeep, 3:4 ) ; y = species ( classkeep ) ;
przykład 1. klasyfikacja kwiatków irysa (setosa-versicolor) żródło: pomoc MATLABa: http://www.mathworks.com/help/stats/compactclassificationsvm.predict.html naucz klasyfikator: svmmodel = fitcsvm ( x, y, 'ClassNames', { 'setosa', 'versicolor' } ); dobierz optymalną funkcje prawdopodobieństwa: (optimal score transformation function) rng(1) ; % for reproducubility [ svmmodel, scoreparameters ] = fitposterior ( svmmodel ) ; scoreparameters
przykład 1. klasyfikacja kwiatków irysa (setosa-versicolor) żródło: pomoc MATLABa: http://www.mathworks.com/help/stats/compactclassificationsvm.predict.html przygotuj siatkę pod wykres prawdopodobieństwa: xmin = min ( x ) ; xmax = max ( x ) ; h = 0.01 ; [ x1grid, x2grid ] = meshgrid ( xmin(1) : h : xmax(1),... xmin(2) : h : xmax(2) ) ; zaklasyfikuj wszystkie punkty na siatce oblicz prawdopodobieństwo a posteriori: [ ~,posteriorregion ] = predict ( svmmodel, [ x1grid(:), x2grid(:) ] ) ;
przykład 1. klasyfikacja kwiatków irysa (setosa-versicolor) żródło: pomoc MATLABa: http://www.mathworks.com/help/stats/compactclassificationsvm.predict.html narysuj dane uczące, wektory wspierające oraz obszary prawdopodobieństwa a posteriori przynależności do klasy +1: contourf ( x1grid, x2grid,... reshape ( posteriorregion(:,2), size ( x1grid, 1 ),... size ( x2grid, 2 ) ) ) ; h(1) = colorbar ; set ( get(h(1),'ylabel ), 'String, 'P({\it versicolor})',... 'FontSize', 16 ) ; hold on gscatter ( x(:,1), x(:,2), y, 'mc, '.x, [ 15 10 ] ) sv = x ( svmmodel.issupportvector, : ) ; plot ( sv(:,1), sv(:,2), 'ro', 'MarkerSize', 15, 'LineWidth, 2 ) ; axis tight, hold off
przykład 1 KLASYFIKACJA KWIATKÓW IRYSA - DO KILKU KLAS - KERNEL: RBF
przykład 1. klasyfikacja kwiatków irysa (setosa-versicolor-virginica) żródło: pomoc MATLABa: http://www.mathworks.com/help/stats/fitcsvm.html zrób porządek załaduj jeszcze raz dane: clear all close all clc load fisheriris wybierz dwie ostatnie cechy: x = meas(:,3:4) ; y = species ;
przykład 1. klasyfikacja kwiatków irysa (setosa-versicolor-virginica) żródło: pomoc MATLABa: http://www.mathworks.com/help/stats/fitcsvm.html narysuj wykres danych uczących: figure gscatter ( x(:,1), x(:,2), y ) ; title ( '{\bf Scatter Diagram of Iris Measurements}' ) ; xlabel ( 'Petal length(cm)' ) ; ylabel ( 'Petal width(cm)' ) ; legend ( 'Location', 'Northwest' ) ; lims = get ( gca, {} ) ; % granice osi
przykład 1. klasyfikacja kwiatków irysa (setosa-versicolor-virginica) żródło: pomoc MATLABa: http://www.mathworks.com/help/stats/fitcsvm.html dla każdej klasy: utwórz wektor indx pokazujący, czy dana obserwacja należy do danej klasy naucz klasyfikator SVM - używając danych uczących oraz indx schowaj nauczony klasyfikator do komórki w macierzy komórkowej % It is good practice to define the class order and standardize the predictors. SVMModels = cell(3,1) ; classes = unique(y) ; rng(1); % for reproducibility for j = 1 : numel(classes), end indx = strcmp ( y, classes(j) ) ; svmmodels{j} = fitcsvm ( X, indx, 'ClassNames', [ false true],... 'Standardize, true,... 'KernelFunction', 'rbf',... 'BoxConstraint, 1 ) ;
przykład 1. klasyfikacja kwiatków irysa (setosa-versicolor-virginica) żródło: pomoc MATLABa: http://www.mathworks.com/help/stats/fitcsvm.html utwórz siatkę punktów testujących dla każdego punktu oblicz score d = 0.02; [ x1grid, x2grid] = meshgrid ( min(x(:,1)) : d : max(x(:,1)),... min(x(:,2)) : d : max(x(:,2)) ) ; xgrid = [ x1grid(:), x2grid(:) ] ; N = size ( xgrid, 1 ) ; scores = zeros ( N, numel(classes) ) ; for j = 1 : numel(classes), [ ~, score ] = predict ( svmmodels{j}, xgrid ) ; scores(:,j) = score(:,2) ; % second column contains positive-class scores end
przykład 1. klasyfikacja kwiatków irysa (setosa-versicolor-virginica) żródło: pomoc MATLABa: http://www.mathworks.com/help/stats/fitcsvm.html przyporządkuj każdą obserwację do klasy o największym score [ ~, maxscore ] = max (scores, [], 2 ) ; pokoloruj siatkę punktów testowych figure h(1:3) = gscatter ( xgrid(:,1), xgrid(:,2), maxscore,... [0.1 0.5 0.5; 0.5 0.1 0.5; 0.5 0.5 0.1] ) ; hold on h(4:6) = gscatter ( x(:,1), x(:,2), y ) ; title( '{\bf Iris Classification Regions}' ) ; xlabel( 'Petal Length (cm)' ) ; ylabel( 'Petal Width (cm)' ) ; legend ( h, {'setosa region','versicolor region','virginica region',... 'observed setosa','observed versicolor','observed virginica'},... 'Location','Northwest' ) ; axis tight, hold off