voting classifier grid search

Solutions on MaxInterview for voting classifier grid search by the best coders in the world

showing results for - "voting classifier grid search"
Edoardo
16 Nov 2018
1from sklearn.linear_model import LogisticRegression
2from sklearn.svm import SVC
3from sklearn.ensemble import VotingClassifier
4from sklearn.model_selection import GridSearchCV
5
6X = np.array([[-1.0, -1.0], [-1.2, -1.4], [-3.4, -2.2], [1.1, 1.2],[-1.0, -1.0], [-1.2, -1.4], [-3.4, -2.2], [1.1, 1.2]])
7y = np.array([1, 1, 2, 2,1, 1, 2, 2])
8
9eclf = VotingClassifier(estimators=[ 
10    ('svm', SVC(probability=True)),
11    ('lr', LogisticRegression()),
12    ], voting='soft')
13
14#Use the key for the classifier followed by __ and the attribute
15params = {'lr__C': [1.0, 100.0],
16      'svm__C': [2,3,4],}
17
18grid = GridSearchCV(estimator=eclf, param_grid=params, cv=2)
19
20grid.fit(X,y)
21
22print (grid.best_params_)
23#{'lr__C': 1.0, 'svm__C': 2}
24