List of usage examples for weka.attributeSelection Ranker Ranker
public Ranker()
From source file:FeatureSelectionClass.java
public AttributeSelection withGainRatio(String path) throws Exception { int N;/*from w w w. j a v a 2 s. c o m*/ PreparingSteps pr = new PreparingSteps(); N = pr.getReadFileData(path).numAttributes(); Instances data = pr.getReadFileData(path); AttributeSelection selector = new AttributeSelection(); InfoGainAttributeEval evaluator = new InfoGainAttributeEval(); Ranker ranker = new Ranker(); ranker.setNumToSelect(Math.min(500, N - 1)); selector.setEvaluator(evaluator); selector.setSearch(ranker); selector.SelectAttributes(data); return selector; }
From source file:FeatureSelectionClass.java
public AttributeSelection withInfoGain(String path) throws Exception { int N;//from w w w . ja v a 2 s . c o m PreparingSteps pr = new PreparingSteps(); N = pr.getReadFileData(path).numAttributes(); Instances data = pr.getReadFileData(path); AttributeSelection selector = new AttributeSelection(); GainRatioAttributeEval evaluator = new GainRatioAttributeEval(); Ranker ranker = new Ranker(); ranker.setNumToSelect(Math.min(500, N - 1)); selector.setEvaluator(evaluator); selector.setSearch(ranker); selector.SelectAttributes(data); return selector; }
From source file:FeatureSelectionClass.java
public AttributeSelection withChiSquare(String path) throws Exception { int N;// ww w. j a va 2 s.co m PreparingSteps pr = new PreparingSteps(); N = pr.getReadFileData(path).numAttributes(); Instances data = pr.getReadFileData(path); AttributeSelection selector = new AttributeSelection(); ChiSquaredAttributeEval evaluator = new ChiSquaredAttributeEval(); Ranker ranker = new Ranker(); ranker.setNumToSelect(Math.min(500, N - 1)); selector.setEvaluator(evaluator); selector.setSearch(ranker); selector.SelectAttributes(data); return selector; }
From source file:PCADetector.java
License:Apache License
public boolean runPCA(ArrayList<Double> newData, int slidewdSz, double cAlpha, int nAttrs) { try {/*w w w .ja va 2 s . c o m*/ if (m_nDims == 0) { m_nDims = nAttrs; for (int i = 0; i < this.m_nDims; i++) { m_oriDataMatrix.add(new ArrayList<Double>()); // one list for each attribute } } verifyData(newData); this.c_alpha = cAlpha; if (false == prepareData(newData, slidewdSz)) return false; Instances oriDataInsts = getInstances(); if (oriDataInsts != null) { // standardization + PCA covariance matrix m_scaledInstances = new Instances(oriDataInsts); Standardize filter = new Standardize(); filter.setInputFormat(m_scaledInstances); m_scaledInstances = Standardize.useFilter(m_scaledInstances, filter); // standardization PrincipalComponents PCA = new PrincipalComponents(); PCA.setVarianceCovered(1.0); // means 100% PCA.setMaximumAttributeNames(-1); PCA.setCenterData(true); Ranker ranker = new Ranker(); AttributeSelection selector = new AttributeSelection(); selector.setSearch(ranker); selector.setEvaluator(PCA); selector.SelectAttributes(m_scaledInstances); // Instances transformedData = selector.reduceDimensionality(m_scaledInstances); // get sorted eigens double[] eigenValues = PCA.getEigenValues(); // eigenVectors[i][j] i: rows; j: cols double[][] eigenVectors = PCA.getUnsortedEigenVectors(); Sort(eigenValues, eigenVectors); setEigens(eigenValues); // get residual start dimension int residualStartDimension = -1; double sum = 0; double major = 0; for (int ss = 0; ss < eigenValues.length; ss++) { sum += eigenValues[ss]; } for (int ss = 0; ss < eigenValues.length; ss++) { major += eigenValues[ss]; if ((residualStartDimension < 0) && (major / sum > 0.95)) { residualStartDimension = ss + 1; break; } } // System.out.println("residualStartDim: "+residualStartDimension); m_threshold = computeThreshold(eigenValues, residualStartDimension); // check new data abnormal or not boolean bAbnormal = checkSPE(eigenVectors, residualStartDimension, newData); computeProjPCs(eigenVectors, residualStartDimension, newData); // only for demo if (bAbnormal) { // anomaly, now to diagnosis // check original space using all the lists diagnosis(eigenVectors, residualStartDimension, newData); } } } catch (Exception exc) { } return true; }
From source file:ca.uottawa.balie.WekaAttributeSelection.java
License:Open Source License
/** * Select the top attributes/*from www . j a v a 2s. c o m*/ */ public void Select(boolean pi_Debug) { Instances insts = m_DummyLearner.GetTrainInstances(); try { ASEvaluation eval = null; ASSearch search = null; if (m_Evaluator == WEKA_CHI_SQUARE) { eval = new ChiSquaredAttributeEval(); search = new Ranker(); ((Ranker) search).setNumToSelect(m_NumAttributes); } else if (m_Evaluator == WEKA_INFO_GAIN) { eval = new InfoGainAttributeEval(); search = new Ranker(); ((Ranker) search).setNumToSelect(m_NumAttributes); } else if (m_Evaluator == WEKA_WRAPPER) { eval = new ClassifierSubsetEval(); ((ClassifierSubsetEval) eval).setClassifier(new NaiveBayes()); search = new Ranker(); // TODO: use something else than ranker ((Ranker) search).setNumToSelect(m_NumAttributes); } else if (m_Evaluator == WEKA_SYM_UNCERT) { eval = new SymmetricalUncertAttributeEval(); search = new Ranker(); ((Ranker) search).setNumToSelect(m_NumAttributes); } else if (m_Evaluator == WEKA_SVM) { eval = new SVMAttributeEval(); search = new Ranker(); ((Ranker) search).setNumToSelect(m_NumAttributes); } else if (m_Evaluator == WEKA_RELIEF) { eval = new ReliefFAttributeEval(); search = new Ranker(); ((Ranker) search).setNumToSelect(m_NumAttributes); } else if (m_Evaluator == WEKA_ONER) { eval = new OneRAttributeEval(); search = new Ranker(); ((Ranker) search).setNumToSelect(m_NumAttributes); } m_AttributeSelection = new AttributeSelection(); m_AttributeSelection.setEvaluator(eval); m_AttributeSelection.setSearch(search); m_AttributeSelection.SelectAttributes(insts); if (pi_Debug) System.out.println(m_AttributeSelection.toResultsString()); } catch (Exception e) { System.err.println(e.getMessage()); } }
From source file:com.ivanrf.smsspam.SpamClassifier.java
License:Apache License
private static FilteredClassifier initFilterClassifier(int wordsToKeep, String tokenizerOp, boolean useAttributeSelection, String classifierOp, boolean boosting) throws Exception { StringToWordVector filter = new StringToWordVector(); filter.setDoNotOperateOnPerClassBasis(true); filter.setLowerCaseTokens(true);//from ww w. ja v a 2 s . com filter.setWordsToKeep(wordsToKeep); if (!tokenizerOp.equals(TOKENIZER_DEFAULT)) { //Make a tokenizer WordTokenizer wt = new WordTokenizer(); if (tokenizerOp.equals(TOKENIZER_COMPLETE)) wt.setDelimiters(" \r\n\t.,;:\'\"()?!-+*&#$%/=<>[]_`@\\^{}"); else //TOKENIZER_COMPLETE_NUMBERS) wt.setDelimiters(" \r\n\t.,;:\'\"()?!-+*&#$%/=<>[]_`@\\^{}|~0123456789"); filter.setTokenizer(wt); } FilteredClassifier classifier = new FilteredClassifier(); classifier.setFilter(filter); if (useAttributeSelection) { AttributeSelection as = new AttributeSelection(); as.setEvaluator(new InfoGainAttributeEval()); Ranker r = new Ranker(); r.setThreshold(0); as.setSearch(r); MultiFilter mf = new MultiFilter(); mf.setFilters(new Filter[] { filter, as }); classifier.setFilter(mf); } if (classifierOp.equals(CLASSIFIER_SMO)) classifier.setClassifier(new SMO()); else if (classifierOp.equals(CLASSIFIER_NB)) classifier.setClassifier(new NaiveBayes()); else if (classifierOp.equals(CLASSIFIER_IB1)) classifier.setClassifier(new IBk(1)); else if (classifierOp.equals(CLASSIFIER_IB3)) classifier.setClassifier(new IBk(3)); else if (classifierOp.equals(CLASSIFIER_IB5)) classifier.setClassifier(new IBk(5)); else if (classifierOp.equals(CLASSIFIER_PART)) classifier.setClassifier(new PART()); //Tarda mucho if (boosting) { AdaBoostM1 boost = new AdaBoostM1(); boost.setClassifier(classifier.getClassifier()); classifier.setClassifier(boost); //Con NB tarda mucho } return classifier; }
From source file:es.uvigo.ei.sing.gc.model.entities.GeneSelectionMetaData.java
License:Open Source License
public FeatureSelector createFeatureSelection() throws Exception { final Ranker ranker = new Ranker(); ranker.setNumToSelect(this.numGenes); return new WekaFeatureSelector(this.getASEvaluationBuilder().buildEvaluator(), ranker); }
From source file:etc.aloe.oilspill2010.FeatureGenerationImpl.java
License:Open Source License
protected Filter getFeatureSelectionFilter(ExampleSet examples) throws Exception { AttributeSelection filter = new AttributeSelection(); // package weka.filters.supervised.attribute! //CfsSubsetEval eval = new CfsSubsetEval(); //CorrelationAttributeEval eval = new CorrelationAttributeEval(); //InfoGainAttributeEval eval = new InfoGainAttributeEval(); ReliefFAttributeEval eval = new ReliefFAttributeEval(); //GreedyStepwise search = new GreedyStepwise(); //search.setNumToSelect(980); //search.setSearchBackwards(true); Ranker search = new Ranker(); search.setNumToSelect(980);//from w w w . ja va 2s. c o m filter.setEvaluator(eval); filter.setSearch(search); filter.setInputFormat(examples.getInstances()); Instances filtered = Filter.useFilter(examples.getInstances(), filter); examples.setInstances(filtered); return filter; }
From source file:Helper.CustomFilter.java
public Instances removeAttribute(Instances structure) throws Exception { //NORMALIZE AND REMOVE USELESS ATTRIBUTES Normalize norm = new Normalize(); norm.setInputFormat(structure);/*from w ww. j a v a2s .c om*/ structure = Filter.useFilter(structure, norm); RemoveUseless ru = new RemoveUseless(); ru.setInputFormat(structure); structure = Filter.useFilter(structure, ru); Ranker rank = new Ranker(); InfoGainAttributeEval eval = new InfoGainAttributeEval(); eval.buildEvaluator(structure); //END OF NORMALIZATION return structure; }
From source file:ia02classificacao.IA02Classificacao.java
/** * @param args the command line arguments *//*w w w . ja va2 s .co m*/ public static void main(String[] args) throws Exception { // abre o banco de dados arff e mostra a quantidade de instancias (linhas) DataSource arquivo = new DataSource("data/zoo.arff"); Instances dados = arquivo.getDataSet(); System.out.println("Instancias lidas: " + dados.numInstances()); // FILTER: remove o atributo nome do animal da classificao String[] parametros = new String[] { "-R", "1" }; Remove filtro = new Remove(); filtro.setOptions(parametros); filtro.setInputFormat(dados); dados = Filter.useFilter(dados, filtro); AttributeSelection selAtributo = new AttributeSelection(); InfoGainAttributeEval avaliador = new InfoGainAttributeEval(); Ranker busca = new Ranker(); selAtributo.setEvaluator(avaliador); selAtributo.setSearch(busca); selAtributo.SelectAttributes(dados); int[] indices = selAtributo.selectedAttributes(); System.out.println("Selected attributes: " + Utils.arrayToString(indices)); // Usa o algoritimo J48 e mostra a classificao dos dados em forma textual String[] opcoes = new String[1]; opcoes[0] = "-U"; J48 arvore = new J48(); arvore.setOptions(opcoes); arvore.buildClassifier(dados); System.out.println(arvore); // Usa o algoritimo J48 e mostra a classificao de dados em forma grafica /* TreeVisualizer tv = new TreeVisualizer(null, arvore.graph(), new PlaceNode2()); JFrame frame = new javax.swing.JFrame("?rvore de Conhecimento"); frame.setSize(800,500); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().add(tv); frame.setVisible(true); tv.fitToScreen(); */ /* * Classificao de novos dados */ System.out.println("\n\nCLASSIFICAO DE NOVOS DADOS"); // criar atributos double[] vals = new double[dados.numAttributes()]; vals[0] = 1.0; // hair vals[1] = 0.0; // feathers vals[2] = 0.0; // eggs vals[3] = 1.0; // milk vals[4] = 1.0; // airborne vals[5] = 0.0; // aquatic vals[6] = 0.0; // predator vals[7] = 1.0; // toothed vals[8] = 1.0; // backbone vals[9] = 1.0; // breathes vals[10] = 0.0; // venomous vals[11] = 0.0; // fins vals[12] = 4.0; // legs vals[13] = 1.0; // tail vals[14] = 1.0; // domestic vals[15] = 1.0; // catsize // Criar uma instncia baseada nestes atributos Instance meuUnicornio = new DenseInstance(1.0, vals); // Adicionar a instncia nos dados meuUnicornio.setDataset(dados); // Classificar esta nova instncia double label = arvore.classifyInstance(meuUnicornio); // Imprimir o resultado da classificao System.out.println("Novo Animal: Unicrnio"); System.out.println("classificacao: " + dados.classAttribute().value((int) label)); /* * Avaliao e predio de erros de mtrica */ System.out.println("\n\nAVALIAO E PREDIO DE ERROS DE MTRICA"); Classifier cl = new J48(); Evaluation eval_roc = new Evaluation(dados); eval_roc.crossValidateModel(cl, dados, 10, new Random(1), new Object[] {}); System.out.println(eval_roc.toSummaryString()); /* * Matriz de confuso */ System.out.println("\n\nMATRIZ DE CONFUSO"); double[][] confusionMatrix = eval_roc.confusionMatrix(); System.out.println(eval_roc.toMatrixString()); }