Grid Search
Grid Search
SVC Parameters Example
# using grid search for sklearn.svm.SVC
from sklearn.model_selection import GridSearchCV
parameters = [{'C': [1, 10, 100, 1000], 'kernel': ['linear']},
{'C': [1, 10, 100, 1000], 'kernel': ['rbf'], 'gamma': [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9]}]
grid_search = GridSearchCV(estimator = classifier,
param_grid = parameters,
scoring = 'accuracy',
cv = 10,
n_jobs = -1)
grid_search = grid_search.fit(X_train, y_train)
best_accuracy = grid_search.best_score_
best_parameters = grid_search.best_params_
K-Fold Cross Validation
Sklearn
Towards Data Science
Kaggle
# imports needed
from sklearn.model_selection import cross_val_score, cross_val_predict
from sklearn import metrics
scores = cross_val_score(estimator, X, y, cv=10)
print ("Cross-validated scores:", scores)
r2 = metrics.r2_score(y, yhat)
print ("R-squared:", r2)
Test Train Split
Test Train Split
https://www.udemy.com/machinelearning/
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.2, random_state = 0)