亚洲欧美第一页_禁久久精品乱码_粉嫩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一区二区三区免费野_久草精品视频
日韩国产欧美视频| 在线观看91精品国产麻豆| 色猫猫国产区一区二在线视频| 欧美亚洲日本国产| 中文字幕免费不卡在线| 天天av天天翘天天综合网色鬼国产| 成人三级伦理片| 欧美大尺度电影在线| 日本vs亚洲vs韩国一区三区| 99视频精品在线| 国产日韩欧美精品在线| 日韩精品亚洲一区二区三区免费| av一区二区三区| 亚洲国产精品二十页| 国产在线播精品第三| 精品国产露脸精彩对白 | 欧美国产亚洲另类动漫| 日本欧美加勒比视频| 欧美欧美欧美欧美| 夜夜嗨av一区二区三区中文字幕| 白白色 亚洲乱淫| 国产欧美综合在线观看第十页| 麻豆久久一区二区| 日韩亚洲欧美综合| 欧美bbbbb| 日韩视频在线永久播放| 日韩精品免费专区| 欧美一级欧美一级在线播放| 日日骚欧美日韩| 日韩一区二区视频| 日本女人一区二区三区| 欧美一区二区成人| 久久er精品视频| 2欧美一区二区三区在线观看视频| 日本aⅴ免费视频一区二区三区| 在线播放/欧美激情| 亚洲成av人片一区二区梦乃| 国产高清久久久| 日韩一区二区免费视频| 日韩精品色哟哟| 精品精品国产高清一毛片一天堂| 日韩高清一区在线| xnxx国产精品| 国产成人免费视| 亚洲天堂免费看| 欧美日韩成人综合| 九九精品视频在线看| 久久久高清一区二区三区| 成人午夜又粗又硬又大| 亚洲色图在线看| 67194成人在线观看| 激情六月婷婷久久| 国产精品国产馆在线真实露脸 | 成人综合在线视频| 国产欧美精品国产国产专区| av电影在线观看一区| 亚洲一区在线看| 日韩免费成人网| 91一区二区三区在线观看| 天堂va蜜桃一区二区三区| 久久久蜜臀国产一区二区| 一本一道综合狠狠老| 免费国产亚洲视频| 成人欧美一区二区三区| 91麻豆精品国产91久久久| 国产99精品国产| 日本最新不卡在线| 中文字幕一区二区三区乱码在线| 91成人国产精品| 国产福利一区在线| 亚洲电影中文字幕在线观看| 精品精品欲导航| 欧美中文字幕久久| 成人综合在线网站| 麻豆精品在线看| 亚洲永久精品国产| 国产亚洲美州欧州综合国| 欧美私人免费视频| 成人综合激情网| 免费成人你懂的| 亚洲国产综合人成综合网站| 亚洲国产高清在线观看视频| 欧美一区二区黄色| 欧美亚洲另类激情小说| 成人国产精品免费观看| 久久97超碰色| 免费在线成人网| 一区二区三区 在线观看视频| 久久夜色精品一区| 欧美一级免费观看| 欧美日韩一区二区三区视频| 99天天综合性| 岛国一区二区三区| 精东粉嫩av免费一区二区三区| 亚洲一卡二卡三卡四卡| 国产精品久久福利| 久久久夜色精品亚洲| 91精品国产入口在线| 欧美艳星brazzers| 色偷偷88欧美精品久久久| 国产凹凸在线观看一区二区| 美腿丝袜在线亚洲一区| 日本v片在线高清不卡在线观看| 亚洲国产va精品久久久不卡综合| 亚洲视频一区二区在线观看| 国产精品污www在线观看| 国产亚洲精品超碰| 久久婷婷综合激情| 久久亚洲一级片| 国产亚洲制服色| 国产欧美日韩在线| 国产精品无遮挡| 亚洲欧洲综合另类在线| 亚洲精品亚洲人成人网| 亚洲欧美色图小说| 亚洲伊人伊色伊影伊综合网| 一区二区三区四区五区视频在线观看 | 日本va欧美va精品发布| 日韩av一区二区三区四区| 日韩高清一区在线| 老司机精品视频线观看86| 国模大尺度一区二区三区| 久久成人久久爱| 成人夜色视频网站在线观看| 成人黄色综合网站| 在线视频中文字幕一区二区| 欧美日韩中文字幕一区二区| 日韩一区二区免费在线观看| 欧美精品一区二区三区蜜桃视频| 久久久久99精品国产片| 国产精品成人午夜| 亚洲一二三四区| 久草在线在线精品观看| 国产精品538一区二区在线| 成人爱爱电影网址| 91久久精品一区二区三| 欧美一区日韩一区| 久久精品一区四区| 综合亚洲深深色噜噜狠狠网站| 亚洲高清久久久| 国产一区 二区 三区一级| 成人黄色免费短视频| 欧美日韩高清不卡| 久久久国产一区二区三区四区小说| 国产精品无圣光一区二区| 午夜视频在线观看一区二区| 国产一区视频导航| 欧美在线一区二区三区| 欧美成人性福生活免费看| 日韩美女啊v在线免费观看| 免费观看30秒视频久久| aaa欧美日韩| 日韩欧美另类在线| 国产精品白丝在线| 丝袜亚洲精品中文字幕一区| 国产一区二区毛片| 欧美私模裸体表演在线观看| 国产欧美日韩精品在线| 午夜国产精品一区| gogo大胆日本视频一区| 国产精品久线观看视频| 日韩成人精品在线观看| 91伊人久久大香线蕉| 在线不卡中文字幕播放| 成人免费小视频| 久久er精品视频| 欧美日产国产精品| 亚洲欧洲无码一区二区三区| 久草热8精品视频在线观看| 91国偷自产一区二区使用方法| 久久久久久久久97黄色工厂| 亚洲成人久久影院| 99re成人在线| 久久久高清一区二区三区| 美女在线一区二区| 欧美日韩另类一区| 国产精品第五页| 国产成人av资源| 日韩欧美电影一二三| 日韩av电影天堂| 欧美人xxxx| 香蕉久久一区二区不卡无毒影院| 91同城在线观看| 国产精品久久久久久久裸模| 国产精品一区二区久久精品爱涩| 日韩欧美一区二区三区在线| 午夜国产精品一区| 欧美酷刑日本凌虐凌虐| 亚洲一区二区三区小说| 91久久精品一区二区二区| 亚洲欧美另类小说| 色综合久久久久综合99| 亚洲三级在线看| 色综合久久综合| 亚洲美女一区二区三区| 在线亚洲人成电影网站色www| 成人欧美一区二区三区1314| 91理论电影在线观看| 亚洲激情自拍视频| 欧美日精品一区视频|