Got a YouTube account?

New: enable viewer-created translations and captions on your YouTube channel!

English subtitles

← PCA in sklearn - Intro to Machine Learning

Get Embed Code
4 Languages

Showing Revision 3 created 05/25/2016 by Udacity Robot.

  1. The principal components analysis that I'm doing happens to live in
  2. this function called doPCA.
  3. And it looks very familiar to a lot of the stuff we've done
  4. before in scikit-learn.
  5. You have an import statement where you
  6. actually get the module out that has the code that you want.
  7. You create, in this case, the principal components analysis.
  8. You fit it.
  9. And then you can return that as an object.
  10. And so what I do is I, I get my principal components that, analysis that way.
  11. And I can ask some very interesting,
  12. some very important questions of it by accessing the attributes.
  13. So let's explain these three lines.
  14. This is how I actually get the information out of my PCA object.
  15. The first, the explained variance ratio is actually where the eigenvalues live.
  16. So by printing out this, this line here.
  17. This is the way that I know that the first principle component has about 90,
  18. 91 percent of the variation in the data, and the second one has about 9, or
  19. 10 percent.
  20. Those numbers come from this statement.
  21. And then the second thing that I do is I look at the first and
  22. second principle components.
  23. I get these out of the components attribute of my PCA object.
  24. So the components is going to be a list,
  25. a Python list that has as many principle components in it.
  26. As I ask for as a parameter.
  27. So in that case, I have two principal components that I'm getting.
  28. So I name them the first and second pc.
  29. So in previous quizzes where we were talking about,
  30. what's the direction in of, say, x prime in the xy original feature space,
  31. we came up with two numbers that were sort of packaged together into a vector.
  32. You can access that directional information through these components.
  33. Once I've fit my principle components analysis I have to,
  34. in order to do anything, perform something like a transformation of the data.
  35. And this code I will just give you in the starter code for the quiz.
  36. What I'm doing here is, I'm visualizing it.
  37. The first line is in red.
  38. I'll be plotting the first principle component,
  39. the locations of all the points along that principle component.
  40. As well as the direction of the principle components.
  41. I'm accessing that information by using the elements of the first PC vector.
  42. Then in Cyan or kind of a teal color, I'll be accessing the second
  43. principle component, and in blue I have the original data.
  44. So let me show you what this looks like,
  45. and then you give it a try yourself in the quiz.
  46. The first thing that you get is that you print out the eigenvalues.
  47. Remember that's this explained variance ratio information.
  48. And then the second thing is that you'll get a scatter plot.
  49. And it should look something like this.
  50. So you remember the red was the direction of our first principle component.
  51. And that's hopefully exactly where you guessed it was.
  52. Certainly intuitively seems like it's in the right place.
  53. The cyan is perpendicular to that.
  54. And then the blue is the original data points.
  55. One thing that I'll add is that it looks to the eye like the red and
  56. the cyan are not perfectly orthogonal.
  57. This doesn't quite look like a 90 degree angle.
  58. But remember that our axis have different scales.
  59. That this one goes all the way out to ten million,
  60. the y axis only goes out to less than half of that, about four million.
  61. So, in reality, if we were to plot everything proportionally,
  62. this graph should be twice as long as it is tall.
  63. And if we were to visualize it in exactly that way, they would be orthogonal