id
int32 0
165k
| repo
stringlengths 7
58
| path
stringlengths 12
218
| func_name
stringlengths 3
140
| original_string
stringlengths 73
34.1k
| language
stringclasses 1
value | code
stringlengths 73
34.1k
| code_tokens
list | docstring
stringlengths 3
16k
| docstring_tokens
list | sha
stringlengths 40
40
| url
stringlengths 105
339
|
|---|---|---|---|---|---|---|---|---|---|---|---|
17,300
|
haifengl/smile
|
math/src/main/java/smile/stat/distribution/GaussianDistribution.java
|
GaussianDistribution.randInverseCDF
|
public double randInverseCDF() {
final double a0 = 2.50662823884;
final double a1 = -18.61500062529;
final double a2 = 41.39119773534;
final double a3 = -25.44106049637;
final double b0 = -8.47351093090;
final double b1 = 23.08336743743;
final double b2 = -21.06224101826;
final double b3 = 3.13082909833;
final double c0 = 0.3374754822726147;
final double c1 = 0.9761690190917186;
final double c2 = 0.1607979714918209;
final double c3 = 0.0276438810333863;
final double c4 = 0.0038405729373609;
final double c5 = 0.0003951896511919;
final double c6 = 0.0000321767881768;
final double c7 = 0.0000002888167364;
final double c8 = 0.0000003960315187;
double y, r, x;
double u = Math.random();
while (u == 0.0) {
u = Math.random();
}
y = u - 0.5;
if (Math.abs(y) < 0.42) {
r = y * y;
x = y * (((a3 * r + a2) * r + a1) * r + a0)
/ ((((b3 * r + b2) * r + b1) * r + b0) * r + 1);
} else {
r = u;
if (y > 0) {
r = 1 - u;
}
r = Math.log(-Math.log(r));
x = c0 + r * (c1 + r * (c2 + r * (c3 + r * (c4 + r * (c5 + r * (c6 + r * (c7 + r * c8)))))));
if (y < 0) {
x = -(x);
}
}
return mu + sigma * x;
}
|
java
|
public double randInverseCDF() {
final double a0 = 2.50662823884;
final double a1 = -18.61500062529;
final double a2 = 41.39119773534;
final double a3 = -25.44106049637;
final double b0 = -8.47351093090;
final double b1 = 23.08336743743;
final double b2 = -21.06224101826;
final double b3 = 3.13082909833;
final double c0 = 0.3374754822726147;
final double c1 = 0.9761690190917186;
final double c2 = 0.1607979714918209;
final double c3 = 0.0276438810333863;
final double c4 = 0.0038405729373609;
final double c5 = 0.0003951896511919;
final double c6 = 0.0000321767881768;
final double c7 = 0.0000002888167364;
final double c8 = 0.0000003960315187;
double y, r, x;
double u = Math.random();
while (u == 0.0) {
u = Math.random();
}
y = u - 0.5;
if (Math.abs(y) < 0.42) {
r = y * y;
x = y * (((a3 * r + a2) * r + a1) * r + a0)
/ ((((b3 * r + b2) * r + b1) * r + b0) * r + 1);
} else {
r = u;
if (y > 0) {
r = 1 - u;
}
r = Math.log(-Math.log(r));
x = c0 + r * (c1 + r * (c2 + r * (c3 + r * (c4 + r * (c5 + r * (c6 + r * (c7 + r * c8)))))));
if (y < 0) {
x = -(x);
}
}
return mu + sigma * x;
}
|
[
"public",
"double",
"randInverseCDF",
"(",
")",
"{",
"final",
"double",
"a0",
"=",
"2.50662823884",
";",
"final",
"double",
"a1",
"=",
"-",
"18.61500062529",
";",
"final",
"double",
"a2",
"=",
"41.39119773534",
";",
"final",
"double",
"a3",
"=",
"-",
"25.44106049637",
";",
"final",
"double",
"b0",
"=",
"-",
"8.47351093090",
";",
"final",
"double",
"b1",
"=",
"23.08336743743",
";",
"final",
"double",
"b2",
"=",
"-",
"21.06224101826",
";",
"final",
"double",
"b3",
"=",
"3.13082909833",
";",
"final",
"double",
"c0",
"=",
"0.3374754822726147",
";",
"final",
"double",
"c1",
"=",
"0.9761690190917186",
";",
"final",
"double",
"c2",
"=",
"0.1607979714918209",
";",
"final",
"double",
"c3",
"=",
"0.0276438810333863",
";",
"final",
"double",
"c4",
"=",
"0.0038405729373609",
";",
"final",
"double",
"c5",
"=",
"0.0003951896511919",
";",
"final",
"double",
"c6",
"=",
"0.0000321767881768",
";",
"final",
"double",
"c7",
"=",
"0.0000002888167364",
";",
"final",
"double",
"c8",
"=",
"0.0000003960315187",
";",
"double",
"y",
",",
"r",
",",
"x",
";",
"double",
"u",
"=",
"Math",
".",
"random",
"(",
")",
";",
"while",
"(",
"u",
"==",
"0.0",
")",
"{",
"u",
"=",
"Math",
".",
"random",
"(",
")",
";",
"}",
"y",
"=",
"u",
"-",
"0.5",
";",
"if",
"(",
"Math",
".",
"abs",
"(",
"y",
")",
"<",
"0.42",
")",
"{",
"r",
"=",
"y",
"*",
"y",
";",
"x",
"=",
"y",
"*",
"(",
"(",
"(",
"a3",
"*",
"r",
"+",
"a2",
")",
"*",
"r",
"+",
"a1",
")",
"*",
"r",
"+",
"a0",
")",
"/",
"(",
"(",
"(",
"(",
"b3",
"*",
"r",
"+",
"b2",
")",
"*",
"r",
"+",
"b1",
")",
"*",
"r",
"+",
"b0",
")",
"*",
"r",
"+",
"1",
")",
";",
"}",
"else",
"{",
"r",
"=",
"u",
";",
"if",
"(",
"y",
">",
"0",
")",
"{",
"r",
"=",
"1",
"-",
"u",
";",
"}",
"r",
"=",
"Math",
".",
"log",
"(",
"-",
"Math",
".",
"log",
"(",
"r",
")",
")",
";",
"x",
"=",
"c0",
"+",
"r",
"*",
"(",
"c1",
"+",
"r",
"*",
"(",
"c2",
"+",
"r",
"*",
"(",
"c3",
"+",
"r",
"*",
"(",
"c4",
"+",
"r",
"*",
"(",
"c5",
"+",
"r",
"*",
"(",
"c6",
"+",
"r",
"*",
"(",
"c7",
"+",
"r",
"*",
"c8",
")",
")",
")",
")",
")",
")",
")",
";",
"if",
"(",
"y",
"<",
"0",
")",
"{",
"x",
"=",
"-",
"(",
"x",
")",
";",
"}",
"}",
"return",
"mu",
"+",
"sigma",
"*",
"x",
";",
"}"
] |
Uses Inverse CDF method to generate a Gaussian deviate.
|
[
"Uses",
"Inverse",
"CDF",
"method",
"to",
"generate",
"a",
"Gaussian",
"deviate",
"."
] |
e27e43e90fbaacce3f99d30120cf9dd6a764c33d
|
https://github.com/haifengl/smile/blob/e27e43e90fbaacce3f99d30120cf9dd6a764c33d/math/src/main/java/smile/stat/distribution/GaussianDistribution.java#L172-L218
|
17,301
|
haifengl/smile
|
core/src/main/java/smile/classification/AdaBoost.java
|
AdaBoost.predict
|
@Override
public int predict(double[] x, double[] posteriori) {
Arrays.fill(posteriori, 0.0);
for (int i = 0; i < trees.length; i++) {
posteriori[trees[i].predict(x)] += alpha[i];
}
double sum = Math.sum(posteriori);
for (int i = 0; i < k; i++) {
posteriori[i] /= sum;
}
return Math.whichMax(posteriori);
}
|
java
|
@Override
public int predict(double[] x, double[] posteriori) {
Arrays.fill(posteriori, 0.0);
for (int i = 0; i < trees.length; i++) {
posteriori[trees[i].predict(x)] += alpha[i];
}
double sum = Math.sum(posteriori);
for (int i = 0; i < k; i++) {
posteriori[i] /= sum;
}
return Math.whichMax(posteriori);
}
|
[
"@",
"Override",
"public",
"int",
"predict",
"(",
"double",
"[",
"]",
"x",
",",
"double",
"[",
"]",
"posteriori",
")",
"{",
"Arrays",
".",
"fill",
"(",
"posteriori",
",",
"0.0",
")",
";",
"for",
"(",
"int",
"i",
"=",
"0",
";",
"i",
"<",
"trees",
".",
"length",
";",
"i",
"++",
")",
"{",
"posteriori",
"[",
"trees",
"[",
"i",
"]",
".",
"predict",
"(",
"x",
")",
"]",
"+=",
"alpha",
"[",
"i",
"]",
";",
"}",
"double",
"sum",
"=",
"Math",
".",
"sum",
"(",
"posteriori",
")",
";",
"for",
"(",
"int",
"i",
"=",
"0",
";",
"i",
"<",
"k",
";",
"i",
"++",
")",
"{",
"posteriori",
"[",
"i",
"]",
"/=",
"sum",
";",
"}",
"return",
"Math",
".",
"whichMax",
"(",
"posteriori",
")",
";",
"}"
] |
Predicts the class label of an instance and also calculate a posteriori
probabilities. Not supported.
|
[
"Predicts",
"the",
"class",
"label",
"of",
"an",
"instance",
"and",
"also",
"calculate",
"a",
"posteriori",
"probabilities",
".",
"Not",
"supported",
"."
] |
e27e43e90fbaacce3f99d30120cf9dd6a764c33d
|
https://github.com/haifengl/smile/blob/e27e43e90fbaacce3f99d30120cf9dd6a764c33d/core/src/main/java/smile/classification/AdaBoost.java#L413-L427
|
17,302
|
haifengl/smile
|
math/src/main/java/smile/sort/IQAgent.java
|
IQAgent.quantile
|
public double quantile(double p) {
if (nd > 0) {
update();
}
int jl = 0, jh = nq - 1, j;
while (jh - jl > 1) {
j = (jh + jl) >> 1;
if (p > pval[j]) {
jl = j;
} else {
jh = j;
}
}
j = jl;
double q = qile[j] + (qile[j + 1] - qile[j]) * (p - pval[j]) / (pval[j + 1] - pval[j]);
return Math.max(qile[0], Math.min(qile[nq - 1], q));
}
|
java
|
public double quantile(double p) {
if (nd > 0) {
update();
}
int jl = 0, jh = nq - 1, j;
while (jh - jl > 1) {
j = (jh + jl) >> 1;
if (p > pval[j]) {
jl = j;
} else {
jh = j;
}
}
j = jl;
double q = qile[j] + (qile[j + 1] - qile[j]) * (p - pval[j]) / (pval[j + 1] - pval[j]);
return Math.max(qile[0], Math.min(qile[nq - 1], q));
}
|
[
"public",
"double",
"quantile",
"(",
"double",
"p",
")",
"{",
"if",
"(",
"nd",
">",
"0",
")",
"{",
"update",
"(",
")",
";",
"}",
"int",
"jl",
"=",
"0",
",",
"jh",
"=",
"nq",
"-",
"1",
",",
"j",
";",
"while",
"(",
"jh",
"-",
"jl",
">",
"1",
")",
"{",
"j",
"=",
"(",
"jh",
"+",
"jl",
")",
">>",
"1",
";",
"if",
"(",
"p",
">",
"pval",
"[",
"j",
"]",
")",
"{",
"jl",
"=",
"j",
";",
"}",
"else",
"{",
"jh",
"=",
"j",
";",
"}",
"}",
"j",
"=",
"jl",
";",
"double",
"q",
"=",
"qile",
"[",
"j",
"]",
"+",
"(",
"qile",
"[",
"j",
"+",
"1",
"]",
"-",
"qile",
"[",
"j",
"]",
")",
"*",
"(",
"p",
"-",
"pval",
"[",
"j",
"]",
")",
"/",
"(",
"pval",
"[",
"j",
"+",
"1",
"]",
"-",
"pval",
"[",
"j",
"]",
")",
";",
"return",
"Math",
".",
"max",
"(",
"qile",
"[",
"0",
"]",
",",
"Math",
".",
"min",
"(",
"qile",
"[",
"nq",
"-",
"1",
"]",
",",
"q",
")",
")",
";",
"}"
] |
Returns the estimated p-quantile for the data seen so far. For example,
p = 0.5 for median.
|
[
"Returns",
"the",
"estimated",
"p",
"-",
"quantile",
"for",
"the",
"data",
"seen",
"so",
"far",
".",
"For",
"example",
"p",
"=",
"0",
".",
"5",
"for",
"median",
"."
] |
e27e43e90fbaacce3f99d30120cf9dd6a764c33d
|
https://github.com/haifengl/smile/blob/e27e43e90fbaacce3f99d30120cf9dd6a764c33d/math/src/main/java/smile/sort/IQAgent.java#L149-L165
|
17,303
|
haifengl/smile
|
nlp/src/main/java/smile/nlp/Trie.java
|
Trie.put
|
public void put(K[] key, V value) {
Node child = root.get(key[0]);
if (child == null) {
child = new Node(key[0]);
root.put(key[0], child);
}
child.addChild(key, value, 1);
}
|
java
|
public void put(K[] key, V value) {
Node child = root.get(key[0]);
if (child == null) {
child = new Node(key[0]);
root.put(key[0], child);
}
child.addChild(key, value, 1);
}
|
[
"public",
"void",
"put",
"(",
"K",
"[",
"]",
"key",
",",
"V",
"value",
")",
"{",
"Node",
"child",
"=",
"root",
".",
"get",
"(",
"key",
"[",
"0",
"]",
")",
";",
"if",
"(",
"child",
"==",
"null",
")",
"{",
"child",
"=",
"new",
"Node",
"(",
"key",
"[",
"0",
"]",
")",
";",
"root",
".",
"put",
"(",
"key",
"[",
"0",
"]",
",",
"child",
")",
";",
"}",
"child",
".",
"addChild",
"(",
"key",
",",
"value",
",",
"1",
")",
";",
"}"
] |
Add a key with associated value to the trie.
@param key the key.
@param value the value.
|
[
"Add",
"a",
"key",
"with",
"associated",
"value",
"to",
"the",
"trie",
"."
] |
e27e43e90fbaacce3f99d30120cf9dd6a764c33d
|
https://github.com/haifengl/smile/blob/e27e43e90fbaacce3f99d30120cf9dd6a764c33d/nlp/src/main/java/smile/nlp/Trie.java#L136-L143
|
17,304
|
haifengl/smile
|
nlp/src/main/java/smile/nlp/Trie.java
|
Trie.get
|
public V get(K[] key) {
Node child = root.get(key[0]);
if (child != null) {
return child.getChild(key, 1);
}
return null;
}
|
java
|
public V get(K[] key) {
Node child = root.get(key[0]);
if (child != null) {
return child.getChild(key, 1);
}
return null;
}
|
[
"public",
"V",
"get",
"(",
"K",
"[",
"]",
"key",
")",
"{",
"Node",
"child",
"=",
"root",
".",
"get",
"(",
"key",
"[",
"0",
"]",
")",
";",
"if",
"(",
"child",
"!=",
"null",
")",
"{",
"return",
"child",
".",
"getChild",
"(",
"key",
",",
"1",
")",
";",
"}",
"return",
"null",
";",
"}"
] |
Returns the associated value of a given key.
Returns null if the key doesn't exist in the trie.
@param key the key.
@return the associated value or null.
|
[
"Returns",
"the",
"associated",
"value",
"of",
"a",
"given",
"key",
".",
"Returns",
"null",
"if",
"the",
"key",
"doesn",
"t",
"exist",
"in",
"the",
"trie",
"."
] |
e27e43e90fbaacce3f99d30120cf9dd6a764c33d
|
https://github.com/haifengl/smile/blob/e27e43e90fbaacce3f99d30120cf9dd6a764c33d/nlp/src/main/java/smile/nlp/Trie.java#L151-L157
|
17,305
|
haifengl/smile
|
math/src/main/java/smile/math/matrix/LU.java
|
LU.inverse
|
public DenseMatrix inverse() {
int m = lu.nrows();
int n = lu.ncols();
if (m != n) {
throw new IllegalArgumentException(String.format("Matrix is not square: %d x %d", m, n));
}
DenseMatrix inv = Matrix.zeros(n, n);
for (int i = 0; i < n; i++) {
inv.set(i, piv[i], 1.0);
}
solve(inv);
return inv;
}
|
java
|
public DenseMatrix inverse() {
int m = lu.nrows();
int n = lu.ncols();
if (m != n) {
throw new IllegalArgumentException(String.format("Matrix is not square: %d x %d", m, n));
}
DenseMatrix inv = Matrix.zeros(n, n);
for (int i = 0; i < n; i++) {
inv.set(i, piv[i], 1.0);
}
solve(inv);
return inv;
}
|
[
"public",
"DenseMatrix",
"inverse",
"(",
")",
"{",
"int",
"m",
"=",
"lu",
".",
"nrows",
"(",
")",
";",
"int",
"n",
"=",
"lu",
".",
"ncols",
"(",
")",
";",
"if",
"(",
"m",
"!=",
"n",
")",
"{",
"throw",
"new",
"IllegalArgumentException",
"(",
"String",
".",
"format",
"(",
"\"Matrix is not square: %d x %d\"",
",",
"m",
",",
"n",
")",
")",
";",
"}",
"DenseMatrix",
"inv",
"=",
"Matrix",
".",
"zeros",
"(",
"n",
",",
"n",
")",
";",
"for",
"(",
"int",
"i",
"=",
"0",
";",
"i",
"<",
"n",
";",
"i",
"++",
")",
"{",
"inv",
".",
"set",
"(",
"i",
",",
"piv",
"[",
"i",
"]",
",",
"1.0",
")",
";",
"}",
"solve",
"(",
"inv",
")",
";",
"return",
"inv",
";",
"}"
] |
Returns the matrix inverse. For pseudo inverse, use QRDecomposition.
|
[
"Returns",
"the",
"matrix",
"inverse",
".",
"For",
"pseudo",
"inverse",
"use",
"QRDecomposition",
"."
] |
e27e43e90fbaacce3f99d30120cf9dd6a764c33d
|
https://github.com/haifengl/smile/blob/e27e43e90fbaacce3f99d30120cf9dd6a764c33d/math/src/main/java/smile/math/matrix/LU.java#L118-L133
|
17,306
|
haifengl/smile
|
core/src/main/java/smile/clustering/GMeans.java
|
GMeans.AndersonDarling
|
private static double AndersonDarling(double[] x) {
int n = x.length;
Arrays.sort(x);
for (int i = 0; i < n; i++) {
x[i] = GaussianDistribution.getInstance().cdf(x[i]);
// in case overflow when taking log later.
if (x[i] == 0) x[i] = 0.0000001;
if (x[i] == 1) x[i] = 0.9999999;
}
double A = 0.0;
for (int i = 0; i < n; i++) {
A -= (2*i+1) * (Math.log(x[i]) + Math.log(1-x[n-i-1]));
}
A = A / n - n;
A *= (1 + 4.0/n - 25.0/(n*n));
return A;
}
|
java
|
private static double AndersonDarling(double[] x) {
int n = x.length;
Arrays.sort(x);
for (int i = 0; i < n; i++) {
x[i] = GaussianDistribution.getInstance().cdf(x[i]);
// in case overflow when taking log later.
if (x[i] == 0) x[i] = 0.0000001;
if (x[i] == 1) x[i] = 0.9999999;
}
double A = 0.0;
for (int i = 0; i < n; i++) {
A -= (2*i+1) * (Math.log(x[i]) + Math.log(1-x[n-i-1]));
}
A = A / n - n;
A *= (1 + 4.0/n - 25.0/(n*n));
return A;
}
|
[
"private",
"static",
"double",
"AndersonDarling",
"(",
"double",
"[",
"]",
"x",
")",
"{",
"int",
"n",
"=",
"x",
".",
"length",
";",
"Arrays",
".",
"sort",
"(",
"x",
")",
";",
"for",
"(",
"int",
"i",
"=",
"0",
";",
"i",
"<",
"n",
";",
"i",
"++",
")",
"{",
"x",
"[",
"i",
"]",
"=",
"GaussianDistribution",
".",
"getInstance",
"(",
")",
".",
"cdf",
"(",
"x",
"[",
"i",
"]",
")",
";",
"// in case overflow when taking log later.",
"if",
"(",
"x",
"[",
"i",
"]",
"==",
"0",
")",
"x",
"[",
"i",
"]",
"=",
"0.0000001",
";",
"if",
"(",
"x",
"[",
"i",
"]",
"==",
"1",
")",
"x",
"[",
"i",
"]",
"=",
"0.9999999",
";",
"}",
"double",
"A",
"=",
"0.0",
";",
"for",
"(",
"int",
"i",
"=",
"0",
";",
"i",
"<",
"n",
";",
"i",
"++",
")",
"{",
"A",
"-=",
"(",
"2",
"*",
"i",
"+",
"1",
")",
"*",
"(",
"Math",
".",
"log",
"(",
"x",
"[",
"i",
"]",
")",
"+",
"Math",
".",
"log",
"(",
"1",
"-",
"x",
"[",
"n",
"-",
"i",
"-",
"1",
"]",
")",
")",
";",
"}",
"A",
"=",
"A",
"/",
"n",
"-",
"n",
";",
"A",
"*=",
"(",
"1",
"+",
"4.0",
"/",
"n",
"-",
"25.0",
"/",
"(",
"n",
"*",
"n",
")",
")",
";",
"return",
"A",
";",
"}"
] |
Calculates the Anderson-Darling statistic for one-dimensional normality test.
@param x the samples to test if drawn from a Gaussian distribution.
|
[
"Calculates",
"the",
"Anderson",
"-",
"Darling",
"statistic",
"for",
"one",
"-",
"dimensional",
"normality",
"test",
"."
] |
e27e43e90fbaacce3f99d30120cf9dd6a764c33d
|
https://github.com/haifengl/smile/blob/e27e43e90fbaacce3f99d30120cf9dd6a764c33d/core/src/main/java/smile/clustering/GMeans.java#L183-L203
|
17,307
|
haifengl/smile
|
demo/src/main/java/smile/demo/util/ParameterParser.java
|
ParameterParser.get
|
public String get(String name) {
Parameter param = parameters.get(name);
if (param != null)
return param.value;
else
return null;
}
|
java
|
public String get(String name) {
Parameter param = parameters.get(name);
if (param != null)
return param.value;
else
return null;
}
|
[
"public",
"String",
"get",
"(",
"String",
"name",
")",
"{",
"Parameter",
"param",
"=",
"parameters",
".",
"get",
"(",
"name",
")",
";",
"if",
"(",
"param",
"!=",
"null",
")",
"return",
"param",
".",
"value",
";",
"else",
"return",
"null",
";",
"}"
] |
Returns the value of a given parameter. Null if the parameter
does not present.
|
[
"Returns",
"the",
"value",
"of",
"a",
"given",
"parameter",
".",
"Null",
"if",
"the",
"parameter",
"does",
"not",
"present",
"."
] |
e27e43e90fbaacce3f99d30120cf9dd6a764c33d
|
https://github.com/haifengl/smile/blob/e27e43e90fbaacce3f99d30120cf9dd6a764c33d/demo/src/main/java/smile/demo/util/ParameterParser.java#L121-L127
|
17,308
|
haifengl/smile
|
demo/src/main/java/smile/demo/util/ParameterParser.java
|
ParameterParser.filterMonadics
|
private List<String> filterMonadics(String[] args) {// name-value for monads
List<String> filteredArgs = new ArrayList<>(); // Y <- return List
for (String arg : args) { // iterate over args
filteredArgs.add(arg);
Parameter param = parameters.get(arg);
if (param != null && param.paramType == ParameterType.MONADIC) {
filteredArgs.add("1"); // insert a value to "1"
}
}
return filteredArgs; // return List of args
}
|
java
|
private List<String> filterMonadics(String[] args) {// name-value for monads
List<String> filteredArgs = new ArrayList<>(); // Y <- return List
for (String arg : args) { // iterate over args
filteredArgs.add(arg);
Parameter param = parameters.get(arg);
if (param != null && param.paramType == ParameterType.MONADIC) {
filteredArgs.add("1"); // insert a value to "1"
}
}
return filteredArgs; // return List of args
}
|
[
"private",
"List",
"<",
"String",
">",
"filterMonadics",
"(",
"String",
"[",
"]",
"args",
")",
"{",
"// name-value for monads",
"List",
"<",
"String",
">",
"filteredArgs",
"=",
"new",
"ArrayList",
"<>",
"(",
")",
";",
"// Y <- return List",
"for",
"(",
"String",
"arg",
":",
"args",
")",
"{",
"// iterate over args",
"filteredArgs",
".",
"add",
"(",
"arg",
")",
";",
"Parameter",
"param",
"=",
"parameters",
".",
"get",
"(",
"arg",
")",
";",
"if",
"(",
"param",
"!=",
"null",
"&&",
"param",
".",
"paramType",
"==",
"ParameterType",
".",
"MONADIC",
")",
"{",
"filteredArgs",
".",
"add",
"(",
"\"1\"",
")",
";",
"// insert a value to \"1\"",
"}",
"}",
"return",
"filteredArgs",
";",
"// return List of args",
"}"
] |
Parses the args array looking for monads, arguments without value
such as "-verbose" and if found inserts, forces, a value of "1" and
returns the transformed args as a List
@param args array of input values
@return a list of expanded name-value pair if monads are detected
|
[
"Parses",
"the",
"args",
"array",
"looking",
"for",
"monads",
"arguments",
"without",
"value",
"such",
"as",
"-",
"verbose",
"and",
"if",
"found",
"inserts",
"forces",
"a",
"value",
"of",
"1",
"and",
"returns",
"the",
"transformed",
"args",
"as",
"a",
"List"
] |
e27e43e90fbaacce3f99d30120cf9dd6a764c33d
|
https://github.com/haifengl/smile/blob/e27e43e90fbaacce3f99d30120cf9dd6a764c33d/demo/src/main/java/smile/demo/util/ParameterParser.java#L137-L147
|
17,309
|
haifengl/smile
|
demo/src/main/java/smile/demo/util/ParameterParser.java
|
ParameterParser.parse
|
public List<String> parse(String[] args) { // merge args & defaults
List<String> extras = new ArrayList<>();
List<String> filteredArgs = filterMonadics(args); // detect and fill mons
for (int i = 0; i < filteredArgs.size(); i++) {
String key = filteredArgs.get(i);
if (key.equalsIgnoreCase("-h") || key.equalsIgnoreCase("-help")) {
System.out.println(usage);
System.exit(0);
}
if (parameters.containsKey(key)) {
Parameter param = parameters.get(key);
param.value = filteredArgs.get(i+1);
switch(param.paramType) {
case INTEGER:
try {
Integer.parseInt(param.value);
} catch (Exception ex) {
System.err.println("Invalid parameter " + param.name + ' ' + param.value);
System.err.println(usage);
System.exit(1);
}
break;
case FLOAT:
try {
Double.parseDouble(param.value);
} catch (Exception ex) {
System.err.println("Invalid parameter " + param.name + ' ' + param.value);
System.err.println(usage);
System.exit(1);
}
break;
default:
// Just to remove unmatched case warning
}
i++;
} else {
extras.add(key);
}
}
for(String key : parameters.keySet()) {
Parameter param = parameters.get(key);
if (param.mandatory && param.value == null) {
System.err.println("Missing mandatory parameter: " + key);
System.err.println(usage);
System.exit(1);
}
}
return extras; // parsed + defaults
}
|
java
|
public List<String> parse(String[] args) { // merge args & defaults
List<String> extras = new ArrayList<>();
List<String> filteredArgs = filterMonadics(args); // detect and fill mons
for (int i = 0; i < filteredArgs.size(); i++) {
String key = filteredArgs.get(i);
if (key.equalsIgnoreCase("-h") || key.equalsIgnoreCase("-help")) {
System.out.println(usage);
System.exit(0);
}
if (parameters.containsKey(key)) {
Parameter param = parameters.get(key);
param.value = filteredArgs.get(i+1);
switch(param.paramType) {
case INTEGER:
try {
Integer.parseInt(param.value);
} catch (Exception ex) {
System.err.println("Invalid parameter " + param.name + ' ' + param.value);
System.err.println(usage);
System.exit(1);
}
break;
case FLOAT:
try {
Double.parseDouble(param.value);
} catch (Exception ex) {
System.err.println("Invalid parameter " + param.name + ' ' + param.value);
System.err.println(usage);
System.exit(1);
}
break;
default:
// Just to remove unmatched case warning
}
i++;
} else {
extras.add(key);
}
}
for(String key : parameters.keySet()) {
Parameter param = parameters.get(key);
if (param.mandatory && param.value == null) {
System.err.println("Missing mandatory parameter: " + key);
System.err.println(usage);
System.exit(1);
}
}
return extras; // parsed + defaults
}
|
[
"public",
"List",
"<",
"String",
">",
"parse",
"(",
"String",
"[",
"]",
"args",
")",
"{",
"// merge args & defaults",
"List",
"<",
"String",
">",
"extras",
"=",
"new",
"ArrayList",
"<>",
"(",
")",
";",
"List",
"<",
"String",
">",
"filteredArgs",
"=",
"filterMonadics",
"(",
"args",
")",
";",
"// detect and fill mons",
"for",
"(",
"int",
"i",
"=",
"0",
";",
"i",
"<",
"filteredArgs",
".",
"size",
"(",
")",
";",
"i",
"++",
")",
"{",
"String",
"key",
"=",
"filteredArgs",
".",
"get",
"(",
"i",
")",
";",
"if",
"(",
"key",
".",
"equalsIgnoreCase",
"(",
"\"-h\"",
")",
"||",
"key",
".",
"equalsIgnoreCase",
"(",
"\"-help\"",
")",
")",
"{",
"System",
".",
"out",
".",
"println",
"(",
"usage",
")",
";",
"System",
".",
"exit",
"(",
"0",
")",
";",
"}",
"if",
"(",
"parameters",
".",
"containsKey",
"(",
"key",
")",
")",
"{",
"Parameter",
"param",
"=",
"parameters",
".",
"get",
"(",
"key",
")",
";",
"param",
".",
"value",
"=",
"filteredArgs",
".",
"get",
"(",
"i",
"+",
"1",
")",
";",
"switch",
"(",
"param",
".",
"paramType",
")",
"{",
"case",
"INTEGER",
":",
"try",
"{",
"Integer",
".",
"parseInt",
"(",
"param",
".",
"value",
")",
";",
"}",
"catch",
"(",
"Exception",
"ex",
")",
"{",
"System",
".",
"err",
".",
"println",
"(",
"\"Invalid parameter \"",
"+",
"param",
".",
"name",
"+",
"'",
"'",
"+",
"param",
".",
"value",
")",
";",
"System",
".",
"err",
".",
"println",
"(",
"usage",
")",
";",
"System",
".",
"exit",
"(",
"1",
")",
";",
"}",
"break",
";",
"case",
"FLOAT",
":",
"try",
"{",
"Double",
".",
"parseDouble",
"(",
"param",
".",
"value",
")",
";",
"}",
"catch",
"(",
"Exception",
"ex",
")",
"{",
"System",
".",
"err",
".",
"println",
"(",
"\"Invalid parameter \"",
"+",
"param",
".",
"name",
"+",
"'",
"'",
"+",
"param",
".",
"value",
")",
";",
"System",
".",
"err",
".",
"println",
"(",
"usage",
")",
";",
"System",
".",
"exit",
"(",
"1",
")",
";",
"}",
"break",
";",
"default",
":",
"// Just to remove unmatched case warning",
"}",
"i",
"++",
";",
"}",
"else",
"{",
"extras",
".",
"add",
"(",
"key",
")",
";",
"}",
"}",
"for",
"(",
"String",
"key",
":",
"parameters",
".",
"keySet",
"(",
")",
")",
"{",
"Parameter",
"param",
"=",
"parameters",
".",
"get",
"(",
"key",
")",
";",
"if",
"(",
"param",
".",
"mandatory",
"&&",
"param",
".",
"value",
"==",
"null",
")",
"{",
"System",
".",
"err",
".",
"println",
"(",
"\"Missing mandatory parameter: \"",
"+",
"key",
")",
";",
"System",
".",
"err",
".",
"println",
"(",
"usage",
")",
";",
"System",
".",
"exit",
"(",
"1",
")",
";",
"}",
"}",
"return",
"extras",
";",
"// parsed + defaults",
"}"
] |
Parse the arguments.
@param args arguments
@return a list of strings which are not parameters, e.g. a list of file
name.
|
[
"Parse",
"the",
"arguments",
"."
] |
e27e43e90fbaacce3f99d30120cf9dd6a764c33d
|
https://github.com/haifengl/smile/blob/e27e43e90fbaacce3f99d30120cf9dd6a764c33d/demo/src/main/java/smile/demo/util/ParameterParser.java#L155-L208
|
17,310
|
haifengl/smile
|
core/src/main/java/smile/classification/RandomForest.java
|
RandomForest.getTrees
|
public DecisionTree[] getTrees() {
DecisionTree[] forest = new DecisionTree[trees.size()];
for (int i = 0; i < forest.length; i++)
forest[i] = trees.get(i).tree;
return forest;
}
|
java
|
public DecisionTree[] getTrees() {
DecisionTree[] forest = new DecisionTree[trees.size()];
for (int i = 0; i < forest.length; i++)
forest[i] = trees.get(i).tree;
return forest;
}
|
[
"public",
"DecisionTree",
"[",
"]",
"getTrees",
"(",
")",
"{",
"DecisionTree",
"[",
"]",
"forest",
"=",
"new",
"DecisionTree",
"[",
"trees",
".",
"size",
"(",
")",
"]",
";",
"for",
"(",
"int",
"i",
"=",
"0",
";",
"i",
"<",
"forest",
".",
"length",
";",
"i",
"++",
")",
"forest",
"[",
"i",
"]",
"=",
"trees",
".",
"get",
"(",
"i",
")",
".",
"tree",
";",
"return",
"forest",
";",
"}"
] |
Returns the decision trees.
|
[
"Returns",
"the",
"decision",
"trees",
"."
] |
e27e43e90fbaacce3f99d30120cf9dd6a764c33d
|
https://github.com/haifengl/smile/blob/e27e43e90fbaacce3f99d30120cf9dd6a764c33d/core/src/main/java/smile/classification/RandomForest.java#L859-L865
|
17,311
|
haifengl/smile
|
plot/src/main/java/smile/swing/table/ButtonCellRenderer.java
|
ButtonCellRenderer.setMnemonic
|
public void setMnemonic(int mnemonic) {
this.mnemonic = mnemonic;
renderButton.setMnemonic(mnemonic);
editButton.setMnemonic(mnemonic);
}
|
java
|
public void setMnemonic(int mnemonic) {
this.mnemonic = mnemonic;
renderButton.setMnemonic(mnemonic);
editButton.setMnemonic(mnemonic);
}
|
[
"public",
"void",
"setMnemonic",
"(",
"int",
"mnemonic",
")",
"{",
"this",
".",
"mnemonic",
"=",
"mnemonic",
";",
"renderButton",
".",
"setMnemonic",
"(",
"mnemonic",
")",
";",
"editButton",
".",
"setMnemonic",
"(",
"mnemonic",
")",
";",
"}"
] |
The mnemonic to activate the button when the cell has focus
@param mnemonic the mnemonic
|
[
"The",
"mnemonic",
"to",
"activate",
"the",
"button",
"when",
"the",
"cell",
"has",
"focus"
] |
e27e43e90fbaacce3f99d30120cf9dd6a764c33d
|
https://github.com/haifengl/smile/blob/e27e43e90fbaacce3f99d30120cf9dd6a764c33d/plot/src/main/java/smile/swing/table/ButtonCellRenderer.java#L122-L126
|
17,312
|
haifengl/smile
|
core/src/main/java/smile/association/TotalSupportTree.java
|
TotalSupportTree.add
|
private void add(Node node, int size, int index, int[] itemset, int support) {
if (node.children == null) {
node.children = new Node[size];
}
int item = order[itemset[index]];
if (node.children[item] == null) {
node.children[item] = new Node(itemset[index]);
}
if (index == 0) {
node.children[item].support += support;
} else {
add(node.children[item], item, index - 1, itemset, support);
}
}
|
java
|
private void add(Node node, int size, int index, int[] itemset, int support) {
if (node.children == null) {
node.children = new Node[size];
}
int item = order[itemset[index]];
if (node.children[item] == null) {
node.children[item] = new Node(itemset[index]);
}
if (index == 0) {
node.children[item].support += support;
} else {
add(node.children[item], item, index - 1, itemset, support);
}
}
|
[
"private",
"void",
"add",
"(",
"Node",
"node",
",",
"int",
"size",
",",
"int",
"index",
",",
"int",
"[",
"]",
"itemset",
",",
"int",
"support",
")",
"{",
"if",
"(",
"node",
".",
"children",
"==",
"null",
")",
"{",
"node",
".",
"children",
"=",
"new",
"Node",
"[",
"size",
"]",
";",
"}",
"int",
"item",
"=",
"order",
"[",
"itemset",
"[",
"index",
"]",
"]",
";",
"if",
"(",
"node",
".",
"children",
"[",
"item",
"]",
"==",
"null",
")",
"{",
"node",
".",
"children",
"[",
"item",
"]",
"=",
"new",
"Node",
"(",
"itemset",
"[",
"index",
"]",
")",
";",
"}",
"if",
"(",
"index",
"==",
"0",
")",
"{",
"node",
".",
"children",
"[",
"item",
"]",
".",
"support",
"+=",
"support",
";",
"}",
"else",
"{",
"add",
"(",
"node",
".",
"children",
"[",
"item",
"]",
",",
"item",
",",
"index",
"-",
"1",
",",
"itemset",
",",
"support",
")",
";",
"}",
"}"
] |
Inserts a node into a T-tree.
@param node the root of subtree.
@param size the size of the current array in T-tree.
@param index the index of the last item in the item set, which is also
used as a level counter.
@param itemset the given item set.
@param support the support value associated with the given item set.
|
[
"Inserts",
"a",
"node",
"into",
"a",
"T",
"-",
"tree",
"."
] |
e27e43e90fbaacce3f99d30120cf9dd6a764c33d
|
https://github.com/haifengl/smile/blob/e27e43e90fbaacce3f99d30120cf9dd6a764c33d/core/src/main/java/smile/association/TotalSupportTree.java#L108-L123
|
17,313
|
haifengl/smile
|
core/src/main/java/smile/association/TotalSupportTree.java
|
TotalSupportTree.getSupport
|
public int getSupport(int[] itemset) {
if (root.children != null) {
return getSupport(itemset, itemset.length - 1, root);
} else {
return 0;
}
}
|
java
|
public int getSupport(int[] itemset) {
if (root.children != null) {
return getSupport(itemset, itemset.length - 1, root);
} else {
return 0;
}
}
|
[
"public",
"int",
"getSupport",
"(",
"int",
"[",
"]",
"itemset",
")",
"{",
"if",
"(",
"root",
".",
"children",
"!=",
"null",
")",
"{",
"return",
"getSupport",
"(",
"itemset",
",",
"itemset",
".",
"length",
"-",
"1",
",",
"root",
")",
";",
"}",
"else",
"{",
"return",
"0",
";",
"}",
"}"
] |
Returns the support value for the given item set.
@param itemset the given item set. The items in the set has to be in the
descending order according to their frequency.
@return the support value (0 if not found)
|
[
"Returns",
"the",
"support",
"value",
"for",
"the",
"given",
"item",
"set",
"."
] |
e27e43e90fbaacce3f99d30120cf9dd6a764c33d
|
https://github.com/haifengl/smile/blob/e27e43e90fbaacce3f99d30120cf9dd6a764c33d/core/src/main/java/smile/association/TotalSupportTree.java#L131-L137
|
17,314
|
haifengl/smile
|
core/src/main/java/smile/association/TotalSupportTree.java
|
TotalSupportTree.getSupport
|
private int getSupport(int[] itemset, int index, Node node) {
int item = order[itemset[index]];
Node child = node.children[item];
if (child != null) {
// If the index is 0, then this is the last element (i.e the
// input is a 1 itemset) and therefore item set found
if (index == 0) {
return child.support;
} else {
if (child.children != null) {
return getSupport(itemset, index - 1, child);
}
}
}
return 0;
}
|
java
|
private int getSupport(int[] itemset, int index, Node node) {
int item = order[itemset[index]];
Node child = node.children[item];
if (child != null) {
// If the index is 0, then this is the last element (i.e the
// input is a 1 itemset) and therefore item set found
if (index == 0) {
return child.support;
} else {
if (child.children != null) {
return getSupport(itemset, index - 1, child);
}
}
}
return 0;
}
|
[
"private",
"int",
"getSupport",
"(",
"int",
"[",
"]",
"itemset",
",",
"int",
"index",
",",
"Node",
"node",
")",
"{",
"int",
"item",
"=",
"order",
"[",
"itemset",
"[",
"index",
"]",
"]",
";",
"Node",
"child",
"=",
"node",
".",
"children",
"[",
"item",
"]",
";",
"if",
"(",
"child",
"!=",
"null",
")",
"{",
"// If the index is 0, then this is the last element (i.e the",
"// input is a 1 itemset) and therefore item set found",
"if",
"(",
"index",
"==",
"0",
")",
"{",
"return",
"child",
".",
"support",
";",
"}",
"else",
"{",
"if",
"(",
"child",
".",
"children",
"!=",
"null",
")",
"{",
"return",
"getSupport",
"(",
"itemset",
",",
"index",
"-",
"1",
",",
"child",
")",
";",
"}",
"}",
"}",
"return",
"0",
";",
"}"
] |
Returns the support value for the given item set if found in the T-tree
and 0 otherwise.
@param itemset the given item set.
@param index the current index in the given item set.
@param nodes the nodes of the current T-tree level.
@return the support value (0 if not found)
|
[
"Returns",
"the",
"support",
"value",
"for",
"the",
"given",
"item",
"set",
"if",
"found",
"in",
"the",
"T",
"-",
"tree",
"and",
"0",
"otherwise",
"."
] |
e27e43e90fbaacce3f99d30120cf9dd6a764c33d
|
https://github.com/haifengl/smile/blob/e27e43e90fbaacce3f99d30120cf9dd6a764c33d/core/src/main/java/smile/association/TotalSupportTree.java#L147-L163
|
17,315
|
haifengl/smile
|
data/src/main/java/smile/data/parser/ArffParser.java
|
ArffParser.initTokenizer
|
private void initTokenizer(StreamTokenizer tokenizer) {
tokenizer.resetSyntax();
tokenizer.whitespaceChars(0, ' ');
tokenizer.wordChars(' ' + 1, '\u00FF');
tokenizer.whitespaceChars(',', ',');
tokenizer.commentChar('%');
tokenizer.quoteChar('"');
tokenizer.quoteChar('\'');
tokenizer.ordinaryChar('{');
tokenizer.ordinaryChar('}');
tokenizer.eolIsSignificant(true);
}
|
java
|
private void initTokenizer(StreamTokenizer tokenizer) {
tokenizer.resetSyntax();
tokenizer.whitespaceChars(0, ' ');
tokenizer.wordChars(' ' + 1, '\u00FF');
tokenizer.whitespaceChars(',', ',');
tokenizer.commentChar('%');
tokenizer.quoteChar('"');
tokenizer.quoteChar('\'');
tokenizer.ordinaryChar('{');
tokenizer.ordinaryChar('}');
tokenizer.eolIsSignificant(true);
}
|
[
"private",
"void",
"initTokenizer",
"(",
"StreamTokenizer",
"tokenizer",
")",
"{",
"tokenizer",
".",
"resetSyntax",
"(",
")",
";",
"tokenizer",
".",
"whitespaceChars",
"(",
"0",
",",
"'",
"'",
")",
";",
"tokenizer",
".",
"wordChars",
"(",
"'",
"'",
"+",
"1",
",",
"'",
"'",
")",
";",
"tokenizer",
".",
"whitespaceChars",
"(",
"'",
"'",
",",
"'",
"'",
")",
";",
"tokenizer",
".",
"commentChar",
"(",
"'",
"'",
")",
";",
"tokenizer",
".",
"quoteChar",
"(",
"'",
"'",
")",
";",
"tokenizer",
".",
"quoteChar",
"(",
"'",
"'",
")",
";",
"tokenizer",
".",
"ordinaryChar",
"(",
"'",
"'",
")",
";",
"tokenizer",
".",
"ordinaryChar",
"(",
"'",
"'",
")",
";",
"tokenizer",
".",
"eolIsSignificant",
"(",
"true",
")",
";",
"}"
] |
Initializes the StreamTokenizer used for reading the ARFF file.
|
[
"Initializes",
"the",
"StreamTokenizer",
"used",
"for",
"reading",
"the",
"ARFF",
"file",
"."
] |
e27e43e90fbaacce3f99d30120cf9dd6a764c33d
|
https://github.com/haifengl/smile/blob/e27e43e90fbaacce3f99d30120cf9dd6a764c33d/data/src/main/java/smile/data/parser/ArffParser.java#L118-L129
|
17,316
|
haifengl/smile
|
data/src/main/java/smile/data/parser/ArffParser.java
|
ArffParser.getFirstToken
|
private void getFirstToken(StreamTokenizer tokenizer) throws IOException {
while (tokenizer.nextToken() == StreamTokenizer.TT_EOL) {
}
if ((tokenizer.ttype == '\'') || (tokenizer.ttype == '"')) {
tokenizer.ttype = StreamTokenizer.TT_WORD;
} else if ((tokenizer.ttype == StreamTokenizer.TT_WORD) && (tokenizer.sval.equals("?"))) {
tokenizer.ttype = '?';
}
}
|
java
|
private void getFirstToken(StreamTokenizer tokenizer) throws IOException {
while (tokenizer.nextToken() == StreamTokenizer.TT_EOL) {
}
if ((tokenizer.ttype == '\'') || (tokenizer.ttype == '"')) {
tokenizer.ttype = StreamTokenizer.TT_WORD;
} else if ((tokenizer.ttype == StreamTokenizer.TT_WORD) && (tokenizer.sval.equals("?"))) {
tokenizer.ttype = '?';
}
}
|
[
"private",
"void",
"getFirstToken",
"(",
"StreamTokenizer",
"tokenizer",
")",
"throws",
"IOException",
"{",
"while",
"(",
"tokenizer",
".",
"nextToken",
"(",
")",
"==",
"StreamTokenizer",
".",
"TT_EOL",
")",
"{",
"}",
"if",
"(",
"(",
"tokenizer",
".",
"ttype",
"==",
"'",
"'",
")",
"||",
"(",
"tokenizer",
".",
"ttype",
"==",
"'",
"'",
")",
")",
"{",
"tokenizer",
".",
"ttype",
"=",
"StreamTokenizer",
".",
"TT_WORD",
";",
"}",
"else",
"if",
"(",
"(",
"tokenizer",
".",
"ttype",
"==",
"StreamTokenizer",
".",
"TT_WORD",
")",
"&&",
"(",
"tokenizer",
".",
"sval",
".",
"equals",
"(",
"\"?\"",
")",
")",
")",
"{",
"tokenizer",
".",
"ttype",
"=",
"'",
"'",
";",
"}",
"}"
] |
Gets next token, skipping empty lines.
@throws IOException if reading the next token fails
|
[
"Gets",
"next",
"token",
"skipping",
"empty",
"lines",
"."
] |
e27e43e90fbaacce3f99d30120cf9dd6a764c33d
|
https://github.com/haifengl/smile/blob/e27e43e90fbaacce3f99d30120cf9dd6a764c33d/data/src/main/java/smile/data/parser/ArffParser.java#L136-L145
|
17,317
|
haifengl/smile
|
data/src/main/java/smile/data/parser/ArffParser.java
|
ArffParser.getLastToken
|
private void getLastToken(StreamTokenizer tokenizer, boolean endOfFileOk) throws IOException, ParseException {
if ((tokenizer.nextToken() != StreamTokenizer.TT_EOL) && ((tokenizer.ttype != StreamTokenizer.TT_EOF) || !endOfFileOk)) {
throw new ParseException("end of line expected", tokenizer.lineno());
}
}
|
java
|
private void getLastToken(StreamTokenizer tokenizer, boolean endOfFileOk) throws IOException, ParseException {
if ((tokenizer.nextToken() != StreamTokenizer.TT_EOL) && ((tokenizer.ttype != StreamTokenizer.TT_EOF) || !endOfFileOk)) {
throw new ParseException("end of line expected", tokenizer.lineno());
}
}
|
[
"private",
"void",
"getLastToken",
"(",
"StreamTokenizer",
"tokenizer",
",",
"boolean",
"endOfFileOk",
")",
"throws",
"IOException",
",",
"ParseException",
"{",
"if",
"(",
"(",
"tokenizer",
".",
"nextToken",
"(",
")",
"!=",
"StreamTokenizer",
".",
"TT_EOL",
")",
"&&",
"(",
"(",
"tokenizer",
".",
"ttype",
"!=",
"StreamTokenizer",
".",
"TT_EOF",
")",
"||",
"!",
"endOfFileOk",
")",
")",
"{",
"throw",
"new",
"ParseException",
"(",
"\"end of line expected\"",
",",
"tokenizer",
".",
"lineno",
"(",
")",
")",
";",
"}",
"}"
] |
Gets token and checks if it's end of line.
@param endOfFileOk true if EOF is OK
@throws IllegalStateException if it doesn't find an end of line
|
[
"Gets",
"token",
"and",
"checks",
"if",
"it",
"s",
"end",
"of",
"line",
"."
] |
e27e43e90fbaacce3f99d30120cf9dd6a764c33d
|
https://github.com/haifengl/smile/blob/e27e43e90fbaacce3f99d30120cf9dd6a764c33d/data/src/main/java/smile/data/parser/ArffParser.java#L153-L157
|
17,318
|
haifengl/smile
|
data/src/main/java/smile/data/parser/ArffParser.java
|
ArffParser.getNextToken
|
private void getNextToken(StreamTokenizer tokenizer) throws IOException, ParseException {
if (tokenizer.nextToken() == StreamTokenizer.TT_EOL) {
throw new ParseException("premature end of line", tokenizer.lineno());
}
if (tokenizer.ttype == StreamTokenizer.TT_EOF) {
throw new ParseException(PREMATURE_END_OF_FILE, tokenizer.lineno());
} else if ((tokenizer.ttype == '\'') || (tokenizer.ttype == '"')) {
tokenizer.ttype = StreamTokenizer.TT_WORD;
} else if ((tokenizer.ttype == StreamTokenizer.TT_WORD) && (tokenizer.sval.equals("?"))) {
tokenizer.ttype = '?';
}
}
|
java
|
private void getNextToken(StreamTokenizer tokenizer) throws IOException, ParseException {
if (tokenizer.nextToken() == StreamTokenizer.TT_EOL) {
throw new ParseException("premature end of line", tokenizer.lineno());
}
if (tokenizer.ttype == StreamTokenizer.TT_EOF) {
throw new ParseException(PREMATURE_END_OF_FILE, tokenizer.lineno());
} else if ((tokenizer.ttype == '\'') || (tokenizer.ttype == '"')) {
tokenizer.ttype = StreamTokenizer.TT_WORD;
} else if ((tokenizer.ttype == StreamTokenizer.TT_WORD) && (tokenizer.sval.equals("?"))) {
tokenizer.ttype = '?';
}
}
|
[
"private",
"void",
"getNextToken",
"(",
"StreamTokenizer",
"tokenizer",
")",
"throws",
"IOException",
",",
"ParseException",
"{",
"if",
"(",
"tokenizer",
".",
"nextToken",
"(",
")",
"==",
"StreamTokenizer",
".",
"TT_EOL",
")",
"{",
"throw",
"new",
"ParseException",
"(",
"\"premature end of line\"",
",",
"tokenizer",
".",
"lineno",
"(",
")",
")",
";",
"}",
"if",
"(",
"tokenizer",
".",
"ttype",
"==",
"StreamTokenizer",
".",
"TT_EOF",
")",
"{",
"throw",
"new",
"ParseException",
"(",
"PREMATURE_END_OF_FILE",
",",
"tokenizer",
".",
"lineno",
"(",
")",
")",
";",
"}",
"else",
"if",
"(",
"(",
"tokenizer",
".",
"ttype",
"==",
"'",
"'",
")",
"||",
"(",
"tokenizer",
".",
"ttype",
"==",
"'",
"'",
")",
")",
"{",
"tokenizer",
".",
"ttype",
"=",
"StreamTokenizer",
".",
"TT_WORD",
";",
"}",
"else",
"if",
"(",
"(",
"tokenizer",
".",
"ttype",
"==",
"StreamTokenizer",
".",
"TT_WORD",
")",
"&&",
"(",
"tokenizer",
".",
"sval",
".",
"equals",
"(",
"\"?\"",
")",
")",
")",
"{",
"tokenizer",
".",
"ttype",
"=",
"'",
"'",
";",
"}",
"}"
] |
Gets next token, checking for a premature and of line.
@throws IllegalStateException if it finds a premature end of line
|
[
"Gets",
"next",
"token",
"checking",
"for",
"a",
"premature",
"and",
"of",
"line",
"."
] |
e27e43e90fbaacce3f99d30120cf9dd6a764c33d
|
https://github.com/haifengl/smile/blob/e27e43e90fbaacce3f99d30120cf9dd6a764c33d/data/src/main/java/smile/data/parser/ArffParser.java#L164-L175
|
17,319
|
haifengl/smile
|
data/src/main/java/smile/data/parser/ArffParser.java
|
ArffParser.readHeader
|
private String readHeader(StreamTokenizer tokenizer, List<Attribute> attributes) throws IOException, ParseException {
/// The name of dataset.
String relationName = null;
// clear attribute set, which may be from previous parsing of other datasets.
attributes.clear();
// Get name of relation.
getFirstToken(tokenizer);
if (tokenizer.ttype == StreamTokenizer.TT_EOF) {
throw new ParseException(PREMATURE_END_OF_FILE, tokenizer.lineno());
}
if (ARFF_RELATION.equalsIgnoreCase(tokenizer.sval)) {
getNextToken(tokenizer);
relationName = tokenizer.sval;
getLastToken(tokenizer, false);
} else {
throw new ParseException("keyword " + ARFF_RELATION + " expected", tokenizer.lineno());
}
// Get attribute declarations.
getFirstToken(tokenizer);
if (tokenizer.ttype == StreamTokenizer.TT_EOF) {
throw new ParseException(PREMATURE_END_OF_FILE, tokenizer.lineno());
}
while (ARFF_ATTRIBUTE.equalsIgnoreCase(tokenizer.sval)) {
attributes.add(parseAttribute(tokenizer));
}
// Check if data part follows. We can't easily check for EOL.
if (!ARFF_DATA.equalsIgnoreCase(tokenizer.sval)) {
throw new ParseException("keyword " + ARFF_DATA + " expected", tokenizer.lineno());
}
// Check if any attributes have been declared.
if (attributes.isEmpty()) {
throw new ParseException("no attributes declared", tokenizer.lineno());
}
if (responseIndex >= attributes.size()) {
throw new ParseException("Invalid response variable index", responseIndex);
}
return relationName;
}
|
java
|
private String readHeader(StreamTokenizer tokenizer, List<Attribute> attributes) throws IOException, ParseException {
/// The name of dataset.
String relationName = null;
// clear attribute set, which may be from previous parsing of other datasets.
attributes.clear();
// Get name of relation.
getFirstToken(tokenizer);
if (tokenizer.ttype == StreamTokenizer.TT_EOF) {
throw new ParseException(PREMATURE_END_OF_FILE, tokenizer.lineno());
}
if (ARFF_RELATION.equalsIgnoreCase(tokenizer.sval)) {
getNextToken(tokenizer);
relationName = tokenizer.sval;
getLastToken(tokenizer, false);
} else {
throw new ParseException("keyword " + ARFF_RELATION + " expected", tokenizer.lineno());
}
// Get attribute declarations.
getFirstToken(tokenizer);
if (tokenizer.ttype == StreamTokenizer.TT_EOF) {
throw new ParseException(PREMATURE_END_OF_FILE, tokenizer.lineno());
}
while (ARFF_ATTRIBUTE.equalsIgnoreCase(tokenizer.sval)) {
attributes.add(parseAttribute(tokenizer));
}
// Check if data part follows. We can't easily check for EOL.
if (!ARFF_DATA.equalsIgnoreCase(tokenizer.sval)) {
throw new ParseException("keyword " + ARFF_DATA + " expected", tokenizer.lineno());
}
// Check if any attributes have been declared.
if (attributes.isEmpty()) {
throw new ParseException("no attributes declared", tokenizer.lineno());
}
if (responseIndex >= attributes.size()) {
throw new ParseException("Invalid response variable index", responseIndex);
}
return relationName;
}
|
[
"private",
"String",
"readHeader",
"(",
"StreamTokenizer",
"tokenizer",
",",
"List",
"<",
"Attribute",
">",
"attributes",
")",
"throws",
"IOException",
",",
"ParseException",
"{",
"/// The name of dataset.",
"String",
"relationName",
"=",
"null",
";",
"// clear attribute set, which may be from previous parsing of other datasets.",
"attributes",
".",
"clear",
"(",
")",
";",
"// Get name of relation.",
"getFirstToken",
"(",
"tokenizer",
")",
";",
"if",
"(",
"tokenizer",
".",
"ttype",
"==",
"StreamTokenizer",
".",
"TT_EOF",
")",
"{",
"throw",
"new",
"ParseException",
"(",
"PREMATURE_END_OF_FILE",
",",
"tokenizer",
".",
"lineno",
"(",
")",
")",
";",
"}",
"if",
"(",
"ARFF_RELATION",
".",
"equalsIgnoreCase",
"(",
"tokenizer",
".",
"sval",
")",
")",
"{",
"getNextToken",
"(",
"tokenizer",
")",
";",
"relationName",
"=",
"tokenizer",
".",
"sval",
";",
"getLastToken",
"(",
"tokenizer",
",",
"false",
")",
";",
"}",
"else",
"{",
"throw",
"new",
"ParseException",
"(",
"\"keyword \"",
"+",
"ARFF_RELATION",
"+",
"\" expected\"",
",",
"tokenizer",
".",
"lineno",
"(",
")",
")",
";",
"}",
"// Get attribute declarations.",
"getFirstToken",
"(",
"tokenizer",
")",
";",
"if",
"(",
"tokenizer",
".",
"ttype",
"==",
"StreamTokenizer",
".",
"TT_EOF",
")",
"{",
"throw",
"new",
"ParseException",
"(",
"PREMATURE_END_OF_FILE",
",",
"tokenizer",
".",
"lineno",
"(",
")",
")",
";",
"}",
"while",
"(",
"ARFF_ATTRIBUTE",
".",
"equalsIgnoreCase",
"(",
"tokenizer",
".",
"sval",
")",
")",
"{",
"attributes",
".",
"add",
"(",
"parseAttribute",
"(",
"tokenizer",
")",
")",
";",
"}",
"// Check if data part follows. We can't easily check for EOL.",
"if",
"(",
"!",
"ARFF_DATA",
".",
"equalsIgnoreCase",
"(",
"tokenizer",
".",
"sval",
")",
")",
"{",
"throw",
"new",
"ParseException",
"(",
"\"keyword \"",
"+",
"ARFF_DATA",
"+",
"\" expected\"",
",",
"tokenizer",
".",
"lineno",
"(",
")",
")",
";",
"}",
"// Check if any attributes have been declared.",
"if",
"(",
"attributes",
".",
"isEmpty",
"(",
")",
")",
"{",
"throw",
"new",
"ParseException",
"(",
"\"no attributes declared\"",
",",
"tokenizer",
".",
"lineno",
"(",
")",
")",
";",
"}",
"if",
"(",
"responseIndex",
">=",
"attributes",
".",
"size",
"(",
")",
")",
"{",
"throw",
"new",
"ParseException",
"(",
"\"Invalid response variable index\"",
",",
"responseIndex",
")",
";",
"}",
"return",
"relationName",
";",
"}"
] |
Reads and stores header of an ARFF file.
@param attributes the set of attributes in this relation.
@return the name of relation.
@throws IllegalStateException if the information is not read successfully
|
[
"Reads",
"and",
"stores",
"header",
"of",
"an",
"ARFF",
"file",
"."
] |
e27e43e90fbaacce3f99d30120cf9dd6a764c33d
|
https://github.com/haifengl/smile/blob/e27e43e90fbaacce3f99d30120cf9dd6a764c33d/data/src/main/java/smile/data/parser/ArffParser.java#L184-L228
|
17,320
|
haifengl/smile
|
data/src/main/java/smile/data/parser/ArffParser.java
|
ArffParser.parseAttribute
|
private Attribute parseAttribute(StreamTokenizer tokenizer) throws IOException, ParseException {
Attribute attribute = null;
// Get attribute name.
getNextToken(tokenizer);
String attributeName = tokenizer.sval;
getNextToken(tokenizer);
// Check if attribute is nominal.
if (tokenizer.ttype == StreamTokenizer.TT_WORD) {
// Attribute is real, integer, or string.
if (tokenizer.sval.equalsIgnoreCase(ARFF_ATTRIBUTE_REAL) ||
tokenizer.sval.equalsIgnoreCase(ARFF_ATTRIBUTE_INTEGER) ||
tokenizer.sval.equalsIgnoreCase(ARFF_ATTRIBUTE_NUMERIC)) {
attribute = new NumericAttribute(attributeName);
readTillEOL(tokenizer);
} else if (tokenizer.sval.equalsIgnoreCase(ARFF_ATTRIBUTE_STRING)) {
attribute = new StringAttribute(attributeName);
readTillEOL(tokenizer);
} else if (tokenizer.sval.equalsIgnoreCase(ARFF_ATTRIBUTE_DATE)) {
String format = null;
if (tokenizer.nextToken() != StreamTokenizer.TT_EOL) {
if ((tokenizer.ttype != StreamTokenizer.TT_WORD) && (tokenizer.ttype != '\'') && (tokenizer.ttype != '\"')) {
throw new ParseException("not a valid date format", tokenizer.lineno());
}
format = tokenizer.sval;
readTillEOL(tokenizer);
} else {
tokenizer.pushBack();
}
attribute = new DateAttribute(attributeName, null, format);
readTillEOL(tokenizer);
} else if (tokenizer.sval.equalsIgnoreCase(ARFF_ATTRIBUTE_RELATIONAL)) {
readTillEOL(tokenizer);
} else if (tokenizer.sval.equalsIgnoreCase(ARFF_END_SUBRELATION)) {
getNextToken(tokenizer);
} else {
throw new ParseException("Invalid attribute type or invalid enumeration", tokenizer.lineno());
}
} else {
// Attribute is nominal.
List<String> attributeValues = new ArrayList<>();
tokenizer.pushBack();
// Get values for nominal attribute.
if (tokenizer.nextToken() != '{') {
throw new ParseException("{ expected at beginning of enumeration", tokenizer.lineno());
}
while (tokenizer.nextToken() != '}') {
if (tokenizer.ttype == StreamTokenizer.TT_EOL) {
throw new ParseException("} expected at end of enumeration", tokenizer.lineno());
} else {
attributeValues.add(tokenizer.sval.trim());
}
}
String[] values = new String[attributeValues.size()];
for (int i = 0; i < values.length; i++) {
values[i] = attributeValues.get(i);
}
attribute = new NominalAttribute(attributeName, values);
}
getLastToken(tokenizer, false);
getFirstToken(tokenizer);
if (tokenizer.ttype == StreamTokenizer.TT_EOF) {
throw new ParseException(PREMATURE_END_OF_FILE, tokenizer.lineno());
}
return attribute;
}
|
java
|
private Attribute parseAttribute(StreamTokenizer tokenizer) throws IOException, ParseException {
Attribute attribute = null;
// Get attribute name.
getNextToken(tokenizer);
String attributeName = tokenizer.sval;
getNextToken(tokenizer);
// Check if attribute is nominal.
if (tokenizer.ttype == StreamTokenizer.TT_WORD) {
// Attribute is real, integer, or string.
if (tokenizer.sval.equalsIgnoreCase(ARFF_ATTRIBUTE_REAL) ||
tokenizer.sval.equalsIgnoreCase(ARFF_ATTRIBUTE_INTEGER) ||
tokenizer.sval.equalsIgnoreCase(ARFF_ATTRIBUTE_NUMERIC)) {
attribute = new NumericAttribute(attributeName);
readTillEOL(tokenizer);
} else if (tokenizer.sval.equalsIgnoreCase(ARFF_ATTRIBUTE_STRING)) {
attribute = new StringAttribute(attributeName);
readTillEOL(tokenizer);
} else if (tokenizer.sval.equalsIgnoreCase(ARFF_ATTRIBUTE_DATE)) {
String format = null;
if (tokenizer.nextToken() != StreamTokenizer.TT_EOL) {
if ((tokenizer.ttype != StreamTokenizer.TT_WORD) && (tokenizer.ttype != '\'') && (tokenizer.ttype != '\"')) {
throw new ParseException("not a valid date format", tokenizer.lineno());
}
format = tokenizer.sval;
readTillEOL(tokenizer);
} else {
tokenizer.pushBack();
}
attribute = new DateAttribute(attributeName, null, format);
readTillEOL(tokenizer);
} else if (tokenizer.sval.equalsIgnoreCase(ARFF_ATTRIBUTE_RELATIONAL)) {
readTillEOL(tokenizer);
} else if (tokenizer.sval.equalsIgnoreCase(ARFF_END_SUBRELATION)) {
getNextToken(tokenizer);
} else {
throw new ParseException("Invalid attribute type or invalid enumeration", tokenizer.lineno());
}
} else {
// Attribute is nominal.
List<String> attributeValues = new ArrayList<>();
tokenizer.pushBack();
// Get values for nominal attribute.
if (tokenizer.nextToken() != '{') {
throw new ParseException("{ expected at beginning of enumeration", tokenizer.lineno());
}
while (tokenizer.nextToken() != '}') {
if (tokenizer.ttype == StreamTokenizer.TT_EOL) {
throw new ParseException("} expected at end of enumeration", tokenizer.lineno());
} else {
attributeValues.add(tokenizer.sval.trim());
}
}
String[] values = new String[attributeValues.size()];
for (int i = 0; i < values.length; i++) {
values[i] = attributeValues.get(i);
}
attribute = new NominalAttribute(attributeName, values);
}
getLastToken(tokenizer, false);
getFirstToken(tokenizer);
if (tokenizer.ttype == StreamTokenizer.TT_EOF) {
throw new ParseException(PREMATURE_END_OF_FILE, tokenizer.lineno());
}
return attribute;
}
|
[
"private",
"Attribute",
"parseAttribute",
"(",
"StreamTokenizer",
"tokenizer",
")",
"throws",
"IOException",
",",
"ParseException",
"{",
"Attribute",
"attribute",
"=",
"null",
";",
"// Get attribute name.",
"getNextToken",
"(",
"tokenizer",
")",
";",
"String",
"attributeName",
"=",
"tokenizer",
".",
"sval",
";",
"getNextToken",
"(",
"tokenizer",
")",
";",
"// Check if attribute is nominal.",
"if",
"(",
"tokenizer",
".",
"ttype",
"==",
"StreamTokenizer",
".",
"TT_WORD",
")",
"{",
"// Attribute is real, integer, or string.",
"if",
"(",
"tokenizer",
".",
"sval",
".",
"equalsIgnoreCase",
"(",
"ARFF_ATTRIBUTE_REAL",
")",
"||",
"tokenizer",
".",
"sval",
".",
"equalsIgnoreCase",
"(",
"ARFF_ATTRIBUTE_INTEGER",
")",
"||",
"tokenizer",
".",
"sval",
".",
"equalsIgnoreCase",
"(",
"ARFF_ATTRIBUTE_NUMERIC",
")",
")",
"{",
"attribute",
"=",
"new",
"NumericAttribute",
"(",
"attributeName",
")",
";",
"readTillEOL",
"(",
"tokenizer",
")",
";",
"}",
"else",
"if",
"(",
"tokenizer",
".",
"sval",
".",
"equalsIgnoreCase",
"(",
"ARFF_ATTRIBUTE_STRING",
")",
")",
"{",
"attribute",
"=",
"new",
"StringAttribute",
"(",
"attributeName",
")",
";",
"readTillEOL",
"(",
"tokenizer",
")",
";",
"}",
"else",
"if",
"(",
"tokenizer",
".",
"sval",
".",
"equalsIgnoreCase",
"(",
"ARFF_ATTRIBUTE_DATE",
")",
")",
"{",
"String",
"format",
"=",
"null",
";",
"if",
"(",
"tokenizer",
".",
"nextToken",
"(",
")",
"!=",
"StreamTokenizer",
".",
"TT_EOL",
")",
"{",
"if",
"(",
"(",
"tokenizer",
".",
"ttype",
"!=",
"StreamTokenizer",
".",
"TT_WORD",
")",
"&&",
"(",
"tokenizer",
".",
"ttype",
"!=",
"'",
"'",
")",
"&&",
"(",
"tokenizer",
".",
"ttype",
"!=",
"'",
"'",
")",
")",
"{",
"throw",
"new",
"ParseException",
"(",
"\"not a valid date format\"",
",",
"tokenizer",
".",
"lineno",
"(",
")",
")",
";",
"}",
"format",
"=",
"tokenizer",
".",
"sval",
";",
"readTillEOL",
"(",
"tokenizer",
")",
";",
"}",
"else",
"{",
"tokenizer",
".",
"pushBack",
"(",
")",
";",
"}",
"attribute",
"=",
"new",
"DateAttribute",
"(",
"attributeName",
",",
"null",
",",
"format",
")",
";",
"readTillEOL",
"(",
"tokenizer",
")",
";",
"}",
"else",
"if",
"(",
"tokenizer",
".",
"sval",
".",
"equalsIgnoreCase",
"(",
"ARFF_ATTRIBUTE_RELATIONAL",
")",
")",
"{",
"readTillEOL",
"(",
"tokenizer",
")",
";",
"}",
"else",
"if",
"(",
"tokenizer",
".",
"sval",
".",
"equalsIgnoreCase",
"(",
"ARFF_END_SUBRELATION",
")",
")",
"{",
"getNextToken",
"(",
"tokenizer",
")",
";",
"}",
"else",
"{",
"throw",
"new",
"ParseException",
"(",
"\"Invalid attribute type or invalid enumeration\"",
",",
"tokenizer",
".",
"lineno",
"(",
")",
")",
";",
"}",
"}",
"else",
"{",
"// Attribute is nominal.",
"List",
"<",
"String",
">",
"attributeValues",
"=",
"new",
"ArrayList",
"<>",
"(",
")",
";",
"tokenizer",
".",
"pushBack",
"(",
")",
";",
"// Get values for nominal attribute.",
"if",
"(",
"tokenizer",
".",
"nextToken",
"(",
")",
"!=",
"'",
"'",
")",
"{",
"throw",
"new",
"ParseException",
"(",
"\"{ expected at beginning of enumeration\"",
",",
"tokenizer",
".",
"lineno",
"(",
")",
")",
";",
"}",
"while",
"(",
"tokenizer",
".",
"nextToken",
"(",
")",
"!=",
"'",
"'",
")",
"{",
"if",
"(",
"tokenizer",
".",
"ttype",
"==",
"StreamTokenizer",
".",
"TT_EOL",
")",
"{",
"throw",
"new",
"ParseException",
"(",
"\"} expected at end of enumeration\"",
",",
"tokenizer",
".",
"lineno",
"(",
")",
")",
";",
"}",
"else",
"{",
"attributeValues",
".",
"add",
"(",
"tokenizer",
".",
"sval",
".",
"trim",
"(",
")",
")",
";",
"}",
"}",
"String",
"[",
"]",
"values",
"=",
"new",
"String",
"[",
"attributeValues",
".",
"size",
"(",
")",
"]",
";",
"for",
"(",
"int",
"i",
"=",
"0",
";",
"i",
"<",
"values",
".",
"length",
";",
"i",
"++",
")",
"{",
"values",
"[",
"i",
"]",
"=",
"attributeValues",
".",
"get",
"(",
"i",
")",
";",
"}",
"attribute",
"=",
"new",
"NominalAttribute",
"(",
"attributeName",
",",
"values",
")",
";",
"}",
"getLastToken",
"(",
"tokenizer",
",",
"false",
")",
";",
"getFirstToken",
"(",
"tokenizer",
")",
";",
"if",
"(",
"tokenizer",
".",
"ttype",
"==",
"StreamTokenizer",
".",
"TT_EOF",
")",
"{",
"throw",
"new",
"ParseException",
"(",
"PREMATURE_END_OF_FILE",
",",
"tokenizer",
".",
"lineno",
"(",
")",
")",
";",
"}",
"return",
"attribute",
";",
"}"
] |
Parses the attribute declaration.
@return an attributes in this relation
@throws IOException if the information is not read
successfully
|
[
"Parses",
"the",
"attribute",
"declaration",
"."
] |
e27e43e90fbaacce3f99d30120cf9dd6a764c33d
|
https://github.com/haifengl/smile/blob/e27e43e90fbaacce3f99d30120cf9dd6a764c33d/data/src/main/java/smile/data/parser/ArffParser.java#L237-L314
|
17,321
|
haifengl/smile
|
data/src/main/java/smile/data/parser/ArffParser.java
|
ArffParser.readTillEOL
|
private void readTillEOL(StreamTokenizer tokenizer) throws IOException {
while (tokenizer.nextToken() != StreamTokenizer.TT_EOL) {
}
tokenizer.pushBack();
}
|
java
|
private void readTillEOL(StreamTokenizer tokenizer) throws IOException {
while (tokenizer.nextToken() != StreamTokenizer.TT_EOL) {
}
tokenizer.pushBack();
}
|
[
"private",
"void",
"readTillEOL",
"(",
"StreamTokenizer",
"tokenizer",
")",
"throws",
"IOException",
"{",
"while",
"(",
"tokenizer",
".",
"nextToken",
"(",
")",
"!=",
"StreamTokenizer",
".",
"TT_EOL",
")",
"{",
"}",
"tokenizer",
".",
"pushBack",
"(",
")",
";",
"}"
] |
Reads and skips all tokens before next end of line token.
@throws IOException in case something goes wrong
|
[
"Reads",
"and",
"skips",
"all",
"tokens",
"before",
"next",
"end",
"of",
"line",
"token",
"."
] |
e27e43e90fbaacce3f99d30120cf9dd6a764c33d
|
https://github.com/haifengl/smile/blob/e27e43e90fbaacce3f99d30120cf9dd6a764c33d/data/src/main/java/smile/data/parser/ArffParser.java#L321-L326
|
17,322
|
haifengl/smile
|
data/src/main/java/smile/data/parser/ArffParser.java
|
ArffParser.getAttributes
|
public static Attribute[] getAttributes(InputStream stream) throws IOException, ParseException {
Reader r = new BufferedReader(new InputStreamReader(stream));
StreamTokenizer tokenizer = new StreamTokenizer(r);
ArffParser parser = new ArffParser();
parser.initTokenizer(tokenizer);
List<Attribute> attributes = new ArrayList<>();
parser.readHeader(tokenizer, attributes);
return attributes.toArray(new Attribute[attributes.size()]);
}
|
java
|
public static Attribute[] getAttributes(InputStream stream) throws IOException, ParseException {
Reader r = new BufferedReader(new InputStreamReader(stream));
StreamTokenizer tokenizer = new StreamTokenizer(r);
ArffParser parser = new ArffParser();
parser.initTokenizer(tokenizer);
List<Attribute> attributes = new ArrayList<>();
parser.readHeader(tokenizer, attributes);
return attributes.toArray(new Attribute[attributes.size()]);
}
|
[
"public",
"static",
"Attribute",
"[",
"]",
"getAttributes",
"(",
"InputStream",
"stream",
")",
"throws",
"IOException",
",",
"ParseException",
"{",
"Reader",
"r",
"=",
"new",
"BufferedReader",
"(",
"new",
"InputStreamReader",
"(",
"stream",
")",
")",
";",
"StreamTokenizer",
"tokenizer",
"=",
"new",
"StreamTokenizer",
"(",
"r",
")",
";",
"ArffParser",
"parser",
"=",
"new",
"ArffParser",
"(",
")",
";",
"parser",
".",
"initTokenizer",
"(",
"tokenizer",
")",
";",
"List",
"<",
"Attribute",
">",
"attributes",
"=",
"new",
"ArrayList",
"<>",
"(",
")",
";",
"parser",
".",
"readHeader",
"(",
"tokenizer",
",",
"attributes",
")",
";",
"return",
"attributes",
".",
"toArray",
"(",
"new",
"Attribute",
"[",
"attributes",
".",
"size",
"(",
")",
"]",
")",
";",
"}"
] |
Returns the attribute set of given stream.
|
[
"Returns",
"the",
"attribute",
"set",
"of",
"given",
"stream",
"."
] |
e27e43e90fbaacce3f99d30120cf9dd6a764c33d
|
https://github.com/haifengl/smile/blob/e27e43e90fbaacce3f99d30120cf9dd6a764c33d/data/src/main/java/smile/data/parser/ArffParser.java#L355-L366
|
17,323
|
haifengl/smile
|
data/src/main/java/smile/data/parser/ArffParser.java
|
ArffParser.parse
|
public AttributeDataset parse(InputStream stream) throws IOException, ParseException {
try (Reader r = new BufferedReader(new InputStreamReader(stream))) {
StreamTokenizer tokenizer = new StreamTokenizer(r);
initTokenizer(tokenizer);
List<Attribute> attributes = new ArrayList<>();
String relationName = readHeader(tokenizer, attributes);
if (attributes.isEmpty()) {
throw new IOException("no header information available");
}
Attribute response = null;
Attribute[] attr = new Attribute[attributes.size()];
attributes.toArray(attr);
for (int i = 0; i < attributes.size(); i++) {
if (responseIndex == i) {
response = attributes.remove(i);
break;
}
}
AttributeDataset data = new AttributeDataset(relationName, attributes.toArray(new Attribute[attributes.size()]), response);
while (true) {
// Check if end of file reached.
getFirstToken(tokenizer);
if (tokenizer.ttype == StreamTokenizer.TT_EOF) {
break;
}
// Parse instance
if (tokenizer.ttype == '{') {
readSparseInstance(tokenizer, data, attr);
} else {
readInstance(tokenizer, data, attr);
}
}
for (Attribute attribute : attributes) {
if (attribute instanceof NominalAttribute) {
NominalAttribute a = (NominalAttribute) attribute;
a.setOpen(false);
}
if (attribute instanceof StringAttribute) {
StringAttribute a = (StringAttribute) attribute;
a.setOpen(false);
}
}
return data;
}
}
|
java
|
public AttributeDataset parse(InputStream stream) throws IOException, ParseException {
try (Reader r = new BufferedReader(new InputStreamReader(stream))) {
StreamTokenizer tokenizer = new StreamTokenizer(r);
initTokenizer(tokenizer);
List<Attribute> attributes = new ArrayList<>();
String relationName = readHeader(tokenizer, attributes);
if (attributes.isEmpty()) {
throw new IOException("no header information available");
}
Attribute response = null;
Attribute[] attr = new Attribute[attributes.size()];
attributes.toArray(attr);
for (int i = 0; i < attributes.size(); i++) {
if (responseIndex == i) {
response = attributes.remove(i);
break;
}
}
AttributeDataset data = new AttributeDataset(relationName, attributes.toArray(new Attribute[attributes.size()]), response);
while (true) {
// Check if end of file reached.
getFirstToken(tokenizer);
if (tokenizer.ttype == StreamTokenizer.TT_EOF) {
break;
}
// Parse instance
if (tokenizer.ttype == '{') {
readSparseInstance(tokenizer, data, attr);
} else {
readInstance(tokenizer, data, attr);
}
}
for (Attribute attribute : attributes) {
if (attribute instanceof NominalAttribute) {
NominalAttribute a = (NominalAttribute) attribute;
a.setOpen(false);
}
if (attribute instanceof StringAttribute) {
StringAttribute a = (StringAttribute) attribute;
a.setOpen(false);
}
}
return data;
}
}
|
[
"public",
"AttributeDataset",
"parse",
"(",
"InputStream",
"stream",
")",
"throws",
"IOException",
",",
"ParseException",
"{",
"try",
"(",
"Reader",
"r",
"=",
"new",
"BufferedReader",
"(",
"new",
"InputStreamReader",
"(",
"stream",
")",
")",
")",
"{",
"StreamTokenizer",
"tokenizer",
"=",
"new",
"StreamTokenizer",
"(",
"r",
")",
";",
"initTokenizer",
"(",
"tokenizer",
")",
";",
"List",
"<",
"Attribute",
">",
"attributes",
"=",
"new",
"ArrayList",
"<>",
"(",
")",
";",
"String",
"relationName",
"=",
"readHeader",
"(",
"tokenizer",
",",
"attributes",
")",
";",
"if",
"(",
"attributes",
".",
"isEmpty",
"(",
")",
")",
"{",
"throw",
"new",
"IOException",
"(",
"\"no header information available\"",
")",
";",
"}",
"Attribute",
"response",
"=",
"null",
";",
"Attribute",
"[",
"]",
"attr",
"=",
"new",
"Attribute",
"[",
"attributes",
".",
"size",
"(",
")",
"]",
";",
"attributes",
".",
"toArray",
"(",
"attr",
")",
";",
"for",
"(",
"int",
"i",
"=",
"0",
";",
"i",
"<",
"attributes",
".",
"size",
"(",
")",
";",
"i",
"++",
")",
"{",
"if",
"(",
"responseIndex",
"==",
"i",
")",
"{",
"response",
"=",
"attributes",
".",
"remove",
"(",
"i",
")",
";",
"break",
";",
"}",
"}",
"AttributeDataset",
"data",
"=",
"new",
"AttributeDataset",
"(",
"relationName",
",",
"attributes",
".",
"toArray",
"(",
"new",
"Attribute",
"[",
"attributes",
".",
"size",
"(",
")",
"]",
")",
",",
"response",
")",
";",
"while",
"(",
"true",
")",
"{",
"// Check if end of file reached.",
"getFirstToken",
"(",
"tokenizer",
")",
";",
"if",
"(",
"tokenizer",
".",
"ttype",
"==",
"StreamTokenizer",
".",
"TT_EOF",
")",
"{",
"break",
";",
"}",
"// Parse instance",
"if",
"(",
"tokenizer",
".",
"ttype",
"==",
"'",
"'",
")",
"{",
"readSparseInstance",
"(",
"tokenizer",
",",
"data",
",",
"attr",
")",
";",
"}",
"else",
"{",
"readInstance",
"(",
"tokenizer",
",",
"data",
",",
"attr",
")",
";",
"}",
"}",
"for",
"(",
"Attribute",
"attribute",
":",
"attributes",
")",
"{",
"if",
"(",
"attribute",
"instanceof",
"NominalAttribute",
")",
"{",
"NominalAttribute",
"a",
"=",
"(",
"NominalAttribute",
")",
"attribute",
";",
"a",
".",
"setOpen",
"(",
"false",
")",
";",
"}",
"if",
"(",
"attribute",
"instanceof",
"StringAttribute",
")",
"{",
"StringAttribute",
"a",
"=",
"(",
"StringAttribute",
")",
"attribute",
";",
"a",
".",
"setOpen",
"(",
"false",
")",
";",
"}",
"}",
"return",
"data",
";",
"}",
"}"
] |
Parse a dataset from given stream.
|
[
"Parse",
"a",
"dataset",
"from",
"given",
"stream",
"."
] |
e27e43e90fbaacce3f99d30120cf9dd6a764c33d
|
https://github.com/haifengl/smile/blob/e27e43e90fbaacce3f99d30120cf9dd6a764c33d/data/src/main/java/smile/data/parser/ArffParser.java#L395-L449
|
17,324
|
haifengl/smile
|
data/src/main/java/smile/data/parser/ArffParser.java
|
ArffParser.readInstance
|
private void readInstance(StreamTokenizer tokenizer, AttributeDataset data, Attribute[] attributes) throws IOException, ParseException {
double[] x = responseIndex >= 0 ? new double[attributes.length - 1] : new double[attributes.length];
double y = Double.NaN;
// Get values for all attributes.
for (int i = 0, k = 0; i < attributes.length; i++) {
// Get next token
if (i > 0) {
getNextToken(tokenizer);
}
if (i == responseIndex) {
if (tokenizer.ttype == '?') {
y = Double.NaN;
} else {
y = attributes[i].valueOf(tokenizer.sval);
}
} else {
if (tokenizer.ttype == '?') {
x[k++] = Double.NaN;
} else {
x[k++] = attributes[i].valueOf(tokenizer.sval);
}
}
}
if (Double.isNaN(y)) data.add(x); else data.add(x, y);
}
|
java
|
private void readInstance(StreamTokenizer tokenizer, AttributeDataset data, Attribute[] attributes) throws IOException, ParseException {
double[] x = responseIndex >= 0 ? new double[attributes.length - 1] : new double[attributes.length];
double y = Double.NaN;
// Get values for all attributes.
for (int i = 0, k = 0; i < attributes.length; i++) {
// Get next token
if (i > 0) {
getNextToken(tokenizer);
}
if (i == responseIndex) {
if (tokenizer.ttype == '?') {
y = Double.NaN;
} else {
y = attributes[i].valueOf(tokenizer.sval);
}
} else {
if (tokenizer.ttype == '?') {
x[k++] = Double.NaN;
} else {
x[k++] = attributes[i].valueOf(tokenizer.sval);
}
}
}
if (Double.isNaN(y)) data.add(x); else data.add(x, y);
}
|
[
"private",
"void",
"readInstance",
"(",
"StreamTokenizer",
"tokenizer",
",",
"AttributeDataset",
"data",
",",
"Attribute",
"[",
"]",
"attributes",
")",
"throws",
"IOException",
",",
"ParseException",
"{",
"double",
"[",
"]",
"x",
"=",
"responseIndex",
">=",
"0",
"?",
"new",
"double",
"[",
"attributes",
".",
"length",
"-",
"1",
"]",
":",
"new",
"double",
"[",
"attributes",
".",
"length",
"]",
";",
"double",
"y",
"=",
"Double",
".",
"NaN",
";",
"// Get values for all attributes.",
"for",
"(",
"int",
"i",
"=",
"0",
",",
"k",
"=",
"0",
";",
"i",
"<",
"attributes",
".",
"length",
";",
"i",
"++",
")",
"{",
"// Get next token",
"if",
"(",
"i",
">",
"0",
")",
"{",
"getNextToken",
"(",
"tokenizer",
")",
";",
"}",
"if",
"(",
"i",
"==",
"responseIndex",
")",
"{",
"if",
"(",
"tokenizer",
".",
"ttype",
"==",
"'",
"'",
")",
"{",
"y",
"=",
"Double",
".",
"NaN",
";",
"}",
"else",
"{",
"y",
"=",
"attributes",
"[",
"i",
"]",
".",
"valueOf",
"(",
"tokenizer",
".",
"sval",
")",
";",
"}",
"}",
"else",
"{",
"if",
"(",
"tokenizer",
".",
"ttype",
"==",
"'",
"'",
")",
"{",
"x",
"[",
"k",
"++",
"]",
"=",
"Double",
".",
"NaN",
";",
"}",
"else",
"{",
"x",
"[",
"k",
"++",
"]",
"=",
"attributes",
"[",
"i",
"]",
".",
"valueOf",
"(",
"tokenizer",
".",
"sval",
")",
";",
"}",
"}",
"}",
"if",
"(",
"Double",
".",
"isNaN",
"(",
"y",
")",
")",
"data",
".",
"add",
"(",
"x",
")",
";",
"else",
"data",
".",
"add",
"(",
"x",
",",
"y",
")",
";",
"}"
] |
Reads a single instance.
@throws ParseException if the information is not read successfully
|
[
"Reads",
"a",
"single",
"instance",
"."
] |
e27e43e90fbaacce3f99d30120cf9dd6a764c33d
|
https://github.com/haifengl/smile/blob/e27e43e90fbaacce3f99d30120cf9dd6a764c33d/data/src/main/java/smile/data/parser/ArffParser.java#L455-L482
|
17,325
|
haifengl/smile
|
data/src/main/java/smile/data/parser/ArffParser.java
|
ArffParser.readSparseInstance
|
private void readSparseInstance(StreamTokenizer tokenizer, AttributeDataset data, Attribute[] attributes) throws IOException, ParseException {
double[] x = responseIndex >= 0 ? new double[attributes.length - 1] : new double[attributes.length];
double y = Double.NaN;
int index = -1;
// Get values for all attributes.
do {
getNextToken(tokenizer);
// end of instance
if (tokenizer.ttype == '}') {
break;
}
String s = tokenizer.sval.trim();
if (index < 0) {
index = Integer.parseInt(s);
if (index < 0 || index >= attributes.length) {
throw new ParseException("Invalid attribute index: " + index, tokenizer.lineno());
}
} else {
String val = s;
if (index != responseIndex) {
if (val.equals("?")) {
x[index] = Double.NaN;
} else {
x[index] = attributes[index].valueOf(val);
}
} else {
if (val.equals("?")) {
y = Double.NaN;
} else {
y = attributes[index].valueOf(val);
}
}
index = -1;
}
} while (tokenizer.ttype == StreamTokenizer.TT_WORD);
if (Double.isNaN(y)) data.add(x); else data.add(x, y);
}
|
java
|
private void readSparseInstance(StreamTokenizer tokenizer, AttributeDataset data, Attribute[] attributes) throws IOException, ParseException {
double[] x = responseIndex >= 0 ? new double[attributes.length - 1] : new double[attributes.length];
double y = Double.NaN;
int index = -1;
// Get values for all attributes.
do {
getNextToken(tokenizer);
// end of instance
if (tokenizer.ttype == '}') {
break;
}
String s = tokenizer.sval.trim();
if (index < 0) {
index = Integer.parseInt(s);
if (index < 0 || index >= attributes.length) {
throw new ParseException("Invalid attribute index: " + index, tokenizer.lineno());
}
} else {
String val = s;
if (index != responseIndex) {
if (val.equals("?")) {
x[index] = Double.NaN;
} else {
x[index] = attributes[index].valueOf(val);
}
} else {
if (val.equals("?")) {
y = Double.NaN;
} else {
y = attributes[index].valueOf(val);
}
}
index = -1;
}
} while (tokenizer.ttype == StreamTokenizer.TT_WORD);
if (Double.isNaN(y)) data.add(x); else data.add(x, y);
}
|
[
"private",
"void",
"readSparseInstance",
"(",
"StreamTokenizer",
"tokenizer",
",",
"AttributeDataset",
"data",
",",
"Attribute",
"[",
"]",
"attributes",
")",
"throws",
"IOException",
",",
"ParseException",
"{",
"double",
"[",
"]",
"x",
"=",
"responseIndex",
">=",
"0",
"?",
"new",
"double",
"[",
"attributes",
".",
"length",
"-",
"1",
"]",
":",
"new",
"double",
"[",
"attributes",
".",
"length",
"]",
";",
"double",
"y",
"=",
"Double",
".",
"NaN",
";",
"int",
"index",
"=",
"-",
"1",
";",
"// Get values for all attributes.",
"do",
"{",
"getNextToken",
"(",
"tokenizer",
")",
";",
"// end of instance",
"if",
"(",
"tokenizer",
".",
"ttype",
"==",
"'",
"'",
")",
"{",
"break",
";",
"}",
"String",
"s",
"=",
"tokenizer",
".",
"sval",
".",
"trim",
"(",
")",
";",
"if",
"(",
"index",
"<",
"0",
")",
"{",
"index",
"=",
"Integer",
".",
"parseInt",
"(",
"s",
")",
";",
"if",
"(",
"index",
"<",
"0",
"||",
"index",
">=",
"attributes",
".",
"length",
")",
"{",
"throw",
"new",
"ParseException",
"(",
"\"Invalid attribute index: \"",
"+",
"index",
",",
"tokenizer",
".",
"lineno",
"(",
")",
")",
";",
"}",
"}",
"else",
"{",
"String",
"val",
"=",
"s",
";",
"if",
"(",
"index",
"!=",
"responseIndex",
")",
"{",
"if",
"(",
"val",
".",
"equals",
"(",
"\"?\"",
")",
")",
"{",
"x",
"[",
"index",
"]",
"=",
"Double",
".",
"NaN",
";",
"}",
"else",
"{",
"x",
"[",
"index",
"]",
"=",
"attributes",
"[",
"index",
"]",
".",
"valueOf",
"(",
"val",
")",
";",
"}",
"}",
"else",
"{",
"if",
"(",
"val",
".",
"equals",
"(",
"\"?\"",
")",
")",
"{",
"y",
"=",
"Double",
".",
"NaN",
";",
"}",
"else",
"{",
"y",
"=",
"attributes",
"[",
"index",
"]",
".",
"valueOf",
"(",
"val",
")",
";",
"}",
"}",
"index",
"=",
"-",
"1",
";",
"}",
"}",
"while",
"(",
"tokenizer",
".",
"ttype",
"==",
"StreamTokenizer",
".",
"TT_WORD",
")",
";",
"if",
"(",
"Double",
".",
"isNaN",
"(",
"y",
")",
")",
"data",
".",
"add",
"(",
"x",
")",
";",
"else",
"data",
".",
"add",
"(",
"x",
",",
"y",
")",
";",
"}"
] |
Reads a sparse instance using the tokenizer.
@throws ParseException if the information is not read successfully
|
[
"Reads",
"a",
"sparse",
"instance",
"using",
"the",
"tokenizer",
"."
] |
e27e43e90fbaacce3f99d30120cf9dd6a764c33d
|
https://github.com/haifengl/smile/blob/e27e43e90fbaacce3f99d30120cf9dd6a764c33d/data/src/main/java/smile/data/parser/ArffParser.java#L488-L532
|
17,326
|
haifengl/smile
|
math/src/main/java/smile/math/matrix/Lanczos.java
|
Lanczos.startv
|
private static double startv(Matrix A, double[][] q, double[][] wptr, int step) {
// get initial vector; default is random
double rnm = Math.dot(wptr[0], wptr[0]);
double[] r = wptr[0];
for (int id = 0; id < 3; id++) {
if (id > 0 || step > 0 || rnm == 0) {
for (int i = 0; i < r.length; i++) {
r[i] = Math.random() - 0.5;
}
}
Math.copy(wptr[0], wptr[3]);
// apply operator to put r in range (essential if m singular)
A.ax(wptr[3], wptr[0]);
Math.copy(wptr[0], wptr[3]);
rnm = Math.dot(wptr[0], wptr[3]);
if (rnm > 0.0) {
break;
}
}
// fatal error
if (rnm <= 0.0) {
logger.error("Lanczos method was unable to find a starting vector within range.");
return -1;
}
if (step > 0) {
for (int i = 0; i < step; i++) {
double t = Math.dot(wptr[3], q[i]);
Math.axpy(-t, q[i], wptr[0]);
}
// make sure q[step] is orthogonal to q[step-1]
double t = Math.dot(wptr[4], wptr[0]);
Math.axpy(-t, wptr[2], wptr[0]);
Math.copy(wptr[0], wptr[3]);
t = Math.dot(wptr[3], wptr[0]);
if (t <= Math.EPSILON * rnm) {
t = 0.0;
}
rnm = t;
}
return Math.sqrt(rnm);
}
|
java
|
private static double startv(Matrix A, double[][] q, double[][] wptr, int step) {
// get initial vector; default is random
double rnm = Math.dot(wptr[0], wptr[0]);
double[] r = wptr[0];
for (int id = 0; id < 3; id++) {
if (id > 0 || step > 0 || rnm == 0) {
for (int i = 0; i < r.length; i++) {
r[i] = Math.random() - 0.5;
}
}
Math.copy(wptr[0], wptr[3]);
// apply operator to put r in range (essential if m singular)
A.ax(wptr[3], wptr[0]);
Math.copy(wptr[0], wptr[3]);
rnm = Math.dot(wptr[0], wptr[3]);
if (rnm > 0.0) {
break;
}
}
// fatal error
if (rnm <= 0.0) {
logger.error("Lanczos method was unable to find a starting vector within range.");
return -1;
}
if (step > 0) {
for (int i = 0; i < step; i++) {
double t = Math.dot(wptr[3], q[i]);
Math.axpy(-t, q[i], wptr[0]);
}
// make sure q[step] is orthogonal to q[step-1]
double t = Math.dot(wptr[4], wptr[0]);
Math.axpy(-t, wptr[2], wptr[0]);
Math.copy(wptr[0], wptr[3]);
t = Math.dot(wptr[3], wptr[0]);
if (t <= Math.EPSILON * rnm) {
t = 0.0;
}
rnm = t;
}
return Math.sqrt(rnm);
}
|
[
"private",
"static",
"double",
"startv",
"(",
"Matrix",
"A",
",",
"double",
"[",
"]",
"[",
"]",
"q",
",",
"double",
"[",
"]",
"[",
"]",
"wptr",
",",
"int",
"step",
")",
"{",
"// get initial vector; default is random",
"double",
"rnm",
"=",
"Math",
".",
"dot",
"(",
"wptr",
"[",
"0",
"]",
",",
"wptr",
"[",
"0",
"]",
")",
";",
"double",
"[",
"]",
"r",
"=",
"wptr",
"[",
"0",
"]",
";",
"for",
"(",
"int",
"id",
"=",
"0",
";",
"id",
"<",
"3",
";",
"id",
"++",
")",
"{",
"if",
"(",
"id",
">",
"0",
"||",
"step",
">",
"0",
"||",
"rnm",
"==",
"0",
")",
"{",
"for",
"(",
"int",
"i",
"=",
"0",
";",
"i",
"<",
"r",
".",
"length",
";",
"i",
"++",
")",
"{",
"r",
"[",
"i",
"]",
"=",
"Math",
".",
"random",
"(",
")",
"-",
"0.5",
";",
"}",
"}",
"Math",
".",
"copy",
"(",
"wptr",
"[",
"0",
"]",
",",
"wptr",
"[",
"3",
"]",
")",
";",
"// apply operator to put r in range (essential if m singular)",
"A",
".",
"ax",
"(",
"wptr",
"[",
"3",
"]",
",",
"wptr",
"[",
"0",
"]",
")",
";",
"Math",
".",
"copy",
"(",
"wptr",
"[",
"0",
"]",
",",
"wptr",
"[",
"3",
"]",
")",
";",
"rnm",
"=",
"Math",
".",
"dot",
"(",
"wptr",
"[",
"0",
"]",
",",
"wptr",
"[",
"3",
"]",
")",
";",
"if",
"(",
"rnm",
">",
"0.0",
")",
"{",
"break",
";",
"}",
"}",
"// fatal error",
"if",
"(",
"rnm",
"<=",
"0.0",
")",
"{",
"logger",
".",
"error",
"(",
"\"Lanczos method was unable to find a starting vector within range.\"",
")",
";",
"return",
"-",
"1",
";",
"}",
"if",
"(",
"step",
">",
"0",
")",
"{",
"for",
"(",
"int",
"i",
"=",
"0",
";",
"i",
"<",
"step",
";",
"i",
"++",
")",
"{",
"double",
"t",
"=",
"Math",
".",
"dot",
"(",
"wptr",
"[",
"3",
"]",
",",
"q",
"[",
"i",
"]",
")",
";",
"Math",
".",
"axpy",
"(",
"-",
"t",
",",
"q",
"[",
"i",
"]",
",",
"wptr",
"[",
"0",
"]",
")",
";",
"}",
"// make sure q[step] is orthogonal to q[step-1]",
"double",
"t",
"=",
"Math",
".",
"dot",
"(",
"wptr",
"[",
"4",
"]",
",",
"wptr",
"[",
"0",
"]",
")",
";",
"Math",
".",
"axpy",
"(",
"-",
"t",
",",
"wptr",
"[",
"2",
"]",
",",
"wptr",
"[",
"0",
"]",
")",
";",
"Math",
".",
"copy",
"(",
"wptr",
"[",
"0",
"]",
",",
"wptr",
"[",
"3",
"]",
")",
";",
"t",
"=",
"Math",
".",
"dot",
"(",
"wptr",
"[",
"3",
"]",
",",
"wptr",
"[",
"0",
"]",
")",
";",
"if",
"(",
"t",
"<=",
"Math",
".",
"EPSILON",
"*",
"rnm",
")",
"{",
"t",
"=",
"0.0",
";",
"}",
"rnm",
"=",
"t",
";",
"}",
"return",
"Math",
".",
"sqrt",
"(",
"rnm",
")",
";",
"}"
] |
Generate a starting vector in r and returns |r|. It returns zero if the
range is spanned, and throws exception if no starting vector within range
of operator can be found.
@param step starting index for a Lanczos run
|
[
"Generate",
"a",
"starting",
"vector",
"in",
"r",
"and",
"returns",
"|r|",
".",
"It",
"returns",
"zero",
"if",
"the",
"range",
"is",
"spanned",
"and",
"throws",
"exception",
"if",
"no",
"starting",
"vector",
"within",
"range",
"of",
"operator",
"can",
"be",
"found",
"."
] |
e27e43e90fbaacce3f99d30120cf9dd6a764c33d
|
https://github.com/haifengl/smile/blob/e27e43e90fbaacce3f99d30120cf9dd6a764c33d/math/src/main/java/smile/math/matrix/Lanczos.java#L322-L367
|
17,327
|
haifengl/smile
|
math/src/main/java/smile/math/matrix/Lanczos.java
|
Lanczos.ortbnd
|
private static void ortbnd(double[] alf, double[] bet, double[] eta, double[] oldeta, int step, double rnm, double eps) {
if (step < 1) {
return;
}
if (0 != rnm) {
if (step > 1) {
oldeta[0] = (bet[1] * eta[1] + (alf[0] - alf[step]) * eta[0] - bet[step] * oldeta[0]) / rnm + eps;
}
for (int i = 1; i <= step - 2; i++) {
oldeta[i] = (bet[i + 1] * eta[i + 1] + (alf[i] - alf[step]) * eta[i] + bet[i] * eta[i - 1] - bet[step] * oldeta[i]) / rnm + eps;
}
}
oldeta[step - 1] = eps;
for (int i = 0; i < step; i++) {
double swap = eta[i];
eta[i] = oldeta[i];
oldeta[i] = swap;
}
eta[step] = eps;
}
|
java
|
private static void ortbnd(double[] alf, double[] bet, double[] eta, double[] oldeta, int step, double rnm, double eps) {
if (step < 1) {
return;
}
if (0 != rnm) {
if (step > 1) {
oldeta[0] = (bet[1] * eta[1] + (alf[0] - alf[step]) * eta[0] - bet[step] * oldeta[0]) / rnm + eps;
}
for (int i = 1; i <= step - 2; i++) {
oldeta[i] = (bet[i + 1] * eta[i + 1] + (alf[i] - alf[step]) * eta[i] + bet[i] * eta[i - 1] - bet[step] * oldeta[i]) / rnm + eps;
}
}
oldeta[step - 1] = eps;
for (int i = 0; i < step; i++) {
double swap = eta[i];
eta[i] = oldeta[i];
oldeta[i] = swap;
}
eta[step] = eps;
}
|
[
"private",
"static",
"void",
"ortbnd",
"(",
"double",
"[",
"]",
"alf",
",",
"double",
"[",
"]",
"bet",
",",
"double",
"[",
"]",
"eta",
",",
"double",
"[",
"]",
"oldeta",
",",
"int",
"step",
",",
"double",
"rnm",
",",
"double",
"eps",
")",
"{",
"if",
"(",
"step",
"<",
"1",
")",
"{",
"return",
";",
"}",
"if",
"(",
"0",
"!=",
"rnm",
")",
"{",
"if",
"(",
"step",
">",
"1",
")",
"{",
"oldeta",
"[",
"0",
"]",
"=",
"(",
"bet",
"[",
"1",
"]",
"*",
"eta",
"[",
"1",
"]",
"+",
"(",
"alf",
"[",
"0",
"]",
"-",
"alf",
"[",
"step",
"]",
")",
"*",
"eta",
"[",
"0",
"]",
"-",
"bet",
"[",
"step",
"]",
"*",
"oldeta",
"[",
"0",
"]",
")",
"/",
"rnm",
"+",
"eps",
";",
"}",
"for",
"(",
"int",
"i",
"=",
"1",
";",
"i",
"<=",
"step",
"-",
"2",
";",
"i",
"++",
")",
"{",
"oldeta",
"[",
"i",
"]",
"=",
"(",
"bet",
"[",
"i",
"+",
"1",
"]",
"*",
"eta",
"[",
"i",
"+",
"1",
"]",
"+",
"(",
"alf",
"[",
"i",
"]",
"-",
"alf",
"[",
"step",
"]",
")",
"*",
"eta",
"[",
"i",
"]",
"+",
"bet",
"[",
"i",
"]",
"*",
"eta",
"[",
"i",
"-",
"1",
"]",
"-",
"bet",
"[",
"step",
"]",
"*",
"oldeta",
"[",
"i",
"]",
")",
"/",
"rnm",
"+",
"eps",
";",
"}",
"}",
"oldeta",
"[",
"step",
"-",
"1",
"]",
"=",
"eps",
";",
"for",
"(",
"int",
"i",
"=",
"0",
";",
"i",
"<",
"step",
";",
"i",
"++",
")",
"{",
"double",
"swap",
"=",
"eta",
"[",
"i",
"]",
";",
"eta",
"[",
"i",
"]",
"=",
"oldeta",
"[",
"i",
"]",
";",
"oldeta",
"[",
"i",
"]",
"=",
"swap",
";",
"}",
"eta",
"[",
"step",
"]",
"=",
"eps",
";",
"}"
] |
Update the eta recurrence.
@param alf array to store diagonal of the tridiagonal matrix T
@param bet array to store off-diagonal of T
@param eta on input, orthogonality estimate of Lanczos vectors at step j.
On output, orthogonality estimate of Lanczos vectors at step j+1 .
@param oldeta on input, orthogonality estimate of Lanczos vectors at step j-1
On output orthogonality estimate of Lanczos vectors at step j
@param step dimension of T
@param rnm norm of the next residual vector
@param eps roundoff estimate for dot product of two unit vectors
|
[
"Update",
"the",
"eta",
"recurrence",
"."
] |
e27e43e90fbaacce3f99d30120cf9dd6a764c33d
|
https://github.com/haifengl/smile/blob/e27e43e90fbaacce3f99d30120cf9dd6a764c33d/math/src/main/java/smile/math/matrix/Lanczos.java#L381-L404
|
17,328
|
haifengl/smile
|
math/src/main/java/smile/math/matrix/Lanczos.java
|
Lanczos.purge
|
private static double purge(int ll, double[][] Q, double[] r, double[] q, double[] ra, double[] qa, double[] eta, double[] oldeta, int step, double rnm, double tol, double eps, double reps) {
if (step < ll + 2) {
return rnm;
}
double t, tq, tr;
int k = idamax(step - (ll + 1), eta, ll, 1) + ll;
if (Math.abs(eta[k]) > reps) {
double reps1 = eps / reps;
int iteration = 0;
boolean flag = true;
while (iteration < 2 && flag) {
if (rnm > tol) {
// bring in a lanczos vector t and orthogonalize both
// r and q against it
tq = 0.0;
tr = 0.0;
for (int i = ll; i < step; i++) {
t = -Math.dot(qa, Q[i]);
tq += Math.abs(t);
Math.axpy(t, Q[i], q);
t = -Math.dot(ra, Q[i]);
tr += Math.abs(t);
Math.axpy(t, Q[i], r);
}
Math.copy(q, qa);
t = -Math.dot(r, qa);
tr += Math.abs(t);
Math.axpy(t, q, r);
Math.copy(r, ra);
rnm = Math.sqrt(Math.dot(ra, r));
if (tq <= reps1 && tr <= reps1 * rnm) {
flag = false;
}
}
iteration++;
}
for (int i = ll; i <= step; i++) {
eta[i] = eps;
oldeta[i] = eps;
}
}
return rnm;
}
|
java
|
private static double purge(int ll, double[][] Q, double[] r, double[] q, double[] ra, double[] qa, double[] eta, double[] oldeta, int step, double rnm, double tol, double eps, double reps) {
if (step < ll + 2) {
return rnm;
}
double t, tq, tr;
int k = idamax(step - (ll + 1), eta, ll, 1) + ll;
if (Math.abs(eta[k]) > reps) {
double reps1 = eps / reps;
int iteration = 0;
boolean flag = true;
while (iteration < 2 && flag) {
if (rnm > tol) {
// bring in a lanczos vector t and orthogonalize both
// r and q against it
tq = 0.0;
tr = 0.0;
for (int i = ll; i < step; i++) {
t = -Math.dot(qa, Q[i]);
tq += Math.abs(t);
Math.axpy(t, Q[i], q);
t = -Math.dot(ra, Q[i]);
tr += Math.abs(t);
Math.axpy(t, Q[i], r);
}
Math.copy(q, qa);
t = -Math.dot(r, qa);
tr += Math.abs(t);
Math.axpy(t, q, r);
Math.copy(r, ra);
rnm = Math.sqrt(Math.dot(ra, r));
if (tq <= reps1 && tr <= reps1 * rnm) {
flag = false;
}
}
iteration++;
}
for (int i = ll; i <= step; i++) {
eta[i] = eps;
oldeta[i] = eps;
}
}
return rnm;
}
|
[
"private",
"static",
"double",
"purge",
"(",
"int",
"ll",
",",
"double",
"[",
"]",
"[",
"]",
"Q",
",",
"double",
"[",
"]",
"r",
",",
"double",
"[",
"]",
"q",
",",
"double",
"[",
"]",
"ra",
",",
"double",
"[",
"]",
"qa",
",",
"double",
"[",
"]",
"eta",
",",
"double",
"[",
"]",
"oldeta",
",",
"int",
"step",
",",
"double",
"rnm",
",",
"double",
"tol",
",",
"double",
"eps",
",",
"double",
"reps",
")",
"{",
"if",
"(",
"step",
"<",
"ll",
"+",
"2",
")",
"{",
"return",
"rnm",
";",
"}",
"double",
"t",
",",
"tq",
",",
"tr",
";",
"int",
"k",
"=",
"idamax",
"(",
"step",
"-",
"(",
"ll",
"+",
"1",
")",
",",
"eta",
",",
"ll",
",",
"1",
")",
"+",
"ll",
";",
"if",
"(",
"Math",
".",
"abs",
"(",
"eta",
"[",
"k",
"]",
")",
">",
"reps",
")",
"{",
"double",
"reps1",
"=",
"eps",
"/",
"reps",
";",
"int",
"iteration",
"=",
"0",
";",
"boolean",
"flag",
"=",
"true",
";",
"while",
"(",
"iteration",
"<",
"2",
"&&",
"flag",
")",
"{",
"if",
"(",
"rnm",
">",
"tol",
")",
"{",
"// bring in a lanczos vector t and orthogonalize both",
"// r and q against it",
"tq",
"=",
"0.0",
";",
"tr",
"=",
"0.0",
";",
"for",
"(",
"int",
"i",
"=",
"ll",
";",
"i",
"<",
"step",
";",
"i",
"++",
")",
"{",
"t",
"=",
"-",
"Math",
".",
"dot",
"(",
"qa",
",",
"Q",
"[",
"i",
"]",
")",
";",
"tq",
"+=",
"Math",
".",
"abs",
"(",
"t",
")",
";",
"Math",
".",
"axpy",
"(",
"t",
",",
"Q",
"[",
"i",
"]",
",",
"q",
")",
";",
"t",
"=",
"-",
"Math",
".",
"dot",
"(",
"ra",
",",
"Q",
"[",
"i",
"]",
")",
";",
"tr",
"+=",
"Math",
".",
"abs",
"(",
"t",
")",
";",
"Math",
".",
"axpy",
"(",
"t",
",",
"Q",
"[",
"i",
"]",
",",
"r",
")",
";",
"}",
"Math",
".",
"copy",
"(",
"q",
",",
"qa",
")",
";",
"t",
"=",
"-",
"Math",
".",
"dot",
"(",
"r",
",",
"qa",
")",
";",
"tr",
"+=",
"Math",
".",
"abs",
"(",
"t",
")",
";",
"Math",
".",
"axpy",
"(",
"t",
",",
"q",
",",
"r",
")",
";",
"Math",
".",
"copy",
"(",
"r",
",",
"ra",
")",
";",
"rnm",
"=",
"Math",
".",
"sqrt",
"(",
"Math",
".",
"dot",
"(",
"ra",
",",
"r",
")",
")",
";",
"if",
"(",
"tq",
"<=",
"reps1",
"&&",
"tr",
"<=",
"reps1",
"*",
"rnm",
")",
"{",
"flag",
"=",
"false",
";",
"}",
"}",
"iteration",
"++",
";",
"}",
"for",
"(",
"int",
"i",
"=",
"ll",
";",
"i",
"<=",
"step",
";",
"i",
"++",
")",
"{",
"eta",
"[",
"i",
"]",
"=",
"eps",
";",
"oldeta",
"[",
"i",
"]",
"=",
"eps",
";",
"}",
"}",
"return",
"rnm",
";",
"}"
] |
Examine the state of orthogonality between the new Lanczos
vector and the previous ones to decide whether re-orthogonalization
should be performed.
@param ll number of intitial Lanczos vectors in local orthog.
@param r on input, residual vector to become next Lanczos vector.
On output, residual vector orthogonalized against previous Lanczos.
@param q on input, current Lanczos vector. On Output, current
Lanczos vector orthogonalized against previous ones.
@param ra previous Lanczos vector
@param qa previous Lanczos vector
@param eta state of orthogonality between r and prev. Lanczos vectors
@param oldeta state of orthogonality between q and prev. Lanczos vectors
|
[
"Examine",
"the",
"state",
"of",
"orthogonality",
"between",
"the",
"new",
"Lanczos",
"vector",
"and",
"the",
"previous",
"ones",
"to",
"decide",
"whether",
"re",
"-",
"orthogonalization",
"should",
"be",
"performed",
"."
] |
e27e43e90fbaacce3f99d30120cf9dd6a764c33d
|
https://github.com/haifengl/smile/blob/e27e43e90fbaacce3f99d30120cf9dd6a764c33d/math/src/main/java/smile/math/matrix/Lanczos.java#L420-L465
|
17,329
|
haifengl/smile
|
math/src/main/java/smile/math/matrix/Lanczos.java
|
Lanczos.idamax
|
private static int idamax(int n, double[] dx, int ix0, int incx) {
int ix, imax;
double dmax;
if (n < 1) {
return -1;
}
if (n == 1) {
return 0;
}
if (incx == 0) {
return -1;
}
ix = (incx < 0) ? ix0 + ((-n + 1) * incx) : ix0;
imax = ix;
dmax = Math.abs(dx[ix]);
for (int i = 1; i < n; i++) {
ix += incx;
double dtemp = Math.abs(dx[ix]);
if (dtemp > dmax) {
dmax = dtemp;
imax = ix;
}
}
return imax;
}
|
java
|
private static int idamax(int n, double[] dx, int ix0, int incx) {
int ix, imax;
double dmax;
if (n < 1) {
return -1;
}
if (n == 1) {
return 0;
}
if (incx == 0) {
return -1;
}
ix = (incx < 0) ? ix0 + ((-n + 1) * incx) : ix0;
imax = ix;
dmax = Math.abs(dx[ix]);
for (int i = 1; i < n; i++) {
ix += incx;
double dtemp = Math.abs(dx[ix]);
if (dtemp > dmax) {
dmax = dtemp;
imax = ix;
}
}
return imax;
}
|
[
"private",
"static",
"int",
"idamax",
"(",
"int",
"n",
",",
"double",
"[",
"]",
"dx",
",",
"int",
"ix0",
",",
"int",
"incx",
")",
"{",
"int",
"ix",
",",
"imax",
";",
"double",
"dmax",
";",
"if",
"(",
"n",
"<",
"1",
")",
"{",
"return",
"-",
"1",
";",
"}",
"if",
"(",
"n",
"==",
"1",
")",
"{",
"return",
"0",
";",
"}",
"if",
"(",
"incx",
"==",
"0",
")",
"{",
"return",
"-",
"1",
";",
"}",
"ix",
"=",
"(",
"incx",
"<",
"0",
")",
"?",
"ix0",
"+",
"(",
"(",
"-",
"n",
"+",
"1",
")",
"*",
"incx",
")",
":",
"ix0",
";",
"imax",
"=",
"ix",
";",
"dmax",
"=",
"Math",
".",
"abs",
"(",
"dx",
"[",
"ix",
"]",
")",
";",
"for",
"(",
"int",
"i",
"=",
"1",
";",
"i",
"<",
"n",
";",
"i",
"++",
")",
"{",
"ix",
"+=",
"incx",
";",
"double",
"dtemp",
"=",
"Math",
".",
"abs",
"(",
"dx",
"[",
"ix",
"]",
")",
";",
"if",
"(",
"dtemp",
">",
"dmax",
")",
"{",
"dmax",
"=",
"dtemp",
";",
"imax",
"=",
"ix",
";",
"}",
"}",
"return",
"imax",
";",
"}"
] |
Find the index of element having maximum absolute value.
|
[
"Find",
"the",
"index",
"of",
"element",
"having",
"maximum",
"absolute",
"value",
"."
] |
e27e43e90fbaacce3f99d30120cf9dd6a764c33d
|
https://github.com/haifengl/smile/blob/e27e43e90fbaacce3f99d30120cf9dd6a764c33d/math/src/main/java/smile/math/matrix/Lanczos.java#L470-L494
|
17,330
|
haifengl/smile
|
math/src/main/java/smile/math/matrix/Lanczos.java
|
Lanczos.store
|
private static void store(double[][] q, int j, double[] s) {
if (null == q[j]) {
q[j] = s.clone();
} else {
Math.copy(s, q[j]);
}
}
|
java
|
private static void store(double[][] q, int j, double[] s) {
if (null == q[j]) {
q[j] = s.clone();
} else {
Math.copy(s, q[j]);
}
}
|
[
"private",
"static",
"void",
"store",
"(",
"double",
"[",
"]",
"[",
"]",
"q",
",",
"int",
"j",
",",
"double",
"[",
"]",
"s",
")",
"{",
"if",
"(",
"null",
"==",
"q",
"[",
"j",
"]",
")",
"{",
"q",
"[",
"j",
"]",
"=",
"s",
".",
"clone",
"(",
")",
";",
"}",
"else",
"{",
"Math",
".",
"copy",
"(",
"s",
",",
"q",
"[",
"j",
"]",
")",
";",
"}",
"}"
] |
Based on the input operation flag, stores to or retrieves from memory a vector.
@param s contains the vector to be stored
|
[
"Based",
"on",
"the",
"input",
"operation",
"flag",
"stores",
"to",
"or",
"retrieves",
"from",
"memory",
"a",
"vector",
"."
] |
e27e43e90fbaacce3f99d30120cf9dd6a764c33d
|
https://github.com/haifengl/smile/blob/e27e43e90fbaacce3f99d30120cf9dd6a764c33d/math/src/main/java/smile/math/matrix/Lanczos.java#L565-L571
|
17,331
|
haifengl/smile
|
math/src/main/java/smile/math/distance/ChebyshevDistance.java
|
ChebyshevDistance.d
|
public static double d(int[] x, int[] y) {
if (x.length != y.length)
throw new IllegalArgumentException(String.format("Arrays have different length: x[%d], y[%d]", x.length, y.length));
double dist = 0.0;
for (int i = 0; i < x.length; i++) {
double d = Math.abs(x[i] - y[i]);
if (dist < d)
dist = d;
}
return dist;
}
|
java
|
public static double d(int[] x, int[] y) {
if (x.length != y.length)
throw new IllegalArgumentException(String.format("Arrays have different length: x[%d], y[%d]", x.length, y.length));
double dist = 0.0;
for (int i = 0; i < x.length; i++) {
double d = Math.abs(x[i] - y[i]);
if (dist < d)
dist = d;
}
return dist;
}
|
[
"public",
"static",
"double",
"d",
"(",
"int",
"[",
"]",
"x",
",",
"int",
"[",
"]",
"y",
")",
"{",
"if",
"(",
"x",
".",
"length",
"!=",
"y",
".",
"length",
")",
"throw",
"new",
"IllegalArgumentException",
"(",
"String",
".",
"format",
"(",
"\"Arrays have different length: x[%d], y[%d]\"",
",",
"x",
".",
"length",
",",
"y",
".",
"length",
")",
")",
";",
"double",
"dist",
"=",
"0.0",
";",
"for",
"(",
"int",
"i",
"=",
"0",
";",
"i",
"<",
"x",
".",
"length",
";",
"i",
"++",
")",
"{",
"double",
"d",
"=",
"Math",
".",
"abs",
"(",
"x",
"[",
"i",
"]",
"-",
"y",
"[",
"i",
"]",
")",
";",
"if",
"(",
"dist",
"<",
"d",
")",
"dist",
"=",
"d",
";",
"}",
"return",
"dist",
";",
"}"
] |
Chebyshev distance between the two arrays of type integer.
|
[
"Chebyshev",
"distance",
"between",
"the",
"two",
"arrays",
"of",
"type",
"integer",
"."
] |
e27e43e90fbaacce3f99d30120cf9dd6a764c33d
|
https://github.com/haifengl/smile/blob/e27e43e90fbaacce3f99d30120cf9dd6a764c33d/math/src/main/java/smile/math/distance/ChebyshevDistance.java#L43-L55
|
17,332
|
haifengl/smile
|
demo/src/main/java/smile/demo/SmileDemo.java
|
SmileDemo.valueChanged
|
@Override
public void valueChanged(TreeSelectionEvent e) {
DefaultMutableTreeNode node = (DefaultMutableTreeNode) tree.getLastSelectedPathComponent();
if (node != null && node.isLeaf()) {
int pos = workspace.getDividerLocation();
workspace.setTopComponent((JPanel) node.getUserObject());
workspace.setDividerLocation(pos);
}
}
|
java
|
@Override
public void valueChanged(TreeSelectionEvent e) {
DefaultMutableTreeNode node = (DefaultMutableTreeNode) tree.getLastSelectedPathComponent();
if (node != null && node.isLeaf()) {
int pos = workspace.getDividerLocation();
workspace.setTopComponent((JPanel) node.getUserObject());
workspace.setDividerLocation(pos);
}
}
|
[
"@",
"Override",
"public",
"void",
"valueChanged",
"(",
"TreeSelectionEvent",
"e",
")",
"{",
"DefaultMutableTreeNode",
"node",
"=",
"(",
"DefaultMutableTreeNode",
")",
"tree",
".",
"getLastSelectedPathComponent",
"(",
")",
";",
"if",
"(",
"node",
"!=",
"null",
"&&",
"node",
".",
"isLeaf",
"(",
")",
")",
"{",
"int",
"pos",
"=",
"workspace",
".",
"getDividerLocation",
"(",
")",
";",
"workspace",
".",
"setTopComponent",
"(",
"(",
"JPanel",
")",
"node",
".",
"getUserObject",
"(",
")",
")",
";",
"workspace",
".",
"setDividerLocation",
"(",
"pos",
")",
";",
"}",
"}"
] |
Required by TreeSelectionListener interface.
|
[
"Required",
"by",
"TreeSelectionListener",
"interface",
"."
] |
e27e43e90fbaacce3f99d30120cf9dd6a764c33d
|
https://github.com/haifengl/smile/blob/e27e43e90fbaacce3f99d30120cf9dd6a764c33d/demo/src/main/java/smile/demo/SmileDemo.java#L183-L192
|
17,333
|
haifengl/smile
|
core/src/main/java/smile/feature/FeatureTransform.java
|
FeatureTransform.learn
|
public void learn(double[][] data) {
int p = data[0].length;
Attribute[] attributes = new Attribute[p];
for (int i = 0; i < p; i++) {
attributes[i] = new NumericAttribute("V"+i);
}
learn(attributes, data);
}
|
java
|
public void learn(double[][] data) {
int p = data[0].length;
Attribute[] attributes = new Attribute[p];
for (int i = 0; i < p; i++) {
attributes[i] = new NumericAttribute("V"+i);
}
learn(attributes, data);
}
|
[
"public",
"void",
"learn",
"(",
"double",
"[",
"]",
"[",
"]",
"data",
")",
"{",
"int",
"p",
"=",
"data",
"[",
"0",
"]",
".",
"length",
";",
"Attribute",
"[",
"]",
"attributes",
"=",
"new",
"Attribute",
"[",
"p",
"]",
";",
"for",
"(",
"int",
"i",
"=",
"0",
";",
"i",
"<",
"p",
";",
"i",
"++",
")",
"{",
"attributes",
"[",
"i",
"]",
"=",
"new",
"NumericAttribute",
"(",
"\"V\"",
"+",
"i",
")",
";",
"}",
"learn",
"(",
"attributes",
",",
"data",
")",
";",
"}"
] |
Learns transformation parameters from a dataset.
All features are assumed numeric.
@param data The training data.
|
[
"Learns",
"transformation",
"parameters",
"from",
"a",
"dataset",
".",
"All",
"features",
"are",
"assumed",
"numeric",
"."
] |
e27e43e90fbaacce3f99d30120cf9dd6a764c33d
|
https://github.com/haifengl/smile/blob/e27e43e90fbaacce3f99d30120cf9dd6a764c33d/core/src/main/java/smile/feature/FeatureTransform.java#L55-L62
|
17,334
|
haifengl/smile
|
core/src/main/java/smile/feature/FeatureTransform.java
|
FeatureTransform.transform
|
public double[][] transform(double[][] x) {
double[][] y = new double[x.length][];
for (int i = 0; i < y.length; i++) {
y[i] = transform(x[i]);
}
return y;
}
|
java
|
public double[][] transform(double[][] x) {
double[][] y = new double[x.length][];
for (int i = 0; i < y.length; i++) {
y[i] = transform(x[i]);
}
return y;
}
|
[
"public",
"double",
"[",
"]",
"[",
"]",
"transform",
"(",
"double",
"[",
"]",
"[",
"]",
"x",
")",
"{",
"double",
"[",
"]",
"[",
"]",
"y",
"=",
"new",
"double",
"[",
"x",
".",
"length",
"]",
"[",
"",
"]",
";",
"for",
"(",
"int",
"i",
"=",
"0",
";",
"i",
"<",
"y",
".",
"length",
";",
"i",
"++",
")",
"{",
"y",
"[",
"i",
"]",
"=",
"transform",
"(",
"x",
"[",
"i",
"]",
")",
";",
"}",
"return",
"y",
";",
"}"
] |
Transform an array of feature vectors.
@param x an array of feature vectors. The feature
vectors may be modified on output if copy is false.
@return the transformed feature vectors.
|
[
"Transform",
"an",
"array",
"of",
"feature",
"vectors",
"."
] |
e27e43e90fbaacce3f99d30120cf9dd6a764c33d
|
https://github.com/haifengl/smile/blob/e27e43e90fbaacce3f99d30120cf9dd6a764c33d/core/src/main/java/smile/feature/FeatureTransform.java#L86-L93
|
17,335
|
haifengl/smile
|
math/src/main/java/smile/math/distance/CorrelationDistance.java
|
CorrelationDistance.d
|
@Override
public double d(double[] x, double[] y) {
if (x.length != y.length)
throw new IllegalArgumentException(String.format("Arrays have different length: x[%d], y[%d]", x.length, y.length));
return 1 - Math.cor(x, y);
}
|
java
|
@Override
public double d(double[] x, double[] y) {
if (x.length != y.length)
throw new IllegalArgumentException(String.format("Arrays have different length: x[%d], y[%d]", x.length, y.length));
return 1 - Math.cor(x, y);
}
|
[
"@",
"Override",
"public",
"double",
"d",
"(",
"double",
"[",
"]",
"x",
",",
"double",
"[",
"]",
"y",
")",
"{",
"if",
"(",
"x",
".",
"length",
"!=",
"y",
".",
"length",
")",
"throw",
"new",
"IllegalArgumentException",
"(",
"String",
".",
"format",
"(",
"\"Arrays have different length: x[%d], y[%d]\"",
",",
"x",
".",
"length",
",",
"y",
".",
"length",
")",
")",
";",
"return",
"1",
"-",
"Math",
".",
"cor",
"(",
"x",
",",
"y",
")",
";",
"}"
] |
Pearson correlation distance between the two arrays of type double.
|
[
"Pearson",
"correlation",
"distance",
"between",
"the",
"two",
"arrays",
"of",
"type",
"double",
"."
] |
e27e43e90fbaacce3f99d30120cf9dd6a764c33d
|
https://github.com/haifengl/smile/blob/e27e43e90fbaacce3f99d30120cf9dd6a764c33d/math/src/main/java/smile/math/distance/CorrelationDistance.java#L44-L50
|
17,336
|
haifengl/smile
|
math/src/main/java/smile/math/distance/CorrelationDistance.java
|
CorrelationDistance.pearson
|
public static double pearson(int[] x, int[] y) {
if (x.length != y.length)
throw new IllegalArgumentException(String.format("Arrays have different length: x[%d], y[%d]", x.length, y.length));
return 1 - Math.cor(x, y);
}
|
java
|
public static double pearson(int[] x, int[] y) {
if (x.length != y.length)
throw new IllegalArgumentException(String.format("Arrays have different length: x[%d], y[%d]", x.length, y.length));
return 1 - Math.cor(x, y);
}
|
[
"public",
"static",
"double",
"pearson",
"(",
"int",
"[",
"]",
"x",
",",
"int",
"[",
"]",
"y",
")",
"{",
"if",
"(",
"x",
".",
"length",
"!=",
"y",
".",
"length",
")",
"throw",
"new",
"IllegalArgumentException",
"(",
"String",
".",
"format",
"(",
"\"Arrays have different length: x[%d], y[%d]\"",
",",
"x",
".",
"length",
",",
"y",
".",
"length",
")",
")",
";",
"return",
"1",
"-",
"Math",
".",
"cor",
"(",
"x",
",",
"y",
")",
";",
"}"
] |
Pearson correlation distance between the two arrays of type int.
|
[
"Pearson",
"correlation",
"distance",
"between",
"the",
"two",
"arrays",
"of",
"type",
"int",
"."
] |
e27e43e90fbaacce3f99d30120cf9dd6a764c33d
|
https://github.com/haifengl/smile/blob/e27e43e90fbaacce3f99d30120cf9dd6a764c33d/math/src/main/java/smile/math/distance/CorrelationDistance.java#L55-L60
|
17,337
|
haifengl/smile
|
math/src/main/java/smile/math/distance/CorrelationDistance.java
|
CorrelationDistance.spearman
|
public static double spearman(int[] x, int[] y) {
if (x.length != y.length)
throw new IllegalArgumentException(String.format("Arrays have different length: x[%d], y[%d]", x.length, y.length));
return 1 - Math.spearman(x, y);
}
|
java
|
public static double spearman(int[] x, int[] y) {
if (x.length != y.length)
throw new IllegalArgumentException(String.format("Arrays have different length: x[%d], y[%d]", x.length, y.length));
return 1 - Math.spearman(x, y);
}
|
[
"public",
"static",
"double",
"spearman",
"(",
"int",
"[",
"]",
"x",
",",
"int",
"[",
"]",
"y",
")",
"{",
"if",
"(",
"x",
".",
"length",
"!=",
"y",
".",
"length",
")",
"throw",
"new",
"IllegalArgumentException",
"(",
"String",
".",
"format",
"(",
"\"Arrays have different length: x[%d], y[%d]\"",
",",
"x",
".",
"length",
",",
"y",
".",
"length",
")",
")",
";",
"return",
"1",
"-",
"Math",
".",
"spearman",
"(",
"x",
",",
"y",
")",
";",
"}"
] |
Spearman correlation distance between the two arrays of type int.
|
[
"Spearman",
"correlation",
"distance",
"between",
"the",
"two",
"arrays",
"of",
"type",
"int",
"."
] |
e27e43e90fbaacce3f99d30120cf9dd6a764c33d
|
https://github.com/haifengl/smile/blob/e27e43e90fbaacce3f99d30120cf9dd6a764c33d/math/src/main/java/smile/math/distance/CorrelationDistance.java#L85-L90
|
17,338
|
haifengl/smile
|
math/src/main/java/smile/math/distance/CorrelationDistance.java
|
CorrelationDistance.kendall
|
public static double kendall(int[] x, int[] y) {
if (x.length != y.length)
throw new IllegalArgumentException(String.format("Arrays have different length: x[%d], y[%d]", x.length, y.length));
return 1 - Math.kendall(x, y);
}
|
java
|
public static double kendall(int[] x, int[] y) {
if (x.length != y.length)
throw new IllegalArgumentException(String.format("Arrays have different length: x[%d], y[%d]", x.length, y.length));
return 1 - Math.kendall(x, y);
}
|
[
"public",
"static",
"double",
"kendall",
"(",
"int",
"[",
"]",
"x",
",",
"int",
"[",
"]",
"y",
")",
"{",
"if",
"(",
"x",
".",
"length",
"!=",
"y",
".",
"length",
")",
"throw",
"new",
"IllegalArgumentException",
"(",
"String",
".",
"format",
"(",
"\"Arrays have different length: x[%d], y[%d]\"",
",",
"x",
".",
"length",
",",
"y",
".",
"length",
")",
")",
";",
"return",
"1",
"-",
"Math",
".",
"kendall",
"(",
"x",
",",
"y",
")",
";",
"}"
] |
Kendall rank correlation distance between the two arrays of type int.
|
[
"Kendall",
"rank",
"correlation",
"distance",
"between",
"the",
"two",
"arrays",
"of",
"type",
"int",
"."
] |
e27e43e90fbaacce3f99d30120cf9dd6a764c33d
|
https://github.com/haifengl/smile/blob/e27e43e90fbaacce3f99d30120cf9dd6a764c33d/math/src/main/java/smile/math/distance/CorrelationDistance.java#L115-L120
|
17,339
|
haifengl/smile
|
math/src/main/java/smile/sort/SortUtils.java
|
SortUtils.swap
|
public static void swap(float arr[], int i, int j) {
float a = arr[i];
arr[i] = arr[j];
arr[j] = a;
}
|
java
|
public static void swap(float arr[], int i, int j) {
float a = arr[i];
arr[i] = arr[j];
arr[j] = a;
}
|
[
"public",
"static",
"void",
"swap",
"(",
"float",
"arr",
"[",
"]",
",",
"int",
"i",
",",
"int",
"j",
")",
"{",
"float",
"a",
"=",
"arr",
"[",
"i",
"]",
";",
"arr",
"[",
"i",
"]",
"=",
"arr",
"[",
"j",
"]",
";",
"arr",
"[",
"j",
"]",
"=",
"a",
";",
"}"
] |
Swap two positions.
|
[
"Swap",
"two",
"positions",
"."
] |
e27e43e90fbaacce3f99d30120cf9dd6a764c33d
|
https://github.com/haifengl/smile/blob/e27e43e90fbaacce3f99d30120cf9dd6a764c33d/math/src/main/java/smile/sort/SortUtils.java#L43-L47
|
17,340
|
haifengl/smile
|
core/src/main/java/smile/clustering/BBDTree.java
|
BBDTree.getNodeCost
|
private double getNodeCost(Node node, double[] center) {
int d = center.length;
double scatter = 0.0;
for (int i = 0; i < d; i++) {
double x = (node.sum[i] / node.count) - center[i];
scatter += x * x;
}
return node.cost + node.count * scatter;
}
|
java
|
private double getNodeCost(Node node, double[] center) {
int d = center.length;
double scatter = 0.0;
for (int i = 0; i < d; i++) {
double x = (node.sum[i] / node.count) - center[i];
scatter += x * x;
}
return node.cost + node.count * scatter;
}
|
[
"private",
"double",
"getNodeCost",
"(",
"Node",
"node",
",",
"double",
"[",
"]",
"center",
")",
"{",
"int",
"d",
"=",
"center",
".",
"length",
";",
"double",
"scatter",
"=",
"0.0",
";",
"for",
"(",
"int",
"i",
"=",
"0",
";",
"i",
"<",
"d",
";",
"i",
"++",
")",
"{",
"double",
"x",
"=",
"(",
"node",
".",
"sum",
"[",
"i",
"]",
"/",
"node",
".",
"count",
")",
"-",
"center",
"[",
"i",
"]",
";",
"scatter",
"+=",
"x",
"*",
"x",
";",
"}",
"return",
"node",
".",
"cost",
"+",
"node",
".",
"count",
"*",
"scatter",
";",
"}"
] |
Returns the total contribution of all data in the given kd-tree node,
assuming they are all assigned to a mean at the given location.
sum_{x \in node} ||x - mean||^2.
If c denotes the mean of mass of the data in this node and n denotes
the number of data in it, then this quantity is given by
n * ||c - mean||^2 + sum_{x \in node} ||x - c||^2
The sum is precomputed for each node as cost. This formula follows
from expanding both sides as dot products.
|
[
"Returns",
"the",
"total",
"contribution",
"of",
"all",
"data",
"in",
"the",
"given",
"kd",
"-",
"tree",
"node",
"assuming",
"they",
"are",
"all",
"assigned",
"to",
"a",
"mean",
"at",
"the",
"given",
"location",
"."
] |
e27e43e90fbaacce3f99d30120cf9dd6a764c33d
|
https://github.com/haifengl/smile/blob/e27e43e90fbaacce3f99d30120cf9dd6a764c33d/core/src/main/java/smile/clustering/BBDTree.java#L241-L249
|
17,341
|
haifengl/smile
|
core/src/main/java/smile/clustering/BBDTree.java
|
BBDTree.clustering
|
public double clustering(double[][] centroids, double[][] sums, int[] counts, int[] membership) {
int k = centroids.length;
Arrays.fill(counts, 0);
int[] candidates = new int[k];
for (int i = 0; i < k; i++) {
candidates[i] = i;
Arrays.fill(sums[i], 0.0);
}
return filter(root, centroids, candidates, k, sums, counts, membership);
}
|
java
|
public double clustering(double[][] centroids, double[][] sums, int[] counts, int[] membership) {
int k = centroids.length;
Arrays.fill(counts, 0);
int[] candidates = new int[k];
for (int i = 0; i < k; i++) {
candidates[i] = i;
Arrays.fill(sums[i], 0.0);
}
return filter(root, centroids, candidates, k, sums, counts, membership);
}
|
[
"public",
"double",
"clustering",
"(",
"double",
"[",
"]",
"[",
"]",
"centroids",
",",
"double",
"[",
"]",
"[",
"]",
"sums",
",",
"int",
"[",
"]",
"counts",
",",
"int",
"[",
"]",
"membership",
")",
"{",
"int",
"k",
"=",
"centroids",
".",
"length",
";",
"Arrays",
".",
"fill",
"(",
"counts",
",",
"0",
")",
";",
"int",
"[",
"]",
"candidates",
"=",
"new",
"int",
"[",
"k",
"]",
";",
"for",
"(",
"int",
"i",
"=",
"0",
";",
"i",
"<",
"k",
";",
"i",
"++",
")",
"{",
"candidates",
"[",
"i",
"]",
"=",
"i",
";",
"Arrays",
".",
"fill",
"(",
"sums",
"[",
"i",
"]",
",",
"0.0",
")",
";",
"}",
"return",
"filter",
"(",
"root",
",",
"centroids",
",",
"candidates",
",",
"k",
",",
"sums",
",",
"counts",
",",
"membership",
")",
";",
"}"
] |
Given k cluster centroids, this method assigns data to nearest centroids.
The return value is the distortion to the centroids. The parameter sums
will hold the sum of data for each cluster. The parameter counts hold
the number of data of each cluster. If membership is
not null, it should be an array of size n that will be filled with the
index of the cluster [0 - k) that each data point is assigned to.
|
[
"Given",
"k",
"cluster",
"centroids",
"this",
"method",
"assigns",
"data",
"to",
"nearest",
"centroids",
".",
"The",
"return",
"value",
"is",
"the",
"distortion",
"to",
"the",
"centroids",
".",
"The",
"parameter",
"sums",
"will",
"hold",
"the",
"sum",
"of",
"data",
"for",
"each",
"cluster",
".",
"The",
"parameter",
"counts",
"hold",
"the",
"number",
"of",
"data",
"of",
"each",
"cluster",
".",
"If",
"membership",
"is",
"not",
"null",
"it",
"should",
"be",
"an",
"array",
"of",
"size",
"n",
"that",
"will",
"be",
"filled",
"with",
"the",
"index",
"of",
"the",
"cluster",
"[",
"0",
"-",
"k",
")",
"that",
"each",
"data",
"point",
"is",
"assigned",
"to",
"."
] |
e27e43e90fbaacce3f99d30120cf9dd6a764c33d
|
https://github.com/haifengl/smile/blob/e27e43e90fbaacce3f99d30120cf9dd6a764c33d/core/src/main/java/smile/clustering/BBDTree.java#L259-L270
|
17,342
|
haifengl/smile
|
core/src/main/java/smile/imputation/KMeansImputation.java
|
KMeansImputation.columnAverageImpute
|
static void columnAverageImpute(double[][] data) throws MissingValueImputationException {
for (int j = 0; j < data[0].length; j++) {
int n = 0;
double sum = 0.0;
for (int i = 0; i < data.length; i++) {
if (!Double.isNaN(data[i][j])) {
n++;
sum += data[i][j];
}
}
if (n == 0) {
continue;
}
if (n < data.length) {
double avg = sum / n;
for (int i = 0; i < data.length; i++) {
if (Double.isNaN(data[i][j])) {
data[i][j] = avg;
}
}
}
}
}
|
java
|
static void columnAverageImpute(double[][] data) throws MissingValueImputationException {
for (int j = 0; j < data[0].length; j++) {
int n = 0;
double sum = 0.0;
for (int i = 0; i < data.length; i++) {
if (!Double.isNaN(data[i][j])) {
n++;
sum += data[i][j];
}
}
if (n == 0) {
continue;
}
if (n < data.length) {
double avg = sum / n;
for (int i = 0; i < data.length; i++) {
if (Double.isNaN(data[i][j])) {
data[i][j] = avg;
}
}
}
}
}
|
[
"static",
"void",
"columnAverageImpute",
"(",
"double",
"[",
"]",
"[",
"]",
"data",
")",
"throws",
"MissingValueImputationException",
"{",
"for",
"(",
"int",
"j",
"=",
"0",
";",
"j",
"<",
"data",
"[",
"0",
"]",
".",
"length",
";",
"j",
"++",
")",
"{",
"int",
"n",
"=",
"0",
";",
"double",
"sum",
"=",
"0.0",
";",
"for",
"(",
"int",
"i",
"=",
"0",
";",
"i",
"<",
"data",
".",
"length",
";",
"i",
"++",
")",
"{",
"if",
"(",
"!",
"Double",
".",
"isNaN",
"(",
"data",
"[",
"i",
"]",
"[",
"j",
"]",
")",
")",
"{",
"n",
"++",
";",
"sum",
"+=",
"data",
"[",
"i",
"]",
"[",
"j",
"]",
";",
"}",
"}",
"if",
"(",
"n",
"==",
"0",
")",
"{",
"continue",
";",
"}",
"if",
"(",
"n",
"<",
"data",
".",
"length",
")",
"{",
"double",
"avg",
"=",
"sum",
"/",
"n",
";",
"for",
"(",
"int",
"i",
"=",
"0",
";",
"i",
"<",
"data",
".",
"length",
";",
"i",
"++",
")",
"{",
"if",
"(",
"Double",
".",
"isNaN",
"(",
"data",
"[",
"i",
"]",
"[",
"j",
"]",
")",
")",
"{",
"data",
"[",
"i",
"]",
"[",
"j",
"]",
"=",
"avg",
";",
"}",
"}",
"}",
"}",
"}"
] |
Impute the missing values with column averages.
@param data data with missing values.
@throws smile.imputation.MissingValueImputationException
|
[
"Impute",
"the",
"missing",
"values",
"with",
"column",
"averages",
"."
] |
e27e43e90fbaacce3f99d30120cf9dd6a764c33d
|
https://github.com/haifengl/smile/blob/e27e43e90fbaacce3f99d30120cf9dd6a764c33d/core/src/main/java/smile/imputation/KMeansImputation.java#L111-L136
|
17,343
|
haifengl/smile
|
core/src/main/java/smile/validation/Validation.java
|
Validation.loocv
|
public static <T> double loocv(ClassifierTrainer<T> trainer, T[] x, int[] y) {
int m = 0;
int n = x.length;
LOOCV loocv = new LOOCV(n);
for (int i = 0; i < n; i++) {
T[] trainx = Math.slice(x, loocv.train[i]);
int[] trainy = Math.slice(y, loocv.train[i]);
Classifier<T> classifier = trainer.train(trainx, trainy);
if (classifier.predict(x[loocv.test[i]]) == y[loocv.test[i]]) {
m++;
}
}
return (double) m / n;
}
|
java
|
public static <T> double loocv(ClassifierTrainer<T> trainer, T[] x, int[] y) {
int m = 0;
int n = x.length;
LOOCV loocv = new LOOCV(n);
for (int i = 0; i < n; i++) {
T[] trainx = Math.slice(x, loocv.train[i]);
int[] trainy = Math.slice(y, loocv.train[i]);
Classifier<T> classifier = trainer.train(trainx, trainy);
if (classifier.predict(x[loocv.test[i]]) == y[loocv.test[i]]) {
m++;
}
}
return (double) m / n;
}
|
[
"public",
"static",
"<",
"T",
">",
"double",
"loocv",
"(",
"ClassifierTrainer",
"<",
"T",
">",
"trainer",
",",
"T",
"[",
"]",
"x",
",",
"int",
"[",
"]",
"y",
")",
"{",
"int",
"m",
"=",
"0",
";",
"int",
"n",
"=",
"x",
".",
"length",
";",
"LOOCV",
"loocv",
"=",
"new",
"LOOCV",
"(",
"n",
")",
";",
"for",
"(",
"int",
"i",
"=",
"0",
";",
"i",
"<",
"n",
";",
"i",
"++",
")",
"{",
"T",
"[",
"]",
"trainx",
"=",
"Math",
".",
"slice",
"(",
"x",
",",
"loocv",
".",
"train",
"[",
"i",
"]",
")",
";",
"int",
"[",
"]",
"trainy",
"=",
"Math",
".",
"slice",
"(",
"y",
",",
"loocv",
".",
"train",
"[",
"i",
"]",
")",
";",
"Classifier",
"<",
"T",
">",
"classifier",
"=",
"trainer",
".",
"train",
"(",
"trainx",
",",
"trainy",
")",
";",
"if",
"(",
"classifier",
".",
"predict",
"(",
"x",
"[",
"loocv",
".",
"test",
"[",
"i",
"]",
"]",
")",
"==",
"y",
"[",
"loocv",
".",
"test",
"[",
"i",
"]",
"]",
")",
"{",
"m",
"++",
";",
"}",
"}",
"return",
"(",
"double",
")",
"m",
"/",
"n",
";",
"}"
] |
Leave-one-out cross validation of a classification model.
@param <T> the data type of input objects.
@param trainer a classifier trainer that is properly parameterized.
@param x the test data set.
@param y the test data labels.
@return the accuracy on test dataset
|
[
"Leave",
"-",
"one",
"-",
"out",
"cross",
"validation",
"of",
"a",
"classification",
"model",
"."
] |
e27e43e90fbaacce3f99d30120cf9dd6a764c33d
|
https://github.com/haifengl/smile/blob/e27e43e90fbaacce3f99d30120cf9dd6a764c33d/core/src/main/java/smile/validation/Validation.java#L169-L186
|
17,344
|
haifengl/smile
|
core/src/main/java/smile/validation/Validation.java
|
Validation.cv
|
public static <T> double cv(int k, ClassifierTrainer<T> trainer, T[] x, int[] y, ClassificationMeasure measure) {
if (k < 2) {
throw new IllegalArgumentException("Invalid k for k-fold cross validation: " + k);
}
int n = x.length;
int[] predictions = new int[n];
CrossValidation cv = new CrossValidation(n, k);
for (int i = 0; i < k; i++) {
T[] trainx = Math.slice(x, cv.train[i]);
int[] trainy = Math.slice(y, cv.train[i]);
Classifier<T> classifier = trainer.train(trainx, trainy);
for (int j : cv.test[i]) {
predictions[j] = classifier.predict(x[j]);
}
}
return measure.measure(y, predictions);
}
|
java
|
public static <T> double cv(int k, ClassifierTrainer<T> trainer, T[] x, int[] y, ClassificationMeasure measure) {
if (k < 2) {
throw new IllegalArgumentException("Invalid k for k-fold cross validation: " + k);
}
int n = x.length;
int[] predictions = new int[n];
CrossValidation cv = new CrossValidation(n, k);
for (int i = 0; i < k; i++) {
T[] trainx = Math.slice(x, cv.train[i]);
int[] trainy = Math.slice(y, cv.train[i]);
Classifier<T> classifier = trainer.train(trainx, trainy);
for (int j : cv.test[i]) {
predictions[j] = classifier.predict(x[j]);
}
}
return measure.measure(y, predictions);
}
|
[
"public",
"static",
"<",
"T",
">",
"double",
"cv",
"(",
"int",
"k",
",",
"ClassifierTrainer",
"<",
"T",
">",
"trainer",
",",
"T",
"[",
"]",
"x",
",",
"int",
"[",
"]",
"y",
",",
"ClassificationMeasure",
"measure",
")",
"{",
"if",
"(",
"k",
"<",
"2",
")",
"{",
"throw",
"new",
"IllegalArgumentException",
"(",
"\"Invalid k for k-fold cross validation: \"",
"+",
"k",
")",
";",
"}",
"int",
"n",
"=",
"x",
".",
"length",
";",
"int",
"[",
"]",
"predictions",
"=",
"new",
"int",
"[",
"n",
"]",
";",
"CrossValidation",
"cv",
"=",
"new",
"CrossValidation",
"(",
"n",
",",
"k",
")",
";",
"for",
"(",
"int",
"i",
"=",
"0",
";",
"i",
"<",
"k",
";",
"i",
"++",
")",
"{",
"T",
"[",
"]",
"trainx",
"=",
"Math",
".",
"slice",
"(",
"x",
",",
"cv",
".",
"train",
"[",
"i",
"]",
")",
";",
"int",
"[",
"]",
"trainy",
"=",
"Math",
".",
"slice",
"(",
"y",
",",
"cv",
".",
"train",
"[",
"i",
"]",
")",
";",
"Classifier",
"<",
"T",
">",
"classifier",
"=",
"trainer",
".",
"train",
"(",
"trainx",
",",
"trainy",
")",
";",
"for",
"(",
"int",
"j",
":",
"cv",
".",
"test",
"[",
"i",
"]",
")",
"{",
"predictions",
"[",
"j",
"]",
"=",
"classifier",
".",
"predict",
"(",
"x",
"[",
"j",
"]",
")",
";",
"}",
"}",
"return",
"measure",
".",
"measure",
"(",
"y",
",",
"predictions",
")",
";",
"}"
] |
Cross validation of a classification model.
@param <T> the data type of input objects.
@param k k-fold cross validation.
@param trainer a classifier trainer that is properly parameterized.
@param x the test data set.
@param y the test data labels.
@param measure the performance measure of classification.
@return the test results with the same size of order of measures
|
[
"Cross",
"validation",
"of",
"a",
"classification",
"model",
"."
] |
e27e43e90fbaacce3f99d30120cf9dd6a764c33d
|
https://github.com/haifengl/smile/blob/e27e43e90fbaacce3f99d30120cf9dd6a764c33d/core/src/main/java/smile/validation/Validation.java#L410-L431
|
17,345
|
haifengl/smile
|
core/src/main/java/smile/validation/Validation.java
|
Validation.cv
|
public static <T> double cv(int k, RegressionTrainer<T> trainer, T[] x, double[] y, RegressionMeasure measure) {
if (k < 2) {
throw new IllegalArgumentException("Invalid k for k-fold cross validation: " + k);
}
int n = x.length;
double[] predictions = new double[n];
CrossValidation cv = new CrossValidation(n, k);
for (int i = 0; i < k; i++) {
T[] trainx = Math.slice(x, cv.train[i]);
double[] trainy = Math.slice(y, cv.train[i]);
Regression<T> model = trainer.train(trainx, trainy);
for (int j : cv.test[i]) {
predictions[j] = model.predict(x[j]);
}
}
return measure.measure(y, predictions);
}
|
java
|
public static <T> double cv(int k, RegressionTrainer<T> trainer, T[] x, double[] y, RegressionMeasure measure) {
if (k < 2) {
throw new IllegalArgumentException("Invalid k for k-fold cross validation: " + k);
}
int n = x.length;
double[] predictions = new double[n];
CrossValidation cv = new CrossValidation(n, k);
for (int i = 0; i < k; i++) {
T[] trainx = Math.slice(x, cv.train[i]);
double[] trainy = Math.slice(y, cv.train[i]);
Regression<T> model = trainer.train(trainx, trainy);
for (int j : cv.test[i]) {
predictions[j] = model.predict(x[j]);
}
}
return measure.measure(y, predictions);
}
|
[
"public",
"static",
"<",
"T",
">",
"double",
"cv",
"(",
"int",
"k",
",",
"RegressionTrainer",
"<",
"T",
">",
"trainer",
",",
"T",
"[",
"]",
"x",
",",
"double",
"[",
"]",
"y",
",",
"RegressionMeasure",
"measure",
")",
"{",
"if",
"(",
"k",
"<",
"2",
")",
"{",
"throw",
"new",
"IllegalArgumentException",
"(",
"\"Invalid k for k-fold cross validation: \"",
"+",
"k",
")",
";",
"}",
"int",
"n",
"=",
"x",
".",
"length",
";",
"double",
"[",
"]",
"predictions",
"=",
"new",
"double",
"[",
"n",
"]",
";",
"CrossValidation",
"cv",
"=",
"new",
"CrossValidation",
"(",
"n",
",",
"k",
")",
";",
"for",
"(",
"int",
"i",
"=",
"0",
";",
"i",
"<",
"k",
";",
"i",
"++",
")",
"{",
"T",
"[",
"]",
"trainx",
"=",
"Math",
".",
"slice",
"(",
"x",
",",
"cv",
".",
"train",
"[",
"i",
"]",
")",
";",
"double",
"[",
"]",
"trainy",
"=",
"Math",
".",
"slice",
"(",
"y",
",",
"cv",
".",
"train",
"[",
"i",
"]",
")",
";",
"Regression",
"<",
"T",
">",
"model",
"=",
"trainer",
".",
"train",
"(",
"trainx",
",",
"trainy",
")",
";",
"for",
"(",
"int",
"j",
":",
"cv",
".",
"test",
"[",
"i",
"]",
")",
"{",
"predictions",
"[",
"j",
"]",
"=",
"model",
".",
"predict",
"(",
"x",
"[",
"j",
"]",
")",
";",
"}",
"}",
"return",
"measure",
".",
"measure",
"(",
"y",
",",
"predictions",
")",
";",
"}"
] |
Cross validation of a regression model.
@param <T> the data type of input objects.
@param k k-fold cross validation.
@param trainer a regression model trainer that is properly parameterized.
@param x the test data set.
@param y the test data response values.
@param measure the performance measure of regression.
@return the test results with the same size of order of measures
|
[
"Cross",
"validation",
"of",
"a",
"regression",
"model",
"."
] |
e27e43e90fbaacce3f99d30120cf9dd6a764c33d
|
https://github.com/haifengl/smile/blob/e27e43e90fbaacce3f99d30120cf9dd6a764c33d/core/src/main/java/smile/validation/Validation.java#L484-L505
|
17,346
|
haifengl/smile
|
core/src/main/java/smile/validation/Validation.java
|
Validation.bootstrap
|
public static <T> double[] bootstrap(int k, ClassifierTrainer<T> trainer, T[] x, int[] y) {
if (k < 2) {
throw new IllegalArgumentException("Invalid k for k-fold bootstrap: " + k);
}
int n = x.length;
double[] results = new double[k];
Accuracy measure = new Accuracy();
Bootstrap bootstrap = new Bootstrap(n, k);
for (int i = 0; i < k; i++) {
T[] trainx = Math.slice(x, bootstrap.train[i]);
int[] trainy = Math.slice(y, bootstrap.train[i]);
Classifier<T> classifier = trainer.train(trainx, trainy);
int nt = bootstrap.test[i].length;
int[] truth = new int[nt];
int[] predictions = new int[nt];
for (int j = 0; j < nt; j++) {
int l = bootstrap.test[i][j];
truth[j] = y[l];
predictions[j] = classifier.predict(x[l]);
}
results[i] = measure.measure(truth, predictions);
}
return results;
}
|
java
|
public static <T> double[] bootstrap(int k, ClassifierTrainer<T> trainer, T[] x, int[] y) {
if (k < 2) {
throw new IllegalArgumentException("Invalid k for k-fold bootstrap: " + k);
}
int n = x.length;
double[] results = new double[k];
Accuracy measure = new Accuracy();
Bootstrap bootstrap = new Bootstrap(n, k);
for (int i = 0; i < k; i++) {
T[] trainx = Math.slice(x, bootstrap.train[i]);
int[] trainy = Math.slice(y, bootstrap.train[i]);
Classifier<T> classifier = trainer.train(trainx, trainy);
int nt = bootstrap.test[i].length;
int[] truth = new int[nt];
int[] predictions = new int[nt];
for (int j = 0; j < nt; j++) {
int l = bootstrap.test[i][j];
truth[j] = y[l];
predictions[j] = classifier.predict(x[l]);
}
results[i] = measure.measure(truth, predictions);
}
return results;
}
|
[
"public",
"static",
"<",
"T",
">",
"double",
"[",
"]",
"bootstrap",
"(",
"int",
"k",
",",
"ClassifierTrainer",
"<",
"T",
">",
"trainer",
",",
"T",
"[",
"]",
"x",
",",
"int",
"[",
"]",
"y",
")",
"{",
"if",
"(",
"k",
"<",
"2",
")",
"{",
"throw",
"new",
"IllegalArgumentException",
"(",
"\"Invalid k for k-fold bootstrap: \"",
"+",
"k",
")",
";",
"}",
"int",
"n",
"=",
"x",
".",
"length",
";",
"double",
"[",
"]",
"results",
"=",
"new",
"double",
"[",
"k",
"]",
";",
"Accuracy",
"measure",
"=",
"new",
"Accuracy",
"(",
")",
";",
"Bootstrap",
"bootstrap",
"=",
"new",
"Bootstrap",
"(",
"n",
",",
"k",
")",
";",
"for",
"(",
"int",
"i",
"=",
"0",
";",
"i",
"<",
"k",
";",
"i",
"++",
")",
"{",
"T",
"[",
"]",
"trainx",
"=",
"Math",
".",
"slice",
"(",
"x",
",",
"bootstrap",
".",
"train",
"[",
"i",
"]",
")",
";",
"int",
"[",
"]",
"trainy",
"=",
"Math",
".",
"slice",
"(",
"y",
",",
"bootstrap",
".",
"train",
"[",
"i",
"]",
")",
";",
"Classifier",
"<",
"T",
">",
"classifier",
"=",
"trainer",
".",
"train",
"(",
"trainx",
",",
"trainy",
")",
";",
"int",
"nt",
"=",
"bootstrap",
".",
"test",
"[",
"i",
"]",
".",
"length",
";",
"int",
"[",
"]",
"truth",
"=",
"new",
"int",
"[",
"nt",
"]",
";",
"int",
"[",
"]",
"predictions",
"=",
"new",
"int",
"[",
"nt",
"]",
";",
"for",
"(",
"int",
"j",
"=",
"0",
";",
"j",
"<",
"nt",
";",
"j",
"++",
")",
"{",
"int",
"l",
"=",
"bootstrap",
".",
"test",
"[",
"i",
"]",
"[",
"j",
"]",
";",
"truth",
"[",
"j",
"]",
"=",
"y",
"[",
"l",
"]",
";",
"predictions",
"[",
"j",
"]",
"=",
"classifier",
".",
"predict",
"(",
"x",
"[",
"l",
"]",
")",
";",
"}",
"results",
"[",
"i",
"]",
"=",
"measure",
".",
"measure",
"(",
"truth",
",",
"predictions",
")",
";",
"}",
"return",
"results",
";",
"}"
] |
Bootstrap accuracy estimation of a classification model.
@param <T> the data type of input objects.
@param k k-round bootstrap estimation.
@param trainer a classifier trainer that is properly parameterized.
@param x the test data set.
@param y the test data labels.
@return the k-round accuracies
|
[
"Bootstrap",
"accuracy",
"estimation",
"of",
"a",
"classification",
"model",
"."
] |
e27e43e90fbaacce3f99d30120cf9dd6a764c33d
|
https://github.com/haifengl/smile/blob/e27e43e90fbaacce3f99d30120cf9dd6a764c33d/core/src/main/java/smile/validation/Validation.java#L557-L586
|
17,347
|
haifengl/smile
|
core/src/main/java/smile/validation/Validation.java
|
Validation.bootstrap
|
public static <T> double[] bootstrap(int k, RegressionTrainer<T> trainer, T[] x, double[] y, RegressionMeasure measure) {
if (k < 2) {
throw new IllegalArgumentException("Invalid k for k-fold bootstrap: " + k);
}
int n = x.length;
double[] results = new double[k];
Bootstrap bootstrap = new Bootstrap(n, k);
for (int i = 0; i < k; i++) {
T[] trainx = Math.slice(x, bootstrap.train[i]);
double[] trainy = Math.slice(y, bootstrap.train[i]);
Regression<T> model = trainer.train(trainx, trainy);
int nt = bootstrap.test[i].length;
double[] truth = new double[nt];
double[] predictions = new double[nt];
for (int j = 0; j < nt; j++) {
int l = bootstrap.test[i][j];
truth[j] = y[l];
predictions[j] = model.predict(x[l]);
}
results[i] = measure.measure(truth, predictions);
}
return results;
}
|
java
|
public static <T> double[] bootstrap(int k, RegressionTrainer<T> trainer, T[] x, double[] y, RegressionMeasure measure) {
if (k < 2) {
throw new IllegalArgumentException("Invalid k for k-fold bootstrap: " + k);
}
int n = x.length;
double[] results = new double[k];
Bootstrap bootstrap = new Bootstrap(n, k);
for (int i = 0; i < k; i++) {
T[] trainx = Math.slice(x, bootstrap.train[i]);
double[] trainy = Math.slice(y, bootstrap.train[i]);
Regression<T> model = trainer.train(trainx, trainy);
int nt = bootstrap.test[i].length;
double[] truth = new double[nt];
double[] predictions = new double[nt];
for (int j = 0; j < nt; j++) {
int l = bootstrap.test[i][j];
truth[j] = y[l];
predictions[j] = model.predict(x[l]);
}
results[i] = measure.measure(truth, predictions);
}
return results;
}
|
[
"public",
"static",
"<",
"T",
">",
"double",
"[",
"]",
"bootstrap",
"(",
"int",
"k",
",",
"RegressionTrainer",
"<",
"T",
">",
"trainer",
",",
"T",
"[",
"]",
"x",
",",
"double",
"[",
"]",
"y",
",",
"RegressionMeasure",
"measure",
")",
"{",
"if",
"(",
"k",
"<",
"2",
")",
"{",
"throw",
"new",
"IllegalArgumentException",
"(",
"\"Invalid k for k-fold bootstrap: \"",
"+",
"k",
")",
";",
"}",
"int",
"n",
"=",
"x",
".",
"length",
";",
"double",
"[",
"]",
"results",
"=",
"new",
"double",
"[",
"k",
"]",
";",
"Bootstrap",
"bootstrap",
"=",
"new",
"Bootstrap",
"(",
"n",
",",
"k",
")",
";",
"for",
"(",
"int",
"i",
"=",
"0",
";",
"i",
"<",
"k",
";",
"i",
"++",
")",
"{",
"T",
"[",
"]",
"trainx",
"=",
"Math",
".",
"slice",
"(",
"x",
",",
"bootstrap",
".",
"train",
"[",
"i",
"]",
")",
";",
"double",
"[",
"]",
"trainy",
"=",
"Math",
".",
"slice",
"(",
"y",
",",
"bootstrap",
".",
"train",
"[",
"i",
"]",
")",
";",
"Regression",
"<",
"T",
">",
"model",
"=",
"trainer",
".",
"train",
"(",
"trainx",
",",
"trainy",
")",
";",
"int",
"nt",
"=",
"bootstrap",
".",
"test",
"[",
"i",
"]",
".",
"length",
";",
"double",
"[",
"]",
"truth",
"=",
"new",
"double",
"[",
"nt",
"]",
";",
"double",
"[",
"]",
"predictions",
"=",
"new",
"double",
"[",
"nt",
"]",
";",
"for",
"(",
"int",
"j",
"=",
"0",
";",
"j",
"<",
"nt",
";",
"j",
"++",
")",
"{",
"int",
"l",
"=",
"bootstrap",
".",
"test",
"[",
"i",
"]",
"[",
"j",
"]",
";",
"truth",
"[",
"j",
"]",
"=",
"y",
"[",
"l",
"]",
";",
"predictions",
"[",
"j",
"]",
"=",
"model",
".",
"predict",
"(",
"x",
"[",
"l",
"]",
")",
";",
"}",
"results",
"[",
"i",
"]",
"=",
"measure",
".",
"measure",
"(",
"truth",
",",
"predictions",
")",
";",
"}",
"return",
"results",
";",
"}"
] |
Bootstrap performance estimation of a regression model.
@param <T> the data type of input objects.
@param k k-fold bootstrap estimation.
@param trainer a regression model trainer that is properly parameterized.
@param x the test data set.
@param y the test data response values.
@param measure the performance measure of regression.
@return k-by-m test result matrix, where k is the number of
bootstrap samples and m is the number of performance measures.
|
[
"Bootstrap",
"performance",
"estimation",
"of",
"a",
"regression",
"model",
"."
] |
e27e43e90fbaacce3f99d30120cf9dd6a764c33d
|
https://github.com/haifengl/smile/blob/e27e43e90fbaacce3f99d30120cf9dd6a764c33d/core/src/main/java/smile/validation/Validation.java#L728-L756
|
17,348
|
haifengl/smile
|
data/src/main/java/smile/data/BinarySparseDataset.java
|
BinarySparseDataset.toSparseMatrix
|
public SparseMatrix toSparseMatrix() {
int[] pos = new int[numColumns];
int[] colIndex = new int[numColumns + 1];
for (int i = 0; i < numColumns; i++) {
colIndex[i + 1] = colIndex[i] + colSize[i];
}
int nrows = size();
int[] rowIndex = new int[n];
double[] x = new double[n];
for (int i = 0; i < nrows; i++) {
for (int j : get(i).x) {
int k = colIndex[j] + pos[j];
rowIndex[k] = i;
x[k] = 1;
pos[j]++;
}
}
return new SparseMatrix(nrows, numColumns, x, rowIndex, colIndex);
}
|
java
|
public SparseMatrix toSparseMatrix() {
int[] pos = new int[numColumns];
int[] colIndex = new int[numColumns + 1];
for (int i = 0; i < numColumns; i++) {
colIndex[i + 1] = colIndex[i] + colSize[i];
}
int nrows = size();
int[] rowIndex = new int[n];
double[] x = new double[n];
for (int i = 0; i < nrows; i++) {
for (int j : get(i).x) {
int k = colIndex[j] + pos[j];
rowIndex[k] = i;
x[k] = 1;
pos[j]++;
}
}
return new SparseMatrix(nrows, numColumns, x, rowIndex, colIndex);
}
|
[
"public",
"SparseMatrix",
"toSparseMatrix",
"(",
")",
"{",
"int",
"[",
"]",
"pos",
"=",
"new",
"int",
"[",
"numColumns",
"]",
";",
"int",
"[",
"]",
"colIndex",
"=",
"new",
"int",
"[",
"numColumns",
"+",
"1",
"]",
";",
"for",
"(",
"int",
"i",
"=",
"0",
";",
"i",
"<",
"numColumns",
";",
"i",
"++",
")",
"{",
"colIndex",
"[",
"i",
"+",
"1",
"]",
"=",
"colIndex",
"[",
"i",
"]",
"+",
"colSize",
"[",
"i",
"]",
";",
"}",
"int",
"nrows",
"=",
"size",
"(",
")",
";",
"int",
"[",
"]",
"rowIndex",
"=",
"new",
"int",
"[",
"n",
"]",
";",
"double",
"[",
"]",
"x",
"=",
"new",
"double",
"[",
"n",
"]",
";",
"for",
"(",
"int",
"i",
"=",
"0",
";",
"i",
"<",
"nrows",
";",
"i",
"++",
")",
"{",
"for",
"(",
"int",
"j",
":",
"get",
"(",
"i",
")",
".",
"x",
")",
"{",
"int",
"k",
"=",
"colIndex",
"[",
"j",
"]",
"+",
"pos",
"[",
"j",
"]",
";",
"rowIndex",
"[",
"k",
"]",
"=",
"i",
";",
"x",
"[",
"k",
"]",
"=",
"1",
";",
"pos",
"[",
"j",
"]",
"++",
";",
"}",
"}",
"return",
"new",
"SparseMatrix",
"(",
"nrows",
",",
"numColumns",
",",
"x",
",",
"rowIndex",
",",
"colIndex",
")",
";",
"}"
] |
Convert into Harwell-Boeing column-compressed sparse matrix format.
|
[
"Convert",
"into",
"Harwell",
"-",
"Boeing",
"column",
"-",
"compressed",
"sparse",
"matrix",
"format",
"."
] |
e27e43e90fbaacce3f99d30120cf9dd6a764c33d
|
https://github.com/haifengl/smile/blob/e27e43e90fbaacce3f99d30120cf9dd6a764c33d/data/src/main/java/smile/data/BinarySparseDataset.java#L165-L187
|
17,349
|
haifengl/smile
|
nlp/src/main/java/smile/nlp/SimpleCorpus.java
|
SimpleCorpus.add
|
public Text add(String id, String title, String body) {
ArrayList<String> bag = new ArrayList<>();
for (String sentence : splitter.split(body)) {
String[] tokens = tokenizer.split(sentence);
for (int i = 0; i < tokens.length; i++) {
tokens[i] = tokens[i].toLowerCase();
}
for (String w : tokens) {
boolean keep = true;
if (punctuations != null && punctuations.contains(w)) {
keep = false;
} else if (stopWords != null && stopWords.contains(w)) {
keep = false;
}
if (keep) {
size++;
bag.add(w);
Integer f = freq.get(w);
if (f == null) {
f = 1;
} else {
f = f + 1;
}
freq.put(w, f);
}
}
for (int i = 0; i < tokens.length - 1; i++) {
String w1 = tokens[i];
String w2 = tokens[i + 1];
if (freq.containsKey(w1) && freq.containsKey(w2)) {
Bigram bigram = new Bigram(w1, w2);
Integer f = freq2.get(bigram);
if (f == null) {
f = 1;
} else {
f = f + 1;
}
freq2.put(bigram, f);
}
}
}
String[] words = new String[bag.size()];
for (int i = 0; i < words.length; i++) {
words[i] = bag.get(i);
}
SimpleText doc = new SimpleText(id, title, body, words);
docs.add(doc);
for (String term : doc.unique()) {
List<SimpleText> hit = invertedFile.get(term);
if (hit == null) {
hit = new ArrayList<>();
invertedFile.put(term, hit);
}
hit.add(doc);
}
return doc;
}
|
java
|
public Text add(String id, String title, String body) {
ArrayList<String> bag = new ArrayList<>();
for (String sentence : splitter.split(body)) {
String[] tokens = tokenizer.split(sentence);
for (int i = 0; i < tokens.length; i++) {
tokens[i] = tokens[i].toLowerCase();
}
for (String w : tokens) {
boolean keep = true;
if (punctuations != null && punctuations.contains(w)) {
keep = false;
} else if (stopWords != null && stopWords.contains(w)) {
keep = false;
}
if (keep) {
size++;
bag.add(w);
Integer f = freq.get(w);
if (f == null) {
f = 1;
} else {
f = f + 1;
}
freq.put(w, f);
}
}
for (int i = 0; i < tokens.length - 1; i++) {
String w1 = tokens[i];
String w2 = tokens[i + 1];
if (freq.containsKey(w1) && freq.containsKey(w2)) {
Bigram bigram = new Bigram(w1, w2);
Integer f = freq2.get(bigram);
if (f == null) {
f = 1;
} else {
f = f + 1;
}
freq2.put(bigram, f);
}
}
}
String[] words = new String[bag.size()];
for (int i = 0; i < words.length; i++) {
words[i] = bag.get(i);
}
SimpleText doc = new SimpleText(id, title, body, words);
docs.add(doc);
for (String term : doc.unique()) {
List<SimpleText> hit = invertedFile.get(term);
if (hit == null) {
hit = new ArrayList<>();
invertedFile.put(term, hit);
}
hit.add(doc);
}
return doc;
}
|
[
"public",
"Text",
"add",
"(",
"String",
"id",
",",
"String",
"title",
",",
"String",
"body",
")",
"{",
"ArrayList",
"<",
"String",
">",
"bag",
"=",
"new",
"ArrayList",
"<>",
"(",
")",
";",
"for",
"(",
"String",
"sentence",
":",
"splitter",
".",
"split",
"(",
"body",
")",
")",
"{",
"String",
"[",
"]",
"tokens",
"=",
"tokenizer",
".",
"split",
"(",
"sentence",
")",
";",
"for",
"(",
"int",
"i",
"=",
"0",
";",
"i",
"<",
"tokens",
".",
"length",
";",
"i",
"++",
")",
"{",
"tokens",
"[",
"i",
"]",
"=",
"tokens",
"[",
"i",
"]",
".",
"toLowerCase",
"(",
")",
";",
"}",
"for",
"(",
"String",
"w",
":",
"tokens",
")",
"{",
"boolean",
"keep",
"=",
"true",
";",
"if",
"(",
"punctuations",
"!=",
"null",
"&&",
"punctuations",
".",
"contains",
"(",
"w",
")",
")",
"{",
"keep",
"=",
"false",
";",
"}",
"else",
"if",
"(",
"stopWords",
"!=",
"null",
"&&",
"stopWords",
".",
"contains",
"(",
"w",
")",
")",
"{",
"keep",
"=",
"false",
";",
"}",
"if",
"(",
"keep",
")",
"{",
"size",
"++",
";",
"bag",
".",
"add",
"(",
"w",
")",
";",
"Integer",
"f",
"=",
"freq",
".",
"get",
"(",
"w",
")",
";",
"if",
"(",
"f",
"==",
"null",
")",
"{",
"f",
"=",
"1",
";",
"}",
"else",
"{",
"f",
"=",
"f",
"+",
"1",
";",
"}",
"freq",
".",
"put",
"(",
"w",
",",
"f",
")",
";",
"}",
"}",
"for",
"(",
"int",
"i",
"=",
"0",
";",
"i",
"<",
"tokens",
".",
"length",
"-",
"1",
";",
"i",
"++",
")",
"{",
"String",
"w1",
"=",
"tokens",
"[",
"i",
"]",
";",
"String",
"w2",
"=",
"tokens",
"[",
"i",
"+",
"1",
"]",
";",
"if",
"(",
"freq",
".",
"containsKey",
"(",
"w1",
")",
"&&",
"freq",
".",
"containsKey",
"(",
"w2",
")",
")",
"{",
"Bigram",
"bigram",
"=",
"new",
"Bigram",
"(",
"w1",
",",
"w2",
")",
";",
"Integer",
"f",
"=",
"freq2",
".",
"get",
"(",
"bigram",
")",
";",
"if",
"(",
"f",
"==",
"null",
")",
"{",
"f",
"=",
"1",
";",
"}",
"else",
"{",
"f",
"=",
"f",
"+",
"1",
";",
"}",
"freq2",
".",
"put",
"(",
"bigram",
",",
"f",
")",
";",
"}",
"}",
"}",
"String",
"[",
"]",
"words",
"=",
"new",
"String",
"[",
"bag",
".",
"size",
"(",
")",
"]",
";",
"for",
"(",
"int",
"i",
"=",
"0",
";",
"i",
"<",
"words",
".",
"length",
";",
"i",
"++",
")",
"{",
"words",
"[",
"i",
"]",
"=",
"bag",
".",
"get",
"(",
"i",
")",
";",
"}",
"SimpleText",
"doc",
"=",
"new",
"SimpleText",
"(",
"id",
",",
"title",
",",
"body",
",",
"words",
")",
";",
"docs",
".",
"add",
"(",
"doc",
")",
";",
"for",
"(",
"String",
"term",
":",
"doc",
".",
"unique",
"(",
")",
")",
"{",
"List",
"<",
"SimpleText",
">",
"hit",
"=",
"invertedFile",
".",
"get",
"(",
"term",
")",
";",
"if",
"(",
"hit",
"==",
"null",
")",
"{",
"hit",
"=",
"new",
"ArrayList",
"<>",
"(",
")",
";",
"invertedFile",
".",
"put",
"(",
"term",
",",
"hit",
")",
";",
"}",
"hit",
".",
"add",
"(",
"doc",
")",
";",
"}",
"return",
"doc",
";",
"}"
] |
Add a document to the corpus.
|
[
"Add",
"a",
"document",
"to",
"the",
"corpus",
"."
] |
e27e43e90fbaacce3f99d30120cf9dd6a764c33d
|
https://github.com/haifengl/smile/blob/e27e43e90fbaacce3f99d30120cf9dd6a764c33d/nlp/src/main/java/smile/nlp/SimpleCorpus.java#L108-L176
|
17,350
|
haifengl/smile
|
graph/src/main/java/smile/graph/AdjacencyMatrix.java
|
AdjacencyMatrix.dijkstra
|
public double[] dijkstra(int s, boolean weighted) {
double[] wt = new double[n];
Arrays.fill(wt, Double.POSITIVE_INFINITY);
PriorityQueue queue = new PriorityQueue(wt);
for (int v = 0; v < n; v++) {
queue.insert(v);
}
wt[s] = 0.0;
queue.lower(s);
while (!queue.empty()) {
int v = queue.poll();
if (!Double.isInfinite(wt[v])) {
for (int w = 0; w < n; w++) {
if (graph[v][w] != 0.0) {
double p = weighted ? wt[v] + graph[v][w] : wt[v] + 1;
if (p < wt[w]) {
wt[w] = p;
queue.lower(w);
}
}
}
}
}
return wt;
}
|
java
|
public double[] dijkstra(int s, boolean weighted) {
double[] wt = new double[n];
Arrays.fill(wt, Double.POSITIVE_INFINITY);
PriorityQueue queue = new PriorityQueue(wt);
for (int v = 0; v < n; v++) {
queue.insert(v);
}
wt[s] = 0.0;
queue.lower(s);
while (!queue.empty()) {
int v = queue.poll();
if (!Double.isInfinite(wt[v])) {
for (int w = 0; w < n; w++) {
if (graph[v][w] != 0.0) {
double p = weighted ? wt[v] + graph[v][w] : wt[v] + 1;
if (p < wt[w]) {
wt[w] = p;
queue.lower(w);
}
}
}
}
}
return wt;
}
|
[
"public",
"double",
"[",
"]",
"dijkstra",
"(",
"int",
"s",
",",
"boolean",
"weighted",
")",
"{",
"double",
"[",
"]",
"wt",
"=",
"new",
"double",
"[",
"n",
"]",
";",
"Arrays",
".",
"fill",
"(",
"wt",
",",
"Double",
".",
"POSITIVE_INFINITY",
")",
";",
"PriorityQueue",
"queue",
"=",
"new",
"PriorityQueue",
"(",
"wt",
")",
";",
"for",
"(",
"int",
"v",
"=",
"0",
";",
"v",
"<",
"n",
";",
"v",
"++",
")",
"{",
"queue",
".",
"insert",
"(",
"v",
")",
";",
"}",
"wt",
"[",
"s",
"]",
"=",
"0.0",
";",
"queue",
".",
"lower",
"(",
"s",
")",
";",
"while",
"(",
"!",
"queue",
".",
"empty",
"(",
")",
")",
"{",
"int",
"v",
"=",
"queue",
".",
"poll",
"(",
")",
";",
"if",
"(",
"!",
"Double",
".",
"isInfinite",
"(",
"wt",
"[",
"v",
"]",
")",
")",
"{",
"for",
"(",
"int",
"w",
"=",
"0",
";",
"w",
"<",
"n",
";",
"w",
"++",
")",
"{",
"if",
"(",
"graph",
"[",
"v",
"]",
"[",
"w",
"]",
"!=",
"0.0",
")",
"{",
"double",
"p",
"=",
"weighted",
"?",
"wt",
"[",
"v",
"]",
"+",
"graph",
"[",
"v",
"]",
"[",
"w",
"]",
":",
"wt",
"[",
"v",
"]",
"+",
"1",
";",
"if",
"(",
"p",
"<",
"wt",
"[",
"w",
"]",
")",
"{",
"wt",
"[",
"w",
"]",
"=",
"p",
";",
"queue",
".",
"lower",
"(",
"w",
")",
";",
"}",
"}",
"}",
"}",
"}",
"return",
"wt",
";",
"}"
] |
Calculates the shortest path by Dijkstra algorithm.
@param s The source vertex.
@param weighted True to calculate weighted path. Otherwise, the edge weights will be ignored.
@return The distance to all vertices from the source.
|
[
"Calculates",
"the",
"shortest",
"path",
"by",
"Dijkstra",
"algorithm",
"."
] |
e27e43e90fbaacce3f99d30120cf9dd6a764c33d
|
https://github.com/haifengl/smile/blob/e27e43e90fbaacce3f99d30120cf9dd6a764c33d/graph/src/main/java/smile/graph/AdjacencyMatrix.java#L508-L536
|
17,351
|
haifengl/smile
|
demo/src/main/java/smile/demo/data/classification/ToyData.java
|
ToyData.sample
|
public double[][] sample(int n) {
double[][] samples = new double[2 * n][];
MultivariateGaussianDistribution[] gauss = new MultivariateGaussianDistribution[k];
for (int i = 0; i < k; i++) {
gauss[i] = new MultivariateGaussianDistribution(m[i], v);
}
for (int i = 0; i < n; i++) {
samples[i] = gauss[Math.random(prob)].rand();
}
for (int i = 0; i < k; i++) {
gauss[i] = new MultivariateGaussianDistribution(m[k + i], v);
}
for (int i = 0; i < n; i++) {
samples[n + i] = gauss[Math.random(prob)].rand();
}
return samples;
}
|
java
|
public double[][] sample(int n) {
double[][] samples = new double[2 * n][];
MultivariateGaussianDistribution[] gauss = new MultivariateGaussianDistribution[k];
for (int i = 0; i < k; i++) {
gauss[i] = new MultivariateGaussianDistribution(m[i], v);
}
for (int i = 0; i < n; i++) {
samples[i] = gauss[Math.random(prob)].rand();
}
for (int i = 0; i < k; i++) {
gauss[i] = new MultivariateGaussianDistribution(m[k + i], v);
}
for (int i = 0; i < n; i++) {
samples[n + i] = gauss[Math.random(prob)].rand();
}
return samples;
}
|
[
"public",
"double",
"[",
"]",
"[",
"]",
"sample",
"(",
"int",
"n",
")",
"{",
"double",
"[",
"]",
"[",
"]",
"samples",
"=",
"new",
"double",
"[",
"2",
"*",
"n",
"]",
"[",
"",
"]",
";",
"MultivariateGaussianDistribution",
"[",
"]",
"gauss",
"=",
"new",
"MultivariateGaussianDistribution",
"[",
"k",
"]",
";",
"for",
"(",
"int",
"i",
"=",
"0",
";",
"i",
"<",
"k",
";",
"i",
"++",
")",
"{",
"gauss",
"[",
"i",
"]",
"=",
"new",
"MultivariateGaussianDistribution",
"(",
"m",
"[",
"i",
"]",
",",
"v",
")",
";",
"}",
"for",
"(",
"int",
"i",
"=",
"0",
";",
"i",
"<",
"n",
";",
"i",
"++",
")",
"{",
"samples",
"[",
"i",
"]",
"=",
"gauss",
"[",
"Math",
".",
"random",
"(",
"prob",
")",
"]",
".",
"rand",
"(",
")",
";",
"}",
"for",
"(",
"int",
"i",
"=",
"0",
";",
"i",
"<",
"k",
";",
"i",
"++",
")",
"{",
"gauss",
"[",
"i",
"]",
"=",
"new",
"MultivariateGaussianDistribution",
"(",
"m",
"[",
"k",
"+",
"i",
"]",
",",
"v",
")",
";",
"}",
"for",
"(",
"int",
"i",
"=",
"0",
";",
"i",
"<",
"n",
";",
"i",
"++",
")",
"{",
"samples",
"[",
"n",
"+",
"i",
"]",
"=",
"gauss",
"[",
"Math",
".",
"random",
"(",
"prob",
")",
"]",
".",
"rand",
"(",
")",
";",
"}",
"return",
"samples",
";",
"}"
] |
Generate n samples from each class.
|
[
"Generate",
"n",
"samples",
"from",
"each",
"class",
"."
] |
e27e43e90fbaacce3f99d30120cf9dd6a764c33d
|
https://github.com/haifengl/smile/blob/e27e43e90fbaacce3f99d30120cf9dd6a764c33d/demo/src/main/java/smile/demo/data/classification/ToyData.java#L66-L87
|
17,352
|
haifengl/smile
|
core/src/main/java/smile/vq/NeuralMap.java
|
NeuralMap.purge
|
public int purge(int minPts) {
List<Neuron> outliers = new ArrayList<>();
for (Neuron neuron : neurons) {
if (neuron.n < minPts) {
outliers.add(neuron);
}
}
neurons.removeAll(outliers);
for (Neuron neuron : neurons) {
neuron.neighbors.removeAll(outliers);
}
outliers.clear();
for (Neuron neuron : neurons) {
if (neuron.neighbors.isEmpty()) {
outliers.add(neuron);
}
}
neurons.removeAll(outliers);
return neurons.size();
}
|
java
|
public int purge(int minPts) {
List<Neuron> outliers = new ArrayList<>();
for (Neuron neuron : neurons) {
if (neuron.n < minPts) {
outliers.add(neuron);
}
}
neurons.removeAll(outliers);
for (Neuron neuron : neurons) {
neuron.neighbors.removeAll(outliers);
}
outliers.clear();
for (Neuron neuron : neurons) {
if (neuron.neighbors.isEmpty()) {
outliers.add(neuron);
}
}
neurons.removeAll(outliers);
return neurons.size();
}
|
[
"public",
"int",
"purge",
"(",
"int",
"minPts",
")",
"{",
"List",
"<",
"Neuron",
">",
"outliers",
"=",
"new",
"ArrayList",
"<>",
"(",
")",
";",
"for",
"(",
"Neuron",
"neuron",
":",
"neurons",
")",
"{",
"if",
"(",
"neuron",
".",
"n",
"<",
"minPts",
")",
"{",
"outliers",
".",
"add",
"(",
"neuron",
")",
";",
"}",
"}",
"neurons",
".",
"removeAll",
"(",
"outliers",
")",
";",
"for",
"(",
"Neuron",
"neuron",
":",
"neurons",
")",
"{",
"neuron",
".",
"neighbors",
".",
"removeAll",
"(",
"outliers",
")",
";",
"}",
"outliers",
".",
"clear",
"(",
")",
";",
"for",
"(",
"Neuron",
"neuron",
":",
"neurons",
")",
"{",
"if",
"(",
"neuron",
".",
"neighbors",
".",
"isEmpty",
"(",
")",
")",
"{",
"outliers",
".",
"add",
"(",
"neuron",
")",
";",
"}",
"}",
"neurons",
".",
"removeAll",
"(",
"outliers",
")",
";",
"return",
"neurons",
".",
"size",
"(",
")",
";",
"}"
] |
Removes neurons with the number of samples less than a given threshold.
The neurons without neighbors will also be removed.
@param minPts neurons will be removed if the number of its points is
less than minPts.
@return the number of neurons after purging.
|
[
"Removes",
"neurons",
"with",
"the",
"number",
"of",
"samples",
"less",
"than",
"a",
"given",
"threshold",
".",
"The",
"neurons",
"without",
"neighbors",
"will",
"also",
"be",
"removed",
"."
] |
e27e43e90fbaacce3f99d30120cf9dd6a764c33d
|
https://github.com/haifengl/smile/blob/e27e43e90fbaacce3f99d30120cf9dd6a764c33d/core/src/main/java/smile/vq/NeuralMap.java#L532-L554
|
17,353
|
haifengl/smile
|
core/src/main/java/smile/clustering/XMeans.java
|
XMeans.bic
|
private double bic(int n, int d, double distortion) {
double variance = distortion / (n - 1);
double p1 = -n * LOG2PI;
double p2 = -n * d * Math.log(variance);
double p3 = -(n - 1);
double L = (p1 + p2 + p3) / 2;
int numParameters = d + 1;
return L - 0.5 * numParameters * Math.log(n);
}
|
java
|
private double bic(int n, int d, double distortion) {
double variance = distortion / (n - 1);
double p1 = -n * LOG2PI;
double p2 = -n * d * Math.log(variance);
double p3 = -(n - 1);
double L = (p1 + p2 + p3) / 2;
int numParameters = d + 1;
return L - 0.5 * numParameters * Math.log(n);
}
|
[
"private",
"double",
"bic",
"(",
"int",
"n",
",",
"int",
"d",
",",
"double",
"distortion",
")",
"{",
"double",
"variance",
"=",
"distortion",
"/",
"(",
"n",
"-",
"1",
")",
";",
"double",
"p1",
"=",
"-",
"n",
"*",
"LOG2PI",
";",
"double",
"p2",
"=",
"-",
"n",
"*",
"d",
"*",
"Math",
".",
"log",
"(",
"variance",
")",
";",
"double",
"p3",
"=",
"-",
"(",
"n",
"-",
"1",
")",
";",
"double",
"L",
"=",
"(",
"p1",
"+",
"p2",
"+",
"p3",
")",
"/",
"2",
";",
"int",
"numParameters",
"=",
"d",
"+",
"1",
";",
"return",
"L",
"-",
"0.5",
"*",
"numParameters",
"*",
"Math",
".",
"log",
"(",
"n",
")",
";",
"}"
] |
Calculates the BIC for single cluster.
@param n the total number of samples.
@param d the dimensionality of data.
@param distortion the distortion of clusters.
@return the BIC score.
|
[
"Calculates",
"the",
"BIC",
"for",
"single",
"cluster",
"."
] |
e27e43e90fbaacce3f99d30120cf9dd6a764c33d
|
https://github.com/haifengl/smile/blob/e27e43e90fbaacce3f99d30120cf9dd6a764c33d/core/src/main/java/smile/clustering/XMeans.java#L182-L192
|
17,354
|
haifengl/smile
|
core/src/main/java/smile/clustering/XMeans.java
|
XMeans.bic
|
private double bic(int k, int n, int d, double distortion, int[] clusterSize) {
double variance = distortion / (n - k);
double L = 0.0;
for (int i = 0; i < k; i++) {
L += logLikelihood(k, n, clusterSize[i], d, variance);
}
int numParameters = k + k * d;
return L - 0.5 * numParameters * Math.log(n);
}
|
java
|
private double bic(int k, int n, int d, double distortion, int[] clusterSize) {
double variance = distortion / (n - k);
double L = 0.0;
for (int i = 0; i < k; i++) {
L += logLikelihood(k, n, clusterSize[i], d, variance);
}
int numParameters = k + k * d;
return L - 0.5 * numParameters * Math.log(n);
}
|
[
"private",
"double",
"bic",
"(",
"int",
"k",
",",
"int",
"n",
",",
"int",
"d",
",",
"double",
"distortion",
",",
"int",
"[",
"]",
"clusterSize",
")",
"{",
"double",
"variance",
"=",
"distortion",
"/",
"(",
"n",
"-",
"k",
")",
";",
"double",
"L",
"=",
"0.0",
";",
"for",
"(",
"int",
"i",
"=",
"0",
";",
"i",
"<",
"k",
";",
"i",
"++",
")",
"{",
"L",
"+=",
"logLikelihood",
"(",
"k",
",",
"n",
",",
"clusterSize",
"[",
"i",
"]",
",",
"d",
",",
"variance",
")",
";",
"}",
"int",
"numParameters",
"=",
"k",
"+",
"k",
"*",
"d",
";",
"return",
"L",
"-",
"0.5",
"*",
"numParameters",
"*",
"Math",
".",
"log",
"(",
"n",
")",
";",
"}"
] |
Calculates the BIC for the given set of centers.
@param k the number of clusters.
@param n the total number of samples.
@param d the dimensionality of data.
@param distortion the distortion of clusters.
@param clusterSize the number of samples in each cluster.
@return the BIC score.
|
[
"Calculates",
"the",
"BIC",
"for",
"the",
"given",
"set",
"of",
"centers",
"."
] |
e27e43e90fbaacce3f99d30120cf9dd6a764c33d
|
https://github.com/haifengl/smile/blob/e27e43e90fbaacce3f99d30120cf9dd6a764c33d/core/src/main/java/smile/clustering/XMeans.java#L203-L213
|
17,355
|
haifengl/smile
|
core/src/main/java/smile/clustering/XMeans.java
|
XMeans.logLikelihood
|
private static double logLikelihood(int k, int n, int ni, int d, double variance) {
double p1 = -ni * LOG2PI;
double p2 = -ni * d * Math.log(variance);
double p3 = -(ni - k);
double p4 = ni * Math.log(ni);
double p5 = -ni * Math.log(n);
double loglike = (p1 + p2 + p3) / 2 + p4 + p5;
return loglike;
}
|
java
|
private static double logLikelihood(int k, int n, int ni, int d, double variance) {
double p1 = -ni * LOG2PI;
double p2 = -ni * d * Math.log(variance);
double p3 = -(ni - k);
double p4 = ni * Math.log(ni);
double p5 = -ni * Math.log(n);
double loglike = (p1 + p2 + p3) / 2 + p4 + p5;
return loglike;
}
|
[
"private",
"static",
"double",
"logLikelihood",
"(",
"int",
"k",
",",
"int",
"n",
",",
"int",
"ni",
",",
"int",
"d",
",",
"double",
"variance",
")",
"{",
"double",
"p1",
"=",
"-",
"ni",
"*",
"LOG2PI",
";",
"double",
"p2",
"=",
"-",
"ni",
"*",
"d",
"*",
"Math",
".",
"log",
"(",
"variance",
")",
";",
"double",
"p3",
"=",
"-",
"(",
"ni",
"-",
"k",
")",
";",
"double",
"p4",
"=",
"ni",
"*",
"Math",
".",
"log",
"(",
"ni",
")",
";",
"double",
"p5",
"=",
"-",
"ni",
"*",
"Math",
".",
"log",
"(",
"n",
")",
";",
"double",
"loglike",
"=",
"(",
"p1",
"+",
"p2",
"+",
"p3",
")",
"/",
"2",
"+",
"p4",
"+",
"p5",
";",
"return",
"loglike",
";",
"}"
] |
Estimate the log-likelihood of the data for the given model.
@param k the number of clusters.
@param n the total number of samples.
@param ni the number of samples belong to this cluster.
@param d the dimensionality of data.
@param variance the estimated variance of clusters.
@return the likelihood estimate
|
[
"Estimate",
"the",
"log",
"-",
"likelihood",
"of",
"the",
"data",
"for",
"the",
"given",
"model",
"."
] |
e27e43e90fbaacce3f99d30120cf9dd6a764c33d
|
https://github.com/haifengl/smile/blob/e27e43e90fbaacce3f99d30120cf9dd6a764c33d/core/src/main/java/smile/clustering/XMeans.java#L225-L233
|
17,356
|
haifengl/smile
|
math/src/main/java/smile/math/Random.java
|
Random.nextDoubles
|
public void nextDoubles(double[] d, double lo, double hi) {
real.nextDoubles(d);
double l = hi - lo;
int n = d.length;
for (int i = 0; i < n; i++) {
d[i] = lo + l * d[i];
}
}
|
java
|
public void nextDoubles(double[] d, double lo, double hi) {
real.nextDoubles(d);
double l = hi - lo;
int n = d.length;
for (int i = 0; i < n; i++) {
d[i] = lo + l * d[i];
}
}
|
[
"public",
"void",
"nextDoubles",
"(",
"double",
"[",
"]",
"d",
",",
"double",
"lo",
",",
"double",
"hi",
")",
"{",
"real",
".",
"nextDoubles",
"(",
"d",
")",
";",
"double",
"l",
"=",
"hi",
"-",
"lo",
";",
"int",
"n",
"=",
"d",
".",
"length",
";",
"for",
"(",
"int",
"i",
"=",
"0",
";",
"i",
"<",
"n",
";",
"i",
"++",
")",
"{",
"d",
"[",
"i",
"]",
"=",
"lo",
"+",
"l",
"*",
"d",
"[",
"i",
"]",
";",
"}",
"}"
] |
Generate n uniform random numbers in the range [lo, hi)
@param lo lower limit of range
@param hi upper limit of range
@param d array of random numbers to be generated
|
[
"Generate",
"n",
"uniform",
"random",
"numbers",
"in",
"the",
"range",
"[",
"lo",
"hi",
")"
] |
e27e43e90fbaacce3f99d30120cf9dd6a764c33d
|
https://github.com/haifengl/smile/blob/e27e43e90fbaacce3f99d30120cf9dd6a764c33d/math/src/main/java/smile/math/Random.java#L81-L89
|
17,357
|
haifengl/smile
|
math/src/main/java/smile/math/Random.java
|
Random.permutate
|
public int[] permutate(int n) {
int[] x = new int[n];
for (int i = 0; i < n; i++) {
x[i] = i;
}
permutate(x);
return x;
}
|
java
|
public int[] permutate(int n) {
int[] x = new int[n];
for (int i = 0; i < n; i++) {
x[i] = i;
}
permutate(x);
return x;
}
|
[
"public",
"int",
"[",
"]",
"permutate",
"(",
"int",
"n",
")",
"{",
"int",
"[",
"]",
"x",
"=",
"new",
"int",
"[",
"n",
"]",
";",
"for",
"(",
"int",
"i",
"=",
"0",
";",
"i",
"<",
"n",
";",
"i",
"++",
")",
"{",
"x",
"[",
"i",
"]",
"=",
"i",
";",
"}",
"permutate",
"(",
"x",
")",
";",
"return",
"x",
";",
"}"
] |
Generates a permutation of 0, 1, 2, ..., n-1, which is useful for
sampling without replacement.
|
[
"Generates",
"a",
"permutation",
"of",
"0",
"1",
"2",
"...",
"n",
"-",
"1",
"which",
"is",
"useful",
"for",
"sampling",
"without",
"replacement",
"."
] |
e27e43e90fbaacce3f99d30120cf9dd6a764c33d
|
https://github.com/haifengl/smile/blob/e27e43e90fbaacce3f99d30120cf9dd6a764c33d/math/src/main/java/smile/math/Random.java#L121-L130
|
17,358
|
haifengl/smile
|
core/src/main/java/smile/clustering/BIRCH.java
|
BIRCH.add
|
public void add(double[] x) {
if (root == null) {
root = new Node();
root.add(new Leaf(x));
root.update(x);
} else {
root.add(x);
}
}
|
java
|
public void add(double[] x) {
if (root == null) {
root = new Node();
root.add(new Leaf(x));
root.update(x);
} else {
root.add(x);
}
}
|
[
"public",
"void",
"add",
"(",
"double",
"[",
"]",
"x",
")",
"{",
"if",
"(",
"root",
"==",
"null",
")",
"{",
"root",
"=",
"new",
"Node",
"(",
")",
";",
"root",
".",
"add",
"(",
"new",
"Leaf",
"(",
"x",
")",
")",
";",
"root",
".",
"update",
"(",
"x",
")",
";",
"}",
"else",
"{",
"root",
".",
"add",
"(",
"x",
")",
";",
"}",
"}"
] |
Add a data point into CF tree.
|
[
"Add",
"a",
"data",
"point",
"into",
"CF",
"tree",
"."
] |
e27e43e90fbaacce3f99d30120cf9dd6a764c33d
|
https://github.com/haifengl/smile/blob/e27e43e90fbaacce3f99d30120cf9dd6a764c33d/core/src/main/java/smile/clustering/BIRCH.java#L345-L353
|
17,359
|
haifengl/smile
|
plot/src/main/java/smile/plot/Graphics.java
|
Graphics.setGraphics
|
public void setGraphics(java.awt.Graphics2D g2d) {
this.g2d = g2d;
// anti-aliasing methods
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
}
|
java
|
public void setGraphics(java.awt.Graphics2D g2d) {
this.g2d = g2d;
// anti-aliasing methods
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
}
|
[
"public",
"void",
"setGraphics",
"(",
"java",
".",
"awt",
".",
"Graphics2D",
"g2d",
")",
"{",
"this",
".",
"g2d",
"=",
"g2d",
";",
"// anti-aliasing methods",
"g2d",
".",
"setRenderingHint",
"(",
"RenderingHints",
".",
"KEY_ANTIALIASING",
",",
"RenderingHints",
".",
"VALUE_ANTIALIAS_ON",
")",
";",
"g2d",
".",
"setRenderingHint",
"(",
"RenderingHints",
".",
"KEY_RENDERING",
",",
"RenderingHints",
".",
"VALUE_RENDER_QUALITY",
")",
";",
"}"
] |
Set the Java2D graphics object.
|
[
"Set",
"the",
"Java2D",
"graphics",
"object",
"."
] |
e27e43e90fbaacce3f99d30120cf9dd6a764c33d
|
https://github.com/haifengl/smile/blob/e27e43e90fbaacce3f99d30120cf9dd6a764c33d/plot/src/main/java/smile/plot/Graphics.java#L84-L89
|
17,360
|
haifengl/smile
|
plot/src/main/java/smile/plot/Graphics.java
|
Graphics.clip
|
public void clip() {
int x = (int) (projection.canvas.getWidth() * projection.canvas.margin);
int y = (int) (projection.canvas.getHeight() * projection.canvas.margin);
int w = (int) (projection.canvas.getWidth() * (1 - 2 * projection.canvas.margin));
int h = (int) (projection.canvas.getHeight() * (1 - 2 * projection.canvas.margin));
originalClip = g2d.getClip();
g2d.clipRect(x, y, w, h);
}
|
java
|
public void clip() {
int x = (int) (projection.canvas.getWidth() * projection.canvas.margin);
int y = (int) (projection.canvas.getHeight() * projection.canvas.margin);
int w = (int) (projection.canvas.getWidth() * (1 - 2 * projection.canvas.margin));
int h = (int) (projection.canvas.getHeight() * (1 - 2 * projection.canvas.margin));
originalClip = g2d.getClip();
g2d.clipRect(x, y, w, h);
}
|
[
"public",
"void",
"clip",
"(",
")",
"{",
"int",
"x",
"=",
"(",
"int",
")",
"(",
"projection",
".",
"canvas",
".",
"getWidth",
"(",
")",
"*",
"projection",
".",
"canvas",
".",
"margin",
")",
";",
"int",
"y",
"=",
"(",
"int",
")",
"(",
"projection",
".",
"canvas",
".",
"getHeight",
"(",
")",
"*",
"projection",
".",
"canvas",
".",
"margin",
")",
";",
"int",
"w",
"=",
"(",
"int",
")",
"(",
"projection",
".",
"canvas",
".",
"getWidth",
"(",
")",
"*",
"(",
"1",
"-",
"2",
"*",
"projection",
".",
"canvas",
".",
"margin",
")",
")",
";",
"int",
"h",
"=",
"(",
"int",
")",
"(",
"projection",
".",
"canvas",
".",
"getHeight",
"(",
")",
"*",
"(",
"1",
"-",
"2",
"*",
"projection",
".",
"canvas",
".",
"margin",
")",
")",
";",
"originalClip",
"=",
"g2d",
".",
"getClip",
"(",
")",
";",
"g2d",
".",
"clipRect",
"(",
"x",
",",
"y",
",",
"w",
",",
"h",
")",
";",
"}"
] |
Restrict the draw area to the valid base coordinate space.
|
[
"Restrict",
"the",
"draw",
"area",
"to",
"the",
"valid",
"base",
"coordinate",
"space",
"."
] |
e27e43e90fbaacce3f99d30120cf9dd6a764c33d
|
https://github.com/haifengl/smile/blob/e27e43e90fbaacce3f99d30120cf9dd6a764c33d/plot/src/main/java/smile/plot/Graphics.java#L168-L175
|
17,361
|
haifengl/smile
|
plot/src/main/java/smile/plot/Graphics.java
|
Graphics.drawLine
|
private void drawLine(int[]... coord) {
int[] x = new int[coord.length];
for (int i = 0; i < coord.length; i++) {
x[i] = coord[i][0];
}
int[] y = new int[coord.length];
for (int i = 0; i < coord.length; i++) {
y[i] = coord[i][1];
}
g2d.drawPolyline(x, y, coord.length);
}
|
java
|
private void drawLine(int[]... coord) {
int[] x = new int[coord.length];
for (int i = 0; i < coord.length; i++) {
x[i] = coord[i][0];
}
int[] y = new int[coord.length];
for (int i = 0; i < coord.length; i++) {
y[i] = coord[i][1];
}
g2d.drawPolyline(x, y, coord.length);
}
|
[
"private",
"void",
"drawLine",
"(",
"int",
"[",
"]",
"...",
"coord",
")",
"{",
"int",
"[",
"]",
"x",
"=",
"new",
"int",
"[",
"coord",
".",
"length",
"]",
";",
"for",
"(",
"int",
"i",
"=",
"0",
";",
"i",
"<",
"coord",
".",
"length",
";",
"i",
"++",
")",
"{",
"x",
"[",
"i",
"]",
"=",
"coord",
"[",
"i",
"]",
"[",
"0",
"]",
";",
"}",
"int",
"[",
"]",
"y",
"=",
"new",
"int",
"[",
"coord",
".",
"length",
"]",
";",
"for",
"(",
"int",
"i",
"=",
"0",
";",
"i",
"<",
"coord",
".",
"length",
";",
"i",
"++",
")",
"{",
"y",
"[",
"i",
"]",
"=",
"coord",
"[",
"i",
"]",
"[",
"1",
"]",
";",
"}",
"g2d",
".",
"drawPolyline",
"(",
"x",
",",
"y",
",",
"coord",
".",
"length",
")",
";",
"}"
] |
Draw poly line.
|
[
"Draw",
"poly",
"line",
"."
] |
e27e43e90fbaacce3f99d30120cf9dd6a764c33d
|
https://github.com/haifengl/smile/blob/e27e43e90fbaacce3f99d30120cf9dd6a764c33d/plot/src/main/java/smile/plot/Graphics.java#L302-L314
|
17,362
|
haifengl/smile
|
plot/src/main/java/smile/plot/Graphics.java
|
Graphics.drawLine
|
public void drawLine(double[]... coord) {
int[][] sc = new int[coord.length][];
for (int i = 0; i < sc.length; i++) {
sc[i] = projection.screenProjection(coord[i]);
}
drawLine(sc);
}
|
java
|
public void drawLine(double[]... coord) {
int[][] sc = new int[coord.length][];
for (int i = 0; i < sc.length; i++) {
sc[i] = projection.screenProjection(coord[i]);
}
drawLine(sc);
}
|
[
"public",
"void",
"drawLine",
"(",
"double",
"[",
"]",
"...",
"coord",
")",
"{",
"int",
"[",
"]",
"[",
"]",
"sc",
"=",
"new",
"int",
"[",
"coord",
".",
"length",
"]",
"[",
"",
"]",
";",
"for",
"(",
"int",
"i",
"=",
"0",
";",
"i",
"<",
"sc",
".",
"length",
";",
"i",
"++",
")",
"{",
"sc",
"[",
"i",
"]",
"=",
"projection",
".",
"screenProjection",
"(",
"coord",
"[",
"i",
"]",
")",
";",
"}",
"drawLine",
"(",
"sc",
")",
";",
"}"
] |
Draw poly line. The coordinates are in logical coordinates.
|
[
"Draw",
"poly",
"line",
".",
"The",
"coordinates",
"are",
"in",
"logical",
"coordinates",
"."
] |
e27e43e90fbaacce3f99d30120cf9dd6a764c33d
|
https://github.com/haifengl/smile/blob/e27e43e90fbaacce3f99d30120cf9dd6a764c33d/plot/src/main/java/smile/plot/Graphics.java#L319-L326
|
17,363
|
haifengl/smile
|
plot/src/main/java/smile/plot/Graphics.java
|
Graphics.drawLineBaseRatio
|
public void drawLineBaseRatio(double[]... coord) {
int[][] sc = new int[coord.length][];
for (int i = 0; i < sc.length; i++) {
sc[i] = projection.screenProjectionBaseRatio(coord[i]);
}
drawLine(sc);
}
|
java
|
public void drawLineBaseRatio(double[]... coord) {
int[][] sc = new int[coord.length][];
for (int i = 0; i < sc.length; i++) {
sc[i] = projection.screenProjectionBaseRatio(coord[i]);
}
drawLine(sc);
}
|
[
"public",
"void",
"drawLineBaseRatio",
"(",
"double",
"[",
"]",
"...",
"coord",
")",
"{",
"int",
"[",
"]",
"[",
"]",
"sc",
"=",
"new",
"int",
"[",
"coord",
".",
"length",
"]",
"[",
"",
"]",
";",
"for",
"(",
"int",
"i",
"=",
"0",
";",
"i",
"<",
"sc",
".",
"length",
";",
"i",
"++",
")",
"{",
"sc",
"[",
"i",
"]",
"=",
"projection",
".",
"screenProjectionBaseRatio",
"(",
"coord",
"[",
"i",
"]",
")",
";",
"}",
"drawLine",
"(",
"sc",
")",
";",
"}"
] |
Draw poly line. The logical coordinates are proportional to the base
coordinates.
|
[
"Draw",
"poly",
"line",
".",
"The",
"logical",
"coordinates",
"are",
"proportional",
"to",
"the",
"base",
"coordinates",
"."
] |
e27e43e90fbaacce3f99d30120cf9dd6a764c33d
|
https://github.com/haifengl/smile/blob/e27e43e90fbaacce3f99d30120cf9dd6a764c33d/plot/src/main/java/smile/plot/Graphics.java#L332-L339
|
17,364
|
haifengl/smile
|
plot/src/main/java/smile/plot/Graphics.java
|
Graphics.drawPoint
|
public void drawPoint(char dot, double... coord) {
int size = 2;
int midSize = 3;
int bigSize = 4;
int[] sc = projection.screenProjection(coord);
int x = sc[0];
int y = sc[1];
switch (dot) {
case '+':
g2d.drawLine(x - size, y, x + size, y);
g2d.drawLine(x, y - size, x, y + size);
break;
case '-':
g2d.drawLine(x - size, y, x + size, y);
break;
case '|':
g2d.drawLine(x, y - size, x, y + size);
break;
case 'x':
g2d.drawLine(x - size, y - size, x + size, y + size);
g2d.drawLine(x + size, y - size, x - size, y + size);
break;
case '*':
g2d.drawLine(x - bigSize, y, x + bigSize, y);
g2d.drawLine(x, y - bigSize, x, y + bigSize);
g2d.drawLine(x - midSize, y - midSize, x + midSize, y + midSize);
g2d.drawLine(x + midSize, y - midSize, x - midSize, y + midSize);
break;
case 'o':
g2d.drawOval(x - size, y - size, 2 * size, 2 * size);
break;
case 'O':
g2d.drawOval(x - bigSize, y - bigSize, 2 * bigSize, 2 * bigSize);
break;
case '@':
g2d.fillOval(x - size, y - size, 2 * size, 2 * size);
break;
case '#':
g2d.fillOval(x - bigSize, y - bigSize, 2 * bigSize, 2 * bigSize);
break;
case 's':
g2d.drawRect(x - size, y - size, 2 * size, 2 * size);
break;
case 'S':
g2d.drawRect(x - bigSize, y - bigSize, 2 * bigSize, 2 * bigSize);
break;
case 'q':
g2d.fillRect(x - size, y - size, 2 * size, 2 * size);
break;
case 'Q':
g2d.fillRect(x - bigSize, y - bigSize, 2 * bigSize, 2 * bigSize);
break;
default:
g2d.drawRect(x, y, 1, 1);
break;
}
}
|
java
|
public void drawPoint(char dot, double... coord) {
int size = 2;
int midSize = 3;
int bigSize = 4;
int[] sc = projection.screenProjection(coord);
int x = sc[0];
int y = sc[1];
switch (dot) {
case '+':
g2d.drawLine(x - size, y, x + size, y);
g2d.drawLine(x, y - size, x, y + size);
break;
case '-':
g2d.drawLine(x - size, y, x + size, y);
break;
case '|':
g2d.drawLine(x, y - size, x, y + size);
break;
case 'x':
g2d.drawLine(x - size, y - size, x + size, y + size);
g2d.drawLine(x + size, y - size, x - size, y + size);
break;
case '*':
g2d.drawLine(x - bigSize, y, x + bigSize, y);
g2d.drawLine(x, y - bigSize, x, y + bigSize);
g2d.drawLine(x - midSize, y - midSize, x + midSize, y + midSize);
g2d.drawLine(x + midSize, y - midSize, x - midSize, y + midSize);
break;
case 'o':
g2d.drawOval(x - size, y - size, 2 * size, 2 * size);
break;
case 'O':
g2d.drawOval(x - bigSize, y - bigSize, 2 * bigSize, 2 * bigSize);
break;
case '@':
g2d.fillOval(x - size, y - size, 2 * size, 2 * size);
break;
case '#':
g2d.fillOval(x - bigSize, y - bigSize, 2 * bigSize, 2 * bigSize);
break;
case 's':
g2d.drawRect(x - size, y - size, 2 * size, 2 * size);
break;
case 'S':
g2d.drawRect(x - bigSize, y - bigSize, 2 * bigSize, 2 * bigSize);
break;
case 'q':
g2d.fillRect(x - size, y - size, 2 * size, 2 * size);
break;
case 'Q':
g2d.fillRect(x - bigSize, y - bigSize, 2 * bigSize, 2 * bigSize);
break;
default:
g2d.drawRect(x, y, 1, 1);
break;
}
}
|
[
"public",
"void",
"drawPoint",
"(",
"char",
"dot",
",",
"double",
"...",
"coord",
")",
"{",
"int",
"size",
"=",
"2",
";",
"int",
"midSize",
"=",
"3",
";",
"int",
"bigSize",
"=",
"4",
";",
"int",
"[",
"]",
"sc",
"=",
"projection",
".",
"screenProjection",
"(",
"coord",
")",
";",
"int",
"x",
"=",
"sc",
"[",
"0",
"]",
";",
"int",
"y",
"=",
"sc",
"[",
"1",
"]",
";",
"switch",
"(",
"dot",
")",
"{",
"case",
"'",
"'",
":",
"g2d",
".",
"drawLine",
"(",
"x",
"-",
"size",
",",
"y",
",",
"x",
"+",
"size",
",",
"y",
")",
";",
"g2d",
".",
"drawLine",
"(",
"x",
",",
"y",
"-",
"size",
",",
"x",
",",
"y",
"+",
"size",
")",
";",
"break",
";",
"case",
"'",
"'",
":",
"g2d",
".",
"drawLine",
"(",
"x",
"-",
"size",
",",
"y",
",",
"x",
"+",
"size",
",",
"y",
")",
";",
"break",
";",
"case",
"'",
"'",
":",
"g2d",
".",
"drawLine",
"(",
"x",
",",
"y",
"-",
"size",
",",
"x",
",",
"y",
"+",
"size",
")",
";",
"break",
";",
"case",
"'",
"'",
":",
"g2d",
".",
"drawLine",
"(",
"x",
"-",
"size",
",",
"y",
"-",
"size",
",",
"x",
"+",
"size",
",",
"y",
"+",
"size",
")",
";",
"g2d",
".",
"drawLine",
"(",
"x",
"+",
"size",
",",
"y",
"-",
"size",
",",
"x",
"-",
"size",
",",
"y",
"+",
"size",
")",
";",
"break",
";",
"case",
"'",
"'",
":",
"g2d",
".",
"drawLine",
"(",
"x",
"-",
"bigSize",
",",
"y",
",",
"x",
"+",
"bigSize",
",",
"y",
")",
";",
"g2d",
".",
"drawLine",
"(",
"x",
",",
"y",
"-",
"bigSize",
",",
"x",
",",
"y",
"+",
"bigSize",
")",
";",
"g2d",
".",
"drawLine",
"(",
"x",
"-",
"midSize",
",",
"y",
"-",
"midSize",
",",
"x",
"+",
"midSize",
",",
"y",
"+",
"midSize",
")",
";",
"g2d",
".",
"drawLine",
"(",
"x",
"+",
"midSize",
",",
"y",
"-",
"midSize",
",",
"x",
"-",
"midSize",
",",
"y",
"+",
"midSize",
")",
";",
"break",
";",
"case",
"'",
"'",
":",
"g2d",
".",
"drawOval",
"(",
"x",
"-",
"size",
",",
"y",
"-",
"size",
",",
"2",
"*",
"size",
",",
"2",
"*",
"size",
")",
";",
"break",
";",
"case",
"'",
"'",
":",
"g2d",
".",
"drawOval",
"(",
"x",
"-",
"bigSize",
",",
"y",
"-",
"bigSize",
",",
"2",
"*",
"bigSize",
",",
"2",
"*",
"bigSize",
")",
";",
"break",
";",
"case",
"'",
"'",
":",
"g2d",
".",
"fillOval",
"(",
"x",
"-",
"size",
",",
"y",
"-",
"size",
",",
"2",
"*",
"size",
",",
"2",
"*",
"size",
")",
";",
"break",
";",
"case",
"'",
"'",
":",
"g2d",
".",
"fillOval",
"(",
"x",
"-",
"bigSize",
",",
"y",
"-",
"bigSize",
",",
"2",
"*",
"bigSize",
",",
"2",
"*",
"bigSize",
")",
";",
"break",
";",
"case",
"'",
"'",
":",
"g2d",
".",
"drawRect",
"(",
"x",
"-",
"size",
",",
"y",
"-",
"size",
",",
"2",
"*",
"size",
",",
"2",
"*",
"size",
")",
";",
"break",
";",
"case",
"'",
"'",
":",
"g2d",
".",
"drawRect",
"(",
"x",
"-",
"bigSize",
",",
"y",
"-",
"bigSize",
",",
"2",
"*",
"bigSize",
",",
"2",
"*",
"bigSize",
")",
";",
"break",
";",
"case",
"'",
"'",
":",
"g2d",
".",
"fillRect",
"(",
"x",
"-",
"size",
",",
"y",
"-",
"size",
",",
"2",
"*",
"size",
",",
"2",
"*",
"size",
")",
";",
"break",
";",
"case",
"'",
"'",
":",
"g2d",
".",
"fillRect",
"(",
"x",
"-",
"bigSize",
",",
"y",
"-",
"bigSize",
",",
"2",
"*",
"bigSize",
",",
"2",
"*",
"bigSize",
")",
";",
"break",
";",
"default",
":",
"g2d",
".",
"drawRect",
"(",
"x",
",",
"y",
",",
"1",
",",
"1",
")",
";",
"break",
";",
"}",
"}"
] |
Draw a dot with given pattern. The coordinates are in logical coordinates.
@param dot the pattern of dot:
<ul>
<li> . : dot
<li> + : cross
<li> - : -
<li> | : |
<li> * : star
<li> x : x
<li> o : circle
<li> O : large circle
<li> @ : solid circle
<li> # : large sollid circle
<li> s : square
<li> S : large square
<li> q : solid square
<li> Q : large solid square
</ul>
|
[
"Draw",
"a",
"dot",
"with",
"given",
"pattern",
".",
"The",
"coordinates",
"are",
"in",
"logical",
"coordinates",
"."
] |
e27e43e90fbaacce3f99d30120cf9dd6a764c33d
|
https://github.com/haifengl/smile/blob/e27e43e90fbaacce3f99d30120cf9dd6a764c33d/plot/src/main/java/smile/plot/Graphics.java#L368-L440
|
17,365
|
haifengl/smile
|
plot/src/main/java/smile/plot/Graphics.java
|
Graphics.fillPolygon
|
public void fillPolygon(double[]... coord) {
int[][] c = new int[coord.length][2];
for (int i = 0; i < coord.length; i++) {
c[i] = projection.screenProjection(coord[i]);
}
int[] x = new int[c.length];
for (int i = 0; i < c.length; i++) {
x[i] = c[i][0];
}
int[] y = new int[c.length];
for (int i = 0; i < c.length; i++) {
y[i] = c[i][1];
}
g2d.fillPolygon(x, y, c.length);
}
|
java
|
public void fillPolygon(double[]... coord) {
int[][] c = new int[coord.length][2];
for (int i = 0; i < coord.length; i++) {
c[i] = projection.screenProjection(coord[i]);
}
int[] x = new int[c.length];
for (int i = 0; i < c.length; i++) {
x[i] = c[i][0];
}
int[] y = new int[c.length];
for (int i = 0; i < c.length; i++) {
y[i] = c[i][1];
}
g2d.fillPolygon(x, y, c.length);
}
|
[
"public",
"void",
"fillPolygon",
"(",
"double",
"[",
"]",
"...",
"coord",
")",
"{",
"int",
"[",
"]",
"[",
"]",
"c",
"=",
"new",
"int",
"[",
"coord",
".",
"length",
"]",
"[",
"2",
"]",
";",
"for",
"(",
"int",
"i",
"=",
"0",
";",
"i",
"<",
"coord",
".",
"length",
";",
"i",
"++",
")",
"{",
"c",
"[",
"i",
"]",
"=",
"projection",
".",
"screenProjection",
"(",
"coord",
"[",
"i",
"]",
")",
";",
"}",
"int",
"[",
"]",
"x",
"=",
"new",
"int",
"[",
"c",
".",
"length",
"]",
";",
"for",
"(",
"int",
"i",
"=",
"0",
";",
"i",
"<",
"c",
".",
"length",
";",
"i",
"++",
")",
"{",
"x",
"[",
"i",
"]",
"=",
"c",
"[",
"i",
"]",
"[",
"0",
"]",
";",
"}",
"int",
"[",
"]",
"y",
"=",
"new",
"int",
"[",
"c",
".",
"length",
"]",
";",
"for",
"(",
"int",
"i",
"=",
"0",
";",
"i",
"<",
"c",
".",
"length",
";",
"i",
"++",
")",
"{",
"y",
"[",
"i",
"]",
"=",
"c",
"[",
"i",
"]",
"[",
"1",
"]",
";",
"}",
"g2d",
".",
"fillPolygon",
"(",
"x",
",",
"y",
",",
"c",
".",
"length",
")",
";",
"}"
] |
Fill polygon. The coordinates are in logical coordinates.
|
[
"Fill",
"polygon",
".",
"The",
"coordinates",
"are",
"in",
"logical",
"coordinates",
"."
] |
e27e43e90fbaacce3f99d30120cf9dd6a764c33d
|
https://github.com/haifengl/smile/blob/e27e43e90fbaacce3f99d30120cf9dd6a764c33d/plot/src/main/java/smile/plot/Graphics.java#L465-L481
|
17,366
|
haifengl/smile
|
plot/src/main/java/smile/plot/Graphics.java
|
Graphics.fillPolygon
|
public void fillPolygon(float alpha, double[]... coord) {
int[][] c = new int[coord.length][2];
for (int i = 0; i < coord.length; i++) {
c[i] = projection.screenProjection(coord[i]);
}
int[] x = new int[c.length];
for (int i = 0; i < c.length; i++) {
x[i] = c[i][0];
}
int[] y = new int[c.length];
for (int i = 0; i < c.length; i++) {
y[i] = c[i][1];
}
Composite cs = g2d.getComposite();
g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha));
g2d.fillPolygon(x, y, c.length);
g2d.setComposite(cs);
}
|
java
|
public void fillPolygon(float alpha, double[]... coord) {
int[][] c = new int[coord.length][2];
for (int i = 0; i < coord.length; i++) {
c[i] = projection.screenProjection(coord[i]);
}
int[] x = new int[c.length];
for (int i = 0; i < c.length; i++) {
x[i] = c[i][0];
}
int[] y = new int[c.length];
for (int i = 0; i < c.length; i++) {
y[i] = c[i][1];
}
Composite cs = g2d.getComposite();
g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha));
g2d.fillPolygon(x, y, c.length);
g2d.setComposite(cs);
}
|
[
"public",
"void",
"fillPolygon",
"(",
"float",
"alpha",
",",
"double",
"[",
"]",
"...",
"coord",
")",
"{",
"int",
"[",
"]",
"[",
"]",
"c",
"=",
"new",
"int",
"[",
"coord",
".",
"length",
"]",
"[",
"2",
"]",
";",
"for",
"(",
"int",
"i",
"=",
"0",
";",
"i",
"<",
"coord",
".",
"length",
";",
"i",
"++",
")",
"{",
"c",
"[",
"i",
"]",
"=",
"projection",
".",
"screenProjection",
"(",
"coord",
"[",
"i",
"]",
")",
";",
"}",
"int",
"[",
"]",
"x",
"=",
"new",
"int",
"[",
"c",
".",
"length",
"]",
";",
"for",
"(",
"int",
"i",
"=",
"0",
";",
"i",
"<",
"c",
".",
"length",
";",
"i",
"++",
")",
"{",
"x",
"[",
"i",
"]",
"=",
"c",
"[",
"i",
"]",
"[",
"0",
"]",
";",
"}",
"int",
"[",
"]",
"y",
"=",
"new",
"int",
"[",
"c",
".",
"length",
"]",
";",
"for",
"(",
"int",
"i",
"=",
"0",
";",
"i",
"<",
"c",
".",
"length",
";",
"i",
"++",
")",
"{",
"y",
"[",
"i",
"]",
"=",
"c",
"[",
"i",
"]",
"[",
"1",
"]",
";",
"}",
"Composite",
"cs",
"=",
"g2d",
".",
"getComposite",
"(",
")",
";",
"g2d",
".",
"setComposite",
"(",
"AlphaComposite",
".",
"getInstance",
"(",
"AlphaComposite",
".",
"SRC_OVER",
",",
"alpha",
")",
")",
";",
"g2d",
".",
"fillPolygon",
"(",
"x",
",",
"y",
",",
"c",
".",
"length",
")",
";",
"g2d",
".",
"setComposite",
"(",
"cs",
")",
";",
"}"
] |
Fill polygon. The coordinates are in logical coordinates. This also supports
basic alpha compositing rules for combining source and destination
colors to achieve blending and transparency effects with graphics and images.
@param alpha the constant alpha to be multiplied with the alpha of the
source. alpha must be a floating point number in the inclusive range
[0.0, 1.0].
|
[
"Fill",
"polygon",
".",
"The",
"coordinates",
"are",
"in",
"logical",
"coordinates",
".",
"This",
"also",
"supports",
"basic",
"alpha",
"compositing",
"rules",
"for",
"combining",
"source",
"and",
"destination",
"colors",
"to",
"achieve",
"blending",
"and",
"transparency",
"effects",
"with",
"graphics",
"and",
"images",
"."
] |
e27e43e90fbaacce3f99d30120cf9dd6a764c33d
|
https://github.com/haifengl/smile/blob/e27e43e90fbaacce3f99d30120cf9dd6a764c33d/plot/src/main/java/smile/plot/Graphics.java#L492-L513
|
17,367
|
haifengl/smile
|
plot/src/main/java/smile/plot/Graphics.java
|
Graphics.drawRect
|
public void drawRect(double[] topLeft, double[] rightBottom) {
if (!(projection instanceof Projection2D)) {
throw new UnsupportedOperationException("Only 2D graphics supports drawing rectangles.");
}
int[] sc = projection.screenProjection(topLeft);
int[] sc2 = projection.screenProjection(rightBottom);
g2d.drawRect(sc[0], sc[1], sc2[0] - sc[0], sc2[1] - sc[1]);
}
|
java
|
public void drawRect(double[] topLeft, double[] rightBottom) {
if (!(projection instanceof Projection2D)) {
throw new UnsupportedOperationException("Only 2D graphics supports drawing rectangles.");
}
int[] sc = projection.screenProjection(topLeft);
int[] sc2 = projection.screenProjection(rightBottom);
g2d.drawRect(sc[0], sc[1], sc2[0] - sc[0], sc2[1] - sc[1]);
}
|
[
"public",
"void",
"drawRect",
"(",
"double",
"[",
"]",
"topLeft",
",",
"double",
"[",
"]",
"rightBottom",
")",
"{",
"if",
"(",
"!",
"(",
"projection",
"instanceof",
"Projection2D",
")",
")",
"{",
"throw",
"new",
"UnsupportedOperationException",
"(",
"\"Only 2D graphics supports drawing rectangles.\"",
")",
";",
"}",
"int",
"[",
"]",
"sc",
"=",
"projection",
".",
"screenProjection",
"(",
"topLeft",
")",
";",
"int",
"[",
"]",
"sc2",
"=",
"projection",
".",
"screenProjection",
"(",
"rightBottom",
")",
";",
"g2d",
".",
"drawRect",
"(",
"sc",
"[",
"0",
"]",
",",
"sc",
"[",
"1",
"]",
",",
"sc2",
"[",
"0",
"]",
"-",
"sc",
"[",
"0",
"]",
",",
"sc2",
"[",
"1",
"]",
"-",
"sc",
"[",
"1",
"]",
")",
";",
"}"
] |
Draw the outline of the specified rectangle.
|
[
"Draw",
"the",
"outline",
"of",
"the",
"specified",
"rectangle",
"."
] |
e27e43e90fbaacce3f99d30120cf9dd6a764c33d
|
https://github.com/haifengl/smile/blob/e27e43e90fbaacce3f99d30120cf9dd6a764c33d/plot/src/main/java/smile/plot/Graphics.java#L518-L527
|
17,368
|
haifengl/smile
|
plot/src/main/java/smile/plot/Graphics.java
|
Graphics.drawRectBaseRatio
|
public void drawRectBaseRatio(double[] topLeft, double[] rightBottom) {
if (!(projection instanceof Projection2D)) {
throw new UnsupportedOperationException("Only 2D graphics supports drawing rectangles.");
}
int[] sc = projection.screenProjectionBaseRatio(topLeft);
int[] sc2 = projection.screenProjectionBaseRatio(rightBottom);
g2d.drawRect(sc[0], sc[1], sc2[0] - sc[0], sc2[1] - sc[1]);
}
|
java
|
public void drawRectBaseRatio(double[] topLeft, double[] rightBottom) {
if (!(projection instanceof Projection2D)) {
throw new UnsupportedOperationException("Only 2D graphics supports drawing rectangles.");
}
int[] sc = projection.screenProjectionBaseRatio(topLeft);
int[] sc2 = projection.screenProjectionBaseRatio(rightBottom);
g2d.drawRect(sc[0], sc[1], sc2[0] - sc[0], sc2[1] - sc[1]);
}
|
[
"public",
"void",
"drawRectBaseRatio",
"(",
"double",
"[",
"]",
"topLeft",
",",
"double",
"[",
"]",
"rightBottom",
")",
"{",
"if",
"(",
"!",
"(",
"projection",
"instanceof",
"Projection2D",
")",
")",
"{",
"throw",
"new",
"UnsupportedOperationException",
"(",
"\"Only 2D graphics supports drawing rectangles.\"",
")",
";",
"}",
"int",
"[",
"]",
"sc",
"=",
"projection",
".",
"screenProjectionBaseRatio",
"(",
"topLeft",
")",
";",
"int",
"[",
"]",
"sc2",
"=",
"projection",
".",
"screenProjectionBaseRatio",
"(",
"rightBottom",
")",
";",
"g2d",
".",
"drawRect",
"(",
"sc",
"[",
"0",
"]",
",",
"sc",
"[",
"1",
"]",
",",
"sc2",
"[",
"0",
"]",
"-",
"sc",
"[",
"0",
"]",
",",
"sc2",
"[",
"1",
"]",
"-",
"sc",
"[",
"1",
"]",
")",
";",
"}"
] |
Draw the outline of the specified rectangle. The logical coordinates
are proportional to the base coordinates.
|
[
"Draw",
"the",
"outline",
"of",
"the",
"specified",
"rectangle",
".",
"The",
"logical",
"coordinates",
"are",
"proportional",
"to",
"the",
"base",
"coordinates",
"."
] |
e27e43e90fbaacce3f99d30120cf9dd6a764c33d
|
https://github.com/haifengl/smile/blob/e27e43e90fbaacce3f99d30120cf9dd6a764c33d/plot/src/main/java/smile/plot/Graphics.java#L533-L542
|
17,369
|
haifengl/smile
|
plot/src/main/java/smile/plot/Graphics.java
|
Graphics.fillRectBaseRatio
|
public void fillRectBaseRatio(double[] topLeft, double[] rightBottom) {
if (!(projection instanceof Projection2D)) {
throw new UnsupportedOperationException("Only 2D graphics supports drawing rectangles.");
}
int[] sc = projection.screenProjectionBaseRatio(topLeft);
int[] sc2 = projection.screenProjectionBaseRatio(rightBottom);
g2d.fillRect(sc[0], sc[1], sc2[0] - sc[0], sc2[1] - sc[1]);
}
|
java
|
public void fillRectBaseRatio(double[] topLeft, double[] rightBottom) {
if (!(projection instanceof Projection2D)) {
throw new UnsupportedOperationException("Only 2D graphics supports drawing rectangles.");
}
int[] sc = projection.screenProjectionBaseRatio(topLeft);
int[] sc2 = projection.screenProjectionBaseRatio(rightBottom);
g2d.fillRect(sc[0], sc[1], sc2[0] - sc[0], sc2[1] - sc[1]);
}
|
[
"public",
"void",
"fillRectBaseRatio",
"(",
"double",
"[",
"]",
"topLeft",
",",
"double",
"[",
"]",
"rightBottom",
")",
"{",
"if",
"(",
"!",
"(",
"projection",
"instanceof",
"Projection2D",
")",
")",
"{",
"throw",
"new",
"UnsupportedOperationException",
"(",
"\"Only 2D graphics supports drawing rectangles.\"",
")",
";",
"}",
"int",
"[",
"]",
"sc",
"=",
"projection",
".",
"screenProjectionBaseRatio",
"(",
"topLeft",
")",
";",
"int",
"[",
"]",
"sc2",
"=",
"projection",
".",
"screenProjectionBaseRatio",
"(",
"rightBottom",
")",
";",
"g2d",
".",
"fillRect",
"(",
"sc",
"[",
"0",
"]",
",",
"sc",
"[",
"1",
"]",
",",
"sc2",
"[",
"0",
"]",
"-",
"sc",
"[",
"0",
"]",
",",
"sc2",
"[",
"1",
"]",
"-",
"sc",
"[",
"1",
"]",
")",
";",
"}"
] |
Fill the specified rectangle. The logical coordinates are proportional
to the base coordinates.
|
[
"Fill",
"the",
"specified",
"rectangle",
".",
"The",
"logical",
"coordinates",
"are",
"proportional",
"to",
"the",
"base",
"coordinates",
"."
] |
e27e43e90fbaacce3f99d30120cf9dd6a764c33d
|
https://github.com/haifengl/smile/blob/e27e43e90fbaacce3f99d30120cf9dd6a764c33d/plot/src/main/java/smile/plot/Graphics.java#L562-L571
|
17,370
|
haifengl/smile
|
plot/src/main/java/smile/plot/Graphics.java
|
Graphics.rotate
|
public void rotate(double x, double y) {
if (!(projection instanceof Projection3D)) {
throw new UnsupportedOperationException("Only 3D graphics supports rotation.");
}
((Projection3D) projection).rotate(x, y);
}
|
java
|
public void rotate(double x, double y) {
if (!(projection instanceof Projection3D)) {
throw new UnsupportedOperationException("Only 3D graphics supports rotation.");
}
((Projection3D) projection).rotate(x, y);
}
|
[
"public",
"void",
"rotate",
"(",
"double",
"x",
",",
"double",
"y",
")",
"{",
"if",
"(",
"!",
"(",
"projection",
"instanceof",
"Projection3D",
")",
")",
"{",
"throw",
"new",
"UnsupportedOperationException",
"(",
"\"Only 3D graphics supports rotation.\"",
")",
";",
"}",
"(",
"(",
"Projection3D",
")",
"projection",
")",
".",
"rotate",
"(",
"x",
",",
"y",
")",
";",
"}"
] |
Rotate the 3D view based on the changes on mouse position.
@param x changes of mouse position on the x axis.
@param y changes on mouse position on the y axis.
|
[
"Rotate",
"the",
"3D",
"view",
"based",
"on",
"the",
"changes",
"on",
"mouse",
"position",
"."
] |
e27e43e90fbaacce3f99d30120cf9dd6a764c33d
|
https://github.com/haifengl/smile/blob/e27e43e90fbaacce3f99d30120cf9dd6a764c33d/plot/src/main/java/smile/plot/Graphics.java#L578-L584
|
17,371
|
haifengl/smile
|
core/src/main/java/smile/projection/GHA.java
|
GHA.learn
|
public double learn(double[] x) {
if (x.length != n) {
throw new IllegalArgumentException(String.format("Invalid input vector size: %d, expected: %d", x.length, n));
}
projection.ax(x, y);
for (int j = 0; j < p; j++) {
for (int i = 0; i < n; i++) {
double delta = x[i];
for (int l = 0; l <= j; l++) {
delta -= projection.get(l, i) * y[l];
}
projection.add(j, i, r * y[j] * delta);
if (Double.isInfinite(projection.get(j, i))) {
throw new IllegalStateException("GHA lost convergence. Lower learning rate?");
}
}
}
projection.ax(x, y);
projection.atx(y, wy);
return Math.squaredDistance(x, wy);
}
|
java
|
public double learn(double[] x) {
if (x.length != n) {
throw new IllegalArgumentException(String.format("Invalid input vector size: %d, expected: %d", x.length, n));
}
projection.ax(x, y);
for (int j = 0; j < p; j++) {
for (int i = 0; i < n; i++) {
double delta = x[i];
for (int l = 0; l <= j; l++) {
delta -= projection.get(l, i) * y[l];
}
projection.add(j, i, r * y[j] * delta);
if (Double.isInfinite(projection.get(j, i))) {
throw new IllegalStateException("GHA lost convergence. Lower learning rate?");
}
}
}
projection.ax(x, y);
projection.atx(y, wy);
return Math.squaredDistance(x, wy);
}
|
[
"public",
"double",
"learn",
"(",
"double",
"[",
"]",
"x",
")",
"{",
"if",
"(",
"x",
".",
"length",
"!=",
"n",
")",
"{",
"throw",
"new",
"IllegalArgumentException",
"(",
"String",
".",
"format",
"(",
"\"Invalid input vector size: %d, expected: %d\"",
",",
"x",
".",
"length",
",",
"n",
")",
")",
";",
"}",
"projection",
".",
"ax",
"(",
"x",
",",
"y",
")",
";",
"for",
"(",
"int",
"j",
"=",
"0",
";",
"j",
"<",
"p",
";",
"j",
"++",
")",
"{",
"for",
"(",
"int",
"i",
"=",
"0",
";",
"i",
"<",
"n",
";",
"i",
"++",
")",
"{",
"double",
"delta",
"=",
"x",
"[",
"i",
"]",
";",
"for",
"(",
"int",
"l",
"=",
"0",
";",
"l",
"<=",
"j",
";",
"l",
"++",
")",
"{",
"delta",
"-=",
"projection",
".",
"get",
"(",
"l",
",",
"i",
")",
"*",
"y",
"[",
"l",
"]",
";",
"}",
"projection",
".",
"add",
"(",
"j",
",",
"i",
",",
"r",
"*",
"y",
"[",
"j",
"]",
"*",
"delta",
")",
";",
"if",
"(",
"Double",
".",
"isInfinite",
"(",
"projection",
".",
"get",
"(",
"j",
",",
"i",
")",
")",
")",
"{",
"throw",
"new",
"IllegalStateException",
"(",
"\"GHA lost convergence. Lower learning rate?\"",
")",
";",
"}",
"}",
"}",
"projection",
".",
"ax",
"(",
"x",
",",
"y",
")",
";",
"projection",
".",
"atx",
"(",
"y",
",",
"wy",
")",
";",
"return",
"Math",
".",
"squaredDistance",
"(",
"x",
",",
"wy",
")",
";",
"}"
] |
Update the model with a new sample.
@param x the centered learning sample whose E(x) = 0.
@return the approximation error for input sample.
|
[
"Update",
"the",
"model",
"with",
"a",
"new",
"sample",
"."
] |
e27e43e90fbaacce3f99d30120cf9dd6a764c33d
|
https://github.com/haifengl/smile/blob/e27e43e90fbaacce3f99d30120cf9dd6a764c33d/core/src/main/java/smile/projection/GHA.java#L182-L206
|
17,372
|
haifengl/smile
|
core/src/main/java/smile/classification/RBFNetwork.java
|
RBFNetwork.rep
|
private static RadialBasisFunction[] rep(RadialBasisFunction rbf, int k) {
RadialBasisFunction[] arr = new RadialBasisFunction[k];
Arrays.fill(arr, rbf);
return arr;
}
|
java
|
private static RadialBasisFunction[] rep(RadialBasisFunction rbf, int k) {
RadialBasisFunction[] arr = new RadialBasisFunction[k];
Arrays.fill(arr, rbf);
return arr;
}
|
[
"private",
"static",
"RadialBasisFunction",
"[",
"]",
"rep",
"(",
"RadialBasisFunction",
"rbf",
",",
"int",
"k",
")",
"{",
"RadialBasisFunction",
"[",
"]",
"arr",
"=",
"new",
"RadialBasisFunction",
"[",
"k",
"]",
";",
"Arrays",
".",
"fill",
"(",
"arr",
",",
"rbf",
")",
";",
"return",
"arr",
";",
"}"
] |
Returns an array of radial basis functions initialized with given values.
@param rbf the initial value of array.
@param k the size of array.
@return an array of radial basis functions initialized with given values
|
[
"Returns",
"an",
"array",
"of",
"radial",
"basis",
"functions",
"initialized",
"with",
"given",
"values",
"."
] |
e27e43e90fbaacce3f99d30120cf9dd6a764c33d
|
https://github.com/haifengl/smile/blob/e27e43e90fbaacce3f99d30120cf9dd6a764c33d/core/src/main/java/smile/classification/RBFNetwork.java#L254-L258
|
17,373
|
haifengl/smile
|
math/src/main/java/smile/math/matrix/SVD.java
|
SVD.getS
|
public DenseMatrix getS() {
DenseMatrix S = Matrix.zeros(U.nrows(), V.nrows());
for (int i = 0; i < s.length; i++) {
S.set(i, i, s[i]);
}
return S;
}
|
java
|
public DenseMatrix getS() {
DenseMatrix S = Matrix.zeros(U.nrows(), V.nrows());
for (int i = 0; i < s.length; i++) {
S.set(i, i, s[i]);
}
return S;
}
|
[
"public",
"DenseMatrix",
"getS",
"(",
")",
"{",
"DenseMatrix",
"S",
"=",
"Matrix",
".",
"zeros",
"(",
"U",
".",
"nrows",
"(",
")",
",",
"V",
".",
"nrows",
"(",
")",
")",
";",
"for",
"(",
"int",
"i",
"=",
"0",
";",
"i",
"<",
"s",
".",
"length",
";",
"i",
"++",
")",
"{",
"S",
".",
"set",
"(",
"i",
",",
"i",
",",
"s",
"[",
"i",
"]",
")",
";",
"}",
"return",
"S",
";",
"}"
] |
Returns the diagonal matrix of singular values
|
[
"Returns",
"the",
"diagonal",
"matrix",
"of",
"singular",
"values"
] |
e27e43e90fbaacce3f99d30120cf9dd6a764c33d
|
https://github.com/haifengl/smile/blob/e27e43e90fbaacce3f99d30120cf9dd6a764c33d/math/src/main/java/smile/math/matrix/SVD.java#L121-L129
|
17,374
|
haifengl/smile
|
math/src/main/java/smile/math/matrix/SVD.java
|
SVD.rank
|
public int rank() {
if (!full) {
throw new IllegalStateException("This is not a FULL singular value decomposition.");
}
int r = 0;
for (int i = 0; i < s.length; i++) {
if (s[i] > tol) {
r++;
}
}
return r;
}
|
java
|
public int rank() {
if (!full) {
throw new IllegalStateException("This is not a FULL singular value decomposition.");
}
int r = 0;
for (int i = 0; i < s.length; i++) {
if (s[i] > tol) {
r++;
}
}
return r;
}
|
[
"public",
"int",
"rank",
"(",
")",
"{",
"if",
"(",
"!",
"full",
")",
"{",
"throw",
"new",
"IllegalStateException",
"(",
"\"This is not a FULL singular value decomposition.\"",
")",
";",
"}",
"int",
"r",
"=",
"0",
";",
"for",
"(",
"int",
"i",
"=",
"0",
";",
"i",
"<",
"s",
".",
"length",
";",
"i",
"++",
")",
"{",
"if",
"(",
"s",
"[",
"i",
"]",
">",
"tol",
")",
"{",
"r",
"++",
";",
"}",
"}",
"return",
"r",
";",
"}"
] |
Returns the effective numerical matrix rank. The number of nonnegligible
singular values.
|
[
"Returns",
"the",
"effective",
"numerical",
"matrix",
"rank",
".",
"The",
"number",
"of",
"nonnegligible",
"singular",
"values",
"."
] |
e27e43e90fbaacce3f99d30120cf9dd6a764c33d
|
https://github.com/haifengl/smile/blob/e27e43e90fbaacce3f99d30120cf9dd6a764c33d/math/src/main/java/smile/math/matrix/SVD.java#L142-L154
|
17,375
|
haifengl/smile
|
math/src/main/java/smile/math/matrix/SVD.java
|
SVD.nullity
|
public int nullity() {
if (!full) {
throw new IllegalStateException("This is not a FULL singular value decomposition.");
}
int r = 0;
for (int i = 0; i < s.length; i++) {
if (s[i] <= tol) {
r++;
}
}
return r;
}
|
java
|
public int nullity() {
if (!full) {
throw new IllegalStateException("This is not a FULL singular value decomposition.");
}
int r = 0;
for (int i = 0; i < s.length; i++) {
if (s[i] <= tol) {
r++;
}
}
return r;
}
|
[
"public",
"int",
"nullity",
"(",
")",
"{",
"if",
"(",
"!",
"full",
")",
"{",
"throw",
"new",
"IllegalStateException",
"(",
"\"This is not a FULL singular value decomposition.\"",
")",
";",
"}",
"int",
"r",
"=",
"0",
";",
"for",
"(",
"int",
"i",
"=",
"0",
";",
"i",
"<",
"s",
".",
"length",
";",
"i",
"++",
")",
"{",
"if",
"(",
"s",
"[",
"i",
"]",
"<=",
"tol",
")",
"{",
"r",
"++",
";",
"}",
"}",
"return",
"r",
";",
"}"
] |
Returns the dimension of null space. The number of negligible
singular values.
|
[
"Returns",
"the",
"dimension",
"of",
"null",
"space",
".",
"The",
"number",
"of",
"negligible",
"singular",
"values",
"."
] |
e27e43e90fbaacce3f99d30120cf9dd6a764c33d
|
https://github.com/haifengl/smile/blob/e27e43e90fbaacce3f99d30120cf9dd6a764c33d/math/src/main/java/smile/math/matrix/SVD.java#L160-L172
|
17,376
|
haifengl/smile
|
math/src/main/java/smile/math/matrix/SVD.java
|
SVD.range
|
public DenseMatrix range() {
if (!full) {
throw new IllegalStateException("This is not a FULL singular value decomposition.");
}
int nr = 0;
DenseMatrix rnge = Matrix.zeros(m, rank());
for (int j = 0; j < n; j++) {
if (s[j] > tol) {
for (int i = 0; i < m; i++) {
rnge.set(i, nr, U.get(i, j));
}
nr++;
}
}
return rnge;
}
|
java
|
public DenseMatrix range() {
if (!full) {
throw new IllegalStateException("This is not a FULL singular value decomposition.");
}
int nr = 0;
DenseMatrix rnge = Matrix.zeros(m, rank());
for (int j = 0; j < n; j++) {
if (s[j] > tol) {
for (int i = 0; i < m; i++) {
rnge.set(i, nr, U.get(i, j));
}
nr++;
}
}
return rnge;
}
|
[
"public",
"DenseMatrix",
"range",
"(",
")",
"{",
"if",
"(",
"!",
"full",
")",
"{",
"throw",
"new",
"IllegalStateException",
"(",
"\"This is not a FULL singular value decomposition.\"",
")",
";",
"}",
"int",
"nr",
"=",
"0",
";",
"DenseMatrix",
"rnge",
"=",
"Matrix",
".",
"zeros",
"(",
"m",
",",
"rank",
"(",
")",
")",
";",
"for",
"(",
"int",
"j",
"=",
"0",
";",
"j",
"<",
"n",
";",
"j",
"++",
")",
"{",
"if",
"(",
"s",
"[",
"j",
"]",
">",
"tol",
")",
"{",
"for",
"(",
"int",
"i",
"=",
"0",
";",
"i",
"<",
"m",
";",
"i",
"++",
")",
"{",
"rnge",
".",
"set",
"(",
"i",
",",
"nr",
",",
"U",
".",
"get",
"(",
"i",
",",
"j",
")",
")",
";",
"}",
"nr",
"++",
";",
"}",
"}",
"return",
"rnge",
";",
"}"
] |
Returns a matrix of which columns give an orthonormal basis for the range space.
|
[
"Returns",
"a",
"matrix",
"of",
"which",
"columns",
"give",
"an",
"orthonormal",
"basis",
"for",
"the",
"range",
"space",
"."
] |
e27e43e90fbaacce3f99d30120cf9dd6a764c33d
|
https://github.com/haifengl/smile/blob/e27e43e90fbaacce3f99d30120cf9dd6a764c33d/math/src/main/java/smile/math/matrix/SVD.java#L200-L216
|
17,377
|
haifengl/smile
|
math/src/main/java/smile/math/matrix/SVD.java
|
SVD.nullspace
|
public DenseMatrix nullspace() {
if (!full) {
throw new IllegalStateException("This is not a FULL singular value decomposition.");
}
int nn = 0;
DenseMatrix nullsp = Matrix.zeros(n, nullity());
for (int j = 0; j < n; j++) {
if (s[j] <= tol) {
for (int jj = 0; jj < n; jj++) {
nullsp.set(jj, nn, V.get(jj, j));
}
nn++;
}
}
return nullsp;
}
|
java
|
public DenseMatrix nullspace() {
if (!full) {
throw new IllegalStateException("This is not a FULL singular value decomposition.");
}
int nn = 0;
DenseMatrix nullsp = Matrix.zeros(n, nullity());
for (int j = 0; j < n; j++) {
if (s[j] <= tol) {
for (int jj = 0; jj < n; jj++) {
nullsp.set(jj, nn, V.get(jj, j));
}
nn++;
}
}
return nullsp;
}
|
[
"public",
"DenseMatrix",
"nullspace",
"(",
")",
"{",
"if",
"(",
"!",
"full",
")",
"{",
"throw",
"new",
"IllegalStateException",
"(",
"\"This is not a FULL singular value decomposition.\"",
")",
";",
"}",
"int",
"nn",
"=",
"0",
";",
"DenseMatrix",
"nullsp",
"=",
"Matrix",
".",
"zeros",
"(",
"n",
",",
"nullity",
"(",
")",
")",
";",
"for",
"(",
"int",
"j",
"=",
"0",
";",
"j",
"<",
"n",
";",
"j",
"++",
")",
"{",
"if",
"(",
"s",
"[",
"j",
"]",
"<=",
"tol",
")",
"{",
"for",
"(",
"int",
"jj",
"=",
"0",
";",
"jj",
"<",
"n",
";",
"jj",
"++",
")",
"{",
"nullsp",
".",
"set",
"(",
"jj",
",",
"nn",
",",
"V",
".",
"get",
"(",
"jj",
",",
"j",
")",
")",
";",
"}",
"nn",
"++",
";",
"}",
"}",
"return",
"nullsp",
";",
"}"
] |
Returns a matrix of which columns give an orthonormal basis for the null space.
|
[
"Returns",
"a",
"matrix",
"of",
"which",
"columns",
"give",
"an",
"orthonormal",
"basis",
"for",
"the",
"null",
"space",
"."
] |
e27e43e90fbaacce3f99d30120cf9dd6a764c33d
|
https://github.com/haifengl/smile/blob/e27e43e90fbaacce3f99d30120cf9dd6a764c33d/math/src/main/java/smile/math/matrix/SVD.java#L221-L237
|
17,378
|
haifengl/smile
|
math/src/main/java/smile/math/IntArrayList.java
|
IntArrayList.ensureCapacity
|
public void ensureCapacity(int capacity) {
if (capacity > data.length) {
int newCap = Math.max(data.length << 1, capacity);
int[] tmp = new int[newCap];
System.arraycopy(data, 0, tmp, 0, data.length);
data = tmp;
}
}
|
java
|
public void ensureCapacity(int capacity) {
if (capacity > data.length) {
int newCap = Math.max(data.length << 1, capacity);
int[] tmp = new int[newCap];
System.arraycopy(data, 0, tmp, 0, data.length);
data = tmp;
}
}
|
[
"public",
"void",
"ensureCapacity",
"(",
"int",
"capacity",
")",
"{",
"if",
"(",
"capacity",
">",
"data",
".",
"length",
")",
"{",
"int",
"newCap",
"=",
"Math",
".",
"max",
"(",
"data",
".",
"length",
"<<",
"1",
",",
"capacity",
")",
";",
"int",
"[",
"]",
"tmp",
"=",
"new",
"int",
"[",
"newCap",
"]",
";",
"System",
".",
"arraycopy",
"(",
"data",
",",
"0",
",",
"tmp",
",",
"0",
",",
"data",
".",
"length",
")",
";",
"data",
"=",
"tmp",
";",
"}",
"}"
] |
Increases the capacity, if necessary, to ensure that it can hold
at least the number of values specified by the minimum capacity
argument.
@param capacity the desired minimum capacity.
|
[
"Increases",
"the",
"capacity",
"if",
"necessary",
"to",
"ensure",
"that",
"it",
"can",
"hold",
"at",
"least",
"the",
"number",
"of",
"values",
"specified",
"by",
"the",
"minimum",
"capacity",
"argument",
"."
] |
e27e43e90fbaacce3f99d30120cf9dd6a764c33d
|
https://github.com/haifengl/smile/blob/e27e43e90fbaacce3f99d30120cf9dd6a764c33d/math/src/main/java/smile/math/IntArrayList.java#L74-L81
|
17,379
|
haifengl/smile
|
math/src/main/java/smile/math/IntArrayList.java
|
IntArrayList.set
|
public IntArrayList set(int index, int val) {
if (index < 0 || index >= size) {
throw new IndexOutOfBoundsException(String.valueOf(index));
}
data[index] = val;
return this;
}
|
java
|
public IntArrayList set(int index, int val) {
if (index < 0 || index >= size) {
throw new IndexOutOfBoundsException(String.valueOf(index));
}
data[index] = val;
return this;
}
|
[
"public",
"IntArrayList",
"set",
"(",
"int",
"index",
",",
"int",
"val",
")",
"{",
"if",
"(",
"index",
"<",
"0",
"||",
"index",
">=",
"size",
")",
"{",
"throw",
"new",
"IndexOutOfBoundsException",
"(",
"String",
".",
"valueOf",
"(",
"index",
")",
")",
";",
"}",
"data",
"[",
"index",
"]",
"=",
"val",
";",
"return",
"this",
";",
"}"
] |
Replaces the value at the specified position in this list with the
specified value.
@param index index of the value to replace
@param val value to be stored at the specified position
@throws IndexOutOfBoundsException if the index is out of range (index < 0 || index ≥ size())
|
[
"Replaces",
"the",
"value",
"at",
"the",
"specified",
"position",
"in",
"this",
"list",
"with",
"the",
"specified",
"value",
"."
] |
e27e43e90fbaacce3f99d30120cf9dd6a764c33d
|
https://github.com/haifengl/smile/blob/e27e43e90fbaacce3f99d30120cf9dd6a764c33d/math/src/main/java/smile/math/IntArrayList.java#L150-L156
|
17,380
|
haifengl/smile
|
math/src/main/java/smile/math/IntArrayList.java
|
IntArrayList.remove
|
public int remove(int index) {
if (index < 0 || index >= size) {
throw new IndexOutOfBoundsException(String.valueOf(index));
}
int old = get(index);
if (index == 0) {
// data at the front
System.arraycopy(data, 1, data, 0, size - 1);
} else if (size - 1 == index) {
// no copy to make, decrementing pos "deletes" values at
// the end
} else {
// data in the middle
System.arraycopy(data, index + 1, data, index, size - (index + 1));
}
size--;
return old;
}
|
java
|
public int remove(int index) {
if (index < 0 || index >= size) {
throw new IndexOutOfBoundsException(String.valueOf(index));
}
int old = get(index);
if (index == 0) {
// data at the front
System.arraycopy(data, 1, data, 0, size - 1);
} else if (size - 1 == index) {
// no copy to make, decrementing pos "deletes" values at
// the end
} else {
// data in the middle
System.arraycopy(data, index + 1, data, index, size - (index + 1));
}
size--;
return old;
}
|
[
"public",
"int",
"remove",
"(",
"int",
"index",
")",
"{",
"if",
"(",
"index",
"<",
"0",
"||",
"index",
">=",
"size",
")",
"{",
"throw",
"new",
"IndexOutOfBoundsException",
"(",
"String",
".",
"valueOf",
"(",
"index",
")",
")",
";",
"}",
"int",
"old",
"=",
"get",
"(",
"index",
")",
";",
"if",
"(",
"index",
"==",
"0",
")",
"{",
"// data at the front",
"System",
".",
"arraycopy",
"(",
"data",
",",
"1",
",",
"data",
",",
"0",
",",
"size",
"-",
"1",
")",
";",
"}",
"else",
"if",
"(",
"size",
"-",
"1",
"==",
"index",
")",
"{",
"// no copy to make, decrementing pos \"deletes\" values at",
"// the end",
"}",
"else",
"{",
"// data in the middle",
"System",
".",
"arraycopy",
"(",
"data",
",",
"index",
"+",
"1",
",",
"data",
",",
"index",
",",
"size",
"-",
"(",
"index",
"+",
"1",
")",
")",
";",
"}",
"size",
"--",
";",
"return",
"old",
";",
"}"
] |
Removes the value at specified index from the list.
@param index index of the element to remove.
@return the value previously stored at specified index
@throws IndexOutOfBoundsException if the index is out of range (index < 0 || index ≥ size())
|
[
"Removes",
"the",
"value",
"at",
"specified",
"index",
"from",
"the",
"list",
"."
] |
e27e43e90fbaacce3f99d30120cf9dd6a764c33d
|
https://github.com/haifengl/smile/blob/e27e43e90fbaacce3f99d30120cf9dd6a764c33d/math/src/main/java/smile/math/IntArrayList.java#L173-L193
|
17,381
|
haifengl/smile
|
interpolation/src/main/java/smile/interpolation/KrigingInterpolation.java
|
KrigingInterpolation.rdist
|
private double rdist(double[] x1, double[] x2) {
double d = 0.0;
for (int i = 0; i < x1.length; i++) {
double t = x1[i] - x2[i];
d += t * t;
}
return Math.sqrt(d);
}
|
java
|
private double rdist(double[] x1, double[] x2) {
double d = 0.0;
for (int i = 0; i < x1.length; i++) {
double t = x1[i] - x2[i];
d += t * t;
}
return Math.sqrt(d);
}
|
[
"private",
"double",
"rdist",
"(",
"double",
"[",
"]",
"x1",
",",
"double",
"[",
"]",
"x2",
")",
"{",
"double",
"d",
"=",
"0.0",
";",
"for",
"(",
"int",
"i",
"=",
"0",
";",
"i",
"<",
"x1",
".",
"length",
";",
"i",
"++",
")",
"{",
"double",
"t",
"=",
"x1",
"[",
"i",
"]",
"-",
"x2",
"[",
"i",
"]",
";",
"d",
"+=",
"t",
"*",
"t",
";",
"}",
"return",
"Math",
".",
"sqrt",
"(",
"d",
")",
";",
"}"
] |
Cartesian distance.
|
[
"Cartesian",
"distance",
"."
] |
e27e43e90fbaacce3f99d30120cf9dd6a764c33d
|
https://github.com/haifengl/smile/blob/e27e43e90fbaacce3f99d30120cf9dd6a764c33d/interpolation/src/main/java/smile/interpolation/KrigingInterpolation.java#L160-L167
|
17,382
|
haifengl/smile
|
math/src/main/java/smile/math/Histogram.java
|
Histogram.histogram
|
public static double[][] histogram(int[] data, double[] breaks) {
int k = breaks.length - 1;
if (k <= 1) {
throw new IllegalArgumentException("Invalid number of bins: " + k);
}
double[][] freq = new double[3][k];
for (int i = 0; i < k; i++) {
freq[0][i] = breaks[i];
freq[1][i] = breaks[i + 1];
freq[2][i] = 0;
}
for (int d : data) {
int j = Arrays.binarySearch(breaks, d);
if (j >= k) {
j = k - 1;
}
if (j < -1 && j >= -breaks.length) {
j = -j - 2;
}
if (j >= 0) {
freq[2][j]++;
}
}
return freq;
}
|
java
|
public static double[][] histogram(int[] data, double[] breaks) {
int k = breaks.length - 1;
if (k <= 1) {
throw new IllegalArgumentException("Invalid number of bins: " + k);
}
double[][] freq = new double[3][k];
for (int i = 0; i < k; i++) {
freq[0][i] = breaks[i];
freq[1][i] = breaks[i + 1];
freq[2][i] = 0;
}
for (int d : data) {
int j = Arrays.binarySearch(breaks, d);
if (j >= k) {
j = k - 1;
}
if (j < -1 && j >= -breaks.length) {
j = -j - 2;
}
if (j >= 0) {
freq[2][j]++;
}
}
return freq;
}
|
[
"public",
"static",
"double",
"[",
"]",
"[",
"]",
"histogram",
"(",
"int",
"[",
"]",
"data",
",",
"double",
"[",
"]",
"breaks",
")",
"{",
"int",
"k",
"=",
"breaks",
".",
"length",
"-",
"1",
";",
"if",
"(",
"k",
"<=",
"1",
")",
"{",
"throw",
"new",
"IllegalArgumentException",
"(",
"\"Invalid number of bins: \"",
"+",
"k",
")",
";",
"}",
"double",
"[",
"]",
"[",
"]",
"freq",
"=",
"new",
"double",
"[",
"3",
"]",
"[",
"k",
"]",
";",
"for",
"(",
"int",
"i",
"=",
"0",
";",
"i",
"<",
"k",
";",
"i",
"++",
")",
"{",
"freq",
"[",
"0",
"]",
"[",
"i",
"]",
"=",
"breaks",
"[",
"i",
"]",
";",
"freq",
"[",
"1",
"]",
"[",
"i",
"]",
"=",
"breaks",
"[",
"i",
"+",
"1",
"]",
";",
"freq",
"[",
"2",
"]",
"[",
"i",
"]",
"=",
"0",
";",
"}",
"for",
"(",
"int",
"d",
":",
"data",
")",
"{",
"int",
"j",
"=",
"Arrays",
".",
"binarySearch",
"(",
"breaks",
",",
"d",
")",
";",
"if",
"(",
"j",
">=",
"k",
")",
"{",
"j",
"=",
"k",
"-",
"1",
";",
"}",
"if",
"(",
"j",
"<",
"-",
"1",
"&&",
"j",
">=",
"-",
"breaks",
".",
"length",
")",
"{",
"j",
"=",
"-",
"j",
"-",
"2",
";",
"}",
"if",
"(",
"j",
">=",
"0",
")",
"{",
"freq",
"[",
"2",
"]",
"[",
"j",
"]",
"++",
";",
"}",
"}",
"return",
"freq",
";",
"}"
] |
Generate the histogram of n bins.
@param data the data points.
@param breaks an array of size k+1 giving the breakpoints between
histogram cells. Must be in ascending order.
@return a 3-by-n bins array of which first row is the lower bound of bins,
second row is the upper bound of bins, and the third row is the frequence
count.
|
[
"Generate",
"the",
"histogram",
"of",
"n",
"bins",
"."
] |
e27e43e90fbaacce3f99d30120cf9dd6a764c33d
|
https://github.com/haifengl/smile/blob/e27e43e90fbaacce3f99d30120cf9dd6a764c33d/math/src/main/java/smile/math/Histogram.java#L131-L161
|
17,383
|
haifengl/smile
|
math/src/main/java/smile/math/Histogram.java
|
Histogram.breaks
|
public static double[] breaks(double[] x, double h) {
return breaks(Math.min(x), Math.max(x), h);
}
|
java
|
public static double[] breaks(double[] x, double h) {
return breaks(Math.min(x), Math.max(x), h);
}
|
[
"public",
"static",
"double",
"[",
"]",
"breaks",
"(",
"double",
"[",
"]",
"x",
",",
"double",
"h",
")",
"{",
"return",
"breaks",
"(",
"Math",
".",
"min",
"(",
"x",
")",
",",
"Math",
".",
"max",
"(",
"x",
")",
",",
"h",
")",
";",
"}"
] |
Returns the breakpoints between histogram cells for a dataset based on a
suggested bin width h.
@param x the data set.
@param h the bin width.
@return the breakpoints between histogram cells
|
[
"Returns",
"the",
"breakpoints",
"between",
"histogram",
"cells",
"for",
"a",
"dataset",
"based",
"on",
"a",
"suggested",
"bin",
"width",
"h",
"."
] |
e27e43e90fbaacce3f99d30120cf9dd6a764c33d
|
https://github.com/haifengl/smile/blob/e27e43e90fbaacce3f99d30120cf9dd6a764c33d/math/src/main/java/smile/math/Histogram.java#L310-L312
|
17,384
|
haifengl/smile
|
math/src/main/java/smile/math/Histogram.java
|
Histogram.breaks
|
public static double[] breaks(double min, double max, double h) {
if (h <= 0.0) {
throw new IllegalArgumentException("Invalid bin width: " + h);
}
if (min > max) {
throw new IllegalArgumentException("Invalid lower and upper bounds: " + min + " > " + max);
}
int k = (int) Math.ceil((max-min) / h);
double[] breaks = new double[k + 1];
breaks[0] = min - (h * k - (max - min)) / 2;
breaks[k] = max + (h * k - (max - min)) / 2;
for (int i = 1; i < k; i++) {
breaks[i] = breaks[i - 1] + h;
}
return breaks;
}
|
java
|
public static double[] breaks(double min, double max, double h) {
if (h <= 0.0) {
throw new IllegalArgumentException("Invalid bin width: " + h);
}
if (min > max) {
throw new IllegalArgumentException("Invalid lower and upper bounds: " + min + " > " + max);
}
int k = (int) Math.ceil((max-min) / h);
double[] breaks = new double[k + 1];
breaks[0] = min - (h * k - (max - min)) / 2;
breaks[k] = max + (h * k - (max - min)) / 2;
for (int i = 1; i < k; i++) {
breaks[i] = breaks[i - 1] + h;
}
return breaks;
}
|
[
"public",
"static",
"double",
"[",
"]",
"breaks",
"(",
"double",
"min",
",",
"double",
"max",
",",
"double",
"h",
")",
"{",
"if",
"(",
"h",
"<=",
"0.0",
")",
"{",
"throw",
"new",
"IllegalArgumentException",
"(",
"\"Invalid bin width: \"",
"+",
"h",
")",
";",
"}",
"if",
"(",
"min",
">",
"max",
")",
"{",
"throw",
"new",
"IllegalArgumentException",
"(",
"\"Invalid lower and upper bounds: \"",
"+",
"min",
"+",
"\" > \"",
"+",
"max",
")",
";",
"}",
"int",
"k",
"=",
"(",
"int",
")",
"Math",
".",
"ceil",
"(",
"(",
"max",
"-",
"min",
")",
"/",
"h",
")",
";",
"double",
"[",
"]",
"breaks",
"=",
"new",
"double",
"[",
"k",
"+",
"1",
"]",
";",
"breaks",
"[",
"0",
"]",
"=",
"min",
"-",
"(",
"h",
"*",
"k",
"-",
"(",
"max",
"-",
"min",
")",
")",
"/",
"2",
";",
"breaks",
"[",
"k",
"]",
"=",
"max",
"+",
"(",
"h",
"*",
"k",
"-",
"(",
"max",
"-",
"min",
")",
")",
"/",
"2",
";",
"for",
"(",
"int",
"i",
"=",
"1",
";",
"i",
"<",
"k",
";",
"i",
"++",
")",
"{",
"breaks",
"[",
"i",
"]",
"=",
"breaks",
"[",
"i",
"-",
"1",
"]",
"+",
"h",
";",
"}",
"return",
"breaks",
";",
"}"
] |
Returns the breakpoints between histogram cells for a given range based
on a suggested bin width h.
@param min the lower bound of bins.
@param max the upper bound of bins.
@param h the bin width.
@return the breakpoints between histogram cells
|
[
"Returns",
"the",
"breakpoints",
"between",
"histogram",
"cells",
"for",
"a",
"given",
"range",
"based",
"on",
"a",
"suggested",
"bin",
"width",
"h",
"."
] |
e27e43e90fbaacce3f99d30120cf9dd6a764c33d
|
https://github.com/haifengl/smile/blob/e27e43e90fbaacce3f99d30120cf9dd6a764c33d/math/src/main/java/smile/math/Histogram.java#L322-L341
|
17,385
|
haifengl/smile
|
math/src/main/java/smile/math/Histogram.java
|
Histogram.breaks
|
public static double[] breaks(double[] x, int k) {
return breaks(Math.min(x), Math.max(x), k);
}
|
java
|
public static double[] breaks(double[] x, int k) {
return breaks(Math.min(x), Math.max(x), k);
}
|
[
"public",
"static",
"double",
"[",
"]",
"breaks",
"(",
"double",
"[",
"]",
"x",
",",
"int",
"k",
")",
"{",
"return",
"breaks",
"(",
"Math",
".",
"min",
"(",
"x",
")",
",",
"Math",
".",
"max",
"(",
"x",
")",
",",
"k",
")",
";",
"}"
] |
Returns the breakpoints between histogram cells for a dataset.
@param x the data set.
@param k the number of bins.
@return the breakpoints between histogram cells
|
[
"Returns",
"the",
"breakpoints",
"between",
"histogram",
"cells",
"for",
"a",
"dataset",
"."
] |
e27e43e90fbaacce3f99d30120cf9dd6a764c33d
|
https://github.com/haifengl/smile/blob/e27e43e90fbaacce3f99d30120cf9dd6a764c33d/math/src/main/java/smile/math/Histogram.java#L349-L351
|
17,386
|
haifengl/smile
|
math/src/main/java/smile/math/Histogram.java
|
Histogram.breaks
|
public static double[] breaks(double min, double max, int k) {
if (k <= 1) {
throw new IllegalArgumentException("Invalid number of bins: " + k);
}
if (min > max) {
throw new IllegalArgumentException("Invalid lower and upper bounds: " + min + " > " + max);
}
double h = (max - min) / k;
return breaks(min, max, h);
}
|
java
|
public static double[] breaks(double min, double max, int k) {
if (k <= 1) {
throw new IllegalArgumentException("Invalid number of bins: " + k);
}
if (min > max) {
throw new IllegalArgumentException("Invalid lower and upper bounds: " + min + " > " + max);
}
double h = (max - min) / k;
return breaks(min, max, h);
}
|
[
"public",
"static",
"double",
"[",
"]",
"breaks",
"(",
"double",
"min",
",",
"double",
"max",
",",
"int",
"k",
")",
"{",
"if",
"(",
"k",
"<=",
"1",
")",
"{",
"throw",
"new",
"IllegalArgumentException",
"(",
"\"Invalid number of bins: \"",
"+",
"k",
")",
";",
"}",
"if",
"(",
"min",
">",
"max",
")",
"{",
"throw",
"new",
"IllegalArgumentException",
"(",
"\"Invalid lower and upper bounds: \"",
"+",
"min",
"+",
"\" > \"",
"+",
"max",
")",
";",
"}",
"double",
"h",
"=",
"(",
"max",
"-",
"min",
")",
"/",
"k",
";",
"return",
"breaks",
"(",
"min",
",",
"max",
",",
"h",
")",
";",
"}"
] |
Returns the breakpoints between histogram cells for a given range.
@param min the lower bound of bins.
@param max the upper bound of bins.
@param k the number of bins.
@return the breakpoints between histogram cells
|
[
"Returns",
"the",
"breakpoints",
"between",
"histogram",
"cells",
"for",
"a",
"given",
"range",
"."
] |
e27e43e90fbaacce3f99d30120cf9dd6a764c33d
|
https://github.com/haifengl/smile/blob/e27e43e90fbaacce3f99d30120cf9dd6a764c33d/math/src/main/java/smile/math/Histogram.java#L360-L371
|
17,387
|
haifengl/smile
|
math/src/main/java/smile/math/Histogram.java
|
Histogram.bins
|
public static int bins(double[] x, double h) {
if (h <= 0.0) {
throw new IllegalArgumentException("Invalid bin width: " + h);
}
double max = Math.max(x);
double min = Math.min(x);
return (int) Math.ceil((max-min) / h);
}
|
java
|
public static int bins(double[] x, double h) {
if (h <= 0.0) {
throw new IllegalArgumentException("Invalid bin width: " + h);
}
double max = Math.max(x);
double min = Math.min(x);
return (int) Math.ceil((max-min) / h);
}
|
[
"public",
"static",
"int",
"bins",
"(",
"double",
"[",
"]",
"x",
",",
"double",
"h",
")",
"{",
"if",
"(",
"h",
"<=",
"0.0",
")",
"{",
"throw",
"new",
"IllegalArgumentException",
"(",
"\"Invalid bin width: \"",
"+",
"h",
")",
";",
"}",
"double",
"max",
"=",
"Math",
".",
"max",
"(",
"x",
")",
";",
"double",
"min",
"=",
"Math",
".",
"min",
"(",
"x",
")",
";",
"return",
"(",
"int",
")",
"Math",
".",
"ceil",
"(",
"(",
"max",
"-",
"min",
")",
"/",
"h",
")",
";",
"}"
] |
Returns the number of bins for a data based on a suggested bin width h.
@param x the data set.
@param h the bin width.
@return the number of bins k = ceil((max - min) / h)
|
[
"Returns",
"the",
"number",
"of",
"bins",
"for",
"a",
"data",
"based",
"on",
"a",
"suggested",
"bin",
"width",
"h",
"."
] |
e27e43e90fbaacce3f99d30120cf9dd6a764c33d
|
https://github.com/haifengl/smile/blob/e27e43e90fbaacce3f99d30120cf9dd6a764c33d/math/src/main/java/smile/math/Histogram.java#L379-L388
|
17,388
|
haifengl/smile
|
core/src/main/java/smile/feature/GAFeatureSelection.java
|
GAFeatureSelection.learn
|
public BitString[] learn(int size, int generation, ClassifierTrainer<double[]> trainer, ClassificationMeasure measure, double[][] x, int[] y, int k) {
if (size <= 0) {
throw new IllegalArgumentException("Invalid population size: " + size);
}
if (k < 2) {
throw new IllegalArgumentException("Invalid k-fold cross validation: " + k);
}
if (x.length != y.length) {
throw new IllegalArgumentException(String.format("The sizes of X and Y don't match: %d != %d", x.length, y.length));
}
int p = x[0].length;
ClassificationFitness fitness = new ClassificationFitness(trainer, measure, x, y, k);
BitString[] seeds = new BitString[size];
for (int i = 0; i < size; i++) {
seeds[i] = new BitString(p, fitness, crossover, crossoverRate, mutationRate);
}
GeneticAlgorithm<BitString> ga = new GeneticAlgorithm<>(seeds, selection);
ga.evolve(generation);
return seeds;
}
|
java
|
public BitString[] learn(int size, int generation, ClassifierTrainer<double[]> trainer, ClassificationMeasure measure, double[][] x, int[] y, int k) {
if (size <= 0) {
throw new IllegalArgumentException("Invalid population size: " + size);
}
if (k < 2) {
throw new IllegalArgumentException("Invalid k-fold cross validation: " + k);
}
if (x.length != y.length) {
throw new IllegalArgumentException(String.format("The sizes of X and Y don't match: %d != %d", x.length, y.length));
}
int p = x[0].length;
ClassificationFitness fitness = new ClassificationFitness(trainer, measure, x, y, k);
BitString[] seeds = new BitString[size];
for (int i = 0; i < size; i++) {
seeds[i] = new BitString(p, fitness, crossover, crossoverRate, mutationRate);
}
GeneticAlgorithm<BitString> ga = new GeneticAlgorithm<>(seeds, selection);
ga.evolve(generation);
return seeds;
}
|
[
"public",
"BitString",
"[",
"]",
"learn",
"(",
"int",
"size",
",",
"int",
"generation",
",",
"ClassifierTrainer",
"<",
"double",
"[",
"]",
">",
"trainer",
",",
"ClassificationMeasure",
"measure",
",",
"double",
"[",
"]",
"[",
"]",
"x",
",",
"int",
"[",
"]",
"y",
",",
"int",
"k",
")",
"{",
"if",
"(",
"size",
"<=",
"0",
")",
"{",
"throw",
"new",
"IllegalArgumentException",
"(",
"\"Invalid population size: \"",
"+",
"size",
")",
";",
"}",
"if",
"(",
"k",
"<",
"2",
")",
"{",
"throw",
"new",
"IllegalArgumentException",
"(",
"\"Invalid k-fold cross validation: \"",
"+",
"k",
")",
";",
"}",
"if",
"(",
"x",
".",
"length",
"!=",
"y",
".",
"length",
")",
"{",
"throw",
"new",
"IllegalArgumentException",
"(",
"String",
".",
"format",
"(",
"\"The sizes of X and Y don't match: %d != %d\"",
",",
"x",
".",
"length",
",",
"y",
".",
"length",
")",
")",
";",
"}",
"int",
"p",
"=",
"x",
"[",
"0",
"]",
".",
"length",
";",
"ClassificationFitness",
"fitness",
"=",
"new",
"ClassificationFitness",
"(",
"trainer",
",",
"measure",
",",
"x",
",",
"y",
",",
"k",
")",
";",
"BitString",
"[",
"]",
"seeds",
"=",
"new",
"BitString",
"[",
"size",
"]",
";",
"for",
"(",
"int",
"i",
"=",
"0",
";",
"i",
"<",
"size",
";",
"i",
"++",
")",
"{",
"seeds",
"[",
"i",
"]",
"=",
"new",
"BitString",
"(",
"p",
",",
"fitness",
",",
"crossover",
",",
"crossoverRate",
",",
"mutationRate",
")",
";",
"}",
"GeneticAlgorithm",
"<",
"BitString",
">",
"ga",
"=",
"new",
"GeneticAlgorithm",
"<>",
"(",
"seeds",
",",
"selection",
")",
";",
"ga",
".",
"evolve",
"(",
"generation",
")",
";",
"return",
"seeds",
";",
"}"
] |
Genetic algorithm based feature selection for classification.
@param size the population size of Genetic Algorithm.
@param generation the maximum number of iterations.
@param trainer classifier trainer.
@param measure classification measure as the chromosome fitness measure.
@param x training instances.
@param y training labels.
@param k k-fold cross validation for the evaluation.
@return bit strings of last generation.
|
[
"Genetic",
"algorithm",
"based",
"feature",
"selection",
"for",
"classification",
"."
] |
e27e43e90fbaacce3f99d30120cf9dd6a764c33d
|
https://github.com/haifengl/smile/blob/e27e43e90fbaacce3f99d30120cf9dd6a764c33d/core/src/main/java/smile/feature/GAFeatureSelection.java#L107-L132
|
17,389
|
haifengl/smile
|
data/src/main/java/smile/data/NominalAttribute.java
|
NominalAttribute.valueOf
|
@Override
public double valueOf(String s) throws ParseException {
Integer i = map.get(s);
if (i == null) {
if (open) {
i = values.size();
map.put(s, i);
values.add(s);
} else {
throw new ParseException("Invalid string value: " + s, 0);
}
}
return i;
}
|
java
|
@Override
public double valueOf(String s) throws ParseException {
Integer i = map.get(s);
if (i == null) {
if (open) {
i = values.size();
map.put(s, i);
values.add(s);
} else {
throw new ParseException("Invalid string value: " + s, 0);
}
}
return i;
}
|
[
"@",
"Override",
"public",
"double",
"valueOf",
"(",
"String",
"s",
")",
"throws",
"ParseException",
"{",
"Integer",
"i",
"=",
"map",
".",
"get",
"(",
"s",
")",
";",
"if",
"(",
"i",
"==",
"null",
")",
"{",
"if",
"(",
"open",
")",
"{",
"i",
"=",
"values",
".",
"size",
"(",
")",
";",
"map",
".",
"put",
"(",
"s",
",",
"i",
")",
";",
"values",
".",
"add",
"(",
"s",
")",
";",
"}",
"else",
"{",
"throw",
"new",
"ParseException",
"(",
"\"Invalid string value: \"",
"+",
"s",
",",
"0",
")",
";",
"}",
"}",
"return",
"i",
";",
"}"
] |
Returns the ordinal value of a string value.
|
[
"Returns",
"the",
"ordinal",
"value",
"of",
"a",
"string",
"value",
"."
] |
e27e43e90fbaacce3f99d30120cf9dd6a764c33d
|
https://github.com/haifengl/smile/blob/e27e43e90fbaacce3f99d30120cf9dd6a764c33d/data/src/main/java/smile/data/NominalAttribute.java#L164-L178
|
17,390
|
haifengl/smile
|
plot/src/main/java/smile/plot/Projection.java
|
Projection.initBaseCoordsProjection
|
private void initBaseCoordsProjection() {
baseScreenCoords = new int[canvas.base.baseCoords.length][2];
for (int i = 0; i < canvas.base.dimension + 1; i++) {
double[] ratio = baseCoordsScreenProjectionRatio(canvas.base.baseCoords[i]);
baseScreenCoords[i][0] = (int) (canvas.getWidth() * (canvas.margin + (1 - 2 * canvas.margin) * ratio[0]));
baseScreenCoords[i][1] = (int) (canvas.getHeight() - canvas.getHeight() * (canvas.margin + (1 - 2 * canvas.margin) * ratio[1]));
}
}
|
java
|
private void initBaseCoordsProjection() {
baseScreenCoords = new int[canvas.base.baseCoords.length][2];
for (int i = 0; i < canvas.base.dimension + 1; i++) {
double[] ratio = baseCoordsScreenProjectionRatio(canvas.base.baseCoords[i]);
baseScreenCoords[i][0] = (int) (canvas.getWidth() * (canvas.margin + (1 - 2 * canvas.margin) * ratio[0]));
baseScreenCoords[i][1] = (int) (canvas.getHeight() - canvas.getHeight() * (canvas.margin + (1 - 2 * canvas.margin) * ratio[1]));
}
}
|
[
"private",
"void",
"initBaseCoordsProjection",
"(",
")",
"{",
"baseScreenCoords",
"=",
"new",
"int",
"[",
"canvas",
".",
"base",
".",
"baseCoords",
".",
"length",
"]",
"[",
"2",
"]",
";",
"for",
"(",
"int",
"i",
"=",
"0",
";",
"i",
"<",
"canvas",
".",
"base",
".",
"dimension",
"+",
"1",
";",
"i",
"++",
")",
"{",
"double",
"[",
"]",
"ratio",
"=",
"baseCoordsScreenProjectionRatio",
"(",
"canvas",
".",
"base",
".",
"baseCoords",
"[",
"i",
"]",
")",
";",
"baseScreenCoords",
"[",
"i",
"]",
"[",
"0",
"]",
"=",
"(",
"int",
")",
"(",
"canvas",
".",
"getWidth",
"(",
")",
"*",
"(",
"canvas",
".",
"margin",
"+",
"(",
"1",
"-",
"2",
"*",
"canvas",
".",
"margin",
")",
"*",
"ratio",
"[",
"0",
"]",
")",
")",
";",
"baseScreenCoords",
"[",
"i",
"]",
"[",
"1",
"]",
"=",
"(",
"int",
")",
"(",
"canvas",
".",
"getHeight",
"(",
")",
"-",
"canvas",
".",
"getHeight",
"(",
")",
"*",
"(",
"canvas",
".",
"margin",
"+",
"(",
"1",
"-",
"2",
"*",
"canvas",
".",
"margin",
")",
"*",
"ratio",
"[",
"1",
"]",
")",
")",
";",
"}",
"}"
] |
Initialize base coordinates on Java2D screen.
|
[
"Initialize",
"base",
"coordinates",
"on",
"Java2D",
"screen",
"."
] |
e27e43e90fbaacce3f99d30120cf9dd6a764c33d
|
https://github.com/haifengl/smile/blob/e27e43e90fbaacce3f99d30120cf9dd6a764c33d/plot/src/main/java/smile/plot/Projection.java#L55-L62
|
17,391
|
haifengl/smile
|
plot/src/main/java/smile/plot/Projection.java
|
Projection.screenProjection
|
public int[] screenProjection(double... coord) {
double[] sc = new double[2];
sc[0] = baseScreenCoords[0][0];
sc[1] = baseScreenCoords[0][1];
for (int i = 0; i < canvas.base.dimension; i++) {
sc[0] += ((coord[i] - canvas.base.baseCoords[0][i]) / (canvas.base.baseCoords[i + 1][i] - canvas.base.baseCoords[0][i])) * (baseScreenCoords[i + 1][0] - baseScreenCoords[0][0]);
sc[1] += ((coord[i] - canvas.base.baseCoords[0][i]) / (canvas.base.baseCoords[i + 1][i] - canvas.base.baseCoords[0][i])) * (baseScreenCoords[i + 1][1] - baseScreenCoords[0][1]);
}
return new int[]{(int) sc[0], (int) sc[1]};
}
|
java
|
public int[] screenProjection(double... coord) {
double[] sc = new double[2];
sc[0] = baseScreenCoords[0][0];
sc[1] = baseScreenCoords[0][1];
for (int i = 0; i < canvas.base.dimension; i++) {
sc[0] += ((coord[i] - canvas.base.baseCoords[0][i]) / (canvas.base.baseCoords[i + 1][i] - canvas.base.baseCoords[0][i])) * (baseScreenCoords[i + 1][0] - baseScreenCoords[0][0]);
sc[1] += ((coord[i] - canvas.base.baseCoords[0][i]) / (canvas.base.baseCoords[i + 1][i] - canvas.base.baseCoords[0][i])) * (baseScreenCoords[i + 1][1] - baseScreenCoords[0][1]);
}
return new int[]{(int) sc[0], (int) sc[1]};
}
|
[
"public",
"int",
"[",
"]",
"screenProjection",
"(",
"double",
"...",
"coord",
")",
"{",
"double",
"[",
"]",
"sc",
"=",
"new",
"double",
"[",
"2",
"]",
";",
"sc",
"[",
"0",
"]",
"=",
"baseScreenCoords",
"[",
"0",
"]",
"[",
"0",
"]",
";",
"sc",
"[",
"1",
"]",
"=",
"baseScreenCoords",
"[",
"0",
"]",
"[",
"1",
"]",
";",
"for",
"(",
"int",
"i",
"=",
"0",
";",
"i",
"<",
"canvas",
".",
"base",
".",
"dimension",
";",
"i",
"++",
")",
"{",
"sc",
"[",
"0",
"]",
"+=",
"(",
"(",
"coord",
"[",
"i",
"]",
"-",
"canvas",
".",
"base",
".",
"baseCoords",
"[",
"0",
"]",
"[",
"i",
"]",
")",
"/",
"(",
"canvas",
".",
"base",
".",
"baseCoords",
"[",
"i",
"+",
"1",
"]",
"[",
"i",
"]",
"-",
"canvas",
".",
"base",
".",
"baseCoords",
"[",
"0",
"]",
"[",
"i",
"]",
")",
")",
"*",
"(",
"baseScreenCoords",
"[",
"i",
"+",
"1",
"]",
"[",
"0",
"]",
"-",
"baseScreenCoords",
"[",
"0",
"]",
"[",
"0",
"]",
")",
";",
"sc",
"[",
"1",
"]",
"+=",
"(",
"(",
"coord",
"[",
"i",
"]",
"-",
"canvas",
".",
"base",
".",
"baseCoords",
"[",
"0",
"]",
"[",
"i",
"]",
")",
"/",
"(",
"canvas",
".",
"base",
".",
"baseCoords",
"[",
"i",
"+",
"1",
"]",
"[",
"i",
"]",
"-",
"canvas",
".",
"base",
".",
"baseCoords",
"[",
"0",
"]",
"[",
"i",
"]",
")",
")",
"*",
"(",
"baseScreenCoords",
"[",
"i",
"+",
"1",
"]",
"[",
"1",
"]",
"-",
"baseScreenCoords",
"[",
"0",
"]",
"[",
"1",
"]",
")",
";",
"}",
"return",
"new",
"int",
"[",
"]",
"{",
"(",
"int",
")",
"sc",
"[",
"0",
"]",
",",
"(",
"int",
")",
"sc",
"[",
"1",
"]",
"}",
";",
"}"
] |
Project logical coordinates to Java2D coordinates.
|
[
"Project",
"logical",
"coordinates",
"to",
"Java2D",
"coordinates",
"."
] |
e27e43e90fbaacce3f99d30120cf9dd6a764c33d
|
https://github.com/haifengl/smile/blob/e27e43e90fbaacce3f99d30120cf9dd6a764c33d/plot/src/main/java/smile/plot/Projection.java#L67-L78
|
17,392
|
haifengl/smile
|
plot/src/main/java/smile/plot/Projection.java
|
Projection.screenProjectionBaseRatio
|
public int[] screenProjectionBaseRatio(double... coord) {
double[] sc = new double[2];
sc[0] = baseScreenCoords[0][0];
sc[1] = baseScreenCoords[0][1];
for (int i = 0; i < canvas.base.dimension; i++) {
sc[0] += coord[i] * (baseScreenCoords[i + 1][0] - baseScreenCoords[0][0]);
sc[1] += coord[i] * (baseScreenCoords[i + 1][1] - baseScreenCoords[0][1]);
}
return new int[]{(int) sc[0], (int) sc[1]};
}
|
java
|
public int[] screenProjectionBaseRatio(double... coord) {
double[] sc = new double[2];
sc[0] = baseScreenCoords[0][0];
sc[1] = baseScreenCoords[0][1];
for (int i = 0; i < canvas.base.dimension; i++) {
sc[0] += coord[i] * (baseScreenCoords[i + 1][0] - baseScreenCoords[0][0]);
sc[1] += coord[i] * (baseScreenCoords[i + 1][1] - baseScreenCoords[0][1]);
}
return new int[]{(int) sc[0], (int) sc[1]};
}
|
[
"public",
"int",
"[",
"]",
"screenProjectionBaseRatio",
"(",
"double",
"...",
"coord",
")",
"{",
"double",
"[",
"]",
"sc",
"=",
"new",
"double",
"[",
"2",
"]",
";",
"sc",
"[",
"0",
"]",
"=",
"baseScreenCoords",
"[",
"0",
"]",
"[",
"0",
"]",
";",
"sc",
"[",
"1",
"]",
"=",
"baseScreenCoords",
"[",
"0",
"]",
"[",
"1",
"]",
";",
"for",
"(",
"int",
"i",
"=",
"0",
";",
"i",
"<",
"canvas",
".",
"base",
".",
"dimension",
";",
"i",
"++",
")",
"{",
"sc",
"[",
"0",
"]",
"+=",
"coord",
"[",
"i",
"]",
"*",
"(",
"baseScreenCoords",
"[",
"i",
"+",
"1",
"]",
"[",
"0",
"]",
"-",
"baseScreenCoords",
"[",
"0",
"]",
"[",
"0",
"]",
")",
";",
"sc",
"[",
"1",
"]",
"+=",
"coord",
"[",
"i",
"]",
"*",
"(",
"baseScreenCoords",
"[",
"i",
"+",
"1",
"]",
"[",
"1",
"]",
"-",
"baseScreenCoords",
"[",
"0",
"]",
"[",
"1",
"]",
")",
";",
"}",
"return",
"new",
"int",
"[",
"]",
"{",
"(",
"int",
")",
"sc",
"[",
"0",
"]",
",",
"(",
"int",
")",
"sc",
"[",
"1",
"]",
"}",
";",
"}"
] |
Project logical coordinates in base ratio to Java2D coordinates.
|
[
"Project",
"logical",
"coordinates",
"in",
"base",
"ratio",
"to",
"Java2D",
"coordinates",
"."
] |
e27e43e90fbaacce3f99d30120cf9dd6a764c33d
|
https://github.com/haifengl/smile/blob/e27e43e90fbaacce3f99d30120cf9dd6a764c33d/plot/src/main/java/smile/plot/Projection.java#L83-L94
|
17,393
|
haifengl/smile
|
math/src/main/java/smile/stat/distribution/AbstractDistribution.java
|
AbstractDistribution.logLikelihood
|
@Override
public double logLikelihood(double[] x) {
double L = 0.0;
for (double xi : x)
L += logp(xi);
return L;
}
|
java
|
@Override
public double logLikelihood(double[] x) {
double L = 0.0;
for (double xi : x)
L += logp(xi);
return L;
}
|
[
"@",
"Override",
"public",
"double",
"logLikelihood",
"(",
"double",
"[",
"]",
"x",
")",
"{",
"double",
"L",
"=",
"0.0",
";",
"for",
"(",
"double",
"xi",
":",
"x",
")",
"L",
"+=",
"logp",
"(",
"xi",
")",
";",
"return",
"L",
";",
"}"
] |
The likelihood given a sample set following the distribution.
|
[
"The",
"likelihood",
"given",
"a",
"sample",
"set",
"following",
"the",
"distribution",
"."
] |
e27e43e90fbaacce3f99d30120cf9dd6a764c33d
|
https://github.com/haifengl/smile/blob/e27e43e90fbaacce3f99d30120cf9dd6a764c33d/math/src/main/java/smile/stat/distribution/AbstractDistribution.java#L125-L133
|
17,394
|
haifengl/smile
|
core/src/main/java/smile/util/MulticoreExecutor.java
|
MulticoreExecutor.createThreadPool
|
private static void createThreadPool() {
if (nprocs == -1) {
int n = -1;
try {
String env = System.getProperty("smile.threads");
if (env != null) {
n = Integer.parseInt(env);
}
} catch (Exception ex) {
logger.error("Failed to create multi-core execution thread pool", ex);
}
if (n < 1) {
nprocs = Runtime.getRuntime().availableProcessors();
} else {
nprocs = n;
}
if (nprocs > 1) {
threads = (ThreadPoolExecutor) Executors.newFixedThreadPool(nprocs, new SimpleDeamonThreadFactory());
}
}
}
|
java
|
private static void createThreadPool() {
if (nprocs == -1) {
int n = -1;
try {
String env = System.getProperty("smile.threads");
if (env != null) {
n = Integer.parseInt(env);
}
} catch (Exception ex) {
logger.error("Failed to create multi-core execution thread pool", ex);
}
if (n < 1) {
nprocs = Runtime.getRuntime().availableProcessors();
} else {
nprocs = n;
}
if (nprocs > 1) {
threads = (ThreadPoolExecutor) Executors.newFixedThreadPool(nprocs, new SimpleDeamonThreadFactory());
}
}
}
|
[
"private",
"static",
"void",
"createThreadPool",
"(",
")",
"{",
"if",
"(",
"nprocs",
"==",
"-",
"1",
")",
"{",
"int",
"n",
"=",
"-",
"1",
";",
"try",
"{",
"String",
"env",
"=",
"System",
".",
"getProperty",
"(",
"\"smile.threads\"",
")",
";",
"if",
"(",
"env",
"!=",
"null",
")",
"{",
"n",
"=",
"Integer",
".",
"parseInt",
"(",
"env",
")",
";",
"}",
"}",
"catch",
"(",
"Exception",
"ex",
")",
"{",
"logger",
".",
"error",
"(",
"\"Failed to create multi-core execution thread pool\"",
",",
"ex",
")",
";",
"}",
"if",
"(",
"n",
"<",
"1",
")",
"{",
"nprocs",
"=",
"Runtime",
".",
"getRuntime",
"(",
")",
".",
"availableProcessors",
"(",
")",
";",
"}",
"else",
"{",
"nprocs",
"=",
"n",
";",
"}",
"if",
"(",
"nprocs",
">",
"1",
")",
"{",
"threads",
"=",
"(",
"ThreadPoolExecutor",
")",
"Executors",
".",
"newFixedThreadPool",
"(",
"nprocs",
",",
"new",
"SimpleDeamonThreadFactory",
"(",
")",
")",
";",
"}",
"}",
"}"
] |
Creates the worker thread pool.
|
[
"Creates",
"the",
"worker",
"thread",
"pool",
"."
] |
e27e43e90fbaacce3f99d30120cf9dd6a764c33d
|
https://github.com/haifengl/smile/blob/e27e43e90fbaacce3f99d30120cf9dd6a764c33d/core/src/main/java/smile/util/MulticoreExecutor.java#L52-L74
|
17,395
|
haifengl/smile
|
core/src/main/java/smile/util/MulticoreExecutor.java
|
MulticoreExecutor.run
|
public static <T> List<T> run(Collection<? extends Callable<T>> tasks) throws Exception {
createThreadPool();
List<T> results = new ArrayList<>();
if (threads == null) {
for (Callable<T> task : tasks) {
results.add(task.call());
}
} else {
if (threads.getActiveCount() < nprocs) {
List<Future<T>> futures = threads.invokeAll(tasks);
for (Future<T> future : futures) {
results.add(future.get());
}
} else {
// Thread pool is busy. Just run in the caller's thread.
for (Callable<T> task : tasks) {
results.add(task.call());
}
}
}
return results;
}
|
java
|
public static <T> List<T> run(Collection<? extends Callable<T>> tasks) throws Exception {
createThreadPool();
List<T> results = new ArrayList<>();
if (threads == null) {
for (Callable<T> task : tasks) {
results.add(task.call());
}
} else {
if (threads.getActiveCount() < nprocs) {
List<Future<T>> futures = threads.invokeAll(tasks);
for (Future<T> future : futures) {
results.add(future.get());
}
} else {
// Thread pool is busy. Just run in the caller's thread.
for (Callable<T> task : tasks) {
results.add(task.call());
}
}
}
return results;
}
|
[
"public",
"static",
"<",
"T",
">",
"List",
"<",
"T",
">",
"run",
"(",
"Collection",
"<",
"?",
"extends",
"Callable",
"<",
"T",
">",
">",
"tasks",
")",
"throws",
"Exception",
"{",
"createThreadPool",
"(",
")",
";",
"List",
"<",
"T",
">",
"results",
"=",
"new",
"ArrayList",
"<>",
"(",
")",
";",
"if",
"(",
"threads",
"==",
"null",
")",
"{",
"for",
"(",
"Callable",
"<",
"T",
">",
"task",
":",
"tasks",
")",
"{",
"results",
".",
"add",
"(",
"task",
".",
"call",
"(",
")",
")",
";",
"}",
"}",
"else",
"{",
"if",
"(",
"threads",
".",
"getActiveCount",
"(",
")",
"<",
"nprocs",
")",
"{",
"List",
"<",
"Future",
"<",
"T",
">>",
"futures",
"=",
"threads",
".",
"invokeAll",
"(",
"tasks",
")",
";",
"for",
"(",
"Future",
"<",
"T",
">",
"future",
":",
"futures",
")",
"{",
"results",
".",
"add",
"(",
"future",
".",
"get",
"(",
")",
")",
";",
"}",
"}",
"else",
"{",
"// Thread pool is busy. Just run in the caller's thread.",
"for",
"(",
"Callable",
"<",
"T",
">",
"task",
":",
"tasks",
")",
"{",
"results",
".",
"add",
"(",
"task",
".",
"call",
"(",
")",
")",
";",
"}",
"}",
"}",
"return",
"results",
";",
"}"
] |
Executes the given tasks serially or parallel depending on the number
of cores of the system. Returns a list of result objects of each task.
The results of this method are undefined if the given collection is
modified while this operation is in progress.
@param tasks the collection of tasks.
@return a list of result objects in the same sequential order as
produced by the iterator for the given task list.
@throws Exception if unable to compute a result.
|
[
"Executes",
"the",
"given",
"tasks",
"serially",
"or",
"parallel",
"depending",
"on",
"the",
"number",
"of",
"cores",
"of",
"the",
"system",
".",
"Returns",
"a",
"list",
"of",
"result",
"objects",
"of",
"each",
"task",
".",
"The",
"results",
"of",
"this",
"method",
"are",
"undefined",
"if",
"the",
"given",
"collection",
"is",
"modified",
"while",
"this",
"operation",
"is",
"in",
"progress",
"."
] |
e27e43e90fbaacce3f99d30120cf9dd6a764c33d
|
https://github.com/haifengl/smile/blob/e27e43e90fbaacce3f99d30120cf9dd6a764c33d/core/src/main/java/smile/util/MulticoreExecutor.java#L95-L118
|
17,396
|
haifengl/smile
|
plot/src/main/java/smile/plot/Histogram.java
|
Histogram.plot
|
public static PlotCanvas plot(String id, double[] data) {
Histogram histogram = new Histogram(data);
histogram.setID(id);
double[] lowerBound = {Math.min(data), 0};
double[] upperBound = {Math.max(data), 0};
double[][] freq = histogram.getHistogram();
for (int i = 0; i < freq.length; i++) {
if (freq[i][1] > upperBound[1]) {
upperBound[1] = freq[i][1];
}
}
PlotCanvas canvas = new PlotCanvas(lowerBound, upperBound);
canvas.getAxis(0).setGridVisible(false);
canvas.add(histogram);
return canvas;
}
|
java
|
public static PlotCanvas plot(String id, double[] data) {
Histogram histogram = new Histogram(data);
histogram.setID(id);
double[] lowerBound = {Math.min(data), 0};
double[] upperBound = {Math.max(data), 0};
double[][] freq = histogram.getHistogram();
for (int i = 0; i < freq.length; i++) {
if (freq[i][1] > upperBound[1]) {
upperBound[1] = freq[i][1];
}
}
PlotCanvas canvas = new PlotCanvas(lowerBound, upperBound);
canvas.getAxis(0).setGridVisible(false);
canvas.add(histogram);
return canvas;
}
|
[
"public",
"static",
"PlotCanvas",
"plot",
"(",
"String",
"id",
",",
"double",
"[",
"]",
"data",
")",
"{",
"Histogram",
"histogram",
"=",
"new",
"Histogram",
"(",
"data",
")",
";",
"histogram",
".",
"setID",
"(",
"id",
")",
";",
"double",
"[",
"]",
"lowerBound",
"=",
"{",
"Math",
".",
"min",
"(",
"data",
")",
",",
"0",
"}",
";",
"double",
"[",
"]",
"upperBound",
"=",
"{",
"Math",
".",
"max",
"(",
"data",
")",
",",
"0",
"}",
";",
"double",
"[",
"]",
"[",
"]",
"freq",
"=",
"histogram",
".",
"getHistogram",
"(",
")",
";",
"for",
"(",
"int",
"i",
"=",
"0",
";",
"i",
"<",
"freq",
".",
"length",
";",
"i",
"++",
")",
"{",
"if",
"(",
"freq",
"[",
"i",
"]",
"[",
"1",
"]",
">",
"upperBound",
"[",
"1",
"]",
")",
"{",
"upperBound",
"[",
"1",
"]",
"=",
"freq",
"[",
"i",
"]",
"[",
"1",
"]",
";",
"}",
"}",
"PlotCanvas",
"canvas",
"=",
"new",
"PlotCanvas",
"(",
"lowerBound",
",",
"upperBound",
")",
";",
"canvas",
".",
"getAxis",
"(",
"0",
")",
".",
"setGridVisible",
"(",
"false",
")",
";",
"canvas",
".",
"add",
"(",
"histogram",
")",
";",
"return",
"canvas",
";",
"}"
] |
Create a plot canvas with the histogram plot.
@param id the id of the plot.
@param data a sample set.
|
[
"Create",
"a",
"plot",
"canvas",
"with",
"the",
"histogram",
"plot",
"."
] |
e27e43e90fbaacce3f99d30120cf9dd6a764c33d
|
https://github.com/haifengl/smile/blob/e27e43e90fbaacce3f99d30120cf9dd6a764c33d/plot/src/main/java/smile/plot/Histogram.java#L335-L354
|
17,397
|
haifengl/smile
|
core/src/main/java/smile/association/FPGrowth.java
|
FPGrowth.buildTotalSupportTree
|
TotalSupportTree buildTotalSupportTree() {
TotalSupportTree ttree = new TotalSupportTree(minSupport, T0.numFreqItems, T0.order);
learn(null, null, ttree);
return ttree;
}
|
java
|
TotalSupportTree buildTotalSupportTree() {
TotalSupportTree ttree = new TotalSupportTree(minSupport, T0.numFreqItems, T0.order);
learn(null, null, ttree);
return ttree;
}
|
[
"TotalSupportTree",
"buildTotalSupportTree",
"(",
")",
"{",
"TotalSupportTree",
"ttree",
"=",
"new",
"TotalSupportTree",
"(",
"minSupport",
",",
"T0",
".",
"numFreqItems",
",",
"T0",
".",
"order",
")",
";",
"learn",
"(",
"null",
",",
"null",
",",
"ttree",
")",
";",
"return",
"ttree",
";",
"}"
] |
Mines the frequent item sets. The discovered frequent item sets
will be stored in a total support tree.
|
[
"Mines",
"the",
"frequent",
"item",
"sets",
".",
"The",
"discovered",
"frequent",
"item",
"sets",
"will",
"be",
"stored",
"in",
"a",
"total",
"support",
"tree",
"."
] |
e27e43e90fbaacce3f99d30120cf9dd6a764c33d
|
https://github.com/haifengl/smile/blob/e27e43e90fbaacce3f99d30120cf9dd6a764c33d/core/src/main/java/smile/association/FPGrowth.java#L164-L168
|
17,398
|
haifengl/smile
|
core/src/main/java/smile/association/FPGrowth.java
|
FPGrowth.learn
|
private long learn(PrintStream out, List<ItemSet> list, TotalSupportTree ttree) {
if (MulticoreExecutor.getThreadPoolSize() > 1) {
return grow(out, list, ttree, T0, null, null, null);
} else {
return grow(out, list, ttree, T0, null);
}
}
|
java
|
private long learn(PrintStream out, List<ItemSet> list, TotalSupportTree ttree) {
if (MulticoreExecutor.getThreadPoolSize() > 1) {
return grow(out, list, ttree, T0, null, null, null);
} else {
return grow(out, list, ttree, T0, null);
}
}
|
[
"private",
"long",
"learn",
"(",
"PrintStream",
"out",
",",
"List",
"<",
"ItemSet",
">",
"list",
",",
"TotalSupportTree",
"ttree",
")",
"{",
"if",
"(",
"MulticoreExecutor",
".",
"getThreadPoolSize",
"(",
")",
">",
"1",
")",
"{",
"return",
"grow",
"(",
"out",
",",
"list",
",",
"ttree",
",",
"T0",
",",
"null",
",",
"null",
",",
"null",
")",
";",
"}",
"else",
"{",
"return",
"grow",
"(",
"out",
",",
"list",
",",
"ttree",
",",
"T0",
",",
"null",
")",
";",
"}",
"}"
] |
Mines the frequent item sets. The discovered frequent item sets
will be printed out to the provided stream.
@param out a print stream for output of frequent item sets.
@return the number of discovered frequent item sets.
|
[
"Mines",
"the",
"frequent",
"item",
"sets",
".",
"The",
"discovered",
"frequent",
"item",
"sets",
"will",
"be",
"printed",
"out",
"to",
"the",
"provided",
"stream",
"."
] |
e27e43e90fbaacce3f99d30120cf9dd6a764c33d
|
https://github.com/haifengl/smile/blob/e27e43e90fbaacce3f99d30120cf9dd6a764c33d/core/src/main/java/smile/association/FPGrowth.java#L176-L182
|
17,399
|
haifengl/smile
|
core/src/main/java/smile/association/FPGrowth.java
|
FPGrowth.collect
|
private void collect(PrintStream out, List<ItemSet> list, TotalSupportTree ttree, int[] itemset, int support) {
if (list != null) {
synchronized (list) {
list.add(new ItemSet(itemset, support));
}
}
if (out != null) {
synchronized (out) {
for (int i = 0; i < itemset.length; i++) {
out.format("%d ", itemset[i]);
}
out.format("(%d)%n", support);
}
}
if (ttree != null) {
synchronized (ttree) {
ttree.add(itemset, support);
}
}
}
|
java
|
private void collect(PrintStream out, List<ItemSet> list, TotalSupportTree ttree, int[] itemset, int support) {
if (list != null) {
synchronized (list) {
list.add(new ItemSet(itemset, support));
}
}
if (out != null) {
synchronized (out) {
for (int i = 0; i < itemset.length; i++) {
out.format("%d ", itemset[i]);
}
out.format("(%d)%n", support);
}
}
if (ttree != null) {
synchronized (ttree) {
ttree.add(itemset, support);
}
}
}
|
[
"private",
"void",
"collect",
"(",
"PrintStream",
"out",
",",
"List",
"<",
"ItemSet",
">",
"list",
",",
"TotalSupportTree",
"ttree",
",",
"int",
"[",
"]",
"itemset",
",",
"int",
"support",
")",
"{",
"if",
"(",
"list",
"!=",
"null",
")",
"{",
"synchronized",
"(",
"list",
")",
"{",
"list",
".",
"add",
"(",
"new",
"ItemSet",
"(",
"itemset",
",",
"support",
")",
")",
";",
"}",
"}",
"if",
"(",
"out",
"!=",
"null",
")",
"{",
"synchronized",
"(",
"out",
")",
"{",
"for",
"(",
"int",
"i",
"=",
"0",
";",
"i",
"<",
"itemset",
".",
"length",
";",
"i",
"++",
")",
"{",
"out",
".",
"format",
"(",
"\"%d \"",
",",
"itemset",
"[",
"i",
"]",
")",
";",
"}",
"out",
".",
"format",
"(",
"\"(%d)%n\"",
",",
"support",
")",
";",
"}",
"}",
"if",
"(",
"ttree",
"!=",
"null",
")",
"{",
"synchronized",
"(",
"ttree",
")",
"{",
"ttree",
".",
"add",
"(",
"itemset",
",",
"support",
")",
";",
"}",
"}",
"}"
] |
Adds an item set to the result.
|
[
"Adds",
"an",
"item",
"set",
"to",
"the",
"result",
"."
] |
e27e43e90fbaacce3f99d30120cf9dd6a764c33d
|
https://github.com/haifengl/smile/blob/e27e43e90fbaacce3f99d30120cf9dd6a764c33d/core/src/main/java/smile/association/FPGrowth.java#L322-L341
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.