Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Machine Learning and Trend Analysis in PHP - DPC 18

Machine Learning and Trend Analysis in PHP - DPC 18

Michael C.

June 09, 2018
Tweet

More Decks by Michael C.

Other Decks in Programming

Transcript

  1. TREND ANALYSIS AND MACHINE
    LEARNING IN PHP
    DUTCH PHP CONFERENCE 2018
    @MICHAELCULLUMUK

    View full-size slide

  2. TREND ANALYSIS AND MACHINE
    LEARNING IN PHP
    DUTCH PHP CONFERENCE 2018
    @MICHAELCULLUMUK

    View full-size slide

  3. @MICHAELCULLUMUK
    ME?

    View full-size slide

  4. MICHAEL CULLUM
    @MICHAELCULLUMUK

    View full-size slide

  5. @MICHAELCULLUMUK
    ARTIFICIAL
    INTELLIGENCE

    View full-size slide

  6. @MICHAELCULLUMUK
    MACHINE LEARNING

    View full-size slide

  7. @MICHAELCULLUMUK
    MACHINE LEARNING

    View full-size slide

  8. @MICHAELCULLUMUK
    LEARNING
    Cause

    &

    Effect
    Context
    Process Knowledge

    View full-size slide

  9. @MICHAELCULLUMUK
    USING
    Cause
    Knowledge
    Process
    Prediction

    of an

    effect

    View full-size slide

  10. @MICHAELCULLUMUK
    Cause
    Knowledge
    Process
    Process
    Cause
    Predicted

    effect
    Effect

    View full-size slide

  11. @MICHAELCULLUMUK
    EXAMPLE
    1+1= 2
    1+2= 3
    1+3= 4
    2+1= 3
    3+1= 4
    Cause Effect

    View full-size slide

  12. @MICHAELCULLUMUK
    EXAMPLE
    1+1= 2
    1+2= 3
    1+3= 4
    2+1= 3
    3+1= 4
    Knowledge

    View full-size slide

  13. @MICHAELCULLUMUK
    EXAMPLE
    1+1= 2
    1+2= 3
    1+3= 4
    2+1= 3
    3+1= 4
    Cause
    3+1=
    Predicted Effect
    4
    Knowledge

    View full-size slide

  14. @MICHAELCULLUMUK
    INFERENCE
    1x + 3 = 4
    1x + 4 = 6
    1x + 3 = 5
    2 + 3x = 8
    1x + 4 = ?
    x = 10
    Multiply by 2
    Multiply by 1 Multiply by 2
    Multiply by 2

    View full-size slide

  15. @MICHAELCULLUMUK
    INFERENCE
    “eggs”

    View full-size slide

  16. @MICHAELCULLUMUK
    MACHINE LEARNING AS
    A 4-STEP PROCESS

    View full-size slide

  17. @MICHAELCULLUMUK
    1. ACQUIRE DATA

    View full-size slide

  18. @MICHAELCULLUMUK
    2. TRAIN MODEL

    View full-size slide

  19. @MICHAELCULLUMUK
    3. ASK YOUR QUESTION

    View full-size slide

  20. @MICHAELCULLUMUK
    4. GET PREDICTED
    ANSWER

    View full-size slide

  21. @MICHAELCULLUMUK
    1. ACQUIRE DATA

    View full-size slide

  22. @MICHAELCULLUMUK
    GOOD LUCK

    View full-size slide

  23. @MICHAELCULLUMUK
    2. TRAIN MODEL

    View full-size slide

  24. @MICHAELCULLUMUK
    SUPERVISED LEARNING
    UNSUPERVISED LEARNING

    View full-size slide

  25. @MICHAELCULLUMUK
    SUPERVISED LEARNING

    View full-size slide

  26. @MICHAELCULLUMUK
    KNOWN OUTCOMES

    View full-size slide

  27. @MICHAELCULLUMUK
    QUANTATIVE - CLASSIFICATION
    QUALITATIVE - REGRESSION

    View full-size slide

  28. @MICHAELCULLUMUK
    QUANTATIVE - CLASSIFICATION
    QUALITATIVE - REGRESSION

    View full-size slide

  29. @MICHAELCULLUMUK
    CLASSIFICATION
    Rating Conclusion
    100 Good
    25 Bad
    50 Good
    40 Bad

    View full-size slide

  30. @MICHAELCULLUMUK
    CLASSIFICATION
    Rating Conclusion
    100 Good
    25 Bad
    50 Good
    40 Bad

    View full-size slide

  31. @MICHAELCULLUMUK
    LINEAR CLASSIFIER
    Item Value
    Black
    PHP

    Devs
    White Sales
    Sales Test
    PHP Test

    View full-size slide

  32. @MICHAELCULLUMUK
    QUANTATIVE - CLASSIFICATION
    QUALITATIVE - REGRESSION

    View full-size slide

  33. @MICHAELCULLUMUK
    REGRESSION
    Price Size
    5.00 8.25
    6.00 10
    4.00 6.75
    3.00 5
    0
    2.5
    5
    7.5
    10
    0 1.5 3 4.5 6
    Price
    Size

    View full-size slide

  34. @MICHAELCULLUMUK
    REGRESSION
    Price Size
    5.00 8.25
    6.00 10
    4.00 6.75
    3.00 5
    0
    2.5
    5
    7.5
    10
    0 1.5 3 4.5 6
    Price
    Price
    Size

    View full-size slide

  35. @MICHAELCULLUMUK
    UNSUPERVISED
    LEARNING

    View full-size slide

  36. @MICHAELCULLUMUK
    DISCOVERY

    View full-size slide

  37. @MICHAELCULLUMUK
    ASSOCIATION

    CLUSTERING

    View full-size slide

  38. @MICHAELCULLUMUK
    ASSOCIATION

    CLUSTERING

    View full-size slide

  39. @MICHAELCULLUMUK
    ASSOCIATION
    Item 1 Item 2
    T-shirt Shorts
    Shorts T-shirt
    Suit Black Shoes
    Socks Underwear
    Item 1 Item 2
    Black shoes Suit
    Socks Underwear
    Underwear Socks
    T-shirt Socks

    View full-size slide

  40. @MICHAELCULLUMUK
    ASSOCIATION
    Item 1 Item 2
    T-shirt Shorts
    Shorts T-shirt
    Suit Black Shoes
    Socks Underwear
    Item 1 Item 2
    Black shoes Suit
    Socks Underwear
    Underwear Socks
    T-shirt Socks
    People who buy socks,

    also often buy underwear
    People who who buy

    underwear always buy

    socks

    View full-size slide

  41. @MICHAELCULLUMUK
    ASSOCIATION
    Item 1 Item 2
    T-shirt Shorts
    Shorts T-shirt
    Suit Black Shoes
    Socks Underwear
    Item 1 Item 2
    Black shoes Suit
    Socks Underwear
    Underwear Socks
    T-shirt Socks
    People who buy socks,

    also often buy underwear
    People who buy suits

    always buy black shoes
    People who who buy

    underwear always buy

    socks
    People who buy black

    shoes, always buy suits

    View full-size slide

  42. @MICHAELCULLUMUK
    ASSOCIATION
    Item 1 Item 2
    T-shirt Shorts
    Shorts T-shirt
    Suit Black Shoes
    Socks Underwear
    Item 1 Item 2
    Black shoes Suit
    Socks Underwear
    Underwear Socks
    T-shirt Socks
    People who buy socks,

    also often buy underwear
    People who buy suits

    always buy black shoes
    People who who buy

    underwear always buy

    socks
    People who buy black

    shoes, always buy suits
    People who buy shorts

    always buy t-shirts
    People who buy t-shirts,

    also often buy shorts

    View full-size slide

  43. @MICHAELCULLUMUK
    ASSOCIATION

    CLUSTERING

    View full-size slide

  44. @MICHAELCULLUMUK
    CLUSTER ANALYSIS IS THE TASK OF
    GROUPING A SET OF OBJECTS IN SUCH A
    WAY THAT OBJECTS IN THE SAME GROUP
    ARE MORE SIMILAR TO EACH OTHER
    THAN TO THOSE IN OTHER GROUPS.

    View full-size slide

  45. @MICHAELCULLUMUK
    CLUSTER ANALYSIS

    View full-size slide

  46. @MICHAELCULLUMUK
    3. QUESTION

    4. PREDICTED ANSWER

    View full-size slide

  47. @MICHAELCULLUMUK
    ALGORITHMS

    View full-size slide

  48. @MICHAELCULLUMUK
    LEAST SQUARES

    View full-size slide

  49. @MICHAELCULLUMUK
    LEAST SQUARES REGRESSION LINE

    View full-size slide

  50. @MICHAELCULLUMUK
    MATHS

    View full-size slide

  51. @MICHAELCULLUMUK
    A+BX=Y

    View full-size slide

  52. @MICHAELCULLUMUK
    ▸ Each point has an x and y value
    ▸ We need an equation of a line
    ▸ We move the line an infinite number of times
    ▸ Each time, we draw a box between every point, and the line, with one corner
    on the line, and another on the point
    ▸ The correct line is the one where the sum of the area of all the squares is
    smallest
    PROCESS

    View full-size slide

  53. @MICHAELCULLUMUK
    DEMO

    View full-size slide

  54. @MICHAELCULLUMUK
    PHP

    View full-size slide

  55. @MICHAELCULLUMUK
    DEMO

    View full-size slide

  56. @MICHAELCULLUMUK
    DEMOS
    php-ai/php-ml

    View full-size slide

  57. @MICHAELCULLUMUK
    CODE
    $samples = [[60], [61], [62], [63], [65]];
    $targets = [3.1, 3.6, 3.8, 4, 4.1];
    $regression = new LeastSquares();
    $regression->train($samples, $targets);
    echo $regression->predict([64]);

    View full-size slide

  58. @MICHAELCULLUMUK
    STEPS
    ▸ Turn samples into a matrix (X)
    ▸ Turn targets into a matrix (y)
    ▸ Transpose the samples matrix (XT)
    ▸ Multiply it by itself (XTX) and then inverse the matrix ((XTX)-1); and multiply the
    transposed samples matrix (XT) by the targets matrix (XTy)
    ▸ Multiply those two matrices together ((XTX)-1 (XTy)) and read off the first
    column to get your coefficent(s) and intercept (β1
    )

    View full-size slide

  59. @MICHAELCULLUMUK
    NEAREST NEIGHBOUR

    View full-size slide

  60. @MICHAELCULLUMUK
    NEAREST NEIGHBOUR

    View full-size slide

  61. @MICHAELCULLUMUK
    3-NEAREST NEIGHBOUR

    View full-size slide

  62. @MICHAELCULLUMUK
    5-NEAREST NEIGHBOUR

    View full-size slide

  63. @MICHAELCULLUMUK
    MATHS

    View full-size slide

  64. @MICHAELCULLUMUK
    PHP

    View full-size slide

  65. @MICHAELCULLUMUK
    DEMO

    View full-size slide

  66. @MICHAELCULLUMUK
    DEMO
    0
    1
    2
    3
    4
    0 1 2 3 4
    A - red
    B - blue

    View full-size slide

  67. @MICHAELCULLUMUK
    DEMO
    0
    1.25
    2.5
    3.75
    5
    0 1 2 3 4
    A - red
    B - blue

    View full-size slide

  68. @MICHAELCULLUMUK
    DEMO
    $samples = [[1, 3], [1, 4], [2, 4], [3, 1], [4, 1], [4, 2]];
    $labels = ['a', 'a', 'a', 'b', 'b', 'b'];
    $classifier = new KNearestNeighbors();
    $classifier->train($samples, $labels);
    echo $classifier->predict([3, 2]);

    View full-size slide

  69. @MICHAELCULLUMUK
    USES OF MACHINE
    LEARNING

    View full-size slide

  70. @MICHAELCULLUMUK
    NUMERICAL ANALYSIS

    View full-size slide

  71. @MICHAELCULLUMUK
    EXCEPTIONS

    View full-size slide

  72. @MICHAELCULLUMUK
    E-COMMERCE

    View full-size slide

  73. @MICHAELCULLUMUK
    FAULT DETECTION

    View full-size slide

  74. @MICHAELCULLUMUK
    ROOT CAUSE ANALYSIS

    View full-size slide

  75. @MICHAELCULLUMUK
    CLASSIFICATION

    View full-size slide

  76. @MICHAELCULLUMUK
    NATURAL LANGUAGE
    PROCESSING

    View full-size slide

  77. @MICHAELCULLUMUK
    ANALYSIS OF SUPPORT
    QUERIES

    View full-size slide

  78. @MICHAELCULLUMUK
    ANALYSIS OF LARGE
    NUMBERS OF DOCUMENTS

    View full-size slide

  79. @MICHAELCULLUMUK
    FUN

    View full-size slide

  80. THANKS - DANK JE
    @MICHAELCULLUMUK

    View full-size slide

  81. @MICHAELCULLUMUK
    ANY QUESTIONS?

    View full-size slide

  82. TREND ANALYSIS AND MACHINE
    LEARNING IN PHP
    DUTCH PHP CONFERENCE 2018
    @MICHAELCULLUMUK

    View full-size slide