mirror of
https://github.com/RustyRaptor/learnopencv.git
synced 2026-12-03 22:15:16 -07:00
grid search
This commit is contained in:
1 parent
6cbf4d5222
commit
f62a16cdfe
10 files changed
+52
-56
No files matched your search
File renamed without changes.
File renamed without changes.
@@ -1,56 +0,0 @@
|
||||
import matplotlib.pyplot as plt
|
||||
from sklearn import svm
|
||||
from sklearn.cross_validation import train_test_split
|
||||
import numpy as np
|
||||
from sklearn.metrics import classification_report, confusion_matrix
|
||||
from sklearn.grid_search import GridSearchCV
|
||||
|
||||
import sys
|
||||
import os
|
||||
|
||||
sys.path.append(os.path.abspath("../../"))
|
||||
from utils import read_data, plot_data, plot_decision_function
|
||||
|
||||
# Read data
|
||||
x, labels = read_data("../points_rightside.txt", "../points_leftside.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)
|
||||
|
||||
plot_data(X_train, y_train, X_test, y_test)
|
||||
|
||||
# make a classifier
|
||||
clf = svm.SVC(C = 1.0, kernel='rbf')
|
||||
|
||||
# Train classifier
|
||||
clf.fit(X_train, y_train)
|
||||
|
||||
# Make predictions on unseen test data
|
||||
clf_predictions = clf.predict(X_test)
|
||||
|
||||
# Plot decision function on training and test data
|
||||
plot_decision_function(X_train, y_train, X_test, y_test, clf)
|
||||
|
||||
# Grid Search
|
||||
# Parameter Grid
|
||||
param_grid = {'C': [0.1, 1, 10, 100, 1000], 'gamma': [1, 0.1, 0.5, 0.01, 0.001, 0.00001]}
|
||||
|
||||
# Make grid search classifier
|
||||
grid = GridSearchCV(svm.SVC(), param_grid, verbose=0)
|
||||
|
||||
# Train the classifier
|
||||
grid.fit(X_train, y_train)
|
||||
|
||||
# clf = grid.best_estimator_()
|
||||
print("Best Parameters:\n", grid.best_params_)
|
||||
print("Best Estimators:\n", grid.best_estimator_)
|
||||
|
||||
# Create classifier with best parameters
|
||||
clf = svm.SVC(C = grid.best_params_['C'], gamma = grid.best_params_['gamma'])
|
||||
clf.fit(X_train, y_train)
|
||||
|
||||
# Make predictions on unseen test data
|
||||
grid_predictions = clf.predict(X_test)
|
||||
|
||||
# Plot decision function on training and test data
|
||||
plot_decision_function(X_train, y_train, X_test, y_test, clf)
|
||||
Binary file not shown.
|
Before Width: | Height: | Size: 515 KiB |
BIN
Binary file not shown.
|
Before Width: | Height: | Size: 669 KiB |
BIN
Binary file not shown.
|
Before Width: | Height: | Size: 515 KiB |
Binary file not shown.
|
Before Width: | Height: | Size: 288 KiB |
File renamed without changes.
File renamed without changes.
@@ -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)
|
||||
Reference in new issue
Block a user