1

I've imported a dataset from CSV into Ruby as an array of arrays (each row is an array). I want to normalize each column from 0 to 1. Therefore, I have to find the max and min of each column. Is there an easy way to do this? Or do I have to parse each column value out of each row?

Thanks!

1 Answer 1

2

You can use Array#transpose to make the inner arrays be the columns, then use Enumerable#min and #max to get your values.

copy = csv_data.transpose
copy.each do |a| #do whatever you need here
  a.min
  a.max
end

or something like this

copy = csv_data.transpose
arr_min = copy.map(&:min) # returns an array of the min values
arr_max = copy.map(&:max) # returns an array of the max values
Sign up to request clarification or add additional context in comments.

1 Comment

Great answer. Note that—if speed is an issue—you might be able to roughly halve the running time by (less elegantly) using a single each iteration to calculate both min and max. (But maybe not, given that min/max are implemented in C.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.