अनामिकीकरण के लिए PCA

Python में डेटा प्राइवेसी और अज्ञातिकरण

Rebeca Gonzalez

Data engineer

Principal component analysis (PCA)

$$ $$ एक dimensionality-reduction विधि जो बड़े डेटासेट्स की dimensions घटाने में अक्सर उपयोग होती है.

Python में डेटा प्राइवेसी और अज्ञातिकरण

PCA से डेटा मास्किंग

  • PCA मूल फीचर्स के linear transformations से नए "principal components" बनाता है.

  • बीयर वाले डेटासेट में, PCA नए फीचर्स बनाता है. उदाहरण:

$$ 2\times AlcoholicVolume - BitternessLevel$$

Python में डेटा प्राइवेसी और अज्ञातिकरण

PCA से डेटा मास्किंग

डेटा की नई projections

डेटा पर linear operations करके PCA किस तरह अलग-अलग तरीके से data project कर सकता है, इसे दिखाता Gif

Python में डेटा प्राइवेसी और अज्ञातिकरण

PCA से डेटा मास्किंग

Dimensionality reduction के बिना PCA

$$

  • यह बस डेटासेट के मूल space का एक rotation है.
  • यानी distances बनी रहती हैं.
  • predictive tasks और algorithms के लिए फायदेमंद.
Python में डेटा प्राइवेसी और अज्ञातिकरण

PCA से डेटा मास्किंग

  • यदि ये प्राप्त मान बिना व्याख्या के जारी किए जाएँ, तो algorithms इन्हीं पर train होकर सटीक predictions कर सकते हैं.
  • Adversaries इन masked मानों का अर्थ नहीं समझ पाएँगे.
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 मास्किंग के बाद डेटा उपयोगिता

  • logistic regression से classification करें और मूल व परिणामी डेटा की accuracy की तुलना कर हानि जाँचें.

$$

  • Logistic regression एक classification algorithm है जो independent variables के आधार पर binary outcome की भविष्यवाणी करता है.
Python में डेटा प्राइवेसी और अज्ञातिकरण

PCA डेटा मास्किंग के बाद डेटा उपयोगिता

Logistic regression से classification करें और accuracy loss देखें.

# 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 डेटा मास्किंग से पहले डेटा उपयोगिता

Logistic regression से classification करें और मूल डेटा पर score देखें

# 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 में डेटा प्राइवेसी और अज्ञातिकरण

अभ्यास करते हैं!

Python में डेटा प्राइवेसी और अज्ञातिकरण

Preparing Video For Download...