tableTwoway¶
Tallies observations into two-way frequency table.
Synopsis¶
tableTwoway (x, y, nx, ny)
Required Arguments¶
- float
x[](Input) - Array of length
nObservationscontaining the data for the first variable. - float
y[](Input) - Array of length
nObservationscontaining the data for the second variable. - int
nx(Input) - Number of intervals (bins) for variable
x. - int
ny(Input) - Number of intervals (bins) for variable
y.
Return Value¶
An array of size nx by ny containing the counts.
Optional Arguments¶
dataBounds,xlo,xhi,ylo,yhi(Output)- If none is specified or if
dataBoundsis specified,nIntervalsintervals of equal length are used. Letxminandxmaxbe the minimum and maximum values inx, respectively, with similar meanings foryminandymax. Then,table[0]is the tally of observations with thexvalue less than or equal toxmin+ (xmax−xmin)/nx, and theyvalue less than or equal toymin+ (ymax−ymin)/ny. WhendataBoundsis explicitly specified, the minimum and maximum values inxandyare output inxmin,xmax,ymin, andymax.
or
knownBounds, floatxlo, floatxhi, floatylo, floatyhi(Input)- Intervals of equal lengths are used just as in the case of
dataBounds, except the upper and lower bounds are taken as the user supplied variablesxlo,xhi,ylo, andyhi, instead of the actual minima and maxima in the data. Therefore, the first and last intervals for both variables are semi-infinite in length. Argumentsnxandnymust be greater than or equal to 3.
or
cutpoints, floatcx[], floatcy[](Input)- If
cutpointsis specified, cutpoints (boundaries) must be provided in the arrayscxandcy, of length (nx-1)and (ny-1)respectively. The tally intable[0]is the number of observations for which thexvalue is less than or equal tocx[0], and theyvalue is less than or equal tocy[0]. This option allows unequal interval lengths. Argumentsnxandnymust be greater than or equal to 2.
or
classMarks, floatcx[], floatcy[](Input)If
classMarksis specified, equally spaced class marks in ascending order must be provided in the arrayscxandcy. The class marks are the midpoints of each interval. Each interval is taken to have lengthcx[1]−cx[0]in thexdirection andcy[1]−cy[0]in theydirection. The total number of elements intablemay be less thannObservations. Argumentsnxandnymust be greater than or equal to 2.None or exactly one of the four optional arguments described above can be specified in order to define the intervals or bins for the one-way table.
Examples¶
Example 1¶
The data for x in this example are the same as those used in the
examples for tableOneway. The data for y was created
by adding small integers to the data in x. This example uses the default
tally method, dataBounds, which may be appropriate when the range of the
data is unknown.
from numpy import *
from pyimsl.stat.tableTwoway import tableTwoway
from pyimsl.stat.writeMatrix import writeMatrix
n_intervals = 10
x = array([0.77, 1.74, 0.81, 1.20, 1.95, 1.20, 0.47, 1.43, 3.37,
2.20, 3.00, 3.09, 1.51, 2.10, 0.52, 1.62, 1.31, 0.32,
0.59, 0.81, 2.81, 1.87, 1.18, 1.35, 4.75, 2.48, 0.96,
1.89, 0.90, 2.05])
y = array([1.77, 3.74, 3.81, 2.20, 3.95, 4.20, 1.47, 3.43, 6.37,
3.20, 5.00, 6.09, 2.51, 4.10, 3.52, 2.62, 3.31, 3.32,
1.59, 2.81, 5.81, 2.87, 3.18, 4.35, 5.75, 4.48, 3.96,
2.89, 2.90, 5.05])
table = tableTwoway(x, y, 5, 6)
writeMatrix("Counts", table,
rowNumberZero=True, colNumberZero=True, writeFormat="%8i")
Output¶
Counts
0 1 2 3 4 5
0 4 2 4 2 0 0
1 0 4 3 2 1 0
2 0 0 1 2 0 1
3 0 0 0 0 1 2
4 0 0 0 0 0 1
Example 2¶
In this example, xlo, xhi, ylo, and yhi are chosen so that
the intervals will be 0 to 1, 1 to 2, and so on for x, and 1 to 2, 2 to
3, and so on for y.
from numpy import *
from pyimsl.stat.tableTwoway import tableTwoway
from pyimsl.stat.writeMatrix import writeMatrix
n_intervals = 10
x = array([0.77, 1.74, 0.81, 1.20, 1.95, 1.20, 0.47, 1.43, 3.37,
2.20, 3.00, 3.09, 1.51, 2.10, 0.52, 1.62, 1.31, 0.32,
0.59, 0.81, 2.81, 1.87, 1.18, 1.35, 4.75, 2.48, 0.96,
1.89, 0.90, 2.05])
y = array([1.77, 3.74, 3.81, 2.20, 3.95, 4.20, 1.47, 3.43, 6.37,
3.20, 5.00, 6.09, 2.51, 4.10, 3.52, 2.62, 3.31, 3.32,
1.59, 2.81, 5.81, 2.87, 3.18, 4.35, 5.75, 4.48, 3.96,
2.89, 2.90, 5.05])
knownBounds = {}
knownBounds["xlo"] = 1.0
knownBounds["xhi"] = 4.0
knownBounds["ylo"] = 2.0
knownBounds["yhi"] = 6.0
table = tableTwoway(x, y, 5, 6, knownBounds=knownBounds)
writeMatrix("Counts", table,
rowNumberZero=True, colNumberZero=True, writeFormat="%8i")
Output¶
Counts
0 1 2 3 4 5
0 3 2 4 0 0 0
1 0 5 5 2 0 0
2 0 0 1 3 2 0
3 0 0 0 0 0 2
4 0 0 0 0 1 0
Example 3¶
In this example, the class boundaries are input in cx and cy. The
same intervals are chosen as in Example 2, where the first element of cx
and cy specify the first cutpoint between classes.
from numpy import *
from pyimsl.stat.tableTwoway import tableTwoway
from pyimsl.stat.writeMatrix import writeMatrix
n_intervals = 10
cmx = [0.5, 1.5, 2.5, 3.5, 4.5]
cmy = [1.5, 2.5, 3.5, 4.5, 5.5, 6.5]
x = array([0.77, 1.74, 0.81, 1.20, 1.95, 1.20, 0.47, 1.43, 3.37,
2.20, 3.00, 3.09, 1.51, 2.10, 0.52, 1.62, 1.31, 0.32,
0.59, 0.81, 2.81, 1.87, 1.18, 1.35, 4.75, 2.48, 0.96,
1.89, 0.90, 2.05])
y = array([1.77, 3.74, 3.81, 2.20, 3.95, 4.20, 1.47, 3.43, 6.37,
3.20, 5.00, 6.09, 2.51, 4.10, 3.52, 2.62, 3.31, 3.32,
1.59, 2.81, 5.81, 2.87, 3.18, 4.35, 5.75, 4.48, 3.96,
2.89, 2.90, 5.05])
classMarks = {}
classMarks["cx"] = cmx
classMarks["cy"] = cmy
table = tableTwoway(x, y, 5, 6, classMarks=classMarks)
writeMatrix("Counts", table,
rowNumberZero=True, colNumberZero=True, writeFormat="%8i")
Output¶
Counts
0 1 2 3 4 5
0 3 2 4 0 0 0
1 0 5 5 2 0 0
2 0 0 1 3 2 0
3 0 0 0 0 0 2
4 0 0 0 0 1 0
Example 4¶
This example, uses the cutpoints tally option with cutpoints such that
the intervals are specified as in the previous examples.
from numpy import *
from pyimsl.stat.tableTwoway import tableTwoway
from pyimsl.stat.writeMatrix import writeMatrix
n_intervals = 10
cpx = [1, 2, 3, 4]
cpy = [2, 3, 4, 5, 6]
cmx = [0.5, 1.5, 2.5, 3.5, 4.5]
cmy = [1.5, 2.5, 3.5, 4.5, 5.5, 6.5]
x = array([0.77, 1.74, 0.81, 1.20, 1.95, 1.20, 0.47, 1.43, 3.37,
2.20, 3.00, 3.09, 1.51, 2.10, 0.52, 1.62, 1.31, 0.32,
0.59, 0.81, 2.81, 1.87, 1.18, 1.35, 4.75, 2.48, 0.96,
1.89, 0.90, 2.05])
y = array([1.77, 3.74, 3.81, 2.20, 3.95, 4.20, 1.47, 3.43, 6.37,
3.20, 5.00, 6.09, 2.51, 4.10, 3.52, 2.62, 3.31, 3.32,
1.59, 2.81, 5.81, 2.87, 3.18, 4.35, 5.75, 4.48, 3.96,
2.89, 2.90, 5.05])
cutpoints = {}
cutpoints["cx"] = cpx
cutpoints["cy"] = cpy
table = tableTwoway(x, y, 5, 6, cutpoints=cutpoints)
writeMatrix("Counts", table,
rowNumberZero=True, colNumberZero=True, writeFormat="%8i")
Output¶
Counts
0 1 2 3 4 5
0 3 2 4 0 0 0
1 0 5 5 2 0 0
2 0 0 1 3 2 0
3 0 0 0 0 0 2
4 0 0 0 0 1 0