In [ ]:
from sklearn.grid_search import GridSearchCV
# KNN tuning
knn = KNeighborsClassifier()
k_range = np.arange(1,50)
param_grid = dict(n_neighbors=k_range)
grid = GridSearchCV(knn, param_grid, cv=10, scoring='f1')
grid.fit(X,y)
print(grid.best_score_)
# SVC tuning
tuned_parameters = [{'kernel': ['rbf'], 'gamma': [1e-3, 1e-4],
'C': [1, 10, 100, 1000]},
{'kernel': ['linear'], 'C': [1, 10, 100, 1000]}]
clf = GridSearchCV(SVC(C=1), tuned_parameters, cv=5,
scoring='f1')
clf.fit(X,y)
print(clf.best_score_)
# random forest
max_features = np.arange(1,9,3)
min_samples_split = np.arange(1,11,3)
min_samples_leaf = np.arange(1,11,3)
n_estimators = np.arange(15,26,3)
clf = RandomForestClassifier()
param_grid = {"max_depth": [3,4,5,6,7,8 None],
"n_estimators": n_estimators,
"max_features": max_features,
"min_samples_split": min_samples_split,
"min_samples_leaf": min_samples_leaf,
"bootstrap": [True, False],
"criterion": ["gini", "entropy"]}
# run grid search
grid_search = GridSearchCV(clf, param_grid=param_grid,scoring='f1')
grid_search.fit(X, y)
print(grid_search.best_score_)
# Decision tree
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import AdaBoostClassifier
from sklearn.grid_search import GridSearchCV
param_grid = {"base_estimator__criterion" : ["gini", "entropy"],
"base_estimator__splitter" : ["best", "random"],
"n_estimators": [1, 2]
}
DTC = DecisionTreeClassifier(random_state = 11, max_features = "auto", class_weight = "auto",max_depth = None)
ABC = AdaBoostClassifier(base_estimator = DTC)
# run grid search
grid_search_ABC = GridSearchCV(ABC, param_grid=param_grid, scoring = 'f1')
grid_search_ABC.fit(X, y)
print(grid_search_ABC.best_score_)