离散数学最邻近算法实验报告Word文档下载推荐.docx
《离散数学最邻近算法实验报告Word文档下载推荐.docx》由会员分享,可在线阅读,更多相关《离散数学最邻近算法实验报告Word文档下载推荐.docx(24页珍藏版)》请在冰豆网上搜索。
//与测试元组的距离
privateStringc;
//所属类别
publicKNNNode(intindex,doubledistance,Stringc)
{
super();
this.index=index;
this.distance=distance;
this.c=c;
}
publicintgetIndex()
returnindex;
publicvoidsetIndex(intindex)
publicdoublegetDistance()
returndistance;
publicvoidsetDistance(doubledistance)
publicStringgetC()
returnc;
publicvoidsetC(Stringc)
}
//KNN算法主体类
classKNN
/**
*设置优先级队列的比较函数,距离越大,优先级越高
*/
privateComparator<
KNNNode>
comparator=newComparator<
()
publicintcompare(KNNNodeo1,KNNNodeo2)
{
if(o1.getDistance()>
=o2.getDistance())
{
return-1;
}
else
return1;
}
};
*获取K个不同的随机数
*@paramk随机数的个数
*@parammax随机数最大的范围
*@return生成的随机数数组
publicList<
Integer>
getRandKNum(intk,intmax)
List<
rand=newArrayList<
(k);
for(inti=0;
i<
k;
i++)
inttemp=(int)(Math.random()*max);
if(!
rand.contains(temp))
rand.add(temp);
i--;
returnrand;
*计算测试元组与训练元组之前的距离
*@paramd1测试元组
*@paramd2训练元组
*@return距离值
*/
publicdoublecalDistance(List<
Double>
d1,List<
d2)
doubledistance=0.00;
d1.size();
distance+=(d1.get(i)-d2.get(i))*(d1.get(i)-d2.get(i));
*执行KNN算法,获取测试元组的类别
*@paramdatas训练数据集
*@paramtestData测试元组
*@paramk设定的K值
*@return测试元组的类别
publicStringknn(List<
List<
>
datas,List<
testData,intk)
PriorityQueue<
pq=newPriorityQueue<
(k,comparator);
randNum=getRandKNum(k,datas.size());
intindex=randNum.get(i);
List<
currData=datas.get(index);
Stringc=currData.get(currData.size()-1).toString();
KNNNodenode=newKNNNode(index,calDistance(testData,currData),c);
pq.add(node);
datas.size();
t=datas.get(i);
doubledistance=calDistance(testData,t);
KNNNodetop=pq.peek();
if(top.getDistance()>
distance)
pq.remove();
pq.add(newKNNNode(i,distance,t.get(t.size()-1).toString()));
returngetMostClass(pq);
*获取所得到的k个最近邻元组的多数类
*@parampq存储k个最近近邻元组的优先级队列
*@return多数类的名称
privateStringgetMostClass(PriorityQueue<
pq)
Map<
String,Integer>
classCount=newHashMap<
();
intpqsize=pq.size();
pqsize;
KNNNodenode=pq.remove();
Stringc=node.getC();
if(classCount.containsKey(c))
classCount.put(c,classCount.get(c)+1);
classCount.put(c,1);
intmaxIndex=-1;
intmaxCount=0;
Object[]classes=classCount.keySet().toArray();
classes.length;
if(classCount.get(classes[i])>
maxCount)
maxIndex=i;
maxCount=classCount.get(classes[i]);
returnclasses[maxIndex].toString();
classKNNFrame
{
privateJFrameframe1=newJFrame("
KNN最邻近算法"
);
privateJLabellabel1=newJLabel("
为了方便显示算法可视化,请按照如下步骤操作完成!
"
privateJLabellabel2=newJLabel("
第一步:
请在表格中输入训练数据。
privateJLabellabel3=newJLabel("
第二步:
请在表格中输入测试元组数据。
privateJLabellabel4=newJLabel("
最后一步:
显示比较训练元组和测试元组的相似度类别。
privateJButtonbutton1=newJButton("
现在去输入"
privateJButtonbutton2=newJButton("
privateJButtonbutton3=newJButton("
立刻显示"
//错误窗口。
privateJFrameframe5=newJFrame("
错误窗口!
privateJLabellabel5=newJLabel();
publicstaticintbiaoji_1=0;
//标记1。
publicstaticintbiaoji_2=0;
//标记2。
publicKNNFrame()
{
frame1.setSize(700,500);
frame1.setLocation(400,100);
frame1.setVisible(true);
frame1.setLayout(null);
frame1.addWindowListener(newWindowAdapter(){
publicvoidwindowClosing(WindowEventex)
{
System.exit
(1);
}
});
label1.setFont(newFont("
Serif"
Font.BOLD,25));
label1.setBounds(30,1,670,50);
label1.setForeground(Color.RED);
frame1.add(label1);
label2.setFont(newFont("
Font.BOLD,20));
label2.setBounds(5,50,500,50);
label2.setForeground(Color.BLUE);
frame1.add(label2);
button1.setBounds(100,100,150,50);
frame1.add(button1);
label3.setFont(newFont("
label3.setBounds(5,170,500,50);
label3.setForeground(Color.BLUE);
frame1.add(label3);
button2.setBounds(100,220,150,50);
frame1.add(button2);
label4.setFont(newFont("
label4.setBounds(5,300,600,50);
label4.setForeground(Color.BLUE);
frame1.add(label4);
button3.setBounds(100,350,150,50);
frame1.add(button3);
//按钮一监听表格中输入的训练数据。
button1.addActionListener(newActionListener(){
publicvoidactionPerformed(ActionEventevt)
biaoji_1=1;
newKNNFrame_1();
//按钮二监听表格中输入测试元组的数据。
button2.addActionListener(newActionListener(){
biaoji_2=1;
newKNNFrame_2();
//按钮三显示比较训练元组和测试元组的相似度类别。
button3.addActionListener(newActionListener(){
if(biaoji_1==1&
&
biaoji_2==1)
newKNNFrame_3();
else
{
//显示错误窗口。
frame5.setSize(600,150);
frame5.setLocation(450,300);
frame5.setVisible(true);
frame5.setLayout(null);
frame5.addWindowListener(newWindowAdapter(){
publicvoidwindowClosing(WindowEventex)
{
frame5.dispose();
}
});
label5.setFont(newFont("
Font.BOLD,15));
label5.setForeground(Color.RED);
label5.setBounds(5,50,595,20);
label5.setText("
请先完成以上操作,在表格中输入相关数据,确认数据后再确定相似度类别!
!
frame5.add(label5);
}
}
}
//按钮一窗口。
classKNNFrame_1
privateJFrameframe2=newJFrame("
请先输入表格的相关信息。
表格的行数:
privateJTextFieldtext1=newJTextField();
表格的列数:
privateJTextFieldtext2=newJTextField();
确定"
确定表格数据"
privateJScrollPanescrollPane1=null;
privateJTabletable1=null;
publicstaticdoubletraining_data[][];
//存储训练数据。
publicKNNFrame_1()
frame2.setSize(600,540);
frame2.setLocation(450,150);
frame2.setVisible(true);
frame2.setLayout(null);
frame2.addWindowListener(newWindowAdapter(){
frame2.dispose();
label1.setBounds(150,1,500,50);
frame2.add(label1);
label2.setBounds(100,50,100,30);
frame2.add(label2);
text1.setBounds(200,50,50,30);
frame2.add(text1);
label3.setBounds(100,90,100,30);
frame2.add(label3);
text2.setBounds(200,90,50,30);
frame2.add(text2);
button1.setBounds(270,60,150,50);
frame2.add(button1);
label4.setBounds(150,130,500,30);
frame2.add(label4);
button2.setBounds(200,430,180,50);
frame2.add(button2);
try
biaoji_1=1;
//行数。
Stringrow=text1.getText();
introwi=Integer.parseInt(row);
//列数。
Stringcolumn=text2.getText();
intcolumnj=Integer.parseInt(column);
//用表格显示数据。
Stringtitles[]=newString[columnj];
for(intj=0;
j<
columnj-1;
j++)
titles[j]="
训练数据"
+(j+1);
titles[columnj-1]="
类别"
;
StringuserInfo[][]=newString[rowi][columnj];
DefaultTableModeltableModel=newDefaultTableModel(userInfo,titles);
table1=newJTable(tableModel);
scrollPane1=newJScrollPane(table1);
scrollPane1.setBounds(10,170,580,250);
frame2.add(scrollPane1);
}catch(Exceptione)
e.printStackTrace();
if(biaoji_1==1)
//设置表格为不可编辑。
table1.getCellEditor().stopCellEditing();
table1.setRowSelectionInterval(0,0);
table1.setEnabled(false);
//获取表格中的数据。