-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpreprocessing_utils.py
More file actions
95 lines (85 loc) · 4.01 KB
/
Copy pathpreprocessing_utils.py
File metadata and controls
95 lines (85 loc) · 4.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split
import poison_utils as pu
from sklearn.preprocessing import OneHotEncoder
def heart_poison(poison, percent, number, mode,style):
data = pd.read_csv('heart_failure_clinical_records_dataset.csv')
if poison == "FLIP":
data2 = pu.flip_random_labels(data=data, percent=percent, dataset='heart')
if poison == "INJECT":
data2 = pu.inject_new(data=data, number=number, mode = mode)
if poison == "TAMPER":
data2 = pu.tamper_rows(data= data, percent=percent, mode = mode)
X = data2.drop(columns = ['Tampered'])
y = data2['Tampered']
if style == "SPLIT":
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.20, stratify=y, random_state=43)
return X_train, X_test, y_train, y_test
else:
return data2
def loan_poison(poison, percent, number, mode, style):
data = pd.read_csv('loan_data.csv')
data = pd.get_dummies(data, columns=['person_education', 'person_home_ownership', 'loan_intent','previous_loan_defaults_on_file', 'person_gender'], drop_first=True)
if poison == "FLIP":
data2 = pu.flip_random_labels(data=data, percent=percent, dataset='loan')
if poison == "INJECT":
data2 = pu.inject_new(data=data, number=number, mode = mode)
if poison == "TAMPER":
data2 = pu.tamper_rows(data= data, percent=percent, mode = mode )
X = data2.drop(columns = ['Tampered'])
y = data2['Tampered']
if style == "SPLIT":
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.20, stratify=y, random_state=43)
return X_train, X_test, y_train, y_test
else:
return data2
def cancer_poison(poison, percent, number, mode, style):
data = pd.read_csv('breast-cancer.csv')
data['diagnosis'] = data['diagnosis'].map({'M': 1, 'B': 0})
column_to_move = data.columns[1]
data = data[[col for col in data.columns if col != column_to_move] + [column_to_move]]
if poison == "FLIP":
data2 = pu.flip_random_labels(data=data, percent=percent, dataset='cancer')
if poison == "INJECT":
data2 = pu.inject_new(data=data, number=number, mode=mode)
if poison == "TAMPER":
data2 = pu.tamper_rows(data= data, percent=percent, mode = mode)
X = data2.drop(columns = ['Tampered'])
y = data2['Tampered']
if style == "SPLIT":
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.20, stratify=y, random_state=43)
return X_train, X_test, y_train, y_test
else:
return data2
def machine_poison(poison, percent, number, mode, style):
data = pd.read_csv('machine_failure_dataset.csv')
data['Machine_Type'] = data['Machine_Type'].map({'Lathe': 1, 'Drill': 0, 'Mill': 2,})
if poison == "FLIP":
data2 = pu.flip_random_labels(data=data, percent=percent, dataset='machine')
if poison == "INJECT":
data2 = pu.inject_new(data=data, number=number, mode=mode)
if poison == "TAMPER":
data2 = pu.tamper_rows(data= data, percent=percent, mode = mode)
X = data2.drop(columns = ['Tampered'])
y = data2['Tampered']
if style == "SPLIT":
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.20, stratify=y, random_state=43)
return X_train, X_test, y_train, y_test
else:
return data2
def heart_load():
data = pd.read_csv('heart_failure_clinical_records_dataset.csv')
return data
def machine_load():
data = pd.read_csv('machine_failure_dataset.csv')
data['Machine_Type'] = data['Machine_Type'].map({'Lathe': 1, 'Drill': 0, 'Mill': 2,})
return data
def loan_load():
data = pd.read_csv('loan_data.csv')
data = pd.get_dummies(data, columns=['person_education', 'person_home_ownership', 'loan_intent','previous_loan_defaults_on_file', 'person_gender'], drop_first=True)
return data
def cancer_load():
data = pd.read_csv('breast-cancer.csv')
data['diagnosis'] = data['diagnosis'].map({'M': 1, 'B': 0})
return data