亚洲欧美第一页_禁久久精品乱码_粉嫩av一区二区三区免费野_久草精品视频

? 歡迎來到蟲蟲下載站! | ?? 資源下載 ?? 資源專輯 ?? 關于我們
? 蟲蟲下載站

?? kmeans.java

?? 一個數(shù)據挖掘系統(tǒng)的源碼
?? JAVA
?? 第 1 頁 / 共 2 頁
字號:

/**
 *
 *   AgentAcademy - an open source Data Mining framework for
 *   training intelligent agents
 *
 *   Copyright (C)   2001-2003 AA Consortium.
 *
 *   This library is open source software; you can redistribute it
 *   and/or modify it under the terms of the GNU Lesser General
 *   Public License as published by the Free Software Foundation;
 *   either version 2.0 of the License, or (at your option) any later
 *   version.
 *
 *   This library is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU Lesser General Public
 *   License along with this library; if not, write to the Free
 *   Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
 *   MA  02111-1307 USA
 *
 */

package org.agentacademy.modules.dataminer.clusterers;

/**
 * <p>Title: The Data Miner prototype</p>
 * <p>Description: A prototype for the DataMiner (DM), the Agent Academy (AA) module responsible for performing data mining on the contents of the Agent Use Repository (AUR). The extracted knowledge is to be sent back to the AUR in the form of a PMML document.</p>
 * <p>Copyright: Copyright (c) 2002</p>
 * <p>Company: CERTH</p>
 * @author asymeon
 * @version 0.3
 */

import java.io.*;
import java.util.*;
import org.agentacademy.modules.dataminer.core.*;
import org.agentacademy.modules.dataminer.filters.Filter;
import org.agentacademy.modules.dataminer.filters.ReplaceMissingValuesFilter;
import org.jdom.*;
import org.jdom.output.*;
import org.apache.log4j.Logger;

/**
 * Simple k means clustering class.
 *
 * Valid options are:<p>
 *
 * -N <number of clusters> <br>
 * Specify the number of clusters to generate. <p>
 *
 * -S <seed> <br>
 * Specify random number seed. <p>
 *
 */
public class KMeans extends Clusterer implements OptionHandler {

 public static Logger                log = Logger.getLogger(KMeans.class);
   /** The pmmlDocument Document */
  public static Document pmmlDocument = null;

  /*
   * training instances
   */
  private Instances m_instances;

  /**
   * replace missing values in training instances
   */
  private ReplaceMissingValuesFilter m_ReplaceMissingFilter;

  /**
   * number of clusters to generate
   */
  private int m_NumClusters = 2;

  /**
   * holds the cluster centroids
   */
  private Instances m_ClusterCentroids;

  /**
   * temporary variable holding cluster assignments while iterating
   */
  private int [] m_ClusterAssignments;

  /**
   * random seed
   */
  private int m_Seed = 10;

  /**
   * attribute min values
   */
  private double [] m_Min;

  /**
   * attribute max values
   */
  private double [] m_Max;

  /**
   * Keep track of the number of iterations completed before convergence
   */
  private int m_Iterations = 0;

  /**
   * Returns a string describing this clusterer
   * @return a description of the evaluator suitable for
   * displaying in the explorer/experimenter gui
   */
  public String globalInfo() {
    return "Cluster data using the k means algorithm";
  }

  /**
   * Generates a clusterer. Has to initialize all fields of the clusterer
   * that are not being set via options.
   *
   * @param data set of instances serving as training data
   * @exception Exception if the clusterer has not been
   * generated successfully
   */
  public void buildClusterer(Instances data) throws Exception {
    m_Iterations = 0;
    if (data.checkForStringAttributes()) {
      throw  new Exception("Can't handle string attributes!");
    }

    m_ReplaceMissingFilter = new ReplaceMissingValuesFilter();
    m_ReplaceMissingFilter.setInputFormat(data);
    m_instances = Filter.useFilter(data, m_ReplaceMissingFilter);

    m_Min = new double [m_instances.numAttributes()];
    m_Max = new double [m_instances.numAttributes()];
    for (int i = 0; i < m_instances.numAttributes(); i++) {
      m_Min[i] = m_Max[i] = Double.NaN;
    }

    for (int i = 0; i < m_instances.numInstances(); i++) {
      updateMinMax(m_instances.instance(i));
    }

    m_ClusterCentroids = new Instances(m_instances, m_NumClusters);
    m_ClusterAssignments = new int [m_instances.numInstances()];

    Random RandomO = new Random(m_Seed);
    boolean [] selected = new boolean[m_instances.numInstances()];
    int instIndex;
    for (int i = 0; i < m_NumClusters; i++) {
      do {
	instIndex = Math.abs(RandomO.nextInt()) %
	  m_instances.numInstances();
      } while (selected[instIndex]);
      m_ClusterCentroids.add(m_instances.instance(instIndex));
      selected[instIndex] = true;
    }
    selected = null;

    boolean converged = false;
    while (!converged) {
      m_Iterations++;
      converged = true;
      for (int i = 0; i < m_instances.numInstances(); i++) {
	Instance toCluster = m_instances.instance(i);
	int newC = clusterProcessedInstance(toCluster);
	if (newC != m_ClusterAssignments[i]) {
	  converged = false;
	}
	m_ClusterAssignments[i] = newC;
	//	System.out.println(newC);
      }

      Instances [] tempI = new Instances[m_NumClusters];
      // update centroids
      m_ClusterCentroids = new Instances(m_instances, m_NumClusters);
      for (int i = 0; i < m_NumClusters; i++) {
	tempI[i] = new Instances(m_instances, 0);
      }
      for (int i = 0; i < m_instances.numInstances(); i++) {
	tempI[m_ClusterAssignments[i]].add(m_instances.instance(i));
      }
      for (int i = 0; i < m_NumClusters; i++) {
	double [] vals = new double[m_instances.numAttributes()];
	for (int j = 0; j < m_instances.numAttributes(); j++) {
	  vals[j] = tempI[i].meanOrMode(j);
	}
	m_ClusterCentroids.add(new Instance(1.0, vals));
      }
    }
  }

  /**
   * clusters an instance that has been through the filters
   *
   * @param instance the instance to assign a cluster to
   * @return a cluster number
   */
  private int clusterProcessedInstance(Instance instance) {
    double minDist = Integer.MAX_VALUE;
    int bestCluster = 0;
    for (int i = 0; i < m_NumClusters; i++) {
      double dist = distance(instance, m_ClusterCentroids.instance(i));
      if (dist < minDist) {
	minDist = dist;
	bestCluster = i;
      }
    }
    return bestCluster;
  }

  /**
   * Classifies a given instance.
   *
   * @param instance the instance to be assigned to a cluster
   * @return the number of the assigned cluster as an interger
   * if the class is enumerated, otherwise the predicted value
   * @exception Exception if instance could not be classified
   * successfully
   */
  public int clusterInstance(Instance instance) throws Exception {
    m_ReplaceMissingFilter.input(instance);
    m_ReplaceMissingFilter.batchFinished();
    Instance inst = m_ReplaceMissingFilter.output();

    return clusterProcessedInstance(inst);
  }

  /**
   * Calculates the distance between two instances
   *
   * @param test the first instance
   * @param train the second instance
   * @return the distance between the two given instances, between 0 and 1
   */
  private double distance(Instance first, Instance second) {

    double distance = 0;
    int firstI, secondI;

    for (int p1 = 0, p2 = 0;
	 p1 < first.numValues() || p2 < second.numValues();) {
      if (p1 >= first.numValues()) {
	firstI = m_instances.numAttributes();
      } else {
	firstI = first.index(p1);
      }
      if (p2 >= second.numValues()) {
	secondI = m_instances.numAttributes();
      } else {
	secondI = second.index(p2);
      }
      if (firstI == m_instances.classIndex()) {
	p1++; continue;
      }
      if (secondI == m_instances.classIndex()) {
	p2++; continue;
      }
      double diff;
      if (firstI == secondI) {
	diff = difference(firstI,
			  first.valueSparse(p1),
			  second.valueSparse(p2));
	p1++; p2++;
      } else if (firstI > secondI) {
	diff = difference(secondI,
			  0, second.valueSparse(p2));
	p2++;
      } else {
	diff = difference(firstI,
			  first.valueSparse(p1), 0);
	p1++;
      }
      distance += diff * diff;
    }

    return Math.sqrt(distance / m_instances.numAttributes());
  }

  /**
   * Computes the difference between two given attribute
   * values.
   */
  private double difference(int index, double val1, double val2) {

    switch (m_instances.attribute(index).type()) {
    case org.agentacademy.modules.dataminer.core.Attribute.NOMINAL:

      // If attribute is nominal
      if (Instance.isMissingValue(val1) ||
	  Instance.isMissingValue(val2) ||
	  ((int)val1 != (int)val2)) {
	return 1;
      } else {
	return 0;
      }
    case org.agentacademy.modules.dataminer.core.Attribute.NUMERIC:

      // If attribute is numeric
      if (Instance.isMissingValue(val1) ||
	  Instance.isMissingValue(val2)) {
	if (Instance.isMissingValue(val1) &&
	    Instance.isMissingValue(val2)) {
	  return 1;
	} else {
	  double diff;
	  if (Instance.isMissingValue(val2)) {
	    diff = norm(val1, index);
	  } else {
	    diff = norm(val2, index);
	  }
	  if (diff < 0.5) {
	    diff = 1.0 - diff;
	  }
	  return diff;
	}
      } else {
	return norm(val1, index) - norm(val2, index);
      }
    default:
      return 0;
    }
  }

  /**
   * Normalizes a given value of a numeric attribute.
   *
   * @param x the value to be normalized
   * @param i the attribute's index
   */
  private double norm(double x, int i) {

    if (Double.isNaN(m_Min[i]) || Utils.eq(m_Max[i],m_Min[i])) {
      return 0;
    } else {
      return (x - m_Min[i]) / (m_Max[i] - m_Min[i]);
    }
  }

  /**
   * Updates the minimum and maximum values for all the attributes
   * based on a new instance.
   *
   * @param instance the new instance
   */
  private void updateMinMax(Instance instance) {

    for (int j = 0;j < m_instances.numAttributes(); j++) {
      if (!instance.isMissing(j)) {
	if (Double.isNaN(m_Min[j])) {
	  m_Min[j] = instance.value(j);
	  m_Max[j] = instance.value(j);
	} else {
	  if (instance.value(j) < m_Min[j]) {
	    m_Min[j] = instance.value(j);
	  } else {
	    if (instance.value(j) > m_Max[j]) {
	      m_Max[j] = instance.value(j);
	    }
	  }
	}
      }
    }
  }

  /**

?? 快捷鍵說明

復制代碼 Ctrl + C
搜索代碼 Ctrl + F
全屏模式 F11
切換主題 Ctrl + Shift + D
顯示快捷鍵 ?
增大字號 Ctrl + =
減小字號 Ctrl + -
亚洲欧美第一页_禁久久精品乱码_粉嫩av一区二区三区免费野_久草精品视频
欧美色大人视频| 国内精品视频666| 91色婷婷久久久久合中文| 久久久久久久免费视频了| 久久精品国产亚洲5555| 欧美xxxx老人做受| 懂色av一区二区在线播放| 欧美高清在线一区| 在线观看欧美精品| 日本不卡一区二区| 久久色.com| 成年人国产精品| 亚洲精品视频免费看| 欧美日韩一区在线观看| 久久99精品国产| 欧美国产日韩一二三区| 91精品福利在线| 麻豆精品视频在线观看免费| 久久美女艺术照精彩视频福利播放 | 在线亚洲高清视频| 亚洲成av人片一区二区梦乃| 日韩一级大片在线观看| 国产v综合v亚洲欧| 亚洲国产日韩一级| 久久久久国产一区二区三区四区| 不卡av在线网| 天天色图综合网| 国产欧美精品一区二区三区四区 | 日韩免费看网站| 成人久久久精品乱码一区二区三区| 日韩理论片网站| 欧美乱妇23p| 成人在线视频首页| 日av在线不卡| 亚洲免费在线观看视频| 日韩免费性生活视频播放| av高清久久久| 韩国三级中文字幕hd久久精品| 亚洲视频在线一区观看| 精品成人私密视频| 欧美日韩一级黄| 成人激情小说乱人伦| 日本视频中文字幕一区二区三区| 国产日韩欧美亚洲| 欧美一区午夜精品| 99精品欧美一区二区三区综合在线| 手机精品视频在线观看| 亚洲视频资源在线| 久久久久成人黄色影片| 在线不卡免费欧美| 91论坛在线播放| 国产成人av一区二区三区在线 | 天天操天天干天天综合网| 中文字幕巨乱亚洲| 精品久久人人做人人爱| 欧美日韩高清影院| 91影视在线播放| 豆国产96在线|亚洲| 国产一区二区三区在线观看免费 | 91精品国产高清一区二区三区 | 国产精品丝袜在线| 久久亚洲精华国产精华液| 欧美另类变人与禽xxxxx| 91天堂素人约啪| 丁香婷婷综合色啪| 国产精品亚洲成人| 国产乱对白刺激视频不卡| 日本不卡123| 日韩激情视频在线观看| 亚洲高清免费在线| 一区二区欧美视频| 亚洲视频免费看| 亚洲黄色尤物视频| 亚洲综合在线视频| 综合久久国产九一剧情麻豆| 中文字幕欧美国产| 国产精品久久看| 国产精品久久久久久久午夜片| 国产日韩精品一区| 欧美精品一区二区三| 精品美女在线观看| 欧美精品一区二区蜜臀亚洲| www成人在线观看| 欧美精品一区二区久久久| 欧美mv日韩mv国产网站app| 精品久久免费看| 国产婷婷色一区二区三区在线| 久久精子c满五个校花| 国产日韩欧美高清| 中文字幕视频一区| 亚洲美女屁股眼交3| 一区二区三区毛片| 日韩激情中文字幕| 激情av综合网| jizzjizzjizz欧美| 在线观看国产一区二区| 欧美一区二区福利在线| 精品国产乱码久久久久久闺蜜| 欧美精品一区二区三区蜜桃| 中文字幕av一区二区三区| 综合久久国产九一剧情麻豆| 亚洲成精国产精品女| 美日韩一区二区三区| 国产精品一区二区久久精品爱涩 | 色狠狠色噜噜噜综合网| 欧美在线观看18| 91精品国产高清一区二区三区 | 日韩在线播放一区二区| 麻豆国产精品一区二区三区| 国产成人丝袜美腿| 欧洲一区二区三区免费视频| 欧美一级免费大片| 亚洲国产精品二十页| 亚洲成人动漫在线观看| 国产麻豆精品在线观看| 99re成人精品视频| 精品三级av在线| 亚洲美女在线一区| 久久不见久久见中文字幕免费| 成人午夜电影久久影院| 91.xcao| 国产精品久久久久毛片软件| 亚洲第一在线综合网站| 国产九色sp调教91| 欧美熟乱第一页| 中国av一区二区三区| 丝袜诱惑制服诱惑色一区在线观看| 国产在线不卡一区| 欧美三片在线视频观看| 欧美激情在线看| 美国一区二区三区在线播放| 96av麻豆蜜桃一区二区| 久久在线观看免费| 亚洲一区av在线| 成a人片亚洲日本久久| 欧美岛国在线观看| 亚洲成人av资源| 成人av电影免费观看| 久久视频一区二区| 日韩av一区二区三区| 日本韩国精品在线| 欧美激情艳妇裸体舞| 韩国三级中文字幕hd久久精品| 欧美亚洲国产一区在线观看网站| 国产婷婷色一区二区三区在线| 日产国产高清一区二区三区| 色婷婷国产精品久久包臀| 中文字幕国产精品一区二区| 蜜桃一区二区三区在线观看| 在线免费观看日韩欧美| 亚洲色图一区二区| 国产成人av一区二区三区在线| 欧美电影免费观看高清完整版在线| 亚洲精品第一国产综合野| 成人av电影免费观看| 久久精品人人爽人人爽| 激情久久五月天| 亚洲精品在线三区| 蜜臀久久99精品久久久久宅男| 欧美日韩一区 二区 三区 久久精品| 亚洲欧美一区二区三区国产精品 | 99麻豆久久久国产精品免费优播| 久久久久久久久久久久电影| 久久丁香综合五月国产三级网站| 欧美日韩国产高清一区| 亚洲第一久久影院| 欧美日韩国产三级| 偷拍自拍另类欧美| 欧美精品久久天天躁| 天天综合色天天综合| 欧美一级片免费看| 免费成人美女在线观看.| 日韩欧美中文字幕制服| 美脚の诱脚舐め脚责91| 久久综合九色综合欧美就去吻 | 偷窥少妇高潮呻吟av久久免费| 欧美色图在线观看| 午夜激情久久久| 欧美一区二区三区的| 精品一区二区三区免费观看 | 综合久久久久久| 一本色道**综合亚洲精品蜜桃冫| 亚洲日本成人在线观看| 欧美中文字幕一二三区视频| 午夜成人免费电影| 欧美一级片在线| 国产精品系列在线播放| 亚洲同性gay激情无套| 91成人在线观看喷潮| 亚洲va欧美va人人爽午夜| 制服丝袜亚洲色图| 极品销魂美女一区二区三区| 国产精品无码永久免费888| 成人黄色国产精品网站大全在线免费观看 | 国产精品成人在线观看| 在线影视一区二区三区| 日韩福利电影在线| 国产午夜精品久久| 91福利视频久久久久| 青娱乐精品视频在线|