Exemplo n.º 1
0
 def predict(self, key_data):
     ref_key_attributes = closest_neighbors(self.synthetic_dict.keys(),
                                            key_data)
     ref_sensitive_attributes = []
     for key in ref_key_attributes:
         ref_sensitive_attributes.extend(self.synthetic_dict[key])
     return majority(ref_sensitive_attributes)
Exemplo n.º 2
0
    def score(self, key_data, sensitive_data):
        ref_key_attributes = closest_neighbors(self.synthetic_dict.keys(),
                                               key_data)
        ref_sensitive_attributes = []
        for key in ref_key_attributes:
            ref_sensitive_attributes.extend(self.synthetic_dict[key])

        return count_frequency(ref_sensitive_attributes, sensitive_data)
Exemplo n.º 3
0
def test_closest_neighbors_exact():
    samples = [
        ('a', '1'),
        ('a', '2'),
        ('a', '3'),
        ('b', '1'),
        ('b', '2'),
        ('b', '3'),
    ]
    target = ('a', '2')
    results = closest_neighbors(samples, target)
    assert len(results) == 1
    assert results[0] == ('a', '2')
Exemplo n.º 4
0
def test_closest_neighbors_non_exact():
    samples = [
        ('a', '1'),
        ('a', '3'),
        ('b', '1'),
        ('b', '2'),
        ('b', '3'),
    ]
    target = ('a', '2')
    results = closest_neighbors(samples, target)
    assert len(results) == 3
    assert ('a', '1') in results
    assert ('a', '3') in results
    assert ('b', '2') in results
Exemplo n.º 5
0
    def predict(self, key_data):
        """Make a prediction of the sensitive data given keys.

        Args:
            key_data (tuple):
                The key data.

        Returns:
            tuple:
                The predicted sensitive data.
        """
        ref_key_attributes = closest_neighbors(self.synthetic_dict.keys(), key_data)
        ref_sensitive_attributes = []
        for key in ref_key_attributes:
            ref_sensitive_attributes.extend(self.synthetic_dict[key])

        return majority(ref_sensitive_attributes)
Exemplo n.º 6
0
    def score(self, key_data, sensitive_data):
        """Score based on the belief of the attacker, in the form P(sensitive_data|key|data).

        Args:
            key_data (tuple):
                The key data.
            sensitive_data (tuple):
                The sensitive data.

        Returns:
            float or None:
                The frequency of the correct sensitive entry.
        """
        ref_key_attributes = closest_neighbors(self.synthetic_dict.keys(), key_data)
        ref_sensitive_attributes = []
        for key in ref_key_attributes:
            ref_sensitive_attributes.extend(self.synthetic_dict[key])

        return count_frequency(ref_sensitive_attributes, sensitive_data)