Changeset 6002
- Timestamp:
- 04/11/11 18:41:03 (14 years ago)
- Location:
- trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/Linear/AlglibUtil.cs
r5809 r6002 27 27 public static class AlglibUtil { 28 28 public static double[,] PrepareInputMatrix(Dataset dataset, IEnumerable<string> variables, IEnumerable<int> rows) { 29 List<int> allowedRows = CalculateAllowedRows(dataset, variables, rows).ToList(); 29 List<string> variablesList = variables.ToList(); 30 List<int> rowsList = rows.ToList(); 30 31 31 double[,] matrix = new double[ allowedRows.Count, variables.Count()];32 for (int row = 0; row < allowedRows.Count; row++) {32 double[,] matrix = new double[rowsList.Count, variablesList.Count]; 33 for (int row = 0; row < rowsList.Count; row++) { 33 34 int col = 0; 34 35 foreach (string column in variables) { 35 matrix[row, col] = dataset[column, row ];36 matrix[row, col] = dataset[column, rowsList[row]]; 36 37 col++; 37 38 } … … 39 40 return matrix; 40 41 } 41 42 private static IEnumerable<int> CalculateAllowedRows(Dataset dataset, IEnumerable<string> variables, IEnumerable<int> rows) {43 // return only rows that contain no infinity or NaN values44 return from row in rows45 where (from variable in variables46 let x = dataset[variable, row]47 where double.IsInfinity(x) || double.IsNaN(x)48 select 1)49 .Any() == false50 select row;51 }52 42 } 53 43 } -
trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/Linear/LinearDiscriminantAnalysis.cs
r5809 r6002 73 73 int nClasses = problemData.ClassNames.Count(); 74 74 double[,] inputMatrix = AlglibUtil.PrepareInputMatrix(dataset, allowedInputVariables.Concat(new string[] { targetVariable }), rows); 75 if (inputMatrix.Cast<double>().Any(x => double.IsNaN(x) || double.IsInfinity(x))) 76 throw new NotSupportedException("Linear discriminant analysis does not support NaN or infinity values in the input dataset."); 75 77 76 78 // change class values into class index -
trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/Linear/LinearRegression.cs
r5809 r6002 76 76 IEnumerable<int> rows = Enumerable.Range(samplesStart, samplesEnd - samplesStart); 77 77 double[,] inputMatrix = AlglibUtil.PrepareInputMatrix(dataset, allowedInputVariables.Concat(new string[] { targetVariable }), rows); 78 if (inputMatrix.Cast<double>().Any(x => double.IsNaN(x) || double.IsInfinity(x))) 79 throw new NotSupportedException("Linear regression does not support NaN or infinity values in the input dataset."); 78 80 79 81 alglib.linearmodel lm = new alglib.linearmodel(); -
trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/SupportVectorMachine/SupportVectorMachineUtil.cs
r5809 r6002 41 41 int maxNodeIndex = 0; 42 42 int svmProblemRowIndex = 0; 43 List<string> inputVariablesList = inputVariables.ToList(); 43 44 foreach (int row in rowIndices) { 44 45 tempRow = new List<SVM.Node>(); 45 foreach (var inputVariable in inputVariables) { 46 int col = dataset.GetVariableIndex(inputVariable); 47 double value = dataset[row, col]; 46 int colIndex = 1; // make sure the smallest node index for SVM = 1 47 foreach (var inputVariable in inputVariablesList) { 48 double value = dataset[row, dataset.GetVariableIndex(inputVariable)]; 49 // SVM also works with missing values 50 // => don't add NaN values in the dataset to the sparse SVM matrix representation 48 51 if (!double.IsNaN(value)) { 49 int nodeIndex = col + 1; // make sure the smallest nodeIndex is 1 (libSVM convention) 50 tempRow.Add(new SVM.Node(nodeIndex, value)); 51 if (nodeIndex > maxNodeIndex) maxNodeIndex = nodeIndex; 52 tempRow.Add(new SVM.Node(colIndex, value)); // nodes must be sorted in ascending ordered by column index 53 if (colIndex > maxNodeIndex) maxNodeIndex = colIndex; 52 54 } 55 colIndex++; 53 56 } 54 nodes[svmProblemRowIndex++] = tempRow. OrderBy(x => x.Index).ToArray(); // make sure the values are sorted by node index57 nodes[svmProblemRowIndex++] = tempRow.ToArray(); 55 58 } 56 59 -
trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/kMeans/KMeansClustering.cs
r5914 r6002 92 92 int[] xyc; 93 93 double[,] inputMatrix = AlglibUtil.PrepareInputMatrix(dataset, allowedInputVariables, rows); 94 if (inputMatrix.Cast<double>().Any(x => double.IsNaN(x) || double.IsInfinity(x))) 95 throw new NotSupportedException("k-Means clustering does not support NaN or infinity values in the input dataset."); 96 94 97 alglib.kmeansgenerate(inputMatrix, inputMatrix.GetLength(0), inputMatrix.GetLength(1), k, restarts + 1, out info, out centers, out xyc); 95 98 if (info != 1) throw new ArgumentException("Error in calculation of k-Means clustering solution");
Note: See TracChangeset
for help on using the changeset viewer.