|
|
@@ -12,12 +12,10 @@ from sklearn.linear_model import LogisticRegression
|
|
|
from sklearn.metrics import confusion_matrix
|
|
|
from sklearn.metrics import average_precision_score
|
|
|
from sklearn.metrics import f1_score
|
|
|
-from sklearn.metrics import balanced_accuracy_score
|
|
|
from sklearn.metrics import cohen_kappa_score
|
|
|
from sklearn.ensemble import GradientBoostingClassifier
|
|
|
|
|
|
_tF1 = "f1 score"
|
|
|
-_tBalAcc = "balanced accuracy"
|
|
|
_tTN = "TN"
|
|
|
_tTP = "TP"
|
|
|
_tFN = "FN"
|
|
|
@@ -45,13 +43,12 @@ class TestResult:
|
|
|
*aps* is a real number representing the average precision score.
|
|
|
"""
|
|
|
self.title = title
|
|
|
- self.heading = [_tTN, _tTP, _tFN, _tFP, _tF1, _tBalAcc, _tCks]
|
|
|
+ self.heading = [_tTN, _tTP, _tFN, _tFP, _tF1, _tCks]
|
|
|
if aps is not None:
|
|
|
self.heading.append(_tAps)
|
|
|
self.data = { n: 0.0 for n in self.heading }
|
|
|
|
|
|
if labels is not None and prediction is not None:
|
|
|
- self.data[_tBalAcc] = balanced_accuracy_score(labels, prediction)
|
|
|
self.data[_tF1] = f1_score(labels, prediction)
|
|
|
self.data[_tCks] = cohen_kappa_score(labels, prediction)
|
|
|
conMat = self._enshureConfusionMatrix(confusion_matrix(labels, prediction))
|
|
|
@@ -74,8 +71,8 @@ class TestResult:
|
|
|
tp = self.data[_tTP]
|
|
|
fn = self.data[_tFN]
|
|
|
fp = self.data[_tFP]
|
|
|
- text += f"{self.title} tn, fp: {tn}, {tp}\n"
|
|
|
- text += f"{self.title} fn, tp: {fn}, {tp}"
|
|
|
+ text += f"{self.title} tn, fp: {tn}, {fp}\n"
|
|
|
+ text += f"{self.title} fn, tp: {fn}, {tp}\n"
|
|
|
|
|
|
for k in self.heading:
|
|
|
if k not in [_tTP, _tTN, _tFP, _tFN]:
|
|
|
@@ -161,6 +158,7 @@ def lr(ttd):
|
|
|
logreg = LogisticRegression(
|
|
|
C=1e5,
|
|
|
solver='lbfgs',
|
|
|
+ max_iter=1000,
|
|
|
multi_class='multinomial',
|
|
|
class_weight={0: 1, 1: 1.3}
|
|
|
)
|
|
|
@@ -173,25 +171,6 @@ def lr(ttd):
|
|
|
return TestResult("LR", ttd.test.labels, prediction, aps_lr)
|
|
|
|
|
|
|
|
|
-def svm(ttd):
|
|
|
- """
|
|
|
- Runs a test for a dataset with the support vector machine algorithm.
|
|
|
- It returns a /TestResult./
|
|
|
-
|
|
|
- *ttd* is a /library.dataset.TrainTestData/ instance containing data to test.
|
|
|
- """
|
|
|
- checkType(ttd)
|
|
|
- svmTester = sklearn.svm.SVC(
|
|
|
- kernel='linear',
|
|
|
- decision_function_shape='ovo',
|
|
|
- class_weight={0: 1., 1: 1.},
|
|
|
- probability=True
|
|
|
- )
|
|
|
- svmTester.fit(ttd.train.data, ttd.train.labels)
|
|
|
-
|
|
|
- prediction = svmTester.predict(ttd.test.data)
|
|
|
- return TestResult("SVM", ttd.test.labels, prediction)
|
|
|
-
|
|
|
|
|
|
def knn(ttd):
|
|
|
"""
|