Git Product home page Git Product logo

anonypy's Introduction

AnonyPy

Anonymization library for python. AnonyPy provides following privacy preserving techniques for the anonymization.

  • K Anonymity
  • L Diversity
  • T Closeness

The Anonymization method

  • Anonymization method aims at making the individual record be indistinguishable among a group record by using techniques of generalization and suppression.
  • Turning a dataset into a k-anonymous (and possibly l-diverse or t-close) dataset is a complex problem, and finding the optimal partition into k-anonymous groups is an NP-hard problem.
  • AnonyPy uses "Mondrian" algorithm to partition the original data into smaller and smaller groups
  • The algorithm assumes that we have converted all attributes into numerical or categorical values and that we are able to measure the β€œspan” of a given attribute Xi.

Install

$ pip install anonypy

Usage

import anonypy
import pandas as pd

data = [
    [6, "1", "test1", "x", 20],
    [6, "1", "test1", "x", 30],
    [8, "2", "test2", "x", 50],
    [8, "2", "test3", "w", 45],
    [8, "1", "test2", "y", 35],
    [4, "2", "test3", "y", 20],
    [4, "1", "test3", "y", 20],
    [2, "1", "test3", "z", 22],
    [2, "2", "test3", "y", 32],
]

columns = ["col1", "col2", "col3", "col4", "col5"]
categorical = set(("col2", "col3", "col4"))

def main():
    df = pd.DataFrame(data=data, columns=columns)

    for name in categorical:
        df[name] = df[name].astype("category")

    feature_columns = ["col1", "col2", "col3"]
    sensitive_column = "col4"

    p = anonypy.Preserver(df, feature_columns, sensitive_column)
    rows = p.anonymize_k_anonymity(k=2)

    dfn = pd.DataFrame(rows)
    print(dfn)

Original data

   col1 col2   col3 col4  col5
0     6    1  test1    x    20
1     6    1  test1    x    30
2     8    2  test2    x    50
3     8    2  test3    w    45
4     8    1  test2    y    35
5     4    2  test3    y    20
6     4    1  test3    y    20
7     2    1  test3    z    22
8     2    2  test3    y    32

The created anonymized data is below(Guarantee 2-anonymity).

  col1 col2         col3 col4  count
0  2-4    2        test3    y      2
1  2-4    1        test3    y      1
2  2-4    1        test3    z      1
3  6-8    1  test1,test2    x      2
4  6-8    1  test1,test2    y      1
5    8    2  test3,test2    w      1
6    8    2  test3,test2    x      1

anonypy's People

Contributors

glassonion1 avatar peisuke avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

anonypy's Issues

Example in README returns error

I'm hoping to learn how to use this package, but when I run the example in the README, I get the following error:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Input In [2], in <cell line: 1>()
----> 1 main()

Input In [1], in main()
     26 sensitive_column = "col4"
     28 p = anonypy.Preserver(df, feature_columns, sensitive_column)
---> 29 rows = p.anonymize_k_anonymity(k=2)
     31 dfn = pd.DataFrame(rows)
     32 print(dfn)

File ~/miniconda3/envs/ppla-dev2/lib/python3.8/site-packages/anonypy/anonypy.py:18, in Preserver.anonymize_k_anonymity(self, k)
     17 def anonymize_k_anonymity(self, k):
---> 18     return self.__anonymize(k)

File ~/miniconda3/envs/ppla-dev2/lib/python3.8/site-packages/anonypy/anonypy.py:10, in Preserver.__anonymize(self, k, l, p)
      8 def __anonymize(self, k, l=0, p=0.0):
      9     partitions = self.modrian.partition(k, l, p)
---> 10     return anonymize(
     11         self.modrian.df,
     12         partitions,
     13         self.modrian.feature_columns,
     14         self.modrian.sensitive_column,
     15     )

File ~/miniconda3/envs/ppla-dev2/lib/python3.8/site-packages/anonypy/anonypy.py:79, in anonymize(df, partitions, feature_columns, sensitive_column, max_partitions)
     75 grouped_columns = df.loc[partition].agg(aggregations, squeeze=False)
     76 sensitive_counts = (
     77     df.loc[partition].groupby(sensitive_column).agg({sensitive_column: "count"})
     78 )
---> 79 values = grouped_columns.iloc[0].to_dict()
     80 for sensitive_value, count in sensitive_counts[sensitive_column].items():
     81     if count == 0:

AttributeError: 'list' object has no attribute 'to_dict'

Unable to use package

When trying to use the anonpy package, I get the error:

AttributeError: 'list' object has no attribute 'to_dict'on the anonimize function: anonymize(df, partitions, feature_columns, sensitive_column, max_partitions)
image

This errors happens even while trying to run the example code on the readme file.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.