Skip to content

Commit 553bcc2

Browse files
committed
Adds logistic regression hyperparams
Fixes #5
1 parent a6ef238 commit 553bcc2

File tree

2 files changed

+268
-18
lines changed

2 files changed

+268
-18
lines changed

static/js/dataspace.js

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,16 @@ class Dataspace {
122122
$("#metrics").removeClass("visible").addClass("invisible");
123123
}
124124

125+
// Count the number of classes in the dataset
126+
classes() {
127+
return this.dataset.reduce(function(acc, val) {
128+
if (!acc.includes(val.c)) {
129+
acc.push(val.c);
130+
}
131+
return acc;
132+
}, []);
133+
}
134+
125135
}
126136

127137
$(document).ready(function() {
@@ -270,4 +280,82 @@ $(document).ready(function() {
270280

271281
});
272282

283+
// Enable the correct hyperparameters based on the selected logistic solver
284+
$('#logit select[name="solver"]').change(function(e) {
285+
286+
// Disable all the penalties
287+
$('#logit input[name="penalty"]').each(function(_, elem) {
288+
$(elem).prop("disabled", true);
289+
});
290+
291+
// Disable dual -- this only works with liblinear and l2
292+
$('#logit input[name="dual"]').prop("disabled", true);
293+
294+
// Disable intercept_scaling -- this only works with liblinear and fit_intercept=True
295+
$('#logit input[name="intercept_scaling"]').prop("disabled", true);
296+
297+
// Disable l1_ratio -- this only works with SAGA and penalty=elasticnet
298+
$('#logit input[name="l1_ratio"]').prop("disabled", true);
299+
300+
// Enable/Disable based on the selected penalty
301+
switch ($(this).val()) {
302+
case "newton-cg":
303+
case "sag":
304+
case "lbfgs":
305+
$('#logit input[name="penalty"]#penalty2').prop("disabled", false);
306+
$('#logit input[name="penalty"]#penalty4').prop("disabled", false);
307+
break;
308+
case "liblinear":
309+
$('#logit input[name="penalty"]').each(function (_, elem) {
310+
elem = $(elem);
311+
if (elem.val() != 'none' && elem.val() != 'elasticnet') {
312+
elem.prop("disabled", false);
313+
}
314+
});
315+
if ($('#logit input[name="penalty"]:checked').val() == 'l2') {
316+
$('#logit input[name="dual"]').prop("disabled", false);
317+
}
318+
if ($('#logit input[name="penalty"]:checked').val() == 'elasticnet') {
319+
$('#logit input[name="l1_ratio"]').prop("disabled", false);
320+
}
321+
$('#logit input[name="intercept_scaling"]').prop("disabled", false);
322+
break;
323+
case "saga":
324+
$('#logit input[name="penalty"]').each(function (_, elem) {
325+
$(elem).prop("disabled", false);
326+
});
327+
break;
328+
default:
329+
console.log("unknown solver selected, cannot enable form!");
330+
}
331+
332+
})
333+
334+
// Enable the correct hyperparameters based on the selected logistic penalty
335+
$('#logit input[name="penalty"]').change(function(e) {
336+
// Disable dual -- this only works with liblinear and l2
337+
$('#logit input[name="dual"]').prop("disabled", true);
338+
339+
// Disable l1_ratio -- this only works with elasticnet
340+
$('#logit input[name="l1_ratio"]').prop("disabled", true);
341+
342+
// Enable/Disable based on the selected kernel
343+
switch ($(this).val()) {
344+
case "l1":
345+
break
346+
case "l2":
347+
if ($('#logit select[name="solver"]').val() == "liblinear") {
348+
$('#logit input[name="dual"]').prop("disabled", false);
349+
}
350+
break
351+
case "elasticnet":
352+
$('#logit input[name="l1_ratio"]').prop("disabled", false);
353+
case "none":
354+
break
355+
default:
356+
console.log($(this).val());
357+
console.log("unknown penalty, cannot correctly enable form!");
358+
}
359+
});
360+
273361
});

templates/index.html

Lines changed: 180 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@
284284
</div>
285285
</div>
286286
<div class="form-group form-row mb-1">
287-
<label for="decision_function_shape" class="col-form-label col-md-4">Decision Shape</label>
287+
<label for="decision_function_shape" class="col-form-label col-md-4 my-1">Decision Shape</label>
288288
<div class="col-md-8">
289289
<select class="custom-select my-1 mr-sm-2" name="decision_function_shape" id="decision_function_shape">
290290
<option value="ovr" selected>One v. Rest</option>
@@ -297,9 +297,118 @@
297297
</form>
298298
</div>
299299
<div class="tab-pane fade" id="logit" role="tabpanel">
300-
<p>Logistic Regression</p>
301300
<form class="form">
302301
<input type="hidden" name="model" value="logit" />
302+
<div class="row">
303+
<div class="col-md-3">
304+
<div class="form-group form-row mb-1">
305+
<label for="solver" class="col-form-label col-md-4">Solver</label>
306+
<div class="col-md-8">
307+
<select class="custom-select my-1 mr-sm-2" name="solver" id="solver">
308+
<option value="newton-cg">Newton-CG</option>
309+
<option value="lbfgs">LBFGS</option>
310+
<option value="liblinear" selected>LibLinear</option>
311+
<option value="sag">SAG</option>
312+
<option value="saga">SAGA</option>
313+
</select>
314+
</div>
315+
</div>
316+
<div class="form-row">
317+
<label for="penalty" class="col-form-label col-md-4">Penalty</label>
318+
<div class="col-md-8">
319+
<div class="form-check">
320+
<input class="form-check-input" type="radio" name="penalty" id="penalty1" value="l1">
321+
<label class="form-check-label" for="penalty1">
322+
L1
323+
</label>
324+
</div>
325+
<div class="form-check">
326+
<input class="form-check-input" type="radio" name="penalty" id="penalty2" value="l2" checked>
327+
<label class="form-check-label" for="penalty2">
328+
L2
329+
</label>
330+
</div>
331+
<div class="form-check">
332+
<input class="form-check-input" type="radio" name="penalty" id="penalty3" value="elasticnet" disabled>
333+
<label class="form-check-label" for="penalty3">
334+
ElasticNet
335+
</label>
336+
</div>
337+
<div class="form-check">
338+
<input class="form-check-input" type="radio" name="penalty" id="penalty4" value="none" disabled>
339+
<label class="form-check-label" for="penalty4">
340+
None
341+
</label>
342+
</div>
343+
</div>
344+
</div>
345+
</div>
346+
<div class="col-md-3">
347+
<div class="form-group form-row mb-1">
348+
<label for="C" class="col-form-label col-md-5">C</label>
349+
<div class="col-md-7">
350+
<input class="form-control" type="text" name="C" id="C" value="1.0" />
351+
</div>
352+
</div>
353+
<div class="form-group form-row mb-1">
354+
<label for="intercept_scaling" class="col-form-label col-md-5">Scale Intercept</label>
355+
<div class="col-md-7">
356+
<input class="form-control" type="text" name="intercept_scaling" id="intercept_scaling" value="1.0" />
357+
</div>
358+
</div>
359+
<div class="form-group form-row mb-1">
360+
<label for="l1_ratio" class="col-form-label col-md-5">L1 Ratio</label>
361+
<div class="col-md-7">
362+
<input class="form-control" type="text" name="l1_ratio" id="l1_ratio" value="0.5" disabled />
363+
</div>
364+
</div>
365+
</div>
366+
<div class="col-md-3">
367+
<div class="row">
368+
<div class="col-md-4"></div>
369+
<div class="col-md-8">
370+
<div class="form-check">
371+
<input class="form-check-input" type="checkbox" name="fit_intercept" id="fit_intercept" checked>
372+
<label class="form-check-label" for="fit_intercept">Fit Intercept</label>
373+
</div>
374+
<div class="form-check">
375+
<input class="form-check-input" type="checkbox" name="dual" id="dual">
376+
<label class="form-check-label" for="dual">Dual</label>
377+
</div>
378+
</div>
379+
</div>
380+
<div class="form-group form-row mb-1">
381+
<label for="multi_class" class="col-form-label col-md-4 my-1">Multi-Class</label>
382+
<div class="col-md-8">
383+
<select class="custom-select my-1 mr-sm-2" name="multi_class" id="multi_class">
384+
<option value="auto" selected>Auto</option>
385+
<option value="ovr">One v. Rest</option>
386+
<option value="multinomial">Multinomial</option>
387+
</select>
388+
</div>
389+
</div>
390+
</div>
391+
<div class="col-md-3">
392+
<div class="form-group form-row mb-1">
393+
<label for="class_weight" class="col-form-label col-md-5">Class Weight</label>
394+
<div class="col-md-7">
395+
<input class="form-control" type="text" name="class_weight" id="class_weight" value="null" />
396+
</div>
397+
</div>
398+
<div class="form-group form-row mb-1">
399+
<label for="tol" class="col-form-label col-md-5">Tol</label>
400+
<div class="col-md-7">
401+
<input class="form-control" type="text" name="tol" id="tol" value="0.0001" />
402+
</div>
403+
</div>
404+
<div class="form-group form-row mb-1">
405+
<label for="max_iter" class="col-form-label col-md-5">Max Iter</label>
406+
<div class="col-md-7">
407+
<input class="form-control" type="number" name="max_iter" id="max_iter" value="100" step="10" min="10" max="10000" />
408+
</div>
409+
</div>
410+
</div>
411+
</div>
303412
</form>
304413
</div>
305414
</div><!-- model tabs ends -->
@@ -441,35 +550,35 @@ <h5 class="modal-title" id="svmInfoModalLabel">Support Vector Machines</h5>
441550
</p>
442551
<h6>Hyperparameters</h6>
443552
<dl>
444-
<dt>C <code>float</code></dt>
553+
<dt>C &middot; <code>float</code></dt>
445554
<dd>Penalty parameter C of the error term.</dd>
446-
<dt>kernel <code>string</code></dt>
555+
<dt>kernel &middot; <code>{'linear', 'poly', 'rbf', 'sigmoid', 'precomputed', None}</code></dt>
447556
<dd>
448-
Specifies the kernel type to be used in the algorithm. It must be one of ‘linear’, ‘poly’,
449-
‘rbf’, ‘sigmoid’, ‘precomputed’ or a callable. If none is given, ‘rbf’ will be used. If a
450-
callable is given it is used to pre-compute the kernel matrix from data matrices; that
557+
Specifies the kernel type to be used in the algorithm. It must be one of the string
558+
choices or a callable. If None is given, ‘rbf’ will be used. If a callable is given
559+
it is used to pre-compute the kernel matrix from data matrices; that
451560
matrix should be an array of shape <code>(n_samples, n_samples)</code>.
452561
</dd>
453-
<dt>degree <code>int</code></dt>
562+
<dt>degree &middot; <code>int</code></dt>
454563
<dd>Degree of the polynomial kernel function (‘poly’). Ignored by all other kernels.</dd>
455-
<dt>gamma <code>float</code></dt>
564+
<dt>gamma &middot; <code>float</code></dt>
456565
<dd>Kernel coefficient for ‘rbf’, ‘poly’ and ‘sigmoid’.</dd>
457-
<dt>coef0 <code>float</code></dt>
566+
<dt>coef0 &middot; <code>float</code></dt>
458567
<dd>Independent term in kernel function. It is only significant in ‘poly’ and ‘sigmoid’.</dd>
459-
<dt>shrinking <code>boolean</code></dt>
568+
<dt>shrinking &middot; <code>boolean</code></dt>
460569
<dd>Whether to use the shrinking heuristic.</dd>
461-
<dt>tol <code>float</code></dt>
570+
<dt>tol &middot; <code>float</code></dt>
462571
<dd>Tolerance for stopping criterion.</dd>
463-
<dt>class_weight <code>{dict, 'balanced'}</code></dt>
572+
<dt>class_weight &middot; <code>{dict, 'balanced'}</code></dt>
464573
<dd>
465574
Set the parameter C of class i to class_weight[i]*C for SVC. If not given, all classes are
466575
supposed to have weight one. The “balanced” mode uses the values of y to automatically adjust
467576
weights inversely proportional to class frequencies in the input data as
468577
<code>n_samples / (n_classes * np.bincount(y))</code>
469578
</dd>
470-
<dt>max_iter <code>int</code></dt>
579+
<dt>max_iter &middot; <code>int</code></dt>
471580
<dd>Hard limit on iterations within solver, or -1 for no limit.</dd>
472-
<dt>decision_function_shape <code>{‘ovo’, ‘ovr’}</code></dt>
581+
<dt>decision_function_shape &middot; <code>{‘ovo’, ‘ovr’}</code></dt>
473582
<dd>
474583
Whether to return a one-vs-rest (‘ovr’) decision function of shape <code>(n_samples, n_classes)</code>
475584
as all other classifiers, or the original one-vs-one (‘ovo’) decision function of libsvm which has shape
@@ -496,12 +605,65 @@ <h5 class="modal-title" id="logitInfoModalLabel">Logistic Regression</h5>
496605
</button>
497606
</div>
498607
<div class="modal-body">
499-
<p>TODO</p>
608+
<p>
609+
<a href="https://scikit-learn.org/stable/modules/linear_model.html#logistic-regression" target="_blank">
610+
Logistic Regression</a> is a supervised classification algorithm that models the probabilities
611+
describing the possible outcome (class) of a single trial using a logistic function. This method
612+
is also known as a logit regression, maximum-entropy classifier, or log-linear classifier.
613+
</p>
500614

501615
<h6>Hyperparameters</h6>
502616
<dl>
503-
<dt>param <code>type</code></dt>
504-
<dd>description</dd>
617+
<dt>penalty &middot; <code>{'l1', 'l2', 'elasticnet', 'none'}</code></dt>
618+
<dd>
619+
Used to specify the norm used in the penalization. The ‘newton-cg’, ‘sag’ and ‘lbfgs’ solvers
620+
support only l2 penalties. ‘elasticnet’ is only supported by the ‘saga’ solver. If ‘none’ (not
621+
supported by the liblinear solver), no regularization is applied.
622+
</dd>
623+
<dt>dual &middot; <code>bool</code></dt>
624+
<dd>
625+
Dual or primal formulation. Dual formulation is only implemented for l2 penalty with liblinear
626+
solver. Prefer <code>dual=False</code> when <code>n_samples > n_features</code>.
627+
</dd>
628+
<dt>tol &middot; <code>float</code></dt>
629+
<dd>Tolerance for stopping criteria.</dd>
630+
<dt>C &middot; <code>float</code></dt>
631+
<dd>
632+
Inverse of regularization strength; must be a positive float. Like in support vector machines,
633+
smaller values specify stronger regularization.
634+
</dd>
635+
<dt>fit_intercept &middot; <code>bool</code></dt>
636+
<dd>Specifies if a constant (a.k.a. bias or intercept) should be added to the decision function.</dd>
637+
<dt>intercept_scaling &middot; <code>float</code></dt>
638+
<dd>
639+
Useful only when the solver ‘liblinear’ is used and self.fit_intercept is set to True. In this case,
640+
x becomes <code>[x, self.intercept_scaling]</code>, i.e. a “synthetic” feature with constant value
641+
equal to intercept_scaling is appended to the instance vector.
642+
</dd>
643+
<dt>class_weight &middot; <code>{dict, 'balanced'}</code></dt>
644+
<dd>
645+
Weights associated with classes in the form {class_label: weight}. If not given, all classes are
646+
supposed to have weight one. The “balanced” mode uses the values of y to automatically adjust weights
647+
inversely proportional to class frequencies in the input data as
648+
<code>n_samples / (n_classes * np.bincount(y)).</code>
649+
</dd>
650+
<dt>solver &middot; <code>{'newton-cg', 'lbfgs', 'liblinear', 'sag', 'saga'}</code></dt>
651+
<dd>Algorithm to use in the optimization problem.</dd>
652+
<dt>max_iter &middot; <code>int</code></dt>
653+
<dd>Maximum number of iterations taken for the solvers to converge.</dd>
654+
<dt>multi_class &middot; <code>{'ovr', 'multinomial', 'auto'}</code></dt>
655+
<dd>
656+
If the option chosen is ‘ovr’, then a binary problem is fit for each label. For ‘multinomial’ the
657+
loss minimised is the multinomial loss fit across the entire probability distribution, even when the
658+
data is binary. ‘multinomial’ is unavailable when solver=’liblinear’. ‘auto’ selects ‘ovr’ if the
659+
data is binary, or if solver=’liblinear’, and otherwise selects ‘multinomial’.
660+
</dd>
661+
<dt>l1_ratio &middot; <code>float</code></dt>
662+
<dd>
663+
The Elastic-Net mixing parameter, with <code>0 <= l1_ratio <=1</code>. Only used if <code>penalty='elasticnet'</code>.
664+
Setting <code>l1_ratio=0</code> is equivalent to using <code>penalty='l2'</code>, while setting <code>l1_ratio=1</code>
665+
is equivalent to using <code>penalty='l1'</code>. For <code>0 < l1_ratio < 1</code>, the penalty is a combination of L1 and L2.
666+
</dd>
505667
</dl>
506668
</div>
507669
<div class="modal-footer">

0 commit comments

Comments
 (0)