Skip to content

Instantly share code, notes, and snippets.

@v0dro
Last active September 1, 2016 08:13
Show Gist options
  • Save v0dro/40ad4d6e92fc464789b08d81a659c151 to your computer and use it in GitHub Desktop.
Save v0dro/40ad4d6e92fc464789b08d81a659c151 to your computer and use it in GitHub Desktop.
college admission prediction example
require 'daru'
require 'statsample-glm'
require 'open-uri'
content = open('http://www.ats.ucla.edu/stat/data/binary.csv')
File.write('binary.csv', content.read)
df = Daru::DataFrame.from_csv "binary.csv"
df.vectors = Daru::Index.new(['admit', 'gre', 'gpa', 'rank'])
df.to_category 'rank'
formula = 'admit ~ gpa + gre + rank'
glm = Statsample::GLM::Regression.new formula, df, :logistic, epsilon: 1e-2, constant: 1
new_data = Daru::DataFrame.new({
'gre' => [df['gre'].mean]*4,
'gpa' => [df['gpa'].mean]*4,
'rank' => [1,2,3,4]
})
glm.predict new_data
#ExceptionForMatrix::ErrDimensionMismatch: Matrix dimension mismatch
/home/sameer/.rvm/gems/ruby-2.2.1/gems/backports-3.6.8/lib/backports/1.9.2/stdlib/matrix.rb:818:in `*'
/home/sameer/.rvm/gems/ruby-2.2.1/gems/statsample-glm-0.2.1/lib/statsample-glm/glm/irls/logistic.rb:18:in `measurement'
/home/sameer/.rvm/gems/ruby-2.2.1/gems/statsample-glm-0.2.1/lib/statsample-glm/glm/irls/base.rb:40:in `predict'
/home/sameer/.rvm/gems/ruby-2.2.1/gems/statsample-glm-0.2.1/lib/statsample-glm/glm/base.rb:229:in `predict'
/home/sameer/.rvm/gems/ruby-2.2.1/gems/statsample-glm-0.2.1/lib/statsample-glm/glm/regression.rb:51:in `predict'
(pry):102:in `<main>'
/home/sameer/.rvm/gems/ruby-2.2.1/gems/pry-0.10.4/lib/pry/pry_instance.rb:355:in `eval'
/home/sameer/.rvm/gems/ruby-2.2.1/gems/pry-0.10.4/lib/pry/pry_instance.rb:355:in `evaluate_ruby'
/home/sameer/.rvm/gems/ruby-2.2.1/gems/pry-0.10.4/lib/pry/pry_instance.rb:323:in `handle_line'
/home/sameer/.rvm/gems/ruby-2.2.1/gems/pry-0.10.4/lib/pry/pry_instance.rb:243:in `block (2 levels) in eval'
/home/sameer/.rvm/gems/ruby-2.2.1/gems/pry-0.10.4/lib/pry/pry_instance.rb:242:in `catch'
/home/sameer/.rvm/gems/ruby-2.2.1/gems/pry-0.10.4/lib/pry/pry_instance.rb:242:in `block in eval'
/home/sameer/.rvm/gems/ruby-2.2.1/gems/pry-0.10.4/lib/pry/pry_instance.rb:241:in `catch'
/home/sameer/.rvm/gems/ruby-2.2.1/gems/pry-0.10.4/lib/pry/pry_instance.rb:241:in `eval'
/home/sameer/.rvm/gems/ruby-2.2.1/gems/iruby-0.2.9/lib/iruby/backend.rb:66:in `eval'
/home/sameer/.rvm/gems/ruby-2.2.1/gems/iruby-0.2.9/lib/iruby/backend.rb:12:in `eval'
/home/sameer/.rvm/gems/ruby-2.2.1/gems/iruby-0.2.9/lib/iruby/kernel.rb:87:in `execute_request'
/home/sameer/.rvm/gems/ruby-2.2.1/gems/iruby-0.2.9/lib/iruby/kernel.rb:47:in `dispatch'
/home/sameer/.rvm/gems/ruby-2.2.1/gems/iruby-0.2.9/lib/iruby/kernel.rb:37:in `run'
/home/sameer/.rvm/gems/ruby-2.2.1/gems/iruby-0.2.9/lib/iruby/command.rb:70:in `run_kernel'
/home/sameer/.rvm/gems/ruby-2.2.1/gems/iruby-0.2.9/lib/iruby/command.rb:34:in `run'
/home/sameer/.rvm/gems/ruby-2.2.1/gems/iruby-0.2.9/bin/iruby:5:in `<top (required)>'
/home/sameer/.rvm/gems/ruby-2.2.1/bin/iruby:23:in `load'
/home/sameer/.rvm/gems/ruby-2.2.1/bin/iruby:23:in `<main>'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment