Advertisement
Guest User

Untitled

a guest
Jan 20th, 2018
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.20 KB | None | 0 0
  1. # Create pipeline
  2. pipeline = Pipeline([('scaler', MinMaxScaler()), ('kbest', SelectKBest()), ('SVC', SVC())])
  3. # grid search params
  4. grid_search = GridSearchCV(pipeline, cv=3, param_grid={'kbest__k':[1,2,3,4,5,6,7,8,9,10],
  5.                                                       'SVC__C': np.logspace(-3, 2, 6), 'SVC__gamma': np.logspace(-3, 2, 6)})
  6. # fit the grid search
  7. grid_search.fit(X_train,y_train)
  8.  
  9. print grid_search.best_params_
  10. # assign optimal parameters' values to a variable
  11. optimal_gamma = grid_search.best_params_.values()[0]
  12. optimal_k = grid_search.best_params_.values()[1]
  13. optimal_C =  grid_search.best_params_.values()[2]
  14.  
  15. # update pipeline with optimal parameter values
  16. pipeline.set_params(SVC__gamma= optimal_gamma,kbest__k=optimal_k, SVC__C=optimal_C).fit(X_train, y_train)
  17.  
  18.  
  19. features_list2 = ['salary', 'deferral_payments', 'total_payments',
  20.        'exercised_stock_options', 'bonus', 'restricted_stock',
  21.        'restricted_stock_deferred', 'total_stock_value', 'expenses',
  22.        'loan_advances', 'other', 'director_fees', 'deferred_income',
  23.        'long_term_incentive']
  24.  
  25. # get best features in indices
  26. best_feats = pipeline.named_steps['kbest'].get_support(indices=True)
  27. # print best features
  28. for i in best_feats:
  29.     print features_list2[i]
  30.  
  31. # predict training and testing features
  32. y_pred_train = pipeline.predict(X_train)
  33. y_pred_test = pipeline.predict(X_test)
  34.  
  35. # print training predictions' metrics
  36. print confusion_matrix(y_train, y_pred_train)
  37. print classification_report(y_train, y_pred_train)
  38.  
  39. # print testing predictions' metrics
  40. print confusion_matrix(y_test, y_pred_test)
  41. print classification_report(y_test, y_pred_test)
  42.  
  43.  
  44. # OUTPUT:
  45.  
  46. {'SVC__gamma': 10.0, 'kbest__k': 1, 'SVC__C': 1.0}
  47. bonus
  48. [[87  0]
  49.  [10  3]]
  50.              precision    recall  f1-score   support
  51.  
  52.         0.0       0.90      1.00      0.95        87
  53.         1.0       1.00      0.23      0.38        13
  54.  
  55. avg / total       0.91      0.90      0.87       100
  56.  
  57. [[38  1]
  58.  [ 5  0]]
  59.              precision    recall  f1-score   support
  60.  
  61.         0.0       0.88      0.97      0.93        39
  62.         1.0       0.00      0.00      0.00         5
  63.  
  64. avg / total       0.78      0.86      0.82        44
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement