익명화를 위한 PCA

Python으로 배우는 데이터 프라이버시와 익명화

Rebeca Gonzalez

Data engineer

주성분분석(PCA)

$$ $$ 대규모 데이터셋의 차원을 줄이는 데 자주 쓰이는 차원 축소 기법입니다.

Python으로 배우는 데이터 프라이버시와 익명화

PCA로 데이터 마스킹

  • PCA는 원본 특성의 선형 변환으로 새로운 "주성분"을 만듭니다.

  • 예: 맥주 데이터셋에서 PCA는 새 특성을 구성할 수 있습니다. 예를 들어:

$$ 2\times AlcoholicVolume - BitternessLevel$$

Python으로 배우는 데이터 프라이버시와 익명화

PCA로 데이터 마스킹

데이터의 새로운 투영

선형 연산으로 데이터를 투영·회전하는 PCA 방식을 보여주는 GIF

Python으로 배우는 데이터 프라이버시와 익명화

PCA로 데이터 마스킹

차원 축소 없이 PCA

$$

  • 원본 공간을 회전만 합니다.
  • 거리는 보존됩니다.
  • 예측 작업·알고리즘에 유리합니다.
Python으로 배우는 데이터 프라이버시와 익명화

PCA로 데이터 마스킹

  • 결과 값이 설명 없이 공개되어도, 알고리즘은 이를 학습해 정확히 예측할 수 있습니다.
  • 적대적 공격자는 이 마스킹된 값을 해석하기 어렵습니다.
Python으로 배우는 데이터 프라이버시와 익명화

PCA로 데이터 마스킹

# Explore the dataset
heart_df.head()

    age    sex    cp    trestbps    chol    fbs    restecg    thalach    exang    oldpeak    slope   ca   thal   target
0    63    1      3     145         233     1      0          150        0        2.3        0       0    1      1
1    37    1      2      130         250     0      1          187        0        3.5        0       0    2      1
2    41    0      1      130         204     0      0          172        0        1.4        2       0    2      1
3    56    1      1      120          236     0      1          178        0        0.8        2       0    2      1
4    57    0      0      120          354     0      1          163        1        0.6        2       0    2      1
Python으로 배우는 데이터 프라이버시와 익명화

PCA와 Scikit-learn으로 데이터 마스킹

# Obtain the data without the target column
x_data = df.drop(['target'], axis = 1)

# Target column as array of values y = df.target.values
Python으로 배우는 데이터 프라이버시와 익명화

PCA와 Scikit-learn으로 데이터 마스킹

# Import PCA from Scikit-learn
from sklearn.decomposition import PCA

# Initialize PCA with number of components to be the same as the number of columns pca = PCA(n_components=len(x_data.columns))
# Apply PCA to the data x_data_pca = pca.fit_transform(x_data)
Python으로 배우는 데이터 프라이버시와 익명화

PCA와 Scikit-learn으로 데이터 마스킹

# See the data
x_data_pca
array([[-1.22673448e+01,  2.87383781e+00,  1.49698788e+01, ...,
         7.31102828e-01, -2.90393586e-01,  5.12575925e-01],
       [ 2.69013712e+00, -3.98713736e+01,  8.77882303e-01, ...,
         4.04206943e-01, -4.25920179e-01, -1.48124511e-01],
       [-4.29502141e+01, -2.36368199e+01,  1.75944589e+00, ...,
        -9.15397287e-01,  2.17828257e-01,  7.97593843e-02],
       ...,
Python으로 배우는 데이터 프라이버시와 익명화

PCA와 Scikit-learn으로 데이터 마스킹

# Create a DataFrame from the resulting PCA transformed data
df_x_data_pca = pd.DataFrame(x_data_pca)


# Inspect the shape of the dataset df_x_data_pca.shape
(1213, 13)
Python으로 배우는 데이터 프라이버시와 익명화

PCA 마스킹 후 데이터 유용성

  • 로지스틱 회귀로 분류를 수행하고, 원본과 변환 데이터의 정확도 차이를 확인합니다.

$$

  • 로지스틱 회귀는 독립 변수들로 이진 결과를 예측하는 분류 알고리즘입니다.
Python으로 배우는 데이터 프라이버시와 익명화

PCA 마스킹 후 데이터 유용성

로지스틱 회귀로 분류를 수행하고 정확도 손실을 확인합니다.

# Split the resulting dataset into training and test data
x_train, x_test, y_train, y_test = train_test_split(x_data_pca, y, test_size=0.2)


# Create the model lr = LogisticRegression(max_iter=200)
# Fit train the model lr.fit(x_train,y_train)
# Run the model and perform predictions to obtain accuracy score acc = lr.score(x_test, y_test) * 100 print("Test Accuracy is ", acc)
Test Accuracy is 85.24590163934425
Python으로 배우는 데이터 프라이버시와 익명화

PCA 마스킹 전 데이터 유용성

원본 데이터로 로지스틱 회귀 분류를 수행하고 점수를 확인합니다.

# Split the resulting dataset into training and test data
x_train, x_test, y_train, y_test = train_test_split(x_data.to_numpy(),y,test_size = 0.2)

# Create the model
lr = LogisticRegression(max_iter=200)

# Fit train the model
lr.fit(x_train,y_train)

# Run the model and perform predictions to obtain accuracy score
acc = lr.score(x_test,y_test) * 100
print("Test Accuracy is ", acc)
Test Accuracy is 85.24590163934425
Python으로 배우는 데이터 프라이버시와 익명화

Ayo berlatih!

Python으로 배우는 데이터 프라이버시와 익명화

Preparing Video For Download...