Giter VIP home page Giter VIP logo

dsc-pca-numpy-lab-online-ds-sp-000's Introduction

Performing Principal Component Analysis (PCA) - Lab

Introduction

Now that you have a high-level overview of PCA, as well as some of the details of the algorithm itself, it's time to practice implementing PCA on your own using the NumPy package.

Objectives

You will be able to:

  • Implement PCA from scratch using NumPy

Import the data

  • Import the data stored in the file 'foodusa.csv' (set index_col=0)
  • Print the first five rows of the DataFrame
import pandas as pd
data = None

Normalize the data

Next, normalize your data by subtracting the mean from each of the columns.

data = None
data.head()

Calculate the covariance matrix

The next step is to calculate the covariance matrix for your normalized data.

cov_mat = None
cov_mat

Calculate the eigenvectors

Next, calculate the eigenvectors and eigenvalues for your covariance matrix.

import numpy as np
eig_values, eig_vectors = None

Sort the eigenvectors

Great! Now that you have the eigenvectors and their associated eigenvalues, sort the eigenvectors based on their eigenvalues to determine primary components!

# Get the index values of the sorted eigenvalues
e_indices = None

# Sort 
eigenvectors_sorted = None
eigenvectors_sorted

Reprojecting the data

Finally, reproject the dataset using your eigenvectors. Reproject this dataset down to 2 dimensions.

Summary

Well done! You've now coded PCA on your own using NumPy! With that, it's time to look at further applications of PCA.

dsc-pca-numpy-lab-online-ds-sp-000's People

Contributors

shakeelraja avatar mathymitchell avatar alexgriff avatar h-parker avatar foamofthesea avatar loredirick avatar sumedh10 avatar

Watchers

James Cloos avatar

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.