/// <summary> /// To scale the test data /// </summary> /// <param name="orig_data">Original test data, use NaN for missing features.</param> /// <param name="targe_min">The target min</param> /// <param name="targe_max">The target max</param> /// <param name="features_min">The minimums of all features</param> /// <param name="features_max">The maximums of all features</param> /// <returns>The scaled test data in LibSVM format.</returns> public static LibSVMNode[] ScaleData(double[] orig_data, double targe_min, double targe_max, double[] features_min, double[] features_max) { int feature_n = features_min.Length; double[] k = new double[feature_n]; for (int i = 0; i < feature_n; i++) { k[i] = (targe_max - targe_min) / (features_max[i] - features_min[i]); } List <LibSVMNode> scaled_data = new List <LibSVMNode>(); LibSVMNode node; for (int i = 0; i < feature_n; i++) { if (double.IsNaN(orig_data[i])) { continue; } node = new LibSVMNode(); node.index = i + 1; node.value = k[i] * (orig_data[i] - features_min[i]) + targe_min; scaled_data.Add(node); } node = new LibSVMNode(); node.index = -1; scaled_data.Add(node); return(scaled_data.ToArray()); }
/// <summary> /// To scale the test data /// </summary> /// <param name="orig_data">Original test data, use NaN for missing features.</param> /// <param name="targe_min">The target min</param> /// <param name="targe_max">The target max</param> /// <param name="features_min">The minimums of all features</param> /// <param name="features_max">The maximums of all features</param> /// <returns>The scaled test data in LibSVM format.</returns> public static LibSVMNode[][] ScaleData(List <List <double> > orig_data, double targe_min, double targe_max, double[] features_min, double[] features_max) { int feature_n = features_min.Length; double[] k = new double[feature_n]; for (int i = 0; i < feature_n; i++) { k[i] = (targe_max - targe_min) / (features_max[i] - features_min[i]); } int sample_n = orig_data.Count; LibSVMNode[][] data = new LibSVMNode[sample_n][]; for (int m = 0; m < sample_n; m++) { List <LibSVMNode> scaled_data = new List <LibSVMNode>(); LibSVMNode node; for (int i = 0; i < feature_n; i++) { if (double.IsNaN(orig_data[m][i])) { continue; } node = new LibSVMNode(); node.index = i + 1; node.value = k[i] * (orig_data[m][i] - features_min[i]) + targe_min; scaled_data.Add(node); } node = new LibSVMNode(); node.index = -1; scaled_data.Add(node); data[m] = scaled_data.ToArray(); } return(data); }
/// <summary> /// Convert sample features to LibSVM format /// </summary> /// <param name="sample_s">sample feature in string, i.e., one record in LibSVM file. "Target index:value index:value ..."</param> /// <param name="sample">the converted sample feature in LibSVM format.</param> /// <param name="label">the label</param> public static void ToLibSVMFormat(string sample_s, out LibSVMNode[] sample, out double label) { string[] words = sample_s.Trim().Split(); label = double.Parse(words[0]); sample = new LibSVMNode[words.Length]; for (int i = 1; i < words.Length; i++) { string[] ws = words[i].Split(new char[] { ':' }); sample[i - 1].index = int.Parse(ws[0]); sample[i - 1].value = double.Parse(ws[1]); } sample[words.Length - 1].index = -1; }
/// <summary> /// Convert the SVM Linear model to single vector representation /// </summary> public unsafe float[] ToSingleVector() { LibSVMNode[][] support_vectors = new LibSVMNode[nrSV][]; for (int i = 0; i < nrSV; i++) { LibSVMNode[] sv = new LibSVMNode[nrFeature]; fixed(LibSVMNode *sv_p = sv) { svm_get_sv(svm_model, i, new IntPtr(sv_p)); } support_vectors[i] = sv; } double rho = svm_get_rho(svm_model); double[] coef = new double[nrSV]; fixed(double *coef_p = coef) { svm_get_coef(svm_model, new IntPtr(coef_p)); } float[] single_vector = new float[nrFeature + 1]; int index; double value; for (int i = 0; i < nrSV; i++) { for (int j = 0; j < support_vectors[i].Length; j++) { index = support_vectors[i][j].index; if (index == -1) { break; } value = support_vectors[i][j].value; single_vector[index - 1] += (float)(value * coef[i]); } } single_vector[nrFeature] = -(float)rho; return(single_vector); }
/// <summary> /// To scale the test data /// </summary> /// <param name="orig_data">Original test data in LibSVM format.</param> /// <param name="targe_min">The target min</param> /// <param name="targe_max">The target max</param> /// <param name="features_min">The minimums of all features</param> /// <param name="features_max">The maximums of all features</param> /// <returns>The scaled test data in LibSVM format.</returns> public static LibSVMNode[] ScaleData(LibSVMNode[] orig_data, double targe_min, double targe_max, double[] features_min, double[] features_max) { int feature_n = features_min.Length; double[] k = new double[feature_n]; for (int i = 0; i < feature_n; i++) { k[i] = (targe_max - targe_min) / (features_max[i] - features_min[i]); } int n = orig_data.Length; LibSVMNode[] scaled_data = new LibSVMNode[n]; for (int i = 0; i < n; i++) { int index = orig_data[i].index; scaled_data[i].index = index; if (index == -1) { break; } scaled_data[i].value = k[index - 1] * (orig_data[i].value - features_min[index - 1]) + targe_min; } return(scaled_data); }