Skip to content

Instantly share code, notes, and snippets.

@serialhex
Forked from anonymous/gist:1109434
Created July 27, 2011 14:11
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 serialhex/1109437 to your computer and use it in GitHub Desktop.
Save serialhex/1109437 to your computer and use it in GitHub Desktop.
require 'narray'
def LoadMatrix(filename, type = :numbers)
case type
when :numbers
LoadMatrix.load_numbers(filename)
when :dna
LoadMatrix.load_dna(filename)
when :cubes
LoadMatrix.load_cubes(filename)
when :labels
LoadMatrix.load_labels(filename)
end
end
module LoadMatrix
def load_numbers(filename)
matrix = []
File.open(filename) do |file|
file.each_line do |line|
ary = []
line.split(" ").each{ |n| ary << n.to_f }
matrix << ary
end
end
matrix = NArray.to_na(matrix)
matrix = matrix.transpose
end
def load_dna(filename)
matrix=[]
File.open(filename) do |file|
file.each_line do |line|
line.split("\n").each{|n| matrix << n }
end
end
matrix
end
def load_cubes(filename)
matrix=[]
File.open(filename) do |file|
file.each_line do |line|
line.split("\n").each{|n| matrix << n }
end
end
matrix
end
def load_labels(filename)
matrix = []
File.open(filename) do |file|
file.each_line do |line|
ary = []
line.split(" ").each{ |n| ary << n.to_f }
matrix << ary
end
end
matrix = NArray.to_na(matrix)
matrix = matrix.reshape(1, matrix.total)
end
extend self
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment