Showing posts with label randperm. Show all posts
Showing posts with label randperm. Show all posts
Monday, April 07, 2008
Guest Post on Blinkdagger
Readers of this Web log may be interested in, An Introduction to Combinatorics, an article on the perms, randperm and nchoosek functions which I authored as a guest of Blinkdagger. Blinkdagger covers MATLAB programming, among other things, and I suggest you have a look.
Labels:
Blinkdagger,
combinatorics,
guest post,
nchoosek,
perms,
randperm
Monday, February 19, 2007
Dividing Data Randomly Into Equal-Sized Groups
This is a quick note on dividing items randomly into equal-sized groups. This is an even quicker tip than yesterday's Dividing Values Into Equal-Sized Groups, since in this case, the original data does not affect the outcome.
Start by initializing the pseudo-random number generator (PRNG) for reproducible results:
rand('twister',9596)
Being able to reproduce outcomes exactly from run to run is important for several reasons, not the least of which is debugging. If the outcome of a program changes from run to run, it can be very hard to discover what precisely is going wrong.
With that out of the way, we can assign random groupings, in this case 20 groups for 10,000 individuals:
Group = ceil(20 * randperm(10000)' / 10000);
That's all there is to it. The result, 'Group', is a column vector with 10,000 group assignments, running from 1 to 20. If a different number of groups is desired, change the '20' to some other number. If a different number of items are to be assigned groups, change the '10000' (in both places) to something else. Just to check on this example, we reach for tabulate from the Statistics Toolbox:
tabulate(Group)
Value Count Percent
1 500 5.00%
2 500 5.00%
3 500 5.00%
4 500 5.00%
5 500 5.00%
6 500 5.00%
7 500 5.00%
8 500 5.00%
9 500 5.00%
10 500 5.00%
11 500 5.00%
12 500 5.00%
13 500 5.00%
14 500 5.00%
15 500 5.00%
16 500 5.00%
17 500 5.00%
18 500 5.00%
19 500 5.00%
20 500 5.00%
This process guarantees the the sizes of the largest and smallest groups will differ by no more than 1, and is ideal for assigning observations to folds for k-fold cross-validation.
Start by initializing the pseudo-random number generator (PRNG) for reproducible results:
rand('twister',9596)
Being able to reproduce outcomes exactly from run to run is important for several reasons, not the least of which is debugging. If the outcome of a program changes from run to run, it can be very hard to discover what precisely is going wrong.
With that out of the way, we can assign random groupings, in this case 20 groups for 10,000 individuals:
Group = ceil(20 * randperm(10000)' / 10000);
That's all there is to it. The result, 'Group', is a column vector with 10,000 group assignments, running from 1 to 20. If a different number of groups is desired, change the '20' to some other number. If a different number of items are to be assigned groups, change the '10000' (in both places) to something else. Just to check on this example, we reach for tabulate from the Statistics Toolbox:
tabulate(Group)
Value Count Percent
1 500 5.00%
2 500 5.00%
3 500 5.00%
4 500 5.00%
5 500 5.00%
6 500 5.00%
7 500 5.00%
8 500 5.00%
9 500 5.00%
10 500 5.00%
11 500 5.00%
12 500 5.00%
13 500 5.00%
14 500 5.00%
15 500 5.00%
16 500 5.00%
17 500 5.00%
18 500 5.00%
19 500 5.00%
20 500 5.00%
This process guarantees the the sizes of the largest and smallest groups will differ by no more than 1, and is ideal for assigning observations to folds for k-fold cross-validation.
Labels:
data mining,
dividing,
k-fold cross validation,
machine learning,
MATLAB,
PRNG,
random,
randperm
Saturday, January 13, 2007
Revisiting rand (MATLAB 2007a)
Pseudo-random number generators (PRNG) are frequently used in statistical and machine learning methods for things like train/test selection and solution initialization. (See an interesting discussion of this in the Nov-22-2006 posting, Explicit Randomization in Learning algorithms, on the Machine Learning (Theory) log.) Hence, it is important to understand the operation of the pseudo-random number generator employed in one's code.
I've just gotten word that MATLAB's built-in rand function will be changing (as of MATLAB 2007a) to use the Mersenne Twister method of generating numbers be default. Note that use of 'state' or 'seed' will change to a generator other than the default. Probably the most important impact at a practical level is that code not specifying another generator (not using 'state' or 'seed') will now generate different values.
Note, too, that if the randperm function continues to follow rand's lead (randperm was initialized by initializing rand in the past), then it will also produce different values than in previous releases if rand is not initialized.
I have no word on whether this affects randn or not.
MATLAB programming suggestion: Always initialize the random number functions before calling them. Repeatable results make testing code much easier.
See my post of Dec-07-2006, Quick Tip Regarding rand and randn for more information on rand and randn.
Also see the Mar-19-2008 post, Quasi-Random Numbers.
Also in 2007a, "divide-by-zero" and "log-of-zero" warning messages will be turned off by default.
I've just gotten word that MATLAB's built-in rand function will be changing (as of MATLAB 2007a) to use the Mersenne Twister method of generating numbers be default. Note that use of 'state' or 'seed' will change to a generator other than the default. Probably the most important impact at a practical level is that code not specifying another generator (not using 'state' or 'seed') will now generate different values.
Note, too, that if the randperm function continues to follow rand's lead (randperm was initialized by initializing rand in the past), then it will also produce different values than in previous releases if rand is not initialized.
I have no word on whether this affects randn or not.
MATLAB programming suggestion: Always initialize the random number functions before calling them. Repeatable results make testing code much easier.
See my post of Dec-07-2006, Quick Tip Regarding rand and randn for more information on rand and randn.
Also see the Mar-19-2008 post, Quasi-Random Numbers.
Also in 2007a, "divide-by-zero" and "log-of-zero" warning messages will be turned off by default.
Labels:
2007a,
Mersenne Twister,
PRNG,
pseudorandom,
rand,
randn,
random,
randperm,
seed,
state
Subscribe to:
Posts (Atom)