-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDataProcessor.cs
108 lines (93 loc) · 3.2 KB
/
DataProcessor.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace Kohonen_SOM
{
class DataProcessor
{
private string _path;
private List<double[]> _inputs = new List<double[]>();
public DataProcessor(string path) => _path = path;
public List<double[]> GetNormalizedData()
{
try
{
string line; int lineCount = 0;
var streamReader = new StreamReader(_path);
line = streamReader.ReadLine();
while (line != null)
{
if (lineCount != 0)
{
string[] columns = line.Split(',');
List<double> row = new List<double>();
foreach (var item in columns)
{
string value = item.Replace('.', ',');//double değere parse için ',' gerekiyor
try
{
row.Add(double.Parse(value));
}
catch (FormatException e)
{
row.Add(DummyCoding(item));
}
}
_inputs.Add(row.ToArray());
}
line = streamReader.ReadLine();
lineCount++;
}
streamReader.Close();
}
catch (IOException e)
{
Console.WriteLine(e);
}
catch (Exception e)
{
Console.WriteLine(e);
}
//veriler alındıktan ve double değerlere dönüştürüldükten sonra normalizasyon yapılır;
DataNormalization();
return _inputs;
}
//gelen harflerin alfabe sıralarına göre dummy coding yapan metod;
private double DummyCoding(string input)
{
double letterIndex = 0;
if (input != "TT")
{
char[] cArray = input.ToCharArray();
letterIndex = cArray[0] - 64;//harfin alfabedeki değerini verir
}
else// TT gelirse;
{
letterIndex = 9;
}
return letterIndex;
}
//min-max normalizasyonu
private void DataNormalization()
{
List<double> columnValues = new List<double>();
for (int i = 0; i < _inputs[0].Length; i++)
{
foreach (var row in _inputs)
{
columnValues.Add(row[i]);
}
foreach (var row in _inputs)
{
//min-max normalization
row[i] = (row[i] - columnValues.Min()) /
(columnValues.Max() - columnValues.Min());
}
columnValues.Clear();
}
}
}
}