space_sample.py 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. """
  4. Created on Mon Oct 5 09:50:24 2020
  5. @author: tanya
  6. """
  7. from sklearn.ensemble import RandomForestClassifier
  8. from sklearn.feature_selection import SelectPercentile
  9. from sklearn.linear_model import LogisticRegression
  10. from sklearn.decomposition import PCA
  11. from sklearn.pipeline import Pipeline
  12. from sklearn.preprocessing import StandardScaler
  13. from hyperopt import hp
  14. import numpy as np
  15. space = hp.choice("pipelines", [
  16. {"name": "std_scaler_kbest_rf",
  17. "pipeline": Pipeline([
  18. ("std_scaler", StandardScaler()),
  19. ("kbest", SelectPercentile()),
  20. ("rf", RandomForestClassifier())]),
  21. "params": {"kbest__percentile":
  22. hp.choice('kbest__percentile', range(1, 3)),
  23. "rf__n_estimators":
  24. 50 + hp.randint('rf__n_estimators', 50)}},
  25. {"name": "std_scaler_pca_lr",
  26. "pipeline": Pipeline([
  27. ("std_scaler", StandardScaler()),
  28. ("pca", PCA()),
  29. ("lr", LogisticRegression())]),
  30. "params": {"lr__C":
  31. hp.loguniform("lr__C", np.log(0.01), np.log(0.1)),
  32. "pca__n_components":
  33. 1 + hp.randint("pca__n_components", 4)}}
  34. ])