本文整理汇总了Java中weka.core.Instances.randomize方法的典型用法代码示例。如果您正苦于以下问题:Java Instances.randomize方法的具体用法?Java Instances.randomize怎么用?Java Instances.randomize使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类weka.core.Instances
的用法示例。
在下文中一共展示了Instances.randomize方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: main
import weka.core.Instances; //导入方法依赖的package包/类
public static void main(String[] args){
GRules GRulesLearner = new GRules();
try {
ConverterUtils.DataSource source = new ConverterUtils.DataSource("data/car.arff");
Instances originalDataset = source.getDataSet();
// split data 80/20
int trainSize = (int) Math.round(originalDataset.numInstances() * 0.8
/ 100);
int testSize = originalDataset.numInstances() - trainSize;
Instances train = new Instances(originalDataset, 0, trainSize);
Instances test = new Instances(originalDataset, trainSize, testSize);
train.randomize(new java.util.Random(0));
// train the rules learner
List<List<Term>> rightSideRules = GRulesLearner.learnRightSide(train);
List<Rule> completedRules = GRulesLearner.induceCompleteRules(rightSideRules, originalDataset);
// try to predict an instance
System.out.println("A testing instance: ");
System.out.println(test.get(10));
System.out.println("A rule covered the instance: ");
System.out.println(GRulesLearner.predict(test.get(10), completedRules).nicePrint());
} catch (Exception ex) {
System.err.println(ex.toString());
}
}
示例2: generateARFF
import weka.core.Instances; //导入方法依赖的package包/类
/**<p>Generate Random sample according to random seed on Desktop, each sample has the same distribution of InTrace/OutTrace
* and have <b>SIZE</b> instances.
* </p>
* @param path original arff file to be sampled in path
* @param rand random seed
* @param num the number of selection
* */
public static void generateARFF(String path, int rand, int num) throws Exception{
/*** original dataset reading */
Instances data = DataSource.read(path);
data.setClassIndex(data.numAttributes()-1);
/*** randomize the dataset */
data.randomize(new Random(rand));
/*** dataIn to save instances of InTrace class */
Instances dataIn = new Instances("dataIn", InsMerge.getStandAttrs(), 1);
dataIn.setClassIndex(dataIn.numAttributes() - 1);
/*** dataOut to save instances of OutTrace class */
Instances dataOut = new Instances("dataOut", InsMerge.getStandAttrs(), 1);
dataIn.setClassIndex(dataIn.numAttributes() - 1);
/*** add OutTrace instances into dataOut */
for(int i=0; i<data.numInstances(); i++){
if(data.get(i).stringValue(data.get(i).classAttribute()).equals("OutTrace")){
dataOut.add(data.get(i));
}
}
/** add InTrace instances into dataIn */
for(int i=0; i<data.numInstances(); i++){
if(data.get(i).stringValue(data.get(i).classAttribute()).equals("InTrace")){
dataIn.add(data.get(i));
}
}
/*** get the In/Out ratio in original dataset */
int inTrace = dataIn.numInstances();
int outTrace = dataOut.numInstances();
double ratioI = inTrace*1.0/(outTrace + inTrace);
/*** expected number to select from original dataset*/
int intrace = (int) (num * ratioI);
int outtrace = num - intrace;
/** create new generated dataset train*/
Instances train = new Instances("dataIn", InsMerge.getStandAttrs(), 1);
train.setClassIndex(train.numAttributes() - 1);
/** train get X instances from dataIn*/
for(int i=0; i<intrace; i++){
train.add(dataIn.get(i));
}
/** train get Y instances from dataOut*/
for(int j=0; j<outtrace; j++){
train.add(dataOut.get(j));
}
/** save the dataset in path, we save the arff into D:/Users/LEE/Desktop/New_Data/XXX.arff */
String filename = "files/generated/" + filterName(path) + rand + ".arff";
DataSink.write(filename, train);
}
示例3: showFolds
import weka.core.Instances; //导入方法依赖的package包/类
/***
* <p>Using Feature Selection method to get 10 folds results in the given project</p>
* @param path project path
* @param sel label means we use the Feature Selection method
* @throws Exception
*/
public static void showFolds(String path, int k, int flag) throws Exception{
Instances data1 = DataSource.read(path);
data1.setClassIndex(data1.numAttributes()-1);
if(!data1.classAttribute().isNominal()) // in case of noisy data, return
return;
/** Feature Selection: Correlation */
CorrelationAttributeEval evall = new CorrelationAttributeEval();
Ranker ranker = new Ranker();
AttributeSelection selector = new AttributeSelection();
selector.setEvaluator(evall);
selector.setSearch(ranker);
selector.setInputFormat(data1);
data1 = Filter.useFilter(data1, selector);
/** Randomize and stratify the dataset*/
data1.randomize(new Random(1));
data1.stratify(10); // 10 folds
double[][] matrix = new double[10][7];
for(int i=0; i<10; i++){ // To calculate the results in each fold
Instances test = data1.testCV(10, i);
Instances train = data1.trainCV(10, i);
/** SMOTE */
SMOTE smote = new SMOTE();
smote.setInputFormat(train);
train = Filter.useFilter(train, smote);
/** C4.5 */
J48 rf = new J48();
// RandomForest rf = new RandomForest();
// rf.setNumIterations(300);
rf.buildClassifier(train);
Evaluation eval = new Evaluation(train);
eval.evaluateModel(rf, test);
matrix[i][6] = 1-eval.errorRate();
matrix[i][0] = eval.precision(0);
matrix[i][1] = eval.recall(0);
matrix[i][2] = eval.fMeasure(0);
matrix[i][3] = eval.precision(1);
matrix[i][4] = eval.recall(1);
matrix[i][5] = eval.fMeasure(1);
}
for(int i=0;i<10;i++){
for(int j=0;j<7;j++){
System.out.printf("%15.8f", matrix[i][j]);
}
System.out.println("");
}
}
示例4: random
import weka.core.Instances; //导入方法依赖的package包/类
/**
* Randomize the dataset
* @param data
* @return
*/
public static Instances random(Instances data) {
data.randomize(new Random());
return data;
}