Esempio n. 1
0
def test_aggregation():
    test_decisions = np.array([0, 0, 1, 1, 0, 1, 0, 1, 1, 0, 0, 1])
    given_p = np.array([[1 / 3, 2 / 5, 3 / 7], [1 / 3, 3 / 5, 4 / 7],
                        [1, 3 / 5, 5 / 7]])
    fuzzy_sets = np.zeros((3, 2))
    f = KNNAlgorithmF(aggregation=A1Aggregation())
    uncertainty_intervals = np.zeros(shape=(3, 2))
    expected_uncertainty_intervals = np.array([[0.3873, 0.3873],
                                               [0.5015, 0.5015],
                                               [0.7714, 0.7714]])
    expected_final_ps = np.array([0.3873, 0.5015, 0.7714])
    for i in range(3):
        k = 0
        for j in range(len((3, 5, 7))):
            fuzzy_sets[k] = np.array([given_p[i, j], given_p[i, j]])
            k += 1
        uncertainty_interval = f.aggregation.aggregate(fuzzy_sets)
        uncertainty_intervals[i] = uncertainty_interval
        print(uncertainty_interval)
        final_p = uncertainty_interval.sum() / 2
        print(final_p)
    assert_array_equal(uncertainty_intervals, expected_uncertainty_intervals)
Esempio n. 2
0
y = LabelEncoder().fit_transform(y)

X = MinMaxScaler().fit_transform(X)

missing = (0.0, 0.01, 0.05, 0.1, 0.2, 0.3, 0.4, 0.5)
dfs = []
ind = 0
ks = [(2, 4), (3, 5)]
for miss in missing:
    for agg in (A1Aggregation(), A2Aggregation(), A3Aggregation(), A4Aggregation(p=3), A5Aggregation(), A6Aggregation(),
                A7Aggregation(), A8Aggregation(), A9Aggregation(), A10Aggregation()):
        for r in (2, 5, 10):
            for k in ks:
                X_missing = MissingValuesInserterColumnsIndependent(columns=range(X.shape[1]), nan_representation=-1, percentage=miss)\
                    .fit_transform(X)
                multiclassF = OneVsRestClassifierForRandomBinaryClassifier(KNNAlgorithmF(missing_representation=-1, r=r, aggregation=agg, k_neighbours=k))
                f_result = cross_validate(multiclassF, X_missing, y, scoring='roc_auc_ovo', return_estimator=True, cv=10)
                w = pd.DataFrame({'algorithm': 'f', 'k': str(k), 'r': f_result['estimator'][0].estimator.r,
                              'agg': Aggregation.change_aggregation_to_name(f_result['estimator'][0].estimator.aggregation), 'missing': miss,
                                  'auc': np.mean(f_result['test_score']), 'stddev': stdev(f_result['test_score'])}, index=[ind])
                print(w)
                dfs.append(w)
                ind += 1
                concatenated = pd.concat(dfs)

concatenated = pd.concat(dfs)
concatenated.to_excel('concatenated_vertebral.xlsx')

for miss in missing:
    for k in ks:
        X_missing = MissingValuesInserterColumnsIndependent(columns=range(X.shape[1]), nan_representation=-1, percentage=miss) \
missing = (0.0, 0.01, 0.05, 0.1, 0.2, 0.3, 0.4, 0.5)
dfs = []
ind = 0
ks = [(3, 5), (2, 4)]
for miss in missing:
    for agg in (A1Aggregation(), A2Aggregation(), A3Aggregation(),
                A4Aggregation(p=3), A5Aggregation(), A6Aggregation(),
                A7Aggregation(), A8Aggregation(), A9Aggregation(),
                A10Aggregation()):
        for r in (2, 5, 10):
            for k in ks:
                X_missing = MissingValuesInserterColumnsIndependent(columns=range(X.shape[1]), nan_representation=-1, percentage=miss)\
                    .fit_transform(X)
                multiclassF = OneVsRestClassifierForRandomBinaryClassifier(
                    KNNAlgorithmF(missing_representation=-1,
                                  r=r,
                                  aggregation=agg,
                                  k_neighbours=k))
                f_result = cross_validate(multiclassF,
                                          X_missing,
                                          y,
                                          scoring='roc_auc_ovo',
                                          return_estimator=True,
                                          cv=10)
                w = pd.DataFrame(
                    {
                        'algorithm':
                        'f',
                        'k':
                        str(k),
                        'r':
                        f_result['estimator'][0].estimator.r,
Esempio n. 4
0
def test_compute_final_p():
    X, y = load_iris(True)
    f = KNNAlgorithmF()
    f.fit(X, y)
    assert_array_equal(f.compute_final_p(X), f.compute_final_p_opt(X))