记得组合数学正交拉丁方从0开始!
突然觉得老师说得很有道理,演化计算里活得最好的,不是最优秀的但也不是最差的,是最能适应环境的,别人怎么做,他就怎么做。
动态规划,运筹学
贝叶斯是生成学习算法,生成一个概率模型
判别学习算法
高斯判别分析
/*
* NB.java
* Copyright 2005 Liangxiao Jiang
*/
package weka.classifiers.gla;
import weka.core.*;
import weka.classifiers.*;
import static java.lang.Math.*;
/**
* Implement the NB classifier.
*/
public class myAlgorithm extends Classifier {
/** The number of class and each attribute value occurs in the dataset */
private double [][] m_ClassAttCounts;
/** The number of each class value occurs in the dataset */
private double [] m_ClassCounts;
/** The number of values for each attribute in the dataset */
private int [] m_NumAttValues;
/** The starting index of each attribute in the dataset */
private int [] m_StartAttIndex;
/** The number of values for all attributes in the dataset */
private int m_TotalAttValues;
/** The number of classes in the dataset */
private int m_NumClasses;
/** The number of attributes including class in the dataset */
private int m_NumAttributes;
/** The number of instances in the dataset */
private int m_NumInstances;
/** The index of the class attribute in the dataset */
private int m_ClassIndex;
/**
* Generates the classifier.
*
* @param instances set of instances serving as training data
* @exception Exception if the classifier has not been generated successfully
*/
public void buildClassifier(Instances instances) throws Exception {
// reset variable
m_NumClasses = instances.numClasses();
m_ClassIndex = instances.classIndex();
m_NumAttributes = instances.numAttributes();
m_NumInstances = instances.numInstances();
m_TotalAttValues = 0;
// allocate space for attribute reference arrays
m_StartAttIndex = new int[m_NumAttributes];
m_NumAttValues = new int[m_NumAttributes];
// set the starting index of each attribute and the number of values for
// each attribute and the total number of values for all attributes(not including class).
for(int i = 0; i < m_NumAttributes; i++) {
if(i != m_ClassIndex) {
m_StartAttIndex[i] = m_TotalAttValues;
m_NumAttValues[i] = instances.attribute(i).numValues();
m_TotalAttValues += m_NumAttValues[i];
}
else {
m_StartAttIndex[i] = -1;
m_NumAttValues[i] = m_NumClasses;
}
}
// allocate space for counts and frequencies
m_ClassCounts = new double[m_NumClasses];
m_ClassAttCounts = new double[m_NumClasses][m_TotalAttValues];
// Calculate the counts
for(int k = 0; k < m_NumInstances; k++) {
int classVal=(int)instances.instance(k).classValue();
m_ClassCounts[classVal] ++;
int[] attIndex = new int[m_NumAttributes];
for(int i = 0; i < m_NumAttributes; i++) {
if(i == m_ClassIndex){
attIndex[i] = -1;
}
else{
attIndex[i] = m_StartAttIndex[i] + (int)instances.instance(k).value(i);
m_ClassAttCounts[classVal][attIndex[i]]++;
}
}
}
}
/**
* Calculates the class membership probabilities for the given test instance
*
* @param instance the instance to be classified
* @return predicted class probability distribution
* @exception Exception if there is a problem generating the prediction
*/
public double [] distributionForInstance(Instance instance) throws Exception {
//Definition of local variables
double [] probs = new double[m_NumClasses];
// store instance's att values in an int array
int[] attIndex = new int[m_NumAttributes];
for(int att = 0; att < m_NumAttributes; att++) {
if(att == m_ClassIndex)
attIndex[att] = -1;
else
attIndex[att] = m_StartAttIndex[att] + (int)instance.value(att);
}
int att;
int classVal;
//double PCi;
double [][] weigh = new double[m_NumClasses][m_NumAttributes];
double [] numTheAtt = new double[m_NumAttributes];
for(att = 0; att < m_NumAttributes; att++)
{
numTheAtt[att] = 0;
if(attIndex[att]==-1) continue;
//计算待待实例的相关属性值在该属性中出现的次数
for(classVal = 0; classVal < m_NumClasses; classVal++)
{
numTheAtt[att] += m_ClassAttCounts[classVal][attIndex[att]];
}
//计算各属性对该类的权值
for(classVal = 0; classVal < m_NumClasses; classVal++)
{
//weigh[classVal][att]=m_ClassAttCounts[classVal][attIndex[att]]/(numTheAtt[att]‐m_ClassAttCounts[classVal][attIndex[att]]);
weigh[classVal][att]=(numTheAtt[att])/m_ClassAttCounts[classVal][attIndex[att]];
}
}
for(classVal = 0; classVal < m_NumClasses; classVal++)
{
probs[classVal]=(m_ClassCounts[classVal]+1)/(m_NumInstances+m_NumClasses);//(9+1.0)/(14+2)
//probs[classVal] = 1.0;
for(att = 0; att < m_NumAttributes; att++)
{
if(attIndex[att]==-1) continue;
probs[classVal]*=pow((m_ClassAttCounts[classVal][attIndex[att]] )/(m_ClassCounts[classVal]),weigh[classVal][att]);
}
}
Utils.normalize(probs);
return probs;
}
/**
* Main method for testing this class.
*
* @param argv the options
*/
public static void main(String [] argv) {
try {
System.out.println(Evaluation.evaluateModel(new NB(), argv));
}
catch (Exception e) {
e.printStackTrace();
System.err.println(e.getMessage());
}
}
}
网友评论