Code Monkey home page Code Monkey logo

Comments (4)

aapatel09 avatar aapatel09 commented on August 17, 2024 1

from handson-unsupervised-learning.

aapatel09 avatar aapatel09 commented on August 17, 2024 1

from handson-unsupervised-learning.

steveazzolin avatar steveazzolin commented on August 17, 2024

I have manually executed the code without random_state and the score doesn't change actually (keeping n_components to 27).

Then I tried to change n_component:

import numpy as np
import pandas as pd
from sklearn.preprocessing import StandardScaler
from sklearn.decomposition import PCA
from sklearn.metrics import precision_recall_curve, average_precision_score
from sklearn.model_selection import train_test_split
import matplotlib.pyplot as plt
%matplotlib inline

data = pd.read_csv('credit_card.zip')
X = data.copy().drop(["Class"],axis=1)
y = data['Class'].copy()

featuresToScale = X.columns
scaler = StandardScaler(copy=True)
X.loc[:,featuresToScale] = scaler.fit_transform(X[featuresToScale])

for i in [i+10 for i in range(20)]:
    pca = PCA(n_components=i)

    X_train_PCA = pca.fit_transform(X)
    X_train_PCA = pd.DataFrame(data=X_train_PCA, index=X.index)

    X_train_PCA_inverse = pca.inverse_transform(X_train_PCA)
    X_train_PCA_inverse = pd.DataFrame(data=X_train_PCA_inverse, index=X.index)

    anomalyScoresPCA = anomalyScores(X, X_train_PCA_inverse)
    _ , avg_precision = plotResults(y, anomalyScoresPCA, True,False) 
    print("n_components=" , i , " avg_precision=",avg_precision)

Gives:

n_components= 10  avg_precision= 0.15052200650486952
n_components= 11  avg_precision= 0.1851608802374098
n_components= 12  avg_precision= 0.1820248810643287
n_components= 13  avg_precision= 0.12039760005966599
n_components= 14  avg_precision= 0.13113627677672549
n_components= 15  avg_precision= 0.22555564098028213
n_components= 16  avg_precision= 0.21592680643385742
n_components= 17  avg_precision= 0.10970330227982492
n_components= 18  avg_precision= 0.1420103680819224
n_components= 19  avg_precision= 0.11227862548711573
n_components= 20  avg_precision= 0.15777373139629802
n_components= 21  avg_precision= 0.1592625134410127
n_components= 22  avg_precision= 0.15900773562327336
n_components= 23  avg_precision= 0.19549141342644125
n_components= 24  avg_precision= 0.10344835746696714
n_components= 25  avg_precision= 0.09745140764930849
n_components= 26  avg_precision= 0.11459766332143032
n_components= 27  avg_precision= 0.30317062682018747
n_components= 28  avg_precision= 0.002776650831794116
n_components= 29  avg_precision= 0.009325957416360619

from handson-unsupervised-learning.

steveazzolin avatar steveazzolin commented on August 17, 2024

Running this code

import numpy as np
import pandas as pd
from sklearn.preprocessing import StandardScaler
from sklearn.decomposition import PCA
from sklearn.metrics import precision_recall_curve, average_precision_score
from sklearn.model_selection import train_test_split
import matplotlib.pyplot as plt
%matplotlib inline

data = pd.read_csv('credit_card.zip')
X = data.copy().drop(["Class"],axis=1)
y = data['Class'].copy()

featuresToScale = X.columns
scaler = StandardScaler(copy=True)
X.loc[:,featuresToScale] = scaler.fit_transform(X[featuresToScale])

X, X_test, y, y_test = train_test_split(X, y, test_size=0.33, random_state=2018, stratify=y)

for i in [i+10 for i in range(20)]:
    pca = PCA(n_components=i,random_state=2018)

    X_train_PCA = pca.fit_transform(X)
    X_train_PCA = pd.DataFrame(data=X_train_PCA, index=X.index)

    X_train_PCA_inverse = pca.inverse_transform(X_train_PCA)
    X_train_PCA_inverse = pd.DataFrame(data=X_train_PCA_inverse, index=X.index)

    anomalyScoresPCA = anomalyScores(X, X_train_PCA_inverse)
    _ , avg_precision = plotResults(y, anomalyScoresPCA, True,False) 
    print("n_components=" , i , " avg_precision=",avg_precision)

Scores are:

n_components= 10  avg_precision= 0.2845377775949285
n_components= 11  avg_precision= 0.29364944111841085
n_components= 12  avg_precision= 0.28594075836267696
n_components= 13  avg_precision= 0.31238695971860264
n_components= 14  avg_precision= 0.33482491519829544
n_components= 15  avg_precision= 0.3171596014339625
n_components= 16  avg_precision= 0.36246230687638303
n_components= 17  avg_precision= 0.3551693238316446
n_components= 18  avg_precision= 0.3576986952085249
n_components= 19  avg_precision= 0.3541947874943925
n_components= 20  avg_precision= 0.37019433800859325
n_components= 21  avg_precision= 0.37590377049563356
n_components= 22  avg_precision= 0.38371878690273875
n_components= 23  avg_precision= 0.3819741346612291
n_components= 24  avg_precision= 0.39826963028826606
n_components= 25  avg_precision= 0.4292470766080597
n_components= 26  avg_precision= 0.5661816082602096
n_components= 27  avg_precision= 0.6937067887351663
n_components= 28  avg_precision= 0.002783436997939959
n_components= 29  avg_precision= 0.00910804526226634

from handson-unsupervised-learning.

Related Issues (17)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.