当前位置:首页 » 操作系统 » knn算法及java实现

knn算法及java实现

发布时间: 2022-06-22 14:17:42

1. KNN算法,k近邻

K最近邻(k-Nearest Neighbour,KNN)分类算法,是一个理论上比较成熟的方法,也是最简单的机器学习算法之一。该方法的思路是:如果一个样本在特征空间中的k个最相似(即特征空间中最邻近)的样本中的大多数属于某一个类别,则该样本也属于这个类别。

2. 求KNN文本分类算法java实现源代码【散分了!!!!】

#include <iostream>
#include <cmath>
#include <fstream>
using namespace std;
#define NATTRS 5 //number of attributes
#define MAXSZ 1700 //max size of training set
#define MAXVALUE 10000.0 //the biggest attribute's value is below 10000(int)
#define K 5
struct vector {
double attributes[NATTRS];
double classlabel;
};
struct item {
double distance;
double classlabel;
};
struct vector trSet[MAXSZ];//global variable,the training set
struct item knn[K];//global variable,the k-neareast-neighbour set
int curTSize = 0; //current size of the training set
int AddtoTSet(struct vector v)
{
if(curTSize>=MAXSZ) {
cout<<endl<<"The training set has "<<MAXSZ<<" examples!"<<endl<<endl;
return 0;
}
trSet[curTSize] = v;
curTSize++;
return 1;
}
double Distance(struct vector v1,struct vector v2)
{
double d = 0.0;
double tem = 0.0;
for(int i = 0;i < NATTRS;i++)
tem += (v1.attributes[i]-v2.attributes[i])*(v1.attributes[i]-v2.attributes[i]);
d = sqrt(tem);
return d;
}
int max(struct item knn[]) //return the no. of the item which has biggest distance(
//should be replaced)
{
int maxNo = 0;
if(K > 1)
for(int i = 1;i < K;i++)
if(knn[i].distance>knn[maxNo].distance)
maxNo = i;
return maxNo;
}double Classify(struct vector v)//decide which class label will be assigned to
//a given input vetor with the knn method
{
double dd = 0;
int maxn = 0;
int freq[K];
double mfreqC = 0;//the class label appears most frequently
int i;
for(i = 0;i < K;i++)
knn[i].distance = MAXVALUE;
for(i = 0;i < curTSize;i++)
{
dd = Distance(trSet[i],v);
maxn = max(knn);//for every new state of the training set should update maxn
if(dd < knn[maxn].distance) {
knn[maxn].distance = dd;
knn[maxn].classlabel = trSet[i].classlabel;
}
}
for(i = 0;i < K;i++)//freq[i] represents knn[i].classlabel appears how many times
freq[i] = 1;
for(i = 0;i < K;i++)
for(int j = 0;j < K;j++)
if((i!=j)&&(knn[i].classlabel == knn[j].classlabel))
freq[i]+=1;
int mfreq = 1;
mfreqC = knn[0].classlabel;
for(i = 0;i < K;i++)
if(freq[i] > mfreq) {
mfreq = freq[i];//mfreq represents the most frepuences
mfreqC = knn[i].classlabel; //mfreqNo is the item no. with the most frequent
//classlabel
}
return mfreqC;
}
void main()
{ double classlabel;
double c;
double n;
struct vector trExmp;
int i;
ifstream filein("G:\\data\\for knn\\data.txt");
if(filein.fail()){cout<<"Can't open data.txt"<<endl; return;}
while(!filein.eof()) {
filein>>c;
trExmp.classlabel = c;
cout<<trExmp.classlabel<<" "; for(int i = 0;i < NATTRS;i++) {
filein>>n;
trExmp.attributes[i] = n;
cout<<trExmp.attributes[i]<<" ";
} cout<<endl;
if(!AddtoTSet(trExmp))
break;
}filein.close();struct vector testv={{142,188,11,1159,0.5513196},17};
classlabel = Classify(testv);
cout<<"The classlable of the testv is: ";
cout<<classlabel<<endl;
for(i = 0;i < K;i++)
cout<<knn[i].distance<<"\t"<<knn[i].classlabel<<endl;
//cout<<max(knn);
}

3. 你好,关于KNN算法的maprece化

==================cluster.txt===========================
A 2 2
B 2 4
C 4 2
D 4 4
E 6 6
F 6 8
G 8 6
H 8 8
==================cluster.center.conf===========================
K1 3 2
K2 6 2
====================================================================================
package com.mahout.cluster;
//二维坐标的点
public class DmRecord {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
private double xpodouble;
private double ypodouble;

public DmRecord(){

}

public DmRecord(String name,double x,double y){
this.name = name;
this.xpodouble = x;
this.ypodouble = y;
}
public double getXpoint() {
return xpodouble;
}
public void setXpoint(double xpodouble) {
this.xpodouble = xpodouble;
}
public double getYpoint() {
return ypodouble;
}
public void setYpoint(double ypodouble) {
this.ypodouble = ypodouble;
}

public double distance(DmRecord record){
return Math.sqrt(Math.pow(this.xpodouble-record.xpodouble, 2)+Math.pow(this.ypodouble-record.ypodouble, 2));
}
}
==============================================================================
package com.mahout.cluster;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;
import org.apache.hadoop.io.IOUtils;
public class DmRecordParser {
private Map<String,DmRecord> urlMap = new HashMap<String,DmRecord>();

/**
* 读取配置文件记录,生成对象
*/
public void initialize(File file) throws IOException {
BufferedReader in = null;
try {
in = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
String line;
while ((line = in.readLine()) != null) {
String [] strKey = line.split("\t");
urlMap.put(strKey[0],parse(line));
}
} finally {
IOUtils.closeStream(in);
}
}

/**
* 生成坐标对象
*/
public DmRecord parse(String line){
String [] strPlate = line.split("\t");
DmRecord Dmurl = new DmRecord(strPlate[0],Integer.parseInt(strPlate[1]),Integer.parseInt(strPlate[2]));
return Dmurl;
}

/**
* 获取分类中心坐标
*/
public DmRecord getUrlCode(String cluster){
DmRecord returnCode = null;
DmRecord dmUrl = (DmRecord)urlMap.get(cluster);
if(dmUrl == null){
//35 6
returnCode = null;
}else{
returnCode =dmUrl;
}
return returnCode;
}
}
==============================================================================
package com.mahout.cluster;
import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import org.apache.hadoop.conf.Configured;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.io.compress.CompressionCodec;
import org.apache.hadoop.mapred.FileInputFormat;
import org.apache.hadoop.mapred.FileOutputFormat;
import org.apache.hadoop.mapred.JobClient;
import org.apache.hadoop.mapred.JobConf;
import org.apache.hadoop.mapred.MapReceBase;
import org.apache.hadoop.mapred.Mapper;
import org.apache.hadoop.mapred.OutputCollector;
import org.apache.hadoop.mapred.Recer;
import org.apache.hadoop.mapred.Reporter;
import org.apache.hadoop.mapred.TextInputFormat;
import org.apache.hadoop.mapred.TextOutputFormat;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner;
import com.mahout.test.StringStringPairAsce;
public class Kmeans extends Configured implements Tool {
public static class KmeansMapper extends MapReceBase implements
Mapper<LongWritable, Text, Text, Text> {
private DmRecordParser drp ;
private String clusterNode = "K";
private DmRecord record0 = null;
private DmRecord record1 = new DmRecord();
private double Min_distance = 9999;
private int tmpK = 0;
private Text tKey = new Text();
private Text tValue = new Text();

//获取聚类中心坐标
@Override
public void configure(JobConf conf) {
drp = new DmRecordParser();
try {
drp.initialize(new File("cluster.center.conf"));
} catch (IOException e) {
throw new RuntimeException(e);
}
}

//根据聚类坐标,把文件中的点进行类别划分
@Override
public void map(LongWritable key, Text value,
OutputCollector<Text, Text> output, Reporter arg3)
throws IOException {
String [] strArr = value.toString().split("\t");

for(int i=1; i <= 2; i++){
record0 = drp.getUrlCode("K"+i);
record1.setName(strArr[0]);
record1.setXpoint(Double.parseDouble(strArr[1]));
record1.setXpoint(Integer.parseInt(strArr[2]));

if(record0.distance(record1) < Min_distance){
tmpK = i;
Min_distance = record0.distance(record1);
}
}

tKey.set("C"+tmpK);
output.collect(tKey, value);
}
}

//计算新的聚类中心
public static class KmeansRecer extends MapReceBase implements
Recer<Text, Text, Text, Text> {
private Text tKey = new Text();
private Text tValue = new Text();

@Override
public void rece(Text key, Iterator<Text> value,
OutputCollector<Text, Text> output, Reporter arg3)
throws IOException {
double avgX=0;
double avgY=0;
double sumX=0;
double sumY=0;
int count=0;
String [] strValue = null;

while(value.hasNext()){
count++;
strValue = value.next().toString().split("\t");
sumX = sumX + Integer.parseInt(strValue[1]);
sumY = sumY + Integer.parseInt(strValue[1]);
}

avgX = sumX/count;
avgY = sumY/count;
tKey.set("K"+key.toString().substring(1,2));
tValue.set(avgX + "\t" + avgY);
System.out.println("K"+key.toString().substring(1,2)+"\t"+avgX + "\t" + avgY);
output.collect(tKey, tValue);
}
}

@Override
public int run(String[] args) throws Exception {
JobConf conf = new JobConf(getConf(), Kmeans.class);
conf.setJobName("Kmeans");
//conf.setNumMapTasks(200);
// 设置Map输出的key和value的类型
conf.setMapOutputKeyClass(Text.class);
conf.setMapOutputValueClass(Text.class);
// 设置Rece输出的key和value的类型
conf.setOutputKeyClass(Text.class);
conf.setOutputValueClass(Text.class);
// 设置Mapper和Recer
conf.setMapperClass(KmeansMapper.class);
conf.setRecerClass(KmeansRecer.class);

conf.setInputFormat(TextInputFormat.class);
conf.setOutputFormat(TextOutputFormat.class);
// 设置输入输出目录
FileInputFormat.setInputPaths(conf, new Path(args[0]));
FileOutputFormat.setOutputPath(conf, new Path(args[1]));
JobClient.runJob(conf);
return 0;
}
public static void main(String[] args) throws Exception {
int exitCode = ToolRunner.run(new Kmeans(), args);
System.exit(exitCode);
}
}

4. 你好,请问你现在有基于MapRece的knn算法的Java代码么谢谢~

14/09/0308:08:01INFOjvm.JvmMetrics:=JobTracker,sessionId=
14/09/0308:08:01WARNmapred.JobClient:..
14/09/0308:08:01WARNmapred.JobClient:Nojobjarfileset.Userclassesmaynotbefound.SeeJobConf(Class)orJobConf#setJar(String).
14/09/0308:08:02INFOinput.FileInputFormat:Totalinputpathstoprocess:1
14/09/0308:08:02INFOmapred.JobClient:Runningjob:job_local_0001
14/09/0308:08:02INFOinput.FileInputFormat:Totalinputpathstoprocess:1
14/09/0308:08:02INFOmapred.MapTask:io.sort.mb=100
14/09/0308:08:03INFOmapred.MapTask:databuffer=79691776/99614720
14/09/0308:08:03INFOmapred.MapTask:recordbuffer=262144/327680
14/09/0308:08:03WARNmapred.LocalJobRunner:job_local_0001
java.lang.ClassCastException:classPoint2D
atjava.lang.Class.asSubclass(Class.java:3018)
atorg.apache.hadoop.mapred.JobConf.getOutputKeyComparator(JobConf.java:599)
atorg.apache.hadoop.mapred.MapTask$MapOutputBuffer.<init>(MapTask.java:791)
atorg.apache.hadoop.mapred.MapTask$NewOutputCollector.<init>(MapTask.java:524)
atorg.apache.hadoop.mapred.MapTask.runNewMapper(MapTask.java:613)
atorg.apache.hadoop.mapred.MapTask.run(MapTask.java:305)
atorg.apache.hadoop.mapred.LocalJobRunner$Job.run(LocalJobRunner.java:177)
14/09/0308:08:03INFOmapred.JobClient:map0%rece0%
14/09/0308:08:03INFOmapred.JobClient:Jobcomplete:job_local_0001
14/09/0308:08:03INFOmapred.JobClient:Counters:0

5. knn算法算是一种python模型吗

“算法”不能算是“模型”,更不能说是“python模型”,因为python能实现的,c++、java等通用语言也能实现。

6. knn是什么意思

作为一种非参数的分类算法,K-近邻(KNN)算法是非常有效和容易实现的。它已经广泛应用于分类、回归和模式识别等。

在应用KNN算法解决问题的时候,要注意两个方面的问题——样本权重和特征权重。利用SVM来确定特征的权重,提出了基于SVM的特征加权算法(FWKNN,featureweightedKNN)。实验表明,在一定的条件下,FWKNN能够极大地提高分类准确率。

(6)knn算法及java实现扩展阅读:

KNN(K- Nearest Neighbor)法即K最邻近法,最初由 Cover和Hart于1968年提出,是一个理论上比较成熟的方法,也是最简单的机器学习算法之一。该方法的思路非常简单直观:

如果一个样本在特征空间中的K个最相似(即特征空间中最邻近)的样本中的大多数属于某一个类别,则该样本也属于这个类别。该方法在定类决策上只依据最邻近的一个或者几个样本的类别来决定待分样本所属的类别。

7. 什么是knn算法

作为一种非参数的分类算法,K-近邻(KNN)算法是非常有效和容易实现的。它已经广泛应用于分类、回归和模式识别等。在应用KNN算法解决问题的时候,要注意两个方面的问题——样本权重和特征权重。利用SVM来确定特征的权重,提出了基于SVM的特征加权算法(FWKNN,feature
weighted
KNN)。实验表明,在一定的条件下,FWKNN能够极大地提高分类准确率。

8. 怎样在spark里跑java版的knn算

1、将KNN算法调用Spark的api进行重写。
2、然后就可以在sparkshell里运行了

9. knn算法是什么

KNN(K- Nearest Neighbor)法即K最邻近法,最初由Cover和Hart于1968年提出,是一个理论上比较成熟的方法,也是最简单的机器学习算法之一。

作为一种非参数的分类算法,K-近邻(KNN)算法是非常有效和容易实现的。它已经广泛应用于分类、回归和模式识别等。

介绍

KNN算法本身简单有效,它是一种lazy-learning算法,分类器不需要使用训练集进行训练,训练时间复杂度为0。KNN分类的计算复杂度和训练集中的文档数目成正比,也就是说,如果训练集中文档总数为n,那么KNN的分类时间复杂度为O(n)。

KNN方法虽然从原理上也依赖于极限定理,但在类别决策时,只与极少量的相邻样本有关。由于KNN方法主要靠周围有限的邻近的样本,而不是靠判别类域的方法来确定所属类别的,因此对于类域的交叉或重叠较多的待分样本集来说,KNN方法较其他方法更为适合。

热点内容
编程学习方法 发布:2024-11-08 22:55:48 浏览:261
自己搭建以太服务器 发布:2024-11-08 22:55:41 浏览:472
c语言完美数 发布:2024-11-08 22:27:43 浏览:105
远程桌面服务器搭建h5网页吗 发布:2024-11-08 22:27:37 浏览:959
简单点编程 发布:2024-11-08 22:21:50 浏览:812
mysql存储过程教程 发布:2024-11-08 22:20:56 浏览:201
shell脚本sort 发布:2024-11-08 22:20:55 浏览:182
linux怎么登录 发布:2024-11-08 22:19:07 浏览:410
段页式存储管理中 发布:2024-11-08 22:03:22 浏览:734
易语言注册码源码 发布:2024-11-08 22:03:22 浏览:238