K-Anonymity System Using Tkinter and Python: Data Generalization and Precise Value Retrieval
This Python program uses Tkinter to build a user-friendly K-Anonymity system, allowing for data anonymization and precise value retrieval. Users can upload CSV datasets, select attributes for generalization, define the K-value, and retrieve specific values from the anonymized dataset.
Key Features:
- Graphical User Interface (GUI): A Tkinter-based GUI simplifies interaction and makes the system accessible to users without coding experience.
- Dataset Upload: Users can upload CSV datasets to be anonymized.
- Attribute Selection: Users can choose specific attributes from the dataset to be generalized.
- K-Value Input: Users specify the K-value, determining the minimum number of records in each group after anonymization.
- K-Anonymity Processing: The program performs K-Anonymization on the selected attributes, generalizing values to ensure privacy.
- Anonymized Data Display: The processed dataset, with anonymized values, is displayed within the GUI.
- Download Anonymized Data: Users can save the anonymized dataset to a CSV file.
- Precise Value Search: Users can search for specific values within the anonymized dataset, retrieving the corresponding records.
Code Breakdown:
- Importing Libraries:
import tkinter as tk
import tkinter.filedialog
import tkinter.messagebox
import pandas as pd
- Creating the GUI:
root = tk.Tk()
root.title('K-Anonymity System')
root.geometry('800x600')
- File Upload:
def open_file():
global df
filename = tk.filedialog.askopenfilename()
if filename.endswith('.csv'):
df = pd.read_csv(filename)
text.delete('1.0', 'end')
text.insert('1.0', df.head())
else:
tk.messagebox.showerror('Error', 'File format not supported.')
btn_open = tk.Button(root, text='Open', command=open_file)
btn_open.pack()
text = tk.Text(root)
text.pack()
- Attribute Selection:
def get_columns():
global columns, options
columns = list(df.columns)
options = []
for col in columns:
options.append(col)
option_menu['menu'].delete(0, 'end')
for option in options:
option_menu['menu'].add_command(label=option, command=tk._setit(var, option))
lbl_col = tk.Label(root, text='Select column:')
lbl_col.pack()
var = tk.StringVar()
option_menu = tk.OptionMenu(root, var, '')
option_menu.pack()
btn_columns = tk.Button(root, text='Get columns', command=get_columns)
btn_columns.pack()
- K-Value Input:
lbl_k = tk.Label(root, text='Enter k value:')
lbl_k.pack()
entry_k = tk.Entry(root)
entry_k.pack()
- K-Anonymity Processing:
def k_anonymity():
global df_k
column = var.get()
k = int(entry_k.get())
df_k = pd.DataFrame(columns=df.columns)
for i in df[column].unique():
df_i = df[df[column] == i]
if len(df_i) >= k:
df_i[column] = '*'
df_k = pd.concat([df_k, df_i])
else:
df_k = pd.concat([df_k, df_i])
text.delete('1.0', 'end')
text.insert('1.0', df_k.head())
btn_k_anonymity = tk.Button(root, text='K-Anonymity', command=k_anonymity)
btn_k_anonymity.pack()
- Saving Anonymized Data:
def save_file():
filename = tk.filedialog.asksaveasfilename(defaultextension='.csv')
df_k.to_csv(filename, index=False)
btn_save = tk.Button(root, text='Save', command=save_file)
btn_save.pack()
- Precise Value Search:
def search():
column = var.get()
value = entry_search.get()
df_search = df_k[df_k[column] == value]
text.delete('1.0', 'end')
text.insert('1.0', df_search.head())
lbl_search = tk.Label(root, text='Enter value to search:')
lbl_search.pack()
entry_search = tk.Entry(root)
entry_search.pack()
btn_search = tk.Button(root, text='Search', command=search)
btn_search.pack()
Complete Code:
import tkinter as tk
import tkinter.filedialog
import tkinter.messagebox
import pandas as pd
root = tk.Tk()
root.title('K-Anonymity System')
root.geometry('800x600')
def open_file():
global df
filename = tk.filedialog.askopenfilename()
if filename.endswith('.csv'):
df = pd.read_csv(filename)
text.delete('1.0', 'end')
text.insert('1.0', df.head())
else:
tk.messagebox.showerror('Error', 'File format not supported.')
btn_open = tk.Button(root, text='Open', command=open_file)
btn_open.pack()
text = tk.Text(root)
text.pack()
def get_columns():
global columns, options
columns = list(df.columns)
options = []
for col in columns:
options.append(col)
option_menu['menu'].delete(0, 'end')
for option in options:
option_menu['menu'].add_command(label=option, command=tk._setit(var, option))
lbl_col = tk.Label(root, text='Select column:')
lbl_col.pack()
var = tk.StringVar()
option_menu = tk.OptionMenu(root, var, '')
option_menu.pack()
btn_columns = tk.Button(root, text='Get columns', command=get_columns)
btn_columns.pack()
lbl_k = tk.Label(root, text='Enter k value:')
lbl_k.pack()
entry_k = tk.Entry(root)
entry_k.pack()
def k_anonymity():
global df_k
column = var.get()
k = int(entry_k.get())
df_k = pd.DataFrame(columns=df.columns)
for i in df[column].unique():
df_i = df[df[column] == i]
if len(df_i) >= k:
df_i[column] = '*'
df_k = pd.concat([df_k, df_i])
else:
df_k = pd.concat([df_k, df_i])
text.delete('1.0', 'end')
text.insert('1.0', df_k.head())
btn_k_anonymity = tk.Button(root, text='K-Anonymity', command=k_anonymity)
btn_k_anonymity.pack()
def save_file():
filename = tk.filedialog.asksaveasfilename(defaultextension='.csv')
df_k.to_csv(filename, index=False)
btn_save = tk.Button(root, text='Save', command=save_file)
btn_save.pack()
def search():
column = var.get()
value = entry_search.get()
df_search = df_k[df_k[column] == value]
text.delete('1.0', 'end')
text.insert('1.0', df_search.head())
lbl_search = tk.Label(root, text='Enter value to search:')
lbl_search.pack()
entry_search = tk.Entry(root)
entry_search.pack()
btn_search = tk.Button(root, text='Search', command=search)
btn_search.pack()
root.mainloop()
References:
[1] Anonymization with Python, https://towardsdatascience.com/anonymization-with-python-cd9d1a9ba1a4 [2] k-Anonymity: A Model for Protecting Privacy, https://www.win.tue.nl/~mpechen/publications/pubs/DLV03.pdf [3] Python GUI Programming with Tkinter, https://www.tutorialspoint.com/python/python_gui_programming.htm
原文地址: https://www.cveoy.top/t/topic/ojQT 著作权归作者所有。请勿转载和采集!