KNN(转)

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import edu.udo.cs.wvtool.main.WVTWordVector;

public class KNN {

private int k = 15;
private int cLimit = 1;
private double[] ClassSim = null;
private static Map<Integer, String> indexmap = null;

static {
if (indexmap == null)
{
indexmap = new HashMap<Integer, String>();
indexmap.put(0, "计算机");
indexmap.put(1, "房地产");
/*
indexmap.put(0, "汽车");
indexmap.put(1, "教育");
indexmap.put(2, "娱乐");
indexmap.put(3, "财经");
indexmap.put(4, "房产");
indexmap.put(5, "军事");
indexmap.put(6, "奥运");
indexmap.put(7, "时政");
indexmap.put(8, "体育");
indexmap.put(9, "科技");
*/
}
}

public KNN()
{

}

public List LazyLearning(WVTWordVector v, WVTWordVector[] vectors, int numClasses)
{
if (v == null || vectors == null)
return null;

System.out.println("number of documents : " + vectors.length);
System.out.println("number of classes: " + numClasses);
ClassSim = new double[numClasses];
for (int i = 0; i < numClasses; i++)
{
ClassSim[i] = 0;
}

k = (k < vectors.length)? k : vectors.length;
double[] Sim = new double[vectors.length];
for (int i = 0; i < Sim.length; i++)
{
Sim[i] = 0;
Map map1 = v.getWordMap();
Map map2 = vectors[i].getWordMap();
for (Iterator it = map1.keySet().iterator(); it.hasNext();)
{
String word1 = (String)it.next();
if (map2.containsKey(word1))
{
double value1 = Double.valueOf(map1.get(word1).toString());
double value2 = Double.valueOf(map2.get(word1).toString());
Sim[i] += (value1 * value2);
}
}
}

for (int i = 0; i < k; i++)
{
for (int j = i + 1; j < Sim.length; j++)
{
if (Sim[j] > Sim[i])
{
double dtemp = Sim[i];
Sim[i] = Sim[j];
Sim[j] = dtemp;

WVTWordVector wv = vectors[i];
vectors[i] = vectors[j];
vectors[j] = wv;
}
}
}

double TotalSim = 0;
for (int i = 0; i < k; i++)
{
WVTWordVector wv = vectors[i];
int numClass = wv.getDocumentInfo().getClassValue();
ClassSim[numClass] += Sim[i];
TotalSim += Sim[i];
}

// output the first 3 class
int[] index = new int[ClassSim.length];
for (int i = 0; i < ClassSim.length; i++)
index[i] = i;
for (int i = 0; i < cLimit; i++)
{
for (int j = i + 1; j < ClassSim.length; j++)
{
if (ClassSim[j] > ClassSim[i])
{
double dtemp = ClassSim[i];
ClassSim[i] = ClassSim[j];
ClassSim[j] = dtemp;

int itemp = index[i];
index[i] = index[j];
index[j] = itemp;
}
}
}

List<CategoryResult> result = new ArrayList<CategoryResult>();
for (int i = 0; i < cLimit; i++) {
if (ClassSim[i] > 0) {
CategoryResult cr = new CategoryResult(indexmap.get(index[i]).toString(), ClassSim[i] / TotalSim);
result.add(cr);
}
}

///*
for (int i = 0; i < index.length; i++){
System.out.println("cat:"+index[i] + ": " + ClassSim[i]);
}

for (int i = 0; i < k; i++){
WVTWordVector wv = vectors[i];
String id;
//int cutIndex = wv.getDocumentInfo().getSourceName().lastIndexOf(File.separator);
//if (cutIndex > 0)
// id = wv.getDocumentInfo().getSourceName().substring(cutIndex + 1);
//else
id = wv.getDocumentInfo().getSourceName();
System.out.println("c:"+id + ": " + Sim[i]);
}
//*/
return result;
}

}

class CategoryResult
{
private String CategoryName;
private double similarity;

public CategoryResult(String categoryname, double similarity)
{
this.CategoryName = categoryname;
this.similarity = similarity;
}

public String getCategoryName() {
return CategoryName;
}

public void setCategoryName(String categoryName) {
CategoryName = categoryName;
}

public double getSimilarity() {
return similarity;
}

public void setSimilarity(double similarity) {
this.similarity = similarity;
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

walk walk

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值