本文整理匯總了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;
}