use of com.alibaba.alink.common.linalg.SparseVector in project Alink by alibaba.
the class FeatureLabelUtil method getVectorFeature.
public static Vector getVectorFeature(Object input, boolean hasInterceptItem, Integer vectorSize) {
Vector aVector;
Vector vec = VectorUtil.getVector(input);
if (vec instanceof SparseVector) {
SparseVector tmp = (SparseVector) vec;
if (null != vectorSize && tmp.size() > 0) {
tmp.setSize(vectorSize);
}
aVector = hasInterceptItem ? tmp.prefix(1.0) : tmp;
} else {
DenseVector tmp = (DenseVector) vec;
aVector = hasInterceptItem ? tmp.prefix(1.0) : tmp;
}
return aVector;
}
use of com.alibaba.alink.common.linalg.SparseVector in project Alink by alibaba.
the class FeatureLabelUtil method getFeatureVector.
/**
* Retrieve the feature vector from the input row data.
*/
public static Vector getFeatureVector(Row row, boolean hasInterceptItem, int featureN, int[] featureIdx, int vectorColIndex, Integer vectorSize) {
Vector aVector;
if (vectorColIndex != -1) {
Vector vec = VectorUtil.getVector(row.getField(vectorColIndex));
if (vec instanceof SparseVector) {
SparseVector tmp = (SparseVector) vec;
if (null != vectorSize && tmp.size() > 0) {
tmp.setSize(vectorSize);
}
aVector = hasInterceptItem ? tmp.prefix(1.0) : tmp;
} else {
DenseVector tmp = (DenseVector) vec;
aVector = hasInterceptItem ? tmp.prefix(1.0) : tmp;
}
} else {
if (hasInterceptItem) {
aVector = new DenseVector(featureN + 1);
aVector.set(0, 1.0);
for (int i = 0; i < featureN; i++) {
if (row.getField(featureIdx[i]) instanceof Number) {
aVector.set(i + 1, ((Number) row.getField(featureIdx[i])).doubleValue());
}
}
} else {
aVector = new DenseVector(featureN);
for (int i = 0; i < featureN; i++) {
if (row.getField(featureIdx[i]) instanceof Number) {
aVector.set(i, ((Number) row.getField(featureIdx[i])).doubleValue());
}
}
}
}
return aVector;
}
use of com.alibaba.alink.common.linalg.SparseVector in project Alink by alibaba.
the class PcaModelMapper method map.
@Override
protected void map(SlicedSelectedSample selection, SlicedResult result) throws Exception {
// transform data
double[] data = new double[this.model.nx];
if (isVector) {
Vector parsed = VectorUtil.getVector(selection.get(featureIdxs[0]));
if (parsed instanceof SparseVector) {
if (parsed.size() < 0) {
((SparseVector) parsed).setSize(model.nx);
}
}
for (int i = 0; i < parsed.size(); i++) {
data[i] = parsed.get(i);
}
} else {
for (int i = 0; i < this.featureIdxs.length; ++i) {
data[i] = (Double) selection.get(this.featureIdxs[i]);
}
}
double[] predictData;
if (model.idxNonEqual.length != data.length) {
Integer[] indices = model.idxNonEqual;
double[] dataNe = new double[indices.length];
for (int i = 0; i < indices.length; i++) {
if (Math.abs(this.sourceStd[i]) > 1e-12) {
int idx = indices[i];
dataNe[i] = (data[idx] - this.sourceMean[i]) / this.sourceStd[i];
}
}
predictData = model.calcPrinValue(dataNe);
} else {
for (int i = 0; i < data.length; i++) {
if (Math.abs(this.sourceStd[i]) > 1e-12) {
data[i] = (data[i] - this.sourceMean[i]) / this.sourceStd[i];
}
}
predictData = model.calcPrinValue(data);
}
result.set(0, new DenseVector(predictData));
}
use of com.alibaba.alink.common.linalg.SparseVector in project Alink by alibaba.
the class CrossFeatureModelMapper method map.
@Override
protected void map(SlicedSelectedSample selection, SlicedResult result) throws Exception {
for (int i = 0; i < selectedColIndices.length; i++) {
Object data = selection.get(selectedColIndices[i]);
if (data == null) {
if (nullIndex[i] != -1) {
dataIndices[i] = nullIndex[i];
} else {
SparseVector sv = new SparseVector(svLength);
result.set(0, sv);
return;
}
} else if (tokenAndIndex[i].containsKey(data.toString())) {
dataIndices[i] = tokenAndIndex[i].get(data.toString());
} else {
SparseVector sv = new SparseVector(svLength);
result.set(0, sv);
return;
}
}
int resIndex = 0;
for (int i = 0; i < carry.length; i++) {
resIndex += carry[i] * dataIndices[i];
}
SparseVector sv = new SparseVector(svLength, new int[] { resIndex }, new double[] { 1.0 });
result.set(0, sv);
}
use of com.alibaba.alink.common.linalg.SparseVector in project Alink by alibaba.
the class FeatureHasherMapper method map.
@Override
protected void map(SlicedSelectedSample selection, SlicedResult result) throws Exception {
TreeMap<Integer, Double> feature = new TreeMap<>();
for (int key : numericColIndexes) {
if (null != selection.get(key)) {
double value = ((Number) selection.get(key)).doubleValue();
String colName = selectedCols[key];
updateMap(colName, value, feature, numFeature);
}
}
for (int key : categoricalColIndexes) {
if (null != selection.get(key)) {
String colName = selectedCols[key];
updateMap(colName + "=" + selection.get(key).toString(), 1.0, feature, numFeature);
}
}
result.set(0, new SparseVector(numFeature, feature));
}
Aggregations