Java机器学习库ML之二Feature Selection(特征选择)
生活随笔
收集整理的這篇文章主要介紹了
Java机器学习库ML之二Feature Selection(特征选择)
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
機(jī)器學(xué)習(xí)中訓(xùn)練模型的前提必備工作就是特征選擇,起到降維和降低計(jì)算開銷的目的,當(dāng)然在獲取盡可能小的特征子集的同時(shí),應(yīng)不顯著降低分類精度、不影響類分布、保持特征子集的穩(wěn)定適應(yīng)性強(qiáng)等。
ML庫提供了特征選擇方法,具體有:
1、遞歸特征消除 Recursive feature elimination (RFE):
遞歸特征消除的主要思想是反復(fù)的構(gòu)建模型(如SVM或者回歸模型)然后選出最好的(或者最差的)的特征(可以根據(jù)系數(shù)來選),把選出來的特征排除,然后在剩余的特征上重復(fù)這個(gè)過程,直到所有特征都遍歷了。這個(gè)過程中特征被消除的次序就是特征的排序。因此,這是一種尋找最優(yōu)特征子集的貪心算法。參考代碼如下:
2、Pearson相關(guān)系數(shù) Pearson Correlation
皮爾森相關(guān)系數(shù)是體現(xiàn)特征和響應(yīng)變量之間關(guān)系的方法,該方法衡量的是變量之間的線性相關(guān)性,結(jié)果的取值區(qū)間為[-1,1],-1表示完全的負(fù)相關(guān)(這個(gè)變量下降,那個(gè)就會(huì)上升),+1表示完全的正相關(guān),0表示沒有線性相關(guān)。參考代碼如下: package com.gddx;import java.io.File;import net.sf.javaml.core.Dataset; import net.sf.javaml.distance.PearsonCorrelationCoefficient; import net.sf.javaml.featureselection.subset.GreedyForwardSelection; import net.sf.javaml.tools.data.FileHandler;/*** Shows the basic steps to create use a feature subset selection algorithm.* * @author Thomas Abeel* */ public class TutorialFeatureSubsetSelection {public static void main(String[] args) throws Exception {/* Load the iris data set */Dataset data = FileHandler.loadDataset(new File("D:\\tmp\\iris.data"), 4, ",");/** Construct a greedy forward subset selector that will use the Pearson* correlation to determine the relation between each attribute and the* class label. The first parameter indicates that only one, i.e. 'the* best' attribute will be selected.*/GreedyForwardSelection ga = new GreedyForwardSelection(1, new PearsonCorrelationCoefficient());/* Apply the algorithm to the data set */ga.build(data);/* Print out the attribute that has been selected */System.out.println(ga.selectedAttributes());} }
3、集成特征選擇
? ? 基于模型排序后的集成,參考代碼如下:
??
package com.gddx;import java.io.File;import net.sf.javaml.core.Dataset; import net.sf.javaml.featureselection.ensemble.LinearRankingEnsemble; import net.sf.javaml.featureselection.ranking.RecursiveFeatureEliminationSVM; import net.sf.javaml.tools.data.FileHandler;/*** Tutorial to illustrate ensemble feature selection.* * @author Thomas Abeel* */ public class TutorialEnsembleFeatureSelection {/*** Shows the basic steps to use ensemble feature selection* * @author Thomas Abeel* */public static void main(String[] args) throws Exception {/* Load the iris data set */Dataset data = FileHandler.loadDataset(new File("D:\\tmp\\iris.data"), 4, ",");/* Create a feature ranking algorithm */RecursiveFeatureEliminationSVM[] svmrfes = new RecursiveFeatureEliminationSVM[10];for (int i = 0; i < svmrfes.length; i++)svmrfes[i] = new RecursiveFeatureEliminationSVM(0.2);LinearRankingEnsemble ensemble = new LinearRankingEnsemble(svmrfes);/* Build the ensemble */ensemble.build(data);/* Print out the rank of each attribute */for (int i = 0; i < ensemble.noAttributes(); i++)System.out.println(ensemble.rank(i));}}4、特征評分:
package com.gddx;import java.io.File;import net.sf.javaml.core.Dataset; import net.sf.javaml.featureselection.scoring.GainRatio; import net.sf.javaml.tools.data.FileHandler;public class TutorialFeatureScoring {/*** Shows the basic steps to create use a feature scoring algorithm.* * @author Thomas Abeel* */public static void main(String[] args) throws Exception {/* Load the iris data set */Dataset data = FileHandler.loadDataset(new File("D:\\tmp\\iris.data"), 4, ",");GainRatio ga = new GainRatio();/* Apply the algorithm to the data set */ga.build(data);/* Print out the score of each attribute */for (int i = 0; i < ga.noAttributes(); i++)System.out.println(ga.score(i));}}5、WekaAttributeSelection,這個(gè)主要還是用增益來選擇特征,應(yīng)該在輸出上包括排序和分?jǐn)?shù),參考代碼如下:
package com.gddx;/*** This file is part of the Java Machine Learning Library* * The Java Machine Learning Library is free software; you can redistribute it and/or modify* it under the terms of the GNU General Public License as published by* the Free Software Foundation; either version 2 of the License, or* (at your option) any later version.* * The Java Machine Learning Library is distributed in the hope that it will be useful,* but WITHOUT ANY WARRANTY; without even the implied warranty of* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the* GNU General Public License for more details.* * You should have received a copy of the GNU General Public License* along with the Java Machine Learning Library; if not, write to the Free Software* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA* * Copyright (c) 2006-2012, Thomas Abeel* * Project: http://java-ml.sourceforge.net/* */ import java.io.File; import java.io.IOException;import net.sf.javaml.core.Dataset; import net.sf.javaml.tools.data.FileHandler; import net.sf.javaml.tools.weka.WekaAttributeSelection; import weka.attributeSelection.ASEvaluation; import weka.attributeSelection.ASSearch; import weka.attributeSelection.GainRatioAttributeEval; import weka.attributeSelection.Ranker;/*** Tutorial how to use the Bridge to WEKA AS Evaluation , AS Search and* Evaluator algorithms in Java-ML* * * @author Irwan Krisna*/ public class TutorialWekaAttributeSelection {public static void main(String[] args) throws IOException {/* Load data */Dataset data = FileHandler.loadDataset(new File("D:\\tmp\\iris.data"),4, ",");/* Create a AS Evaluation algorithm */ASEvaluation eval = new GainRatioAttributeEval();/* Create a Weka's AS Search algorithm */ASSearch search = new Ranker();/* Wrap Wekas' Algorithms in bridge */WekaAttributeSelection wekaattrsel = new WekaAttributeSelection(eval,search);/** to apply algorithm to the data set and generate the new data based on* the given parameters*/wekaattrsel.build(data);/* to retrieve the number of attributes */System.out.println("Total number of attributes: "+ wekaattrsel.noAttributes());/* to display all the rank and score for each attribute */for (int i = 0; i < wekaattrsel.noAttributes() - 1; i++) {System.out.println("Attribute " + i + " Ranks "+ wekaattrsel.rank(i) + " and Scores "+ wekaattrsel.score(i));}}}
總結(jié)
以上是生活随笔為你收集整理的Java机器学习库ML之二Feature Selection(特征选择)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Java机器学习库ML之一Dataset
- 下一篇: 机器学习知识点(三十七)特征选择方法总结