/// <summary> /// Generates a prediction based on this Centroid /// </summary> /// <param name="inputData">Map with the ID or name of each field and its value</param> /// <param name="byName">Flag for the inputData key: /// * true if fieldName is the key, /// * false if the key is the field ID</param> /// <returns>The nearest centroid data for the inputData</returns> public LocalCentroid centroid(Dictionary <string, dynamic> inputData, bool byName = true) { LocalCentroid nearest; dynamic cleanInputData = filterInputData(inputData, byName); try { fillNumericDefaults(inputData); } catch (System.Exception e) { throw new System.Exception( "Failed to predict a centroid. Input" + " data must contain values for all " + "numeric fields to find a centroid. \n" + e); } Dictionary <string, string[]> uniqueTerms = getUniqueTerms(cleanInputData); nearest = new LocalCentroid(); nearest.distance = int.MaxValue; foreach (LocalCentroid centroid in this.centroids) { double?distance2 = centroid.distance2(cleanInputData, uniqueTerms, this.scales, nearest.distance); if (distance2 != null) { nearest.centroidId = centroid.centroidId; nearest.centroidName = centroid.centroidName; nearest.distance = distance2; } } nearest.distance = System.Math.Sqrt((double)nearest.distance); return(nearest); }
public LocalCluster(JObject jsonObject) { _jsonObject = jsonObject; resourceId = (string)jsonObject["resource"]; defaultNumericValue = (string)jsonObject["default_numeric_value"]; summaryFields = new List <string>(); foreach (JObject oneSummaryField in jsonObject["summary_fields"]) { summaryFields.Add((string)oneSummaryField); } centroids = new List <LocalCentroid>(); foreach (JObject oneCentroid in jsonObject["clusters"]["clusters"]) { LocalCentroid c = new LocalCentroid(oneCentroid); centroids.Add(c); } clusterGlobal = (JObject)jsonObject["clusters"]["global"]; Dictionary <string, dynamic> fields = jsonObject["clusters"]["fields"].ToObject <Dictionary <string, dynamic> >(); _fields = new Dictionary <string, DataSet.Field>(); foreach (KeyValuePair <string, dynamic> fieldData in fields) { string fieldId = fieldData.Key; DataSet.Field field = new DataSet.Field(fieldData.Value); _fields[fieldId] = field; nameToIdDict.Add(_fields[fieldId].Name, fieldId); fieldAllowEmpty[fieldId] = _fields[fieldId].OpType.Equals(OpType.Text) || _fields[fieldId].OpType.Equals(OpType.Items); if (_fields[fieldId].OpType.Equals(OpType.Text)) { this.termForms[fieldId] = new Dictionary <string, string[]>(); this.termForms[fieldId] = fieldData.Value["summary"]["term_forms"].ToObject <Dictionary <string, string[]> >(); List <string> listTags = new List <string>(); foreach (var tag in fieldData.Value["summary"]["tag_cloud"]) { listTags.Add((string)tag[0]); } this.tagClouds[fieldId] = listTags.ToArray(); this.termAnalysis[fieldId] = new Dictionary <string, dynamic>(); this.termAnalysis[fieldId] = field.TermAnalysis; } if (_fields[fieldId].OpType.Equals(OpType.Items)) { List <string> listItems = new List <string>(); foreach (string item in fieldData.Value["items"]) { listItems.Add(item); } this.items[fieldId] = listItems.ToArray(); this.itemAnalysis[fieldId] = new Dictionary <string, dynamic>(); this.itemAnalysis[fieldId] = field.ItemAnalysis; } } scales = jsonObject["scales"].ToObject <Dictionary <string, double> >(); }