mirror of
https://github.com/RustyRaptor/learnopencv.git
synced 2026-12-03 22:15:16 -07:00
52 lines
1.6 KiB
Python
52 lines
1.6 KiB
Python
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) |