Skip to content

Commit

Permalink
Support Vector Machines Algorithm added
Browse files Browse the repository at this point in the history
  • Loading branch information
ozlemkorpe committed Jun 28, 2020
1 parent c6798c0 commit 65ed547
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 2 deletions.
4 changes: 2 additions & 2 deletions Naive_Bayes_Guide.m
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@

%---------------Visualizing Training Results
labels = unique(data.Purchased);
classifier_name = 'Naive Bayesain (Training Results)';
classifier_name = 'Naive Bayes (Training Results)';

Age_range = min(data.Age(training(cv)))-1:0.01:max(data.Age(training(cv)))+1;
Estimated_salary_range = min(data.EstimatedSalary(training(cv)))-1:0.01:max(data.EstimatedSalary(training(cv)))+1;
Expand Down Expand Up @@ -60,7 +60,7 @@

%---------------Visualizing Test Results
labels = unique(data.Purchased);
classifier_name = 'K-Nearest Neigbor (Testing Results)';
classifier_name = 'Naive Bayes (Testing Results)';

Age_range = min(data.Age(training(cv)))-1:0.01:max(data.Age(training(cv)))+1;
Estimated_salary_range = min(data.EstimatedSalary(training(cv)))-1:0.01:max(data.EstimatedSalary(training(cv)))+1;
Expand Down
89 changes: 89 additions & 0 deletions Support_Vector_Machines_Guide.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
%%%-------------Support Vector Machines (SVM)

%---------------Importing Dataset
data = readtable('C:\Users\Asus\Desktop\necessary\Naive Bayes\Social_Network_Ads.csv');

%---------------Feature Scaling (Standardization Method)
stand_age = (data.Age - mean(data.Age))/std(data.Age);
data.Age = stand_age;

stand_estimted_salary = (data.EstimatedSalary - mean(data.EstimatedSalary))/std(data.EstimatedSalary);
data.EstimatedSalary = stand_estimted_salary;

%---------------Classifying Data
classification_model = fitcsvm(data,'Purchased~Age+EstimatedSalary');
% Uses normal distribution as default
% classification_model = fitcnb(normalized_data, 'Survived~Age+Fare+Parch+SibSp+female+male+Pclass','Distribution','kernel');

%---------------Partitioning
cv = cvpartition(classification_model.NumObservations, 'HoldOut', 0.2);
cross_validated_model = crossval(classification_model,'cvpartition',cv);

%---------------Predictions
Predictions = predict(cross_validated_model.Trained{1},data(test(cv),1:end-1));

%---------------Analyzing the Results
Results = confusionmat(cross_validated_model.Y(test(cv)),Predictions);

%---------------Visualizing Training Results
labels = unique(data.Purchased);
classifier_name = 'SVM (Training Results)';

Age_range = min(data.Age(training(cv)))-1:0.01:max(data.Age(training(cv)))+1;
Estimated_salary_range = min(data.EstimatedSalary(training(cv)))-1:0.01:max(data.EstimatedSalary(training(cv)))+1;

[xx1, xx2] = meshgrid(Age_range,Estimated_salary_range);
XGrid = [xx1(:) xx2(:)];

predictions_meshgrid = predict(cross_validated_model.Trained{1},XGrid);

gscatter(xx1(:), xx2(:), predictions_meshgrid,'rgb');

hold on

training_data = data(training(cv),:);
Y = ismember(training_data.Purchased,labels{1});

scatter(training_data.Age(Y),training_data.EstimatedSalary(Y), 'o' , 'MarkerEdgeColor', 'black', 'MarkerFaceColor', 'red');
scatter(training_data.Age(~Y),training_data.EstimatedSalary(~Y) , 'o' , 'MarkerEdgeColor', 'black', 'MarkerFaceColor', 'green');

xlabel('Age');
ylabel('Estimated Salary');

title(classifier_name);
legend off, axis tight

legend(labels,'Location',[0.45,0.01,0.45,0.05],'Orientation','Horizontal');

%---------------Visualizing Test Results
labels = unique(data.Purchased);
classifier_name = 'SVM (Testing Results)';

Age_range = min(data.Age(training(cv)))-1:0.01:max(data.Age(training(cv)))+1;
Estimated_salary_range = min(data.EstimatedSalary(training(cv)))-1:0.01:max(data.EstimatedSalary(training(cv)))+1;

[xx1, xx2] = meshgrid(Age_range,Estimated_salary_range);
XGrid = [xx1(:) xx2(:)];

predictions_meshgrid = predict(cross_validated_model.Trained{1},XGrid);

figure

gscatter(xx1(:), xx2(:), predictions_meshgrid,'rgb');

hold on

testing_data = data(test(cv),:);
Y = ismember(testing_data.Purchased,labels{1});

scatter(testing_data.Age(Y),testing_data.EstimatedSalary(Y), 'o' , 'MarkerEdgeColor', 'black', 'MarkerFaceColor', 'red');
scatter(testing_data.Age(~Y),testing_data.EstimatedSalary(~Y) , 'o' , 'MarkerEdgeColor', 'black', 'MarkerFaceColor', 'green');

xlabel('Age');
ylabel('Estimated Salary');

title(classifier_name);
legend off, axis tight

legend(labels,'Location',[0.45,0.01,0.45,0.05],'Orientation','Horizontal');

0 comments on commit 65ed547

Please sign in to comment.