How to extract features using PCA in Python?
FEATURE EXTRACTION DATA CLEANING PYTHON DATA MUNGING MACHINE LEARNING RECIPES PANDAS CHEATSHEET     ALL TAGS

How to extract features using PCA in Python?

How to extract features using PCA in Python?

This recipe helps you extract features using PCA in Python

1
This data science python source code does the following: 1. Implements Standard scaler function. 2. Implements PCA to reduces dimensions. 3. Reducing time complexity using PCA. 4. Selecting optimum number of dimensions
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]]

Relevant Projects

Mercari Price Suggestion Challenge Data Science Project
Data Science Project in Python- Build a machine learning algorithm that automatically suggests the right product prices.

Predict Credit Default | Give Me Some Credit Kaggle
In this data science project, you will predict borrowers chance of defaulting on credit loans by building a credit score prediction model.

Time Series Forecasting with LSTM Neural Network Python
Deep Learning Project- Learn to apply deep learning paradigm to forecast univariate time series data.

Learn to prepare data for your next machine learning project
Text data requires special preparation before you can start using it for any machine learning project.In this ML project, you will learn about applying Machine Learning models to create classifiers and learn how to make sense of textual data.

Data Science Project on Wine Quality Prediction in R
In this R data science project, we will explore wine dataset to assess red wine quality. The objective of this data science project is to explore which chemical properties will influence the quality of red wines.

Perform Time series modelling using Facebook Prophet
In this project, we are going to talk about Time Series Forecasting to predict the electricity requirement for a particular house using Prophet.

Predict Churn for a Telecom company using Logistic Regression
Machine Learning Project in R- Predict the customer churn of telecom sector and find out the key drivers that lead to churn. Learn how the logistic regression model using R can be used to identify the customer churn in telecom dataset.

Data Science Project - Instacart Market Basket Analysis
Data Science Project - Build a recommendation engine which will predict the products to be purchased by an Instacart consumer again.

Customer Churn Prediction Analysis using Ensemble Techniques
In this machine learning churn project, we implement a churn prediction model in python using ensemble techniques.

Deep Learning with Keras in R to Predict Customer Churn
In this deep learning project, we will predict customer churn using Artificial Neural Networks and learn how to model an ANN in R with the keras deep learning package.