with

# RUBY

@arnab_deka

## 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

out_filename = File.join(cropped_dir, File.split(filename).last)
i.quantize(2, Magick::GRAYColorspace) \
.contrast(sharpen=true) \
.negate(grayscale=true) \
.enhance \
.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

• Hypothesis:  hθ(x)

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

Minimize squared mean error

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

## 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) =

## SVM

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

## 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

## LIVE DEMO!

... since the other one went so well!

## Demo: Weka

java -jar -Xmx8192m lib/weka.jar

## 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: