Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Create pipeline
- pipeline = Pipeline([('scaler', MinMaxScaler()), ('kbest', SelectKBest()), ('SVC', SVC())])
- # grid search params
- grid_search = GridSearchCV(pipeline, cv=3, param_grid={'kbest__k':[1,2,3,4,5,6,7,8,9,10],
- 'SVC__C': np.logspace(-3, 2, 6), 'SVC__gamma': np.logspace(-3, 2, 6)})
- # fit the grid search
- grid_search.fit(X_train,y_train)
- print grid_search.best_params_
- # assign optimal parameters' values to a variable
- optimal_gamma = grid_search.best_params_.values()[0]
- optimal_k = grid_search.best_params_.values()[1]
- optimal_C = grid_search.best_params_.values()[2]
- # update pipeline with optimal parameter values
- pipeline.set_params(SVC__gamma= optimal_gamma,kbest__k=optimal_k, SVC__C=optimal_C).fit(X_train, y_train)
- features_list2 = ['salary', 'deferral_payments', 'total_payments',
- 'exercised_stock_options', 'bonus', 'restricted_stock',
- 'restricted_stock_deferred', 'total_stock_value', 'expenses',
- 'loan_advances', 'other', 'director_fees', 'deferred_income',
- 'long_term_incentive']
- # get best features in indices
- best_feats = pipeline.named_steps['kbest'].get_support(indices=True)
- # print best features
- for i in best_feats:
- print features_list2[i]
- # predict training and testing features
- y_pred_train = pipeline.predict(X_train)
- y_pred_test = pipeline.predict(X_test)
- # print training predictions' metrics
- print confusion_matrix(y_train, y_pred_train)
- print classification_report(y_train, y_pred_train)
- # print testing predictions' metrics
- print confusion_matrix(y_test, y_pred_test)
- print classification_report(y_test, y_pred_test)
- # OUTPUT:
- {'SVC__gamma': 10.0, 'kbest__k': 1, 'SVC__C': 1.0}
- bonus
- [[87 0]
- [10 3]]
- precision recall f1-score support
- 0.0 0.90 1.00 0.95 87
- 1.0 1.00 0.23 0.38 13
- avg / total 0.91 0.90 0.87 100
- [[38 1]
- [ 5 0]]
- precision recall f1-score support
- 0.0 0.88 0.97 0.93 39
- 1.0 0.00 0.00 0.00 5
- avg / total 0.78 0.86 0.82 44
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement