forked from spmallick/learnopencv
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
52 additions
and
56 deletions.
There are no files selected for viewing
File renamed without changes.
File renamed without changes.
56 changes: 0 additions & 56 deletions
56
SVM-using-Python/Non-Linear-Data/classification/classify-with-grid-search.py
This file was deleted.
Oops, something went wrong.
Binary file removed
BIN
-515 KB
...ing-Python/Non-Linear-Data/classification/decision-function-on-testing-data.png
Binary file not shown.
Binary file removed
BIN
-669 KB
...ng-Python/Non-Linear-Data/classification/decision-function-on-training-data.png
Binary file not shown.
Binary file removed
BIN
-515 KB
...on-Linear-Data/classification/grid-search-decision-function-on-testing-data.png
Binary file not shown.
Binary file removed
BIN
-288 KB
SVM-using-Python/Non-Linear-Data/classification/non-linear-train-data.png
Binary file not shown.
File renamed without changes.
File renamed without changes.
52 changes: 52 additions & 0 deletions
52
SVM-using-Python/Non-Linear-Data/svm-classify-with-gridsearch.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import sys, os | ||
import matplotlib.pyplot as plt | ||
from sklearn import svm | ||
from sklearn.model_selection import train_test_split, GridSearchCV | ||
|
||
|
||
sys.path.append(os.path.abspath("../")) | ||
from utils import read_data, plot_data, plot_decision_function | ||
|
||
# Read data | ||
x, labels = read_data("points_class_0.txt", "points_class_1.txt") | ||
|
||
# Split data to train and test on 80-20 ratio | ||
X_train, X_test, y_train, y_test = train_test_split(x, labels, test_size = 0.2, random_state=0) | ||
|
||
print("Displaying data. Close window to continue") | ||
# Plot data | ||
plot_data(X_train, y_train, X_test, y_test) | ||
|
||
print("Training SVM ...") | ||
# make a classifier | ||
clf = svm.SVC(C = 10.0, kernel='rbf', gamma=0.1) | ||
|
||
# Train classifier | ||
clf.fit(X_train, y_train) | ||
|
||
# Make predictions on unseen test data | ||
clf_predictions = clf.predict(X_test) | ||
|
||
print("Displaying decision function. Close window to continue") | ||
# Plot decision function on training and test data | ||
plot_decision_function(X_train, y_train, X_test, y_test, clf) | ||
|
||
# Grid Search | ||
print("Performing grid search ... ") | ||
|
||
# Parameter Grid | ||
param_grid = {'C': [0.1, 1, 10, 100], 'gamma': [1, 0.1, 0.01, 0.001, 0.00001, 10]} | ||
|
||
# Make grid search classifier | ||
clf_grid = GridSearchCV(svm.SVC(), param_grid, verbose=1) | ||
|
||
# Train the classifier | ||
clf_grid.fit(X_train, y_train) | ||
|
||
# clf = grid.best_estimator_() | ||
print("Best Parameters:\n", clf_grid.best_params_) | ||
print("Best Estimators:\n", clf_grid.best_estimator_) | ||
|
||
print("Displaying decision function for best estimator. Close window to continue.") | ||
# Plot decision function on training and test data | ||
plot_decision_function(X_train, y_train, X_test, y_test, clf_grid) |