2011-09-09 4 views
0

그래서 배열을 렌더링하고 일부 차트에 표시 할 수 있지만 내 데이터 집합이 너무 큽니다. 길이가 20,000 개이며 다른 모든 항목을 놓을 때까지 어떻게 배열을 사용할 수 있습니까? array는 1,000 개의 아이템이거나 그 크기가 될 때까지 배열을 보간합니까?루비 배열 보간?

[ 
    {"timestamp"=>2011-09-05 14:30:00 UTC, "count"=>4488.0}, 
    {"timestamp"=>2011-09-05 14:45:00 UTC, "count"=>4622.0}, 
    {"timestamp"=>2011-09-05 15:00:00 UTC, "count"=>4655.0}, 
    {"timestamp"=>2011-09-05 15:15:00 UTC, "count"=>4533.0}, 
    {"timestamp"=>2011-09-05 15:30:00 UTC, "count"=>4439.0}, 
    {"timestamp"=>2011-09-05 15:45:00 UTC, "count"=>4468.0}, 
    {"timestamp"=>2011-09-05 16:00:00 UTC, "count"=>4419.0}, 
    {"timestamp"=>2011-09-05 16:15:00 UTC, "count"=>4430.0}, 
    {"timestamp"=>2011-09-05 16:30:00 UTC, "count"=>4429.0}, 
    {"timestamp"=>2011-09-05 16:45:00 UTC, "count"=>4502.0}, 
    {"timestamp"=>2011-09-05 17:00:00 UTC, "count"=>4497.0}, 
    {"timestamp"=>2011-09-05 17:15:00 UTC, "count"=>4468.0}, 
    {"timestamp"=>2011-09-05 17:30:00 UTC, "count"=>4510.0}, 
    {"timestamp"=>2011-09-05 17:45:00 UTC, "count"=>4547.0}, 
    {"timestamp"=>2011-09-05 18:00:00 UTC, "count"=>4471.0}, 
    {"timestamp"=>2011-09-05 18:15:00 UTC, "count"=>4501.0}, 
    {"timestamp"=>2011-09-05 18:30:00 UTC, "count"=>4451.0}, 
    {"timestamp"=>2011-09-05 18:45:00 UTC, "count"=>4453.0}, 
    {"timestamp"=>2011-09-05 19:00:00 UTC, "count"=>4593.0}, 
    {"timestamp"=>2011-09-05 19:15:00 UTC, "count"=>4540.0}, 
    {"timestamp"=>2011-09-05 19:30:00 UTC, "count"=>4516.0}, 
    {"timestamp"=>2011-09-05 19:45:00 UTC, "count"=>4494.0} 
] 

그리고 나도 그런 것처럼, 단지 배열에서 떨어 뜨리거나 어떻게 든 보간, 중간 값의 배열을 원하는 :

예, 나는 (해시) 다음과 같은 배열을 가지고 있다고

[ 
    {"timestamp"=>2011-09-05 14:45:00 UTC, "count"=>4622.0}, 
    {"timestamp"=>2011-09-05 15:00:00 UTC, "count"=>4655.0}, 
    {"timestamp"=>2011-09-05 15:30:00 UTC, "count"=>4439.0}, 
    {"timestamp"=>2011-09-05 16:00:00 UTC, "count"=>4419.0}, 
    {"timestamp"=>2011-09-05 16:30:00 UTC, "count"=>4429.0}, 
    {"timestamp"=>2011-09-05 17:00:00 UTC, "count"=>4497.0}, 
    {"timestamp"=>2011-09-05 17:30:00 UTC, "count"=>4510.0}, 
    {"timestamp"=>2011-09-05 18:00:00 UTC, "count"=>4471.0}, 
    {"timestamp"=>2011-09-05 18:30:00 UTC, "count"=>4451.0}, 
    {"timestamp"=>2011-09-05 19:00:00 UTC, "count"=>4593.0}, 
    {"timestamp"=>2011-09-05 19:15:00 UTC, "count"=>4540.0}, 
    {"timestamp"=>2011-09-05 19:45:00 UTC, "count"=>4494.0} 
] 

모든 의견이나 도움을 주시면 대단히 감사하겠습니다. 여기서도 요점을 놓칠 수 있습니다.

+0

주제가 해제되었지만 모든 "타임 스탬프"와 모든 "개수"에 대해 새로운 문자열이 생성됩니다. 심볼 (: timestamp)은 훨씬 더 낫습니다. 또는 구조체를 만듭니다. – steenslag

+0

정확히 무엇을해야합니까? ''timestamp ''순서로 무작위로 1000 개의 항목을 채운다? –

+0

그러나 배열은 MongoDB의 맵 감속기의 결과입니다. (나는 순수한 루비 오브젝트, 어떤 아이디어로 만들지 모르겠다.) –

답변

2
require 'pp' 

# Interval in seconds (30 min) 
INTERVAL = 1800 

# generate the data 
start = Time.mktime(2001, 9, 5, 14, 30) 

data = Array.new 
1000.times do |i| 
    data << {:timestamp => start + i*INTERVAL, :count => rand(4000)} 
end 

# Plain data 
pp data 

puts # blank 

# Simply gets de data from the sample number 300 to 400 
pp data[300..400] 

puts # blank 

# For example, data from from the second hour, for 3 hours long 
pp data[2*60*60/INTERVAL..(2+3)*60*60/INTERVAL] 

puts # blank 

# Make it smaller (50%) 
# We need data.size * 0.5 elements 
# Calculate the step we need to iterate to get 
# 50% elements. In this case skipping one between two 
step = (data.size/(data.size * 0.5)).to_i 

# We use Range#step to get the array of indexes, and then 
# transform it using Enumerable#collect to get the array 
# of Hashes. and filter nils 
# 
# Probably there is a simpler way to do this. Too late to think 
pp (0..data.size).step(step.to_i).collect {|index| data[index]}.reject{|x| x.nil?} 

당신은 (n)은 Enumerable에서 #의 each_slice

(1..10).each_slice(3) {|a| p a} 
    # outputs below 
    [1, 2, 3] 
    [4, 5, 6] 
    [7, 8, 9] 
    [10] 

당신은 n 개의 요소의 조각을 만들고, 다음 각에서 새 요소를 작성하여 설정을 줄일 수를보고 할 수 있습니다 일부분. 중간, 평균 등의 요소입니다.

2

사용 Array#sample :

a = [ 
    # 10 000 little hashes 
] 
smaller = a.sample(1000) 

을 다음 표시 할 smaller을 보내

a = [ 1, 2, 3, 4, 5, 6 ] 
smaller = a.sample(3) 
# [4, 2, 1] 

귀하의 경우에 당신은 같은 것을 할 것입니다.

그리고 당신은 순서대로 원하는 경우에 당신은 단지 그들을 다시 정렬 할 수 있습니다 : 당신이 샘플을 드롭 할 기준에 대한 몇 가지 규칙을 정의 할 필요가

smaller.sort! { |a,b| a['timestamp'] <=> b['timestamp'] } 
+0

나는 원하지 않습니다. 무작위 샘플링, 그들은 날짜이며 데이터는 선형적이고 동일한 순서로 유지되어야합니다. 어떤 아이디어입니까? –

+0

흠, 아직 배열이 잘못되었습니다. –

+1

@Joseph : 다음과 같이 정렬 할 수 있습니다. 그들을 순서대로 돌려 놓으십시오. –

0

배열 응축 합니다. 이해하기 쉽도록 간단한 정수를 대신 타임 스탬프로 사용합니다. 데이터와 함께 사용하려면 거부 메소드를 약간 수정해야합니다.

samples = 100.times.map do |i| 
    {"timestamp" => i, "count" => rand(100)} 
end 

i = samples.size 
samples.reject! do |item| item["timestamp"]%2 == 0 end 

item["timestamp"]%2 == 0은 샘플 세트의 샘플이 삭제되는 규칙입니다. 데이터에 대해 일부 시간 범위 또는 기타 항목을 정의 할 수 있습니다. 또한

$> samples.size # => 50