/
logistic_regressor_impl.dart
136 lines (118 loc) · 4.33 KB
/
logistic_regressor_impl.dart
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import 'package:json_annotation/json_annotation.dart';
import 'package:ml_algo/src/classifier/_mixins/assessable_classifier_mixin.dart';
import 'package:ml_algo/src/classifier/_mixins/linear_classifier_mixin.dart';
import 'package:ml_algo/src/classifier/logistic_regressor/logistic_regressor.dart';
import 'package:ml_algo/src/classifier/logistic_regressor/logistic_regressor_json_keys.dart';
import 'package:ml_algo/src/common/serializable/serializable_mixin.dart';
import 'package:ml_algo/src/helpers/validate_class_labels.dart';
import 'package:ml_algo/src/helpers/validate_coefficients_matrix.dart';
import 'package:ml_algo/src/helpers/validate_probability_threshold.dart';
import 'package:ml_algo/src/link_function/helpers/from_link_function_json.dart';
import 'package:ml_algo/src/link_function/helpers/link_function_to_json.dart';
import 'package:ml_algo/src/link_function/link_function.dart';
import 'package:ml_dataframe/ml_dataframe.dart';
import 'package:ml_linalg/dtype.dart';
import 'package:ml_linalg/dtype_to_json.dart';
import 'package:ml_linalg/from_dtype_json.dart';
import 'package:ml_linalg/from_matrix_json.dart';
import 'package:ml_linalg/matrix.dart';
import 'package:ml_linalg/matrix_to_json.dart';
part 'logistic_regressor_impl.g.dart';
@JsonSerializable()
class LogisticRegressorImpl
with
LinearClassifierMixin,
AssessableClassifierMixin,
SerializableMixin
implements
LogisticRegressor {
LogisticRegressorImpl(
this.targetNames,
this.linkFunction,
this.fitIntercept,
this.interceptScale,
this.coefficientsByClasses,
this.probabilityThreshold,
this.negativeLabel,
this.positiveLabel,
this.costPerIteration,
this.dtype,
) {
validateProbabilityThreshold(probabilityThreshold);
validateClassLabels(positiveLabel, negativeLabel);
validateCoefficientsMatrix(coefficientsByClasses);
// Logistic regression specific check, it cannot be placed in
// `validateCoefficientsMatrix`
if (coefficientsByClasses.columnsNum > 1) {
throw Exception('Expected coefficients for a single class, but '
'coefficients for ${coefficientsByClasses.columnsNum} classes '
'provided. Please, check your linear optimizer implementation '
'(Logistic Regression deals only with single class problem)');
}
}
factory LogisticRegressorImpl.fromJson(Map<String, dynamic> json) =>
_$LogisticRegressorImplFromJson(json);
@override
Map<String, dynamic> toJson() => _$LogisticRegressorImplToJson(this);
/// N x 1 matrix, where N - number of features. It has only one column since
/// in case of Logistic Regression only one class is used
@override
@JsonKey(
name: logisticRegressorCoefficientsByClassesJsonKey,
toJson: matrixToJson,
fromJson: fromMatrixJson,
)
final Matrix coefficientsByClasses;
@override
@JsonKey(name: logisticRegressorClassNamesJsonKey)
final Iterable<String> targetNames;
@override
@JsonKey(name: logisticRegressorFitInterceptJsonKey)
final bool fitIntercept;
@override
@JsonKey(name: logisticRegressorInterceptScaleJsonKey)
final num interceptScale;
@override
@JsonKey(
name: logisticRegressorDTypeJsonKey,
toJson: dTypeToJson,
fromJson: fromDTypeJson,
)
final DType dtype;
@JsonKey(name: logisticRegressorProbabilityThresholdJsonKey)
final num probabilityThreshold;
@override
@JsonKey(name: logisticRegressorPositiveLabelJsonKey)
final num positiveLabel;
@override
@JsonKey(name: logisticRegressorNegativeLabelJsonKey)
final num negativeLabel;
@override
@JsonKey(
name: logisticRegressorLinkFunctionJsonKey,
toJson: linkFunctionToJson,
fromJson: fromLinkFunctionJson,
)
final LinkFunction linkFunction;
@override
@JsonKey(
name: logisticRegressorCostPerIterationJsonKey,
includeIfNull: false,
)
final List<num> costPerIteration;
@override
DataFrame predict(DataFrame testFeatures) {
final predictedLabels = getProbabilitiesMatrix(testFeatures)
.mapColumns(
(column) => column.mapToVector(
(probability) => probability >= probabilityThreshold
? positiveLabel.toDouble()
: negativeLabel.toDouble()
),
);
return DataFrame.fromMatrix(
predictedLabels,
header: targetNames,
);
}
}