下面是几个城市的GDP等信息,根据这些信息,写一个SOM网络,使之对下面城市进行聚类。并且,将结果画在一个二维平面上。
//表1中,X。为人均GDP(元);X2为工业总产值(亿元);X。为社会消费品零售总额(亿元);x。为批发零售贸易总额(亿元);x。为地区货运总量(万吨),表1中数据来自2002年城市统计年鉴。
//城市 X1 X2 X3 Xa X5
北京 27527 2738.30 1494.83 3055.63 30500
青岛 29682 1212.02 182.80 598.06 29068
天津 22073 2663.56 782.33 1465.65 28151
烟台 21017 298.73 92.71 227.39 8178
石家庄 25584 467.42 156.02 763.46 12415
郑州 17330 261.80 215.63 402.98 7373
唐山 19387 338.67 95.73 199.69 14522
武汉 17882 1020.84 685.82 1452 16244
太原 13919 304.13 141.94 155.22 15170
长沙 26327 241.76 269.93 369.83 7550
呼和浩特 13738 82.23 69.27 108.12 2415
衡阳 12386 61.53 63.95 72.65 3004
沈阳 21736 729.04 590.26 1752.4 15156
广州 42828 2446.97 1166.10 3214.19 24500
大连 34659 1003.56 431.83 728.08 19736
深圳 152099 3079.63 609.26 801.06 5167
长春 24799 900.26 309.75 173.99 10346
油头 19414 192.93 112.96 280.84 1443
哈尔滨 20737 402.73 360.38 762.94 8814
湛江 15290 228.45 99.08 149.16 5524
上海 40788 6935.57 1531.89 3921.2 49499
南宁 17715 109.39 142.08 264.32 3371
南京 26697 1579.21 401.20 1253.73 14120
柳州 17598 256.76 68.93 159.44 3397
徐州 19727 295.73 108.17 187.39 7124
海口 24782 100.13 81.03 142.54 2018
连云港 17869 112.18 47.94 134.89 4096
成都 22956 412.23 400.56 754.07 23724
杭州 31784 1615.63 373.28 1788.29 15841
重庆 9778 870.82 389.60 823.72 29470
宁波 46471 751.58 167.70 529.68 11182
贵阳 13176 207.95 108.93 285.27 4885
温州 29781 381.93 233.44 272.84 6292
昆明 24554 303.78 227.44 428.64 12084
合肥 19770 330.14 140.14 328.98 2903
西安 16002 449.14 323.37 558.27 7728
福州 33570 379.51 209.72 613.24 7280
兰州 16629 354.30 163.97 374.9 5401
厦门 42039 803.29 186.55 620.47 2547
西宁 7261 38.00 48.95 91.14 1837
南昌 19923 238.82 14.09 348.21 3246
银川 12779 77.74 41.22 53.16 1573
济南 25642 616.97 323.08 462.39 13057
乌鲁木齐 19793 251.19 129.05 277.8 9283
首先,利用python对这些数据进行处理,具体过程如下:
1,读入文件
2,使用1000,100,10,1三个数字分别替换x1列的数值,判断的标准为中位数和两个四分位数。
3,代替x2列的数值
4,替换x3列的数值
5,替换Xa列的数值
6,替换X5列的数值
最终得到的结果:
excel排序算分段值截图:
应该可以用四分位数直接可以得到这些数字,这里操作的稍微的麻烦了一点。
聚类的java的代码:
package com.cgjr.som; import org.neuroph.core.Neuron; import org.neuroph.core.data.DataSet; import org.neuroph.core.data.DataSetRow; import org.neuroph.nnet.Kohonen; public class AreaClutering { public static double[][] data = { { 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,1,0,0,0,1,0,0,0,1,0,0,0,1}, //北京 { 0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,1,0,1,0,0,0,0,1,0,0,0,0,1}, //青岛 { 0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,1,0,0,0,1,0,0,0,1,0,0,0,1},//天津 { 0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,1,0,0,1,0,0,0,0,1,0,0,0,0,1,0},//烟台 { 0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,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,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,1,0,0,0,1,0,0,0,1,0,0,0,1,0,0,0,1,0}, //济南 { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,1,0,0,0,1,0,0,0,1,0,0,0,1,0,0,0,0,1,0}, //乌鲁木齐 }; public static String[] dataKey={"北京","青岛","天津","烟台","石家庄","郑州","唐山","武汉","太原","长沙","呼和浩特", "衡阳","沈阳","广州","大连","深圳","长春","油头","哈尔滨","湛江","上海","南宁", "南京","柳州","徐州","海口","连云港","成都","杭州","重庆","宁波","贵阳","温州", "昆明","合肥","西安","福州","兰州","厦门","西宁","南昌","银川","济南","乌鲁木齐"}; public static void main(String[] args) { ResultFrame frame = new ResultFrame(); Kohonen som = new Kohonen(64, 100); DataSet ds = new DataSet(64); for (double[] row : data) { ds.addRow(new DataSetRow(row)); } som.learn(ds); for (int i=0;i) { som.setInput(data[i]); som.calculate(); int winnerIndex=getWinnerIndex(som); int x=getRowFromIndex(winnerIndex); int y=getColFromIndex(winnerIndex); System.out.println(dataKey[i]+" "+x+" "+y ); frame.addElementString(new ResultFrame.ElementString(dataKey[i], x, y)); } frame.showMe(); } // get unit with closetst weight vector private static int getWinnerIndex(Kohonen neuralNetwork) { Neuron winner = new Neuron(); double minOutput = 100; int winnerIndex=-1; Neuron[] neurons=neuralNetwork.getLayerAt(1).getNeurons(); for (int i=0;i ) { double out = neurons[i].getOutput(); if (out < minOutput) { minOutput = out; winnerIndex = i; } // if } // while return winnerIndex; } /** * 10行10列中的位置 * @param index * @return */ private static int getRowFromIndex(int index){ return index/10+1; } private static int getColFromIndex(int index){ return index%10+1; } }
package com.cgjr.som; import java.awt.Font; import java.awt.Graphics; import java.awt.Graphics2D; import java.util.ArrayList; import java.util.List; import javax.swing.JFrame; import javax.swing.JPanel; public class ResultFrame extends JFrame { private Listelements=new ArrayList (); public ResultFrame() { } private void init() { setTitle("训练结果"); setSize(800, 800); DrawPanel panel = new DrawPanel(); add(panel); } public void showMe(){ if(elements.size()==0)throw new RuntimeException("elements is empty"); init(); normalCood(); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setVisible(true); } public void addElementString(ElementString str){ elements.add(str); } public void normalCood(){ float minX=Float.MAX_VALUE,maxX=0,minY=Float.MAX_VALUE,maxY=0; for(ElementString es:elements){ if(es.x>maxX)maxX=es.x; if(es.y>maxY)maxY=es.y; if(es.x es.x; if(es.y es.y; } for(ElementString es:elements){ es.x=(es.x-minX)/(maxX-minX)*700+20; es.y=(es.y-minY)/(maxY-minY)*700+20; } } public static void main(String[] args) { ResultFrame frame = new ResultFrame(); frame.showMe(); } class DrawPanel extends JPanel { public void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2 = (Graphics2D) g;//将Graphics对象转换为Graphics2D对象 g2.setFont(new Font("TimesRoman", Font.PLAIN, 20)); for(ElementString es:elements){ g2.drawString(es.text, es.x, es.y); } } } public static class ElementString{ private String text; private float x; private float y; public ElementString(String text, float x, float y) { super(); this.text = text; this.x = x; this.y = y; } public String getText() { return text; } public void setText(String text) { this.text = text; } public float getX() { return x; } public void setX(float x) { this.x = x; } public float getY() { return y; } public void setY(float y) { this.y = y; } } }
运行效果的截图: