/// <summary> /// Data input yang dimasukkan dari array 0 - akhir itu nilai reflectance dari wave length: 2501.982414 sampe 1350.724346 /// </summary> /// <param name="DataReflectance"></param> /// <returns></returns> static ModelInput PreProcessData(List <float> DataReflectance) { try { var item = new ModelInput(); //convert to absorbance for (int col = 0; col < DataReflectance.Count; col++) { DataReflectance[col] = (float)Math.Log(1 / DataReflectance[col]); } //sav gol filter SavitzkyGolayFilter filter = new SavitzkyGolayFilter(11, 2); List <double> rowDatas = new List <double>(); for (int col = 0; col < DataReflectance.Count; col++) { rowDatas.Add(DataReflectance[col]); } var filteredRow = filter.Process(rowDatas.ToArray()); for (int col = 0; col < DataReflectance.Count; col++) { DataReflectance[col] = (float)filteredRow[col]; } //SNV rowDatas = new List <double>(); for (int col = 0; col < DataReflectance.Count; col++) { rowDatas.Add(DataReflectance[col]); } var mean = rowDatas.Average(); var stdDev = MathExt.StdDev(rowDatas); for (int col = 0; col < DataReflectance.Count; col++) { DataReflectance[col] = (float)((DataReflectance[col] - mean) / stdDev); } item.FeatureVector = DataReflectance.ToArray(); return(item); } catch { return(null); } }
static void PreProcessed(string TargetFolder, string PreProcessFolder) { if (!Directory.Exists(PreProcessFolder)) { Directory.CreateDirectory(PreProcessFolder); } var fileCsvs = Directory.GetFiles(TargetFolder, "*.csv"); foreach (var file in fileCsvs) { var dt = ConvertCSVtoDataTable(file); //convert to absorbance foreach (DataRow dr in dt.Rows) { for (int col = 1; col < dt.Columns.Count; col++) { dr[col] = Math.Log(1 / double.Parse(dr[col].ToString())); } } dt.AcceptChanges(); //sav gol filter SavitzkyGolayFilter filter = new SavitzkyGolayFilter(11, 2); foreach (DataRow dr in dt.Rows) { List <double> rowDatas = new List <double>(); for (int col = 1; col < dt.Columns.Count; col++) { rowDatas.Add(double.Parse(dr[col].ToString())); } var filteredRow = filter.Process(rowDatas.ToArray()); for (int col = 1; col < dt.Columns.Count; col++) { dr[col] = filteredRow[col - 1]; } } dt.AcceptChanges(); //SNV foreach (DataRow dr in dt.Rows) { List <double> rowDatas = new List <double>(); for (int col = 1; col < dt.Columns.Count; col++) { rowDatas.Add(double.Parse(dr[col].ToString())); } var mean = rowDatas.Average(); var stdDev = MathExt.StdDev(rowDatas); for (int col = 1; col < dt.Columns.Count; col++) { dr[col] = (double.Parse(dr[col].ToString()) - mean) / stdDev; } } dt.AcceptChanges(); //save to file { var col = 0; StringBuilder sb = new StringBuilder(); //print column name sb.Append(dt.Columns[col].ColumnName); for (int x = 1; x < dt.Columns.Count; x++) { sb.Append("," + dt.Columns[x].ColumnName); } sb.Append(Environment.NewLine); //print data column foreach (DataRow dr in dt.Rows) { sb.Append(dr[col].ToString()); for (int x = 1; x < dt.Columns.Count; x++) { sb.Append("," + dr[x].ToString()); } sb.Append(Environment.NewLine); } var PathStr = PreProcessFolder + "\\" + dt.Columns[col].ColumnName + ".csv"; File.WriteAllText(PathStr, sb.ToString()); ConsoleHelper.Print($"{dt.Columns[col].ColumnName} - file generated."); } } }