📄 cobweb.java
字号:
0934: * if the class is enumerated, otherwise the predicted value
0935: * @throws Exception if instance could not be classified
0936: * successfully
0937: */
0938: public int clusterInstance(Instance instance) throws Exception {
0939: CNode host = m_cobwebTree;
0940: CNode temp = null;
0941:
0942: determineNumberOfClusters();
0943:
0944: do {
0945: if (host.m_children == null) {
0946: temp = null;
0947: break;
0948: }
0949:
0950: host.updateStats(instance, false);
0951: temp = host.findHost(instance, true);
0952: host.updateStats(instance, true);
0953:
0954: if (temp != null) {
0955: host = temp;
0956: }
0957: } while (temp != null);
0958:
0959: return host.m_clusterNum;
0960: }
0961:
0962: /**
0963: * determines the number of clusters if necessary
0964: *
0965: * @see #m_numberOfClusters
0966: * @see #m_numberOfClustersDetermined
0967: */
0968: protected void determineNumberOfClusters() {
0969: if (!m_numberOfClustersDetermined && (m_cobwebTree != null)) {
0970: int[] numClusts = new int[1];
0971: numClusts[0] = 0;
0972: try {
0973: m_cobwebTree.assignClusterNums(numClusts);
0974: } catch (Exception e) {
0975: e.printStackTrace();
0976: numClusts[0] = 0;
0977: }
0978: m_numberOfClusters = numClusts[0];
0979:
0980: m_numberOfClustersDetermined = true;
0981: }
0982: }
0983:
0984: /**
0985: * Returns the number of clusters.
0986: *
0987: * @return the number of clusters
0988: */
0989: public int numberOfClusters() {
0990: determineNumberOfClusters();
0991: return m_numberOfClusters;
0992: }
0993:
0994: /**
0995: * Adds an instance to the clusterer.
0996: *
0997: * @param newInstance the instance to be added
0998: * @throws Exception if something goes wrong
0999: */
1000: public void updateClusterer(Instance newInstance) throws Exception {
1001: m_numberOfClustersDetermined = false;
1002:
1003: if (m_cobwebTree == null) {
1004: m_cobwebTree = new CNode(newInstance.numAttributes(),
1005: newInstance);
1006: } else {
1007: m_cobwebTree.addInstance(newInstance);
1008: }
1009: }
1010:
1011: /**
1012: * Adds an instance to the Cobweb tree.
1013: *
1014: * @param newInstance the instance to be added
1015: * @throws Exception if something goes wrong
1016: * @deprecated updateClusterer(Instance) should be used instead
1017: * @see #updateClusterer(Instance)
1018: */
1019: public void addInstance(Instance newInstance) throws Exception {
1020: updateClusterer(newInstance);
1021: }
1022:
1023: /**
1024: * Returns an enumeration describing the available options.
1025: *
1026: * @return an enumeration of all the available options.
1027: **/
1028: public Enumeration listOptions() {
1029: Vector result = new Vector();
1030:
1031: result.addElement(new Option("\tAcuity.\n" + "\t(default=1.0)",
1032: "A", 1, "-A <acuity>"));
1033:
1034: result.addElement(new Option("\tCutoff.\n"
1035: + "\t(default=0.002)", "C", 1, "-C <cutoff>"));
1036:
1037: Enumeration en = super .listOptions();
1038: while (en.hasMoreElements())
1039: result.addElement(en.nextElement());
1040:
1041: return result.elements();
1042: }
1043:
1044: /**
1045: * Parses a given list of options. <p/>
1046: *
1047: <!-- options-start -->
1048: * Valid options are: <p/>
1049: *
1050: * <pre> -A <acuity>
1051: * Acuity.
1052: * (default=1.0)</pre>
1053: *
1054: * <pre> -C <cutoff>
1055: * Cutoff.
1056: * (default=0.002)</pre>
1057: *
1058: * <pre> -S <num>
1059: * Random number seed.
1060: * (default 42)</pre>
1061: *
1062: <!-- options-end -->
1063: *
1064: * @param options the list of options as an array of strings
1065: * @throws Exception if an option is not supported
1066: */
1067: public void setOptions(String[] options) throws Exception {
1068: String optionString;
1069:
1070: optionString = Utils.getOption('A', options);
1071: if (optionString.length() != 0) {
1072: Double temp = new Double(optionString);
1073: setAcuity(temp.doubleValue());
1074: } else {
1075: m_acuity = 1.0;
1076: }
1077: optionString = Utils.getOption('C', options);
1078: if (optionString.length() != 0) {
1079: Double temp = new Double(optionString);
1080: setCutoff(temp.doubleValue());
1081: } else {
1082: m_cutoff = 0.01 * Cobweb.m_normal;
1083: }
1084:
1085: super .setOptions(options);
1086: }
1087:
1088: /**
1089: * Returns the tip text for this property
1090: * @return tip text for this property suitable for
1091: * displaying in the explorer/experimenter gui
1092: */
1093: public String acuityTipText() {
1094: return "set the minimum standard deviation for numeric attributes";
1095: }
1096:
1097: /**
1098: * set the acuity.
1099: * @param a the acuity value
1100: */
1101: public void setAcuity(double a) {
1102: m_acuity = a;
1103: }
1104:
1105: /**
1106: * get the acuity value
1107: * @return the acuity
1108: */
1109: public double getAcuity() {
1110: return m_acuity;
1111: }
1112:
1113: /**
1114: * Returns the tip text for this property
1115: * @return tip text for this property suitable for
1116: * displaying in the explorer/experimenter gui
1117: */
1118: public String cutoffTipText() {
1119: return "set the category utility threshold by which to prune nodes";
1120: }
1121:
1122: /**
1123: * set the cutoff
1124: * @param c the cutof
1125: */
1126: public void setCutoff(double c) {
1127: m_cutoff = c;
1128: }
1129:
1130: /**
1131: * get the cutoff
1132: * @return the cutoff
1133: */
1134: public double getCutoff() {
1135: return m_cutoff;
1136: }
1137:
1138: /**
1139: * Returns the tip text for this property
1140: * @return tip text for this property suitable for
1141: * displaying in the explorer/experimenter gui
1142: */
1143: public String saveInstanceDataTipText() {
1144: return "save instance information for visualization purposes";
1145: }
1146:
1147: /**
1148: * Get the value of saveInstances.
1149: *
1150: * @return Value of saveInstances.
1151: */
1152: public boolean getSaveInstanceData() {
1153:
1154: return m_saveInstances;
1155: }
1156:
1157: /**
1158: * Set the value of saveInstances.
1159: *
1160: * @param newsaveInstances Value to assign to saveInstances.
1161: */
1162: public void setSaveInstanceData(boolean newsaveInstances) {
1163:
1164: m_saveInstances = newsaveInstances;
1165: }
1166:
1167: /**
1168: * Gets the current settings of Cobweb.
1169: *
1170: * @return an array of strings suitable for passing to setOptions()
1171: */
1172: public String[] getOptions() {
1173: int i;
1174: Vector<String> result;
1175: String[] options;
1176:
1177: result = new Vector<String>();
1178:
1179: result.add("-A");
1180: result.add("" + m_acuity);
1181: result.add("-C");
1182: result.add("" + m_cutoff);
1183:
1184: options = super .getOptions();
1185: for (i = 0; i < options.length; i++)
1186: result.add(options[i]);
1187:
1188: return result.toArray(new String[result.size()]);
1189: }
1190:
1191: /**
1192: * Returns a description of the clusterer as a string.
1193: *
1194: * @return a string describing the clusterer.
1195: */
1196: public String toString() {
1197: StringBuffer text = new StringBuffer();
1198: if (m_cobwebTree == null) {
1199: return "Cobweb hasn't been built yet!";
1200: } else {
1201: m_cobwebTree.dumpTree(0, text);
1202: return "Number of merges: " + m_numberMerges
1203: + "\nNumber of splits: " + m_numberSplits
1204: + "\nNumber of clusters: " + numberOfClusters()
1205: + "\n" + text.toString() + "\n\n";
1206:
1207: }
1208: }
1209:
1210: /**
1211: * Returns the type of graphs this class
1212: * represents
1213: * @return Drawable.TREE
1214: */
1215: public int graphType() {
1216: return Drawable.TREE;
1217: }
1218:
1219: /**
1220: * Generates the graph string of the Cobweb tree
1221: *
1222: * @return a <code>String</code> value
1223: * @throws Exception if an error occurs
1224: */
1225: public String graph() throws Exception {
1226: StringBuffer text = new StringBuffer();
1227:
1228: text.append("digraph CobwebTree {\n");
1229: m_cobwebTree.graphTree(text);
1230: text.append("}\n");
1231: return text.toString();
1232: }
1233:
1234: /**
1235: * Main method
1236: *
1237: * @param argv the commandline options
1238: */
1239: public static void main(String[] argv) {
1240: runClusterer(new Cobweb(), argv);
1241: }
1242: }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -