日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

Java机器学习库ML之二Feature Selection(特征选择)

發布時間:2025/4/16 java 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Java机器学习库ML之二Feature Selection(特征选择) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

機器學習中訓練模型的前提必備工作就是特征選擇,起到降維和降低計算開銷的目的,當然在獲取盡可能小的特征子集的同時,應不顯著降低分類精度、不影響類分布、保持特征子集的穩定適應性強等。

ML庫提供了特征選擇方法,具體有:

1、遞歸特征消除 Recursive feature elimination (RFE):

遞歸特征消除的主要思想是反復的構建模型(如SVM或者回歸模型)然后選出最好的(或者最差的)的特征(可以根據系數來選),把選出來的特征排除,然后在剩余的特征上重復這個過程,直到所有特征都遍歷了。這個過程中特征被消除的次序就是特征的排序。因此,這是一種尋找最優特征子集的貪心算法。參考代碼如下:

package com.gddx;import java.io.File;import net.sf.javaml.core.Dataset; import net.sf.javaml.featureselection.ranking.RecursiveFeatureEliminationSVM; import net.sf.javaml.tools.data.FileHandler;public class TutorialFeatureRanking {/*** Shows the basic steps to create use a feature ranking 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, ",");/* Create a feature ranking algorithm */RecursiveFeatureEliminationSVM svmrfe = new RecursiveFeatureEliminationSVM(0.2);/* Apply the algorithm to the data set */svmrfe.build(data);/* Print out the rank of each attribute */for (int i = 0; i < svmrfe.noAttributes(); i++)System.out.println(svmrfe.rank(i));}}
2、Pearson相關系數 Pearson Correlation

皮爾森相關系數是體現特征和響應變量之間關系的方法,該方法衡量的是變量之間的線性相關性,結果的取值區間為[-1,1],-1表示完全的負相關(這個變量下降,那個就會上升),+1表示完全的正相關,0表示沒有線性相關。參考代碼如下:

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,這個主要還是用增益來選擇特征,應該在輸出上包括排序和分數,參考代碼如下:

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));}}}




《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀

總結

以上是生活随笔為你收集整理的Java机器学习库ML之二Feature Selection(特征选择)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。