* 统计信息,计算删除非0特征后,权重的长度
*
* @throws IOException
*/
public void removeZero1(Linear cl) {
HashFeatureAlphabet feature = (HashFeatureAlphabet) cl.getAlphabetFactory().DefaultFeatureAlphabet();
float[] weights = cl.getWeights();
boolean freeze = false;
if (feature.isStopIncrement()) {
feature.setStopIncrement(false);
freeze = true;
}
TIntIntHashMap index = new TIntIntHashMap();
TIntIntIterator it = feature.iterator();
while (it.hasNext()) {
it.advance();
int value = it.key();
int key = it.value();
index.put(key, value);
}
int[] idx = index.keys();
Arrays.sort(idx);
int length = weights.length;
HashFeatureAlphabet newfeat = new HashFeatureAlphabet();
cl.getAlphabetFactory().setDefaultFeatureAlphabet(newfeat);
TFloatArrayList ww = new TFloatArrayList();
for (int i = 0; i < idx.length; i++) {
int base = idx[i]; //一个特征段起始位置
int end; //一个特征段结束位置
if (i < idx.length - 1)
end = idx[i + 1]; //对应下一个特征段起始位置
else
end = length; //或者整个结束位置
int interv = end - base; //一个特征段长度
float[] sw = new float[interv];
for (int j = 0; j < interv; j++) {
sw[j] = weights[base+j];
}
float var = MyArrays.viarance(sw);
if (var>varsthresh) {
int str = index.get(base);
int id = newfeat.lookupIndex(str, interv);
for (int j = 0; j < interv; j++) {
ww.insert(id + j, weights[base + j]);
}
}else{
// System.out.print(".");
}
}
newfeat.setStopIncrement(freeze);
cl.setWeights(ww.toArray());
index.clear();
ww.clear();
}