Click or drag to resize
Accord.NET (logo)

ClassesRandom Method (Int32, Int32)

Returns a random group assignment for a sample.

Namespace:  Accord.Statistics
Assembly:  Accord.Math (in Accord.Math.dll) Version: 3.8.0
Syntax
public static int[] Random(
	int samples,
	int classes
)
Request Example View Source

Parameters

samples
Type: SystemInt32
The sample size.
classes
Type: SystemInt32
The number of groups.

Return Value

Type: Int32
Examples
// The Classes.Random method can be used to generate random class 
// assignments. For example, let's say we would like to generate 
// 100 binary assignments where the probability of having a positive
// class assignment is of 80%:
int[] y1 = Classes.Random(samples: 100, proportion: 0.8);

// If we compute the histogram of y, the result will be:
int[] hist1 = y1.Histogram(); // new int[] { 80, 20 } 

// Let's say we would like to generate 100 assignments
// where samples can belong to 1 of 3 different classes:
int[] y2 = Classes.Random(samples: 100, classes: 3);

// The histogram of y will be:
int[] hist2 = y2.Histogram(); // new int[] { 34, 33, 33 }

// Let's say we would like to generate the sample sample as above,
// but we would like to control how many samples should fall into
// each of the 3 categories:
int[] y3 = Classes.Random(samples: 100, proportion: new[] { 0.2, 0.5, 0.3});

// The histogram of y will be:
int[] hist3 = y3.Histogram(); // new int[] { 20, 50, 30 }

// The Random method can also generate balanced assignments according
// to some pre-existing division. Let's say we have 100 samples that
// have been already divided into 5 groups (i.e. the last result of y).
// Now, we would like to separate those into two different groups such
// that the proportion between the different classes in y is kept balanced
// within those sub-groups:
int[] y4 = Classes.Random(y3, categories: 2);

// The histogram of y4 will be:
int[] hist4 = y4.Histogram(); // new int[] { 50, 50 }

// However, we can use those assignments to separate
// the y3 labels into two 50-50 groups as we intended:
int[][] r = Classes.Separate(y3, y4);

// And as we can see, proportions are the same in both groups:
int[] rhist1 = r[0].Histogram(); // new int[] { 10, 25, 15 }
int[] rhist2 = r[1].Histogram(); // new int[] { 10, 25, 15 }
See Also