资源简介

weka java 局部加权朴素贝叶斯 数据挖掘 机器学习

资源截图

代码片段和文件信息

package weka.classifiers.xy;

import weka.classifiers.Classifier;
import weka.core.Instance;
import weka.core.Instances;
import weka.core.Utils;

public class LWNB extends Classifier{
int K=30;
    public String[] getOptions() {
     String [] options = new String [1];
     options[0]=“K-number“;
     return options;
    }
     public int getNumber(){
     return K;
     }
     public void setNumber(int number){
     K=number;
     }

Instances train_set;
private int m_NumClasses;
private int m_numInstances;
private int m_numAttribute;

public void buildClassifier(Instances instances) throws Exception {
train_set=new Instances(instances);
m_NumClasses=instances.numClasses();
m_numInstances=instances.numInstances();
m_numAttribute=instances.numAttributes();
}

public double [] distributionForInstance(Instance instance)throws Exception {
double weight[]=new double[K];
double[]distances=calculateDistance(instancetrain_set);
double temp=0;
for(int i=0;i for(int j=i+1;j if(distances[i]>distances[j]){
temp=distances[i];
distances[i]=distances[j];
distances[j]=temp;
train_set.swap(i j);
}
}
}
Instances newinstances=new Instances(train_set0);
for(int i=0;i newinstances.add(train_set.instance(i));
}
double total_weight=0;
for(int i=0;i distances[i]/=distances[K-1];
distances[i]=1-distances[i];
total_weight+=distances[i];
}
for(int i=0;i weight[i]=(distances[i]*K)/total_weight;
}
m_numInstances=newinstances.numInstances();
m_NumClasses=newinstances.numClasses();
double [] probs=new double[m_NumClasses];//p(c|a)
double prob_c[]=new double[m_NumClasses];//p(c)
for(int i=0;i double t_same=0;
double t_weight=0;
for(int j=0;j if((int)newinstances.instance(j).classValue()==i){
t_same+=weight[j];
}
t_weight+=weight[j];
}
prob_c[i]=((1+t_same)/(m_NumClasses+t_weight));
}
// for(int i=0;i// prob_c[i]/=m_NumClasses;
// }
double[][]class_attri=new double[m_numAttribute-1][m_NumClasses];//p(a|c)
for(int i=0;i int num_attri=newinstances.attribute(i).numValues();
double t_same=0;
double t_weight=0;
for(int j=0;j for(int k=0;k if((int)instance.value(i)==(int)newinstances.instance(k).value(i)&&(int)newinstances.instance(k).classValue()==j){
t_same+=weight[k];
}
if((int)newinstances.instance(k).classValue()==j){
t_weight+=weight[k];
}
}
class

评论

共有 条评论