sampling

Random Sampling from Mongo

半世苍凉 提交于 2019-11-28 00:24:02
问题 I have a mongo collection with documents. There is one field in every document which is 0 OR 1. I need to random sample 1000 records from the database and count the number of documents who have that field as 1. I need to do this sampling 1000 times. How do i do it ? 回答1: Here's an example in the mongo shell .. assuming a collection of collname , and a value of interest in thefield : var total = db.collname.count(); var count = 0; var numSamples = 1000; for (i = 0; i < numSamples; i++) { var

Taking a disproportionate sample from a dataset in R

故事扮演 提交于 2019-11-27 13:58:01
问题 If I have a large dataset in R, how can I take random sample of the data taking into consideration the distribution of the original data, particularly if the data are skewed and only 1% belong to a minor class and I want to take a biased sample of the data? 回答1: The sample(x, n, replace = FALSE, prob = NULL) function takes a sample from a vector x of size n . This sample can be with or without replacement , and the probabilities of selecting each element to the sample can be either the same

How to draw waveform of Android's music player? [closed]

无人久伴 提交于 2019-11-27 11:42:33
one of the default live wallpapers that came with my phone was a wallpaper that displayed the wave form of music playing in the background in real time. I was wondering how one could go about doing this. It seems I should somehow access the streaming data of the background music and compute the samples in real time, but I have no idea how to get the streaming data. I think I can display the wave form by following this tutorial http://codeidol.com/java/swing/Audio/Build-an-Audio-Waveform-Display/ , but I'm not exactly sure how to do it. Help would be greatly appreciated/ Christopher Souvey

What does replacement mean in numpy.random.choice?

谁都会走 提交于 2019-11-27 11:04:48
问题 Here explains the function numpy.random.choice . However, I am confused about the third parameter replace . What is it? And in which case will it be useful? Thanks! 回答1: It controls whether the sample is returned to the sample pool. If you want only unique samples then this should be false. 回答2: You can use it when you want sample some elements from a list, and meanwhile you want the elements no repeat , then you can set the " replace=False ". eg. from numpy import random as rd ary = list

What are chunks, samples and frames when using pyaudio

无人久伴 提交于 2019-11-27 10:51:43
问题 After going through the documentation of pyaudio and reading some other articles on the web, I am confused if my understanding is correct. This is the code for audio recording found on pyaudio's site: import pyaudio import wave CHUNK = 1024 FORMAT = pyaudio.paInt16 CHANNELS = 2 RATE = 44100 RECORD_SECONDS = 5 WAVE_OUTPUT_FILENAME = "output.wav" p = pyaudio.PyAudio() stream = p.open(format=FORMAT, channels=CHANNELS, rate=RATE, input=True, frames_per_buffer=CHUNK) print("* recording") frames =

Looping at a constant rate with high precision for signal sampling

半城伤御伤魂 提交于 2019-11-27 04:52:55
问题 I am trying to sample a signal at 10Khz in Python. There is no problem when try to run this code(at 1KHz): import sched, time i = 0 def f(): # sampling function s.enter(0.001, 1, f, ()) global i i += 1 if i == 1000: i = 0 print "one second" s = sched.scheduler(time.time, time.sleep) s.enter(0.001, 1, f, ()) s.run() When I try to make the time less, it starts to exceed one second(in my computer, 1.66s at 10e-6). It it possible to run a sampling function at a specific frequency in Python? 回答1:

sample random point in triangle [closed]

这一生的挚爱 提交于 2019-11-27 01:59:27
问题 Suppose you have an arbitrary triangle with vertices A , B , and C . This paper (section 4.2) says that you can generate a random point, P , uniformly from within triangle ABC by the following convex combination of the vertices: P = (1 - sqrt(r1)) * A + (sqrt(r1) * (1 - r2)) * B + (sqrt(r1) * r2) * C where r1 and r2 are uniformly drawn from [0, 1] , and sqrt is the square root function. How do you justify that the sampled points that are uniformly distributed within triangle ABC ? EDIT As

Take n random elements from a List<E>?

爱⌒轻易说出口 提交于 2019-11-26 20:02:18
How can I take n random elements from an ArrayList<E> ? Ideally, I'd like to be able to make successive calls to the take() method to get another x elements, without replacement. Two main ways. Use Random#nextInt(int) : List<Foo> list = createItSomehow(); Random random = new Random(); Foo foo = list.get(random.nextInt(list.size())); It's however not guaranteed that successive n calls returns unique elements. Use Collections#shuffle() : List<Foo> list = createItSomehow(); Collections.shuffle(list); Foo foo = list.get(0); It enables you to get n unique elements by an incremented index (assuming

How do I double the size of a vector in MATLAB with interpolation?

青春壹個敷衍的年華 提交于 2019-11-26 16:56:08
问题 Essentially, if I have the following matrix: [1, 2, 3 ,4, 10] I need to explode it whilst interpolating, as follows: [1, 1.5, 2, 2.5, 3, 3.5, 4, 7, 10] . Essentially, buff it up by filling in the average of the two surrounding values. Say if I need to do this for n instead of adding just 1 value as we have here. 回答1: You need to use interp1 with 'linear' interpolation method: >> v = [1 2 3 4 10]; >> newNum = 13; % new number of elements in the "buffed" vector >> iv = interp1( linspace(0,1

How to draw waveform of Android&#39;s music player? [closed]

可紊 提交于 2019-11-26 15:42:04
问题 one of the default live wallpapers that came with my phone was a wallpaper that displayed the wave form of music playing in the background in real time. I was wondering how one could go about doing this. It seems I should somehow access the streaming data of the background music and compute the samples in real time, but I have no idea how to get the streaming data. I think I can display the wave form by following this tutorial http://codeidol.com/java/swing/Audio/Build-an-Audio-Waveform