java naive方法_Java NaiveBayes.buildClassifier方法代码示例

import weka.classifiers.bayes.NaiveBayes; //导入方法依赖的package包/类

/**

* Generates the classifier.

*

* @param data set of instances serving as training data

* @throws Exception if the classifier has not been generated successfully

*/

public void buildClassifier(Instances data) throws Exception {

m_saveMemory = false;

if (data.classAttribute().isNumeric()) {

throw new Exception("Can only handle nominal class!");

}

if (m_backwardWithDelete == null) {

setUpSearch();

m_search = m_backwardWithDelete;

}

/* if (m_search != m_backwardWithDelete) {

m_search = m_backwardWithDelete;

} */

super.buildClassifier(data);

// new NB stuff

// delete the features used by the decision table (not the class!!)

for (int i = 0; i < m_theInstances.numAttributes(); i++) {

m_theInstances.attribute(i).setWeight(1.0); // reset all weights

}

// m_nbFeatures = new int [m_decisionFeatures.length - 1];

int count = 0;

for (int i = 0; i < m_decisionFeatures.length; i++) {

if (m_decisionFeatures[i] != m_theInstances.classIndex()) {

count++;

//m_nbFeatures[count++] = m_decisionFeatures[i];

m_theInstances.attribute(m_decisionFeatures[i]).setWeight(0.0); // No influence for NB

}

}

double numDeleted = 0;

// remove any attributes that have been deleted completely from the DTNB

BitSet deleted = ((EvalWithDelete)m_evaluator).getDeletedList();

for (int i = 0; i < m_theInstances.numAttributes(); i++) {

if (deleted.get(i)) {

m_theInstances.attribute(i).setWeight(0.0);

// count--;

numDeleted++;

// System.err.println("Attribute "+i+" was eliminated completely");

}

}

m_percentUsedByDT = (double)count / (m_theInstances.numAttributes() - 1);

m_percentDeleted = numDeleted / (m_theInstances.numAttributes() -1);

m_NB = new NaiveBayes();

m_NB.buildClassifier(m_theInstances);

m_dtInstances = new Instances(m_dtInstances, 0);

m_theInstances = new Instances(m_theInstances, 0);

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值