Recipe: How to extract features using PCA in Python?
FEATURE EXTRACTION

How to extract features using PCA in Python?

This recipe helps you extract features using PCA in Python
In [1]:
## How to extract features using PCA in Python
def Snippet_124():
    print()
    print(format('How to extract features using PCA in Python','*^82'))

    import warnings
    warnings.filterwarnings("ignore")

    # load libraries
    from sklearn import decomposition, datasets
    from sklearn.preprocessing import StandardScaler

    # Load the breast cancer dataset
    dataset = datasets.load_breast_cancer()

    # Load the features
    X = dataset.data

    # View the shape of the dataset
    print(); print(X.shape)
    print(); print(X)

    # Standardize Features
    sc = StandardScaler()

    # Fit the scaler to the features and transform
    X_std = sc.fit_transform(X)

    # View the new feature data's shape    
    print(); print(X_std.shape)
    print(); print(X_std)

    # Create a pca object with the 3 components
    pca = decomposition.PCA(n_components=3)

    # Fit the PCA and transform the data
    X_std_pca = pca.fit_transform(X_std)

    # View the new feature data's shape
    print(); print(X_std_pca.shape)
    print(); print(X_std_pca)

Snippet_124()
*******************How to extract features using PCA in Python********************

(569, 30)

[[1.799e+01 1.038e+01 1.228e+02 ... 2.654e-01 4.601e-01 1.189e-01]
 [2.057e+01 1.777e+01 1.329e+02 ... 1.860e-01 2.750e-01 8.902e-02]
 [1.969e+01 2.125e+01 1.300e+02 ... 2.430e-01 3.613e-01 8.758e-02]
 ...
 [1.660e+01 2.808e+01 1.083e+02 ... 1.418e-01 2.218e-01 7.820e-02]
 [2.060e+01 2.933e+01 1.401e+02 ... 2.650e-01 4.087e-01 1.240e-01]
 [7.760e+00 2.454e+01 4.792e+01 ... 0.000e+00 2.871e-01 7.039e-02]]

(569, 30)

[[ 1.09706398 -2.07333501  1.26993369 ...  2.29607613  2.75062224
   1.93701461]
 [ 1.82982061 -0.35363241  1.68595471 ...  1.0870843  -0.24388967
   0.28118999]
 [ 1.57988811  0.45618695  1.56650313 ...  1.95500035  1.152255
   0.20139121]
 ...
 [ 0.70228425  2.0455738   0.67267578 ...  0.41406869 -1.10454895
  -0.31840916]
 [ 1.83834103  2.33645719  1.98252415 ...  2.28998549  1.91908301
   2.21963528]
 [-1.80840125  1.22179204 -1.81438851 ... -1.74506282 -0.04813821
  -0.75120669]]

(569, 3)

[[ 9.19283683  1.94858275 -1.123163  ]
 [ 2.38780181 -3.76817263 -0.52928187]
 [ 5.73389628 -1.07517383 -0.55174676]
 ...
 [ 1.25617927 -1.90229641  0.56272681]
 [10.37479405  1.67201091 -1.87703905]
 [-5.47524329 -0.67063731  1.49044953]]


Stuck at work?
Can't find the recipe you are looking for. Let us know and we will find an expert to create the recipe for you. Click here
Companies using this Recipe
1 developer from Emids
1 developer from Jio
1 developer from Alexandria.ai
1 developer from Exponentia DataLabs
1 developer from Vodafone
1 developer from ANAC
1 developer from HvH
1 developer from Wipro
1 developer from ICU Medical