Machine Learning 

with

RUBY





META






What's Next?


  • (Very) Brief look at ML
  • Deeper look at Classification & SVM
  • How to use all this with Ruby




Live Demo!

... coz those always go well.

Demo: THe data


  • Pre-process: create CSV
0,0,0,0,0,0... 113,125,125,164,254,254,224,99... 0,0,0,0,0,0,three


Demo: Results



Demo: Try your OWN!


First, pre-process the uploaded file
  i = Magick::Image.read(filename).first
  out_filename = File.join(cropped_dir, File.split(filename).last)
  i.quantize(2, Magick::GRAYColorspace) \
    .contrast(sharpen=true) \
    .negate(grayscale=true) \
    .enhance \
    .adaptive_blur(radius=0.0, sigma=1.0) \
    .despeckle \
    .resize_to_fill(28, 28) \
    .write(out_filename)
 


What is ML?


"Field of study that gives computers the ability to learn without being explicitly programmed."

                                                                     - Arthur Samuel



So... What is ML?


using statistics-driven algorithms
to find patterns in data.

                                                - Arnab Deka


Supervised Learning

  • labelled data
  • predict labels on new data
  • examples
    • housing-price
    • spam-filter
  • algorithms/techniques
    • decision trees
    • bayes networks
    • SVM & neural networks


Unsupervised Learning

  • unlabelled data
  • cluster data-points
  • examples:
    • organizing computer clusters
    • social-network analysis
    • finding market segments
    • recommendations
  • algorithms/techniques:
    • K-Means
    • hierarchical & density-based

Classification: 

LINEAR REGRESSION


Classification: 

LINEAR REGRESSION

CLASSIFICATION: 

LINEAR REGRESSION

  • Hypothesis:  hθ(x)

  • hθ(xi) = θ0 + θ1xi


Minimize squared mean error

Σ(hθ(xi) - yi)^2


Classification:

Logistic Regression

Classification:

LOGISTIC REGRESSION

CLASSIFICATION: 

LOGISTIC REGRESSION


Similar to linear regression:
  • need to minimize squared error
  • y can only be 0 (not-spam) or 1 (spam)
  • So, constrain hθ(x) between 0 and 1
  • and round-off

CLASSIFICATION: 

LOGISTIC REGRESSION


  • So...  hθ(x) = g(x)
  • where g(z) =






Classification: 

SVM

  • Very powerful and widely-used
  • Support Vector Machines
  • "Large Margin Classifier"


Classification: SVM


CLASSIFICATION: 

Multi-class


ML (SVM) wiTH RUBY


  • libsvm and rb-libsvm
  • weka
  • the Matrix class
  • Apache Mahout

ML with Ruby: libsvm



problem = Libsvm::Problem.new
parameter = Libsvm::SvmParameter.new
features = examples.map do |ary|  Libsvm::Node.features(ary)end
problem.set_examples(labels, features)
model = Libsvm::Model.train(problem, parameter)
model.predict(Libsvm::Node.features(*test_example)).to_i

ML with RUBY: WEKA



LIVE DEMO!

... since the other one went so well!

Demo: Weka

 java -jar -Xmx8192m lib/weka.jar



demo: Weka



Using Weka with Ruby

  • weka.classifiers.functions.LibSVM  
  • is  Java::WekaClassifiersFunctions::LibSVM
  • setOptions() is set_options

model = Java::WekaClassifiersFunctions::LibSVM.newmodel.set_options(options)
model.build_classifier(data)

model.classify_instance(d)

And the rest...


What we didn't discuss at all:
  • Regularization
  • normalization
  • polynomial classifiers
  • cross-validation
  • testing
  • attribute-selection (PCA)
  • data-generation

Go forth...


Still not inspired?

Dive deeper:


Machine Learning with Ruby

By Arnab Deka

Machine Learning with Ruby

  • 6,660