text_plain_output_1.png
from sklearn import metrics

from sklearn import preprocessing

from sklearn.model_selection import train_test_split

from sklearn.naive_bayes import GaussianNB

from sklearn.neighbors import KNeighborsClassifier

from sklearn.svm import SVC

from sklearn.tree import DecisionTreeClassifier

import numpy as np

import pandas as pd

df = pd.read_csv('../input/yhydata/Dry_Bean.csv')

minmax = preprocessing.MinMaxScaler()
data_minmax = minmax.fit_transform(df.iloc[:, :-1])
le = preprocessing.LabelEncoder()
df['Class'] = le.fit_transform(df.iloc[:, -1])
label = df['Class']

df = pd.DataFrame(data_minmax, columns=df.columns[:-1])
df['Class'] = label

model = KNeighborsClassifier(n_neighbors=5)
model.fit(train.iloc[:, :-1], train.iloc[:, -1])
predict = model.predict(test.iloc[:, :-1])

recall_original_knn = metrics.recall_score(test.iloc[:, -1], predict, average='micro')

model = SVC()
model.fit(train.iloc[:, :-1], train.iloc[:, -1])
predict = model.predict(test.iloc[:, :-1])

recall_original_svm = metrics.recall_score(test.iloc[:, -1], predict, average='micro')

model = GaussianNB()
model.fit(train.iloc[:, :-1], train.iloc[:, -1])
predict = model.predict(test.iloc[:, :-1])

recall_original_bayes = metrics.recall_score(test.iloc[:, -1], predict, average='micro')

model = DecisionTreeClassifier()
model.fit(train.iloc[:, :-1], train.iloc[:, -1])
predict = model.predict(test.iloc[:, :-1])

recall_original_tree = metrics.recall_score(test.iloc[:, -1], predict, average='micro')

recall_noise = 0
noise_array_knn = []
epsilon_array = []
for r in range(1, 501):
    sensitivety = 1
    epsilon = r
    epsilon_array.append(1 / epsilon)
    noise = np.random.laplace(0.0, sensitivety / epsilon, len(df))
    df_noise = df.copy()
    for i in range(len(df.columns) - 1):
        for j in range(len(df)):
            df_noise.iloc[j, i] += noise[j]
    train_noise, _ = train_test_split(df_noise, test_size=0.2, random_state=7)
    model = KNeighborsClassifier(n_neighbors=5)
    model.fit(train_noise.iloc[:, :-1], train_noise.iloc[:, -1])
    predict = model.predict(test.iloc[:, :-1])
    noise_array_knn.append(metrics.recall_score(test.iloc[:, -1], predict, average='micro'))

print(max(noise_array_knn))
print(min(noise_array_knn))