本文整理汇总了Java中weka.core.Instance.numAttributes方法的典型用法代码示例。如果您正苦于以下问题:Java Instance.numAttributes方法的具体用法?Java Instance.numAttributes怎么用?Java Instance.numAttributes使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类weka.core.Instance
的用法示例。
在下文中一共展示了Instance.numAttributes方法的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: instanceToDenseDMatrix
import weka.core.Instance; //导入方法依赖的package包/类
public static DMatrix instanceToDenseDMatrix(Instance instance) throws XGBoostError {
Attribute classAttribute = instance.classAttribute();
int classAttrIndex = classAttribute.index();
int colNum = instance.numAttributes()-1;
int rowNum = 1;
float[] data = new float[colNum*rowNum];
Enumeration<Attribute> attributeEnumeration = instance.enumerateAttributes();
int dataIndex = 0;
while (attributeEnumeration.hasMoreElements()) {
Attribute attribute = attributeEnumeration.nextElement();
int attrIndex = attribute.index();
if(attrIndex == classAttrIndex){
continue;
}
data[dataIndex]= (float) instance.value(attribute);
dataIndex++;
}
return new DMatrix(data, rowNum, colNum);
}
示例2: getBestPerfFrom
import weka.core.Instance; //导入方法依赖的package包/类
public static void getBestPerfFrom(String path){
try {
BestConf bestconf = new BestConf();
Instances trainingSet = DataIOFile.loadDataFromArffFile(path);
Instance best = trainingSet.firstInstance();
//set the best configuration to the cluster
Map<Attribute,Double> attsmap = new HashMap<Attribute,Double>();
for(int i=0;i<best.numAttributes()-1;i++){
attsmap.put(best.attribute(i), best.value(i));
}
double bestPerf = bestconf.setOptimal(attsmap, "getBestPerfFrom");
System.out.println("=========================================");
System.err.println("The actual performance for the best point is : "+bestPerf);
System.out.println("=========================================");
} catch (IOException e) {
e.printStackTrace();
}
}
示例3: scoreInstance
import weka.core.Instance; //导入方法依赖的package包/类
private double scoreInstance(Instance instance) {
// bias
double score = 1 * this.weights[0];
// ignore id and topic and class label
for (int i = 2; i < instance.numAttributes() - 1; i++) {
score += this.weights[i - 1] * instance.value(i);
}
return score;
}
示例4: classifyInstance
import weka.core.Instance; //导入方法依赖的package包/类
@Override
public double classifyInstance(Instance sample) throws Exception {
// transform instance to sequence
MonoDoubleItemSet[] sequence = new MonoDoubleItemSet[sample.numAttributes() - 1];
int shift = (sample.classIndex() == 0) ? 1 : 0;
for (int t = 0; t < sequence.length; t++) {
sequence[t] = new MonoDoubleItemSet(sample.value(t + shift));
}
SymbolicSequence seq = new SymbolicSequence(sequence);
double minD = Double.MAX_VALUE;
String classValue = null;
seq.LB_KeoghFillUL(bestWarpingWindow, U, L);
for (int i = 0; i < train.length; i++) {
SymbolicSequence s = train[i];
if (SymbolicSequence.LB_KeoghPreFilled(s, U, L) < minD) {
double tmpD = seq.DTW(s,bestWarpingWindow, warpingMatrix);
if (tmpD < minD) {
minD = tmpD;
classValue = classMap[i];
}
}
}
// System.out.println(prototypes.size());
return sample.classAttribute().indexOfValue(classValue);
}
示例5: classifyInstance
import weka.core.Instance; //导入方法依赖的package包/类
@Override
public double classifyInstance(Instance sample) throws Exception {
// transform instance to sequence
MonoDoubleItemSet[] sequence = new MonoDoubleItemSet[sample.numAttributes() - 1];
int shift = (sample.classIndex() == 0) ? 1 : 0;
for (int t = 0; t < sequence.length; t++) {
sequence[t] = new MonoDoubleItemSet(sample.value(t + shift));
}
SymbolicSequence seq = new SymbolicSequence(sequence);
double minD = Double.MAX_VALUE;
String classValue = null;
seq.LB_KeoghFillUL(bestWarpingWindow, U, L);
for (int i = 0; i < train.length; i++) {
SymbolicSequence s = train[i];
if (SymbolicSequence.LB_KeoghPreFilled(s, U, L) < minD) {
double tmpD = seq.DTW(s,bestWarpingWindow);
if (tmpD < minD) {
minD = tmpD;
classValue = classMap[i];
}
}
}
// System.out.println(prototypes.size());
return sample.classAttribute().indexOfValue(classValue);
}
示例6: getMD5
import weka.core.Instance; //导入方法依赖的package包/类
public static String getMD5(Instance ins){
StringBuffer name = new StringBuffer("");
for(int i = 0; i < ins.numAttributes() - 2; i++){
name.append(Math.round(ins.value(ins.attribute(i)))+",");
}
return getMD5(name.toString());
}
示例7: buildClassifier
import weka.core.Instance; //导入方法依赖的package包/类
@Override
public void buildClassifier(Instances data) throws Exception {
// Initialise training dataset
Attribute classAttribute = data.classAttribute();
classedData = new HashMap<>();
classedDataIndices = new HashMap<>();
for (int c = 0; c < data.numClasses(); c++) {
classedData.put(data.classAttribute().value(c), new ArrayList<SymbolicSequence>());
classedDataIndices.put(data.classAttribute().value(c), new ArrayList<Integer>());
}
train = new SymbolicSequence[data.numInstances()];
classMap = new String[train.length];
maxLength = 0;
for (int i = 0; i < train.length; i++) {
Instance sample = data.instance(i);
MonoDoubleItemSet[] sequence = new MonoDoubleItemSet[sample.numAttributes() - 1];
maxLength = Math.max(maxLength, sequence.length);
int shift = (sample.classIndex() == 0) ? 1 : 0;
for (int t = 0; t < sequence.length; t++) {
sequence[t] = new MonoDoubleItemSet(sample.value(t + shift));
}
train[i] = new SymbolicSequence(sequence);
String clas = sample.stringValue(classAttribute);
classMap[i] = clas;
classedData.get(clas).add(train[i]);
classedDataIndices.get(clas).add(i);
}
warpingMatrix = new double[maxLength][maxLength];
U = new double[maxLength];
L = new double[maxLength];
maxWindow = Math.round(1 * maxLength);
searchResults = new String[maxWindow+1];
nns = new int[maxWindow+1][train.length];
dist = new double[train.length][train.length];
// Start searching for the best window
searchBestWarpingWindow();
// Saving best windows found
System.out.println("Windows found=" + bestWarpingWindow + " Best Acc=" + (1-bestScore));
}
示例8: buildClassifier
import weka.core.Instance; //导入方法依赖的package包/类
@Override
public void buildClassifier(Instances data) throws Exception {
// Initialise training dataset
Attribute classAttribute = data.classAttribute();
classedData = new HashMap<>();
classedDataIndices = new HashMap<>();
for (int c = 0; c < data.numClasses(); c++) {
classedData.put(data.classAttribute().value(c), new ArrayList<SymbolicSequence>());
classedDataIndices.put(data.classAttribute().value(c), new ArrayList<Integer>());
}
train = new SymbolicSequence[data.numInstances()];
classMap = new String[train.length];
maxLength = 0;
for (int i = 0; i < train.length; i++) {
Instance sample = data.instance(i);
MonoDoubleItemSet[] sequence = new MonoDoubleItemSet[sample.numAttributes() - 1];
maxLength = Math.max(maxLength, sequence.length);
int shift = (sample.classIndex() == 0) ? 1 : 0;
for (int t = 0; t < sequence.length; t++) {
sequence[t] = new MonoDoubleItemSet(sample.value(t + shift));
}
train[i] = new SymbolicSequence(sequence);
String clas = sample.stringValue(classAttribute);
classMap[i] = clas;
classedData.get(clas).add(train[i]);
classedDataIndices.get(clas).add(i);
}
warpingMatrix = new double[maxLength][maxLength];
U = new double[maxLength];
L = new double[maxLength];
U1 = new double[maxLength];
L1 = new double[maxLength];
maxWindow = Math.round(1 * maxLength);
searchResults = new String[maxWindow+1];
nns = new int[maxWindow+1][train.length];
dist = new double[maxWindow+1][train.length];
cache = new SequenceStatsCache(train, maxWindow);
lazyUCR = new LazyAssessNNEarlyAbandon[train.length][train.length];
for (int i = 0; i < train.length; i++) {
for (int j = 0; j < train.length; j++) {
lazyUCR[i][j] = new LazyAssessNNEarlyAbandon(cache);
}
}
// Start searching for the best window
searchBestWarpingWindow();
// Saving best windows found
System.out.println("Windows found=" + bestWarpingWindow + " Best Acc=" + (1-bestScore));
}
示例9: buildClassifier
import weka.core.Instance; //导入方法依赖的package包/类
@Override
public void buildClassifier(Instances data) throws Exception {
// Initialise training dataset
Attribute classAttribute = data.classAttribute();
classedData = new HashMap<>();
classedDataIndices = new HashMap<>();
for (int c = 0; c < data.numClasses(); c++) {
classedData.put(data.classAttribute().value(c), new ArrayList<SymbolicSequence>());
classedDataIndices.put(data.classAttribute().value(c), new ArrayList<Integer>());
}
train = new SymbolicSequence[data.numInstances()];
classMap = new String[train.length];
maxLength = 0;
for (int i = 0; i < train.length; i++) {
Instance sample = data.instance(i);
MonoDoubleItemSet[] sequence = new MonoDoubleItemSet[sample.numAttributes() - 1];
maxLength = Math.max(maxLength, sequence.length);
int shift = (sample.classIndex() == 0) ? 1 : 0;
for (int t = 0; t < sequence.length; t++) {
sequence[t] = new MonoDoubleItemSet(sample.value(t + shift));
}
train[i] = new SymbolicSequence(sequence);
String clas = sample.stringValue(classAttribute);
classMap[i] = clas;
classedData.get(clas).add(train[i]);
classedDataIndices.get(clas).add(i);
}
warpingMatrix = new double[maxLength][maxLength];
U = new double[maxLength];
L = new double[maxLength];
U1 = new double[maxLength];
L1 = new double[maxLength];
maxWindow = Math.round(1 * maxLength);
searchResults = new String[maxWindow+1];
nns = new int[maxWindow+1][train.length];
dist = new double[train.length][train.length];
cache = new SequenceStatsCache(train, maxWindow);
lazyUCR = new LazyAssessNNEarlyAbandon[train.length][train.length];
for (int i = 0; i < train.length; i++) {
for (int j = 0; j < train.length; j++) {
lazyUCR[i][j] = new LazyAssessNNEarlyAbandon(cache);
}
}
// Start searching for the best window
searchBestWarpingWindow();
// Saving best windows found
System.out.println("Windows found=" + bestWarpingWindow + " Best Acc=" + (1-bestScore));
}
示例10: buildClassifier
import weka.core.Instance; //导入方法依赖的package包/类
@Override
public void buildClassifier(Instances data) throws Exception {
// Initialise training dataset
Attribute classAttribute = data.classAttribute();
classedData = new HashMap<>();
classedDataIndices = new HashMap<>();
for (int c = 0; c < data.numClasses(); c++) {
classedData.put(data.classAttribute().value(c), new ArrayList<SymbolicSequence>());
classedDataIndices.put(data.classAttribute().value(c), new ArrayList<Integer>());
}
train = new SymbolicSequence[data.numInstances()];
classMap = new String[train.length];
maxLength = 0;
for (int i = 0; i < train.length; i++) {
Instance sample = data.instance(i);
MonoDoubleItemSet[] sequence = new MonoDoubleItemSet[sample.numAttributes() - 1];
maxLength = Math.max(maxLength, sequence.length);
int shift = (sample.classIndex() == 0) ? 1 : 0;
for (int t = 0; t < sequence.length; t++) {
sequence[t] = new MonoDoubleItemSet(sample.value(t + shift));
}
train[i] = new SymbolicSequence(sequence);
String clas = sample.stringValue(classAttribute);
classMap[i] = clas;
classedData.get(clas).add(train[i]);
classedDataIndices.get(clas).add(i);
}
warpingMatrix = new double[maxLength][maxLength];
U = new double[maxLength];
L = new double[maxLength];
maxWindow = Math.round(1 * maxLength);
searchResults = new String[maxWindow+1];
nns = new int[maxWindow+1][train.length];
dist = new double[maxWindow+1][train.length];
// Start searching for the best window
searchBestWarpingWindow();
// Saving best windows found
System.out.println("Windows found=" + bestWarpingWindow + " Best Acc=" + (1-bestScore));
}
示例11: scaleDownMindists
import weka.core.Instance; //导入方法依赖的package包/类
private static ArrayList<Attribute> scaleDownMindists(Instances previousSet, Instance center){
ArrayList<Attribute> localAtts = new ArrayList<Attribute>();
int attNum = center.numAttributes();
int pos = previousSet.attribute(PerformanceAttName).index();
//traverse each dimension
Enumeration<Instance> enu;
double minDis;
for(int i=0;i<attNum;i++){
if(i==pos)
continue;
enu = previousSet.enumerateInstances();
minDis = Double.MAX_VALUE;
while(enu.hasMoreElements()){
Instance ins = enu.nextElement();
if(!ins.equals(center))
minDis = Math.min((double)((int)(Math.abs(ins.value(i)-center.value(i))*1000))/1000.0, minDis);
}
//now we set the range
Properties p1 = new Properties();
double upper = center.value(i)+minDis, lower=center.value(i)-minDis;
TreeSet<Double> detourSet = new TreeSet<Double>();
detourSet.add(upper);
detourSet.add(lower);
detourSet.add(previousSet.attribute(i).getUpperNumericBound());
detourSet.add(previousSet.attribute(i).getLowerNumericBound());
switch(detourSet.size()){
case 1:
upper=lower=detourSet.first();
break;
case 2:
upper = detourSet.last();
lower = detourSet.first();
break;
case 3:
upper=lower=detourSet.higher(detourSet.first());
break;
default://case 4:
upper=detourSet.lower(detourSet.last());
lower=detourSet.higher(detourSet.first());
break;
}
p1.setProperty("range", "["+String.valueOf(lower)+","+String.valueOf(upper)+"]");
ProtectedProperties prop1 = new ProtectedProperties(p1);
localAtts.add(new Attribute(previousSet.attribute(i).name(), prop1));
}
return localAtts;
}
示例12: scaleDownDetour
import weka.core.Instance; //导入方法依赖的package包/类
public static ArrayList<Attribute> scaleDownDetour(Instances previousSet, Instance center){
ArrayList<Attribute> localAtts = new ArrayList<Attribute>();
int attNum = center.numAttributes();
int pos = previousSet.attribute(PerformanceAttName).index();
//traverse each dimension
Enumeration<Instance> enu;
double minDis;
for(int i=0;i<attNum;i++){
if(i==pos)
continue;
enu = previousSet.enumerateInstances();
minDis = Double.MAX_VALUE;
while(enu.hasMoreElements()){
Instance ins = enu.nextElement();
if(!ins.equals(center))
minDis = Math.min((double)((int)(Math.abs(ins.value(i)-center.value(i))*100))/100.0, minDis);
}
//now we set the range
Properties p1 = new Properties();
double upper = center.value(i)+minDis, lower=center.value(i)-minDis;
TreeSet<Double> detourSet = new TreeSet<Double>();
detourSet.add(upper);
detourSet.add(lower);
detourSet.add(previousSet.attribute(i).getUpperNumericBound());
detourSet.add(previousSet.attribute(i).getLowerNumericBound());
switch(detourSet.size()){
case 1:
upper=lower=detourSet.first();
break;
case 2:
upper = detourSet.last();
lower = detourSet.first();
break;
case 3:
upper=lower=detourSet.higher(detourSet.first());
break;
default://case 4:
upper=detourSet.lower(detourSet.last());
lower=detourSet.higher(detourSet.first());
break;
}
p1.setProperty("range", "["+String.valueOf(lower)+","+String.valueOf(upper)+"]");
ProtectedProperties prop1 = new ProtectedProperties(p1);
localAtts.add(new Attribute(previousSet.attribute(i).name(), prop1));
}
return localAtts;
}
示例13: runExp
import weka.core.Instance; //导入方法依赖的package包/类
public Instances runExp(Instances samplePoints, String perfAttName){
Instances retVal = null;
if(samplePoints.attribute(perfAttName) == null){
Attribute performance = new Attribute(perfAttName);
samplePoints.insertAttributeAt(performance, samplePoints.numAttributes());
}
int pos = samplePoints.numInstances();
int count = 0;
for (int i = 0; i < pos; i++) {
Instance ins = samplePoints.get(i);
HashMap hm = new HashMap();
int tot = 0;
for (int j = 0; j < ins.numAttributes(); j++) {
hm.put(ins.attribute(j).name(), ins.value(ins.attribute(j)));
}
boolean testRet;
if (Double.isNaN(ins.value(ins.attribute(ins.numAttributes() - 1)))) {
testRet = this.startTest(hm, i, isInterrupt);
double y = 0;
if (!testRet) {// the setting does not work, we skip it
y = -1;
count++;
if (count >= targetTestErrorNum) {
System.out.println("There must be somthing wrong with the system. Please check and restart.....");
System.exit(1);
}
} else {
y = getPerformanceByType(performanceType);
count = 0;
}
ins.setValue(samplePoints.numAttributes() - 1, y);
writePerfstoFile(ins);
} else {
continue;
}
}
retVal = samplePoints;
retVal.setClassIndex(retVal.numAttributes()-1);
return retVal;
}
示例14: scaleDownNeighbordists
import weka.core.Instance; //导入方法依赖的package包/类
private static ArrayList<Attribute> scaleDownNeighbordists(Instances previousSet, Instance center){
ArrayList<Attribute> localAtts = new ArrayList<Attribute>();
int attNum = center.numAttributes();
int pos = -1;
if(previousSet.attribute(PerformanceAttName)!=null)
pos = previousSet.attribute(PerformanceAttName).index();
//traverse each dimension
Enumeration<Instance> enu;
double[] minDists = new double[2];
double val;
for(int i=0;i<attNum;i++){
if(i==pos)
continue;
enu = previousSet.enumerateInstances();
minDists[0] = 1-Double.MAX_VALUE;
minDists[1] = Double.MAX_VALUE;
while(enu.hasMoreElements()){
Instance ins = enu.nextElement();
if(!ins.equals(center)){
val = ins.value(i)-center.value(i);
if(val<0)
minDists[0] = Math.max((double)((int)((ins.value(i)-center.value(i))*1000))/1000.0, minDists[0]);
else
minDists[1] = Math.min((double)((int)((ins.value(i)-center.value(i))*1000))/1000.0, minDists[1]);
}
}
//now we set the range
Properties p1 = new Properties();
double upper = center.value(i)+minDists[1], lower=center.value(i)+minDists[0];
TreeSet<Double> detourSet = new TreeSet<Double>();
detourSet.add(upper);
detourSet.add(lower);
detourSet.add(previousSet.attribute(i).getUpperNumericBound());
detourSet.add(previousSet.attribute(i).getLowerNumericBound());
switch(detourSet.size()){
case 1:
upper=lower=detourSet.first();
break;
case 2:
upper = detourSet.last();
lower = detourSet.first();
break;
case 3:
upper=lower=detourSet.higher(detourSet.first());
break;
default://case 4:
upper=detourSet.lower(detourSet.last());
lower=detourSet.higher(detourSet.first());
break;
}
p1.setProperty("range", "["+String.valueOf(lower)+","+String.valueOf(upper)+"]");
ProtectedProperties prop1 = new ProtectedProperties(p1);
localAtts.add(new Attribute(previousSet.attribute(i).name(), prop1));
}
return localAtts;
}