Skip to content

Instantly share code, notes, and snippets.

@gbuesing
Created September 5, 2015 19:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gbuesing/c32046e8ad896001d5ba to your computer and use it in GitHub Desktop.
Save gbuesing/c32046e8ad896001d5ba to your computer and use it in GitHub Desktop.
NMatrix standardize_columns extension
require 'nmatrix/nmatrix'
class NMatrix
def standardize_columns!
cols.times do |i|
col = self[0..-1,i]
self[0..-1,i] = (col - col.mean[0]) / col.std[0]
end
self
end
def standardize_columns
clone.standardize_columns!
end
end
n = N[[1.0,3.0,4.0],[5.0,7.0,9.0],[24.0,19.0,41.0]]
n.standardize_columns
# [
# [ -0.7324096128940435, -0.8006407690254355, -0.697389673941405]
# [-0.40689422938557973, -0.32025630761017415, -0.4483219332480461]
# [ 1.1393038422796231, 1.12089707663561, 1.145711607189451]
# ]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment