-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathLR_hyperparameter_tune.py
More file actions
43 lines (31 loc) · 1.1 KB
/
LR_hyperparameter_tune.py
File metadata and controls
43 lines (31 loc) · 1.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# -*- coding: utf-8 -*-
"""
Created on Tue Mar 17 20:32:59 2020
@author: ayanca
"""
# Load libraries
import numpy as np
from sklearn import linear_model, datasets
from sklearn.model_selection import GridSearchCV
def RFR_tune(X,y,fold):
iris = datasets.load_iris()
X = iris.data
y = iris.target
# Create logistic regression
logistic = linear_model.LogisticRegression()
# Create regularization penalty space
penalty = ['l1', 'l2']
# Create regularization hyperparameter space
C = np.logspace(0, 4, 10)
# Create hyperparameter options
hyperparameters = dict(C=C, penalty=penalty)
# Create grid search using 5-fold cross validation
clf = GridSearchCV(logistic, hyperparameters, cv=fold, verbose=0)
# Fit grid search
best_model = clf.fit(X, y)
# View best hyperparameters
print('Best Penalty:', best_model.best_estimator_.get_params()['penalty'])
print('Best C:', best_model.best_estimator_.get_params()['C'])
# Predict target vector
best_model.predict(X)
#RFR_tune(3,3,5)