Advertisement
Guest User

Untitled

a guest
Nov 20th, 2019
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.25 KB | None | 0 0
  1. #Split the data into training (80%) and testing (20%) sets
  2. X_train, X_test, y_train, y_test = train_test_split(X, y,test_size=0.20,random_state=42)
  3.  
  4. #Function for hyperparameter optimization
  5. def fitAndScore(logL1):
  6. #Hyperparamter to be optimized is the log of the l1 penalty term
  7. l1 = np.exp(logL1)
  8. #Instatiate and fit the lasso model to the training data
  9. model = LogisticRegression(C=l1,penalty='l1',solver='liblinear')
  10. model.fit(X_train,y_train)
  11. #Use the trained model to predict the probability that y=1 in the test data
  12. y_pred = model.predict_proba(X_test)[:,1]
  13. #Objective to maximize is the negative log loss (binary cross entropy) on the testing data
  14. obj = -log_loss(y_test,y_pred)
  15. return obj
  16.  
  17. #Set the bounds for optimizing the l1 penalty parameter
  18. pb = [np.log(1.0E-4),np.log(10000000.)]
  19. pbounds = {'logL1': list(pb)}
  20. #Instatiate a bayesian optimization (basically just optimization with a gaussian process surrogate model)
  21. optimizer = BayesianOptimization(f=fitAndScore,pbounds=pbounds,verbose=2,random_state=1)
  22. #Run the optimizer (this is totally overkill for this problem and it's good enough to just do a grid search for l1 but this is just to show how things work)
  23. optimizer.maximize(init_points=100,n_iter=0)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement