Skip to content

Commit

Permalink
Q2. add logistic regression
Browse files Browse the repository at this point in the history
  • Loading branch information
ipranjal committed Dec 2, 2023
1 parent 0ec7fa8 commit 44148a8
Show file tree
Hide file tree
Showing 16 changed files with 23 additions and 2 deletions.
4 changes: 4 additions & 0 deletions digits.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@
max_depth = [5,10,15,20,50,100]
classifer_hparam['tree'] = [{'max_depth': depth} for depth in max_depth]

solvers = ['lbfgs', 'liblinear', 'newton-cg', 'sag', 'saga']
classifer_hparam['lr'] = [{'solver': solver} for solver in solvers]


# Predict the value of the digit on the test subset
# 6.Predict and Evaluate
for model in models:
Expand Down
Binary file added models/m23csa018_lr_lbfgs.joblib
Binary file not shown.
Binary file added models/m23csa018_lr_liblinear.joblib
Binary file not shown.
Binary file added models/m23csa018_lr_newton-cg.joblib
Binary file not shown.
Binary file added models/m23csa018_lr_sag.joblib
Binary file not shown.
Binary file added models/m23csa018_lr_saga.joblib
Binary file not shown.
Binary file added models/svmgamma:0.001_C:1.joblib
Binary file not shown.
Binary file added models/svmgamma:0.001_C:2.joblib
Binary file not shown.
Binary file added models/svmgamma:0.001_C:5.joblib
Binary file not shown.
Binary file added models/svmgamma:1_C:1.joblib
Binary file not shown.
Binary file added models/treemax_depth:10.joblib
Binary file not shown.
Binary file added models/treemax_depth:100.joblib
Binary file not shown.
Binary file added models/treemax_depth:15.joblib
Binary file not shown.
Binary file added models/treemax_depth:20.joblib
Binary file not shown.
Binary file added models/treemax_depth:50.joblib
Binary file not shown.
21 changes: 19 additions & 2 deletions utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

# Import datasets, classifiers and performance metrics
from sklearn import svm,datasets
from sklearn import svm,datasets,linear_model
from sklearn.model_selection import train_test_split
from sklearn import tree
import matplotlib.pyplot as plt
Expand Down Expand Up @@ -37,6 +37,8 @@ def train_model(X, y, model_params,model_type = 'svm'):
clf = svm.SVC(**model_params)
if model_type == 'tree':
clf = tree.DecisionTreeClassifier(**model_params)
if model_type == 'lr':
clf = linear_model.LogisticRegression(**model_params)
clf.fit(X, y)
return clf

Expand All @@ -62,18 +64,33 @@ def tune_hparams(X_train, Y_train, X_dev, y_dev, list_of_all_param_combination,
cur_model = train_model(X_train, Y_train, {'gamma': param_combination['gamma'],'C':param_combination['C']}, model_type='svm')
if model_type == 'tree':
cur_model = train_model(X_train, Y_train, {'max_depth': param_combination['max_depth']}, model_type='tree')
if model_type == 'lr':
cur_model = train_model(X_train, Y_train, {'solver': param_combination['solver']}, model_type='lr')

cur_accuracy,_ = predict_and_eval(cur_model, X_dev, y_dev)
if model_type == 'lr':
curr_model_path = "./models/m23csa018_{}".format(model_type)+"_{}".format(param_combination['solver'])+".joblib"
dump(cur_model,curr_model_path)
print(f"For model {model_type} and solver {param_combination['solver']} accuracy is {cur_accuracy}")

if cur_accuracy > best_accuracy_so_far:
best_accuracy_so_far = cur_accuracy
if model_type == 'svm':
optimal_gamma = param_combination['gamma']
optimal_C = param_combination['C']
best_hparams = {'gamma': optimal_gamma,'C':optimal_C}
best_model_path = "./models/{}".format(model_type)+"_".join(["{}:{}".format(k,v) for k,v in best_hparams.items()])+".joblib"

if model_type == 'tree':
optimal_max_depth = param_combination['max_depth']
best_hparams = {'max_depth': optimal_max_depth}
best_model_path = "./models/{}".format(model_type)+"_".join(["{}:{}".format(k,v) for k,v in best_hparams.items()])+".joblib"
best_model_path = "./models/{}".format(model_type)+"_".join(["{}:{}".format(k,v) for k,v in best_hparams.items()])+".joblib"

if model_type == 'lr':
optimal_solver = param_combination['solver']
best_hparams = {'solver': optimal_solver}
best_model_path = "./models/m23csa018_{}".format(model_type)+"_".join(["_{}".format(v) for k,v in best_hparams.items()])+".joblib"


best_model = cur_model

Expand Down

0 comments on commit 44148a8

Please sign in to comment.