/
LogisticRegression.cs
159 lines (131 loc) · 5.42 KB
/
LogisticRegression.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace LinkedList
{
class LogisticReg
{
static void Main(string[] args)
{
DataTable table = new DataTable();
Util u = new Util();
const double ETA = 0.0001;
table = u.FileToTable(@"D:\Bharat\ML CMSC 678\HW2\ionosphere.arff");
table = u.Normalize(table);
DataTable dataSet = table.Clone();
DataView view = new DataView(table);
dataSet = view.ToTable();
dataSet.Columns.Remove("Class");
int total = table.Rows.Count;
int attrCount = table.Columns.Count - 1;
double[] wVector = new double[attrCount];
double[] gVector;
double dotProduct;
double denom;
double pi;
double error;
int cnt = 0;
while (true)
{
gVector = new double[attrCount];
for(int i = 0; i< total;i++)
{
double[] xi = dataSet.Rows[i].ItemArray.ToArray().Select(x => double.Parse(x.ToString())).ToArray<double>();
dotProduct = wVector.Zip(xi, (d1, d2) => d1 * d2).Sum();
denom = (double)(1 + Math.Exp(-dotProduct));
pi = (double)1/denom;
error = double.Parse(table.Rows[i]["Class"].ToString()) - pi;
for (int j = 0; j < gVector.Length; j++)
{
gVector[j] += error * double.Parse(dataSet.Rows[i][j].ToString());
}
}
double[] wVectorOld = wVector;
wVector = wVector.Zip(gVector, (d1, d2) => Math.Round(d1 + d2 * ETA,3)).ToArray<double>();
if (wVector.SequenceEqual(wVectorOld))
break;
cnt++;
}
DataTable testTable = new DataTable();
testTable = u.FileToTable(@"D:\Bharat\ML CMSC 678\HW2\ionoTest.arff");
DataTable testDataSet = testTable.Clone();
DataView testView = new DataView(testTable);
testDataSet = testView.ToTable();
testDataSet.Columns.Remove("Class");
int testTotal = testDataSet.Rows.Count;
testTable.Columns.Add("PClass", typeof(double));
testTable.Columns.Add("Result", typeof(bool));
double vProduct;
for(int i = 0; i < testTotal;i++)
{
double[] xi = testDataSet.Rows[i].ItemArray.ToArray().Select(x => double.Parse(x.ToString())).ToArray<double>();
vProduct = wVector.Zip(xi, (d1, d2) => d1 * d2).Sum();
if (vProduct > 0)
testTable.Rows[i]["PClass"] = 1;
else
testTable.Rows[i]["PClass"] = 0;
if (testTable.Rows[i]["PClass"].ToString() == testTable.Rows[i]["Class"].ToString())
testTable.Rows[i]["Result"] = true;
else
testTable.Rows[i]["Result"] = false;
}
decimal testDataCount = testTable.Rows.Count;
decimal trueCount = testTable.AsEnumerable().Count(row => row.Field<bool>("Result") == true);
double accuracy = (double)(trueCount / testDataCount) * 100;
Console.WriteLine("Accuracy : " + accuracy);
Console.ReadLine(); Console.ReadLine();
}
}
public class Util
{
public DataTable FileToTable(string filename)
{
DataTable table = new DataTable();
table.Columns.Add("C1", typeof(double));
string line;
System.IO.StreamReader file = new System.IO.StreamReader(filename);
while ((line = file.ReadLine()) != null)
{
if (line.ToLower().StartsWith("@attribute"))
{
table.Columns.Add(line.Split(' ')[1],typeof(double));
}
else if (line.StartsWith("@") == false && line.Length > 1)
{
string[] s = line.Split(',');
DataRow r = table.NewRow();
r[0] = 1.0;
int c = 1;
foreach (string val in s)
{
r[c++] = double.Parse(val);
}
table.Rows.Add(r);
}
else
{ }
}
file.Close();
return table;
}
public DataTable Normalize(DataTable table)
{
foreach(DataColumn col in table.Columns)
{
double max = (double)table.Compute("MAX(" + col.ColumnName + ")","");
double min = (double)table.Compute("MIN(" + col.ColumnName + ")", "");
foreach (DataRow row in table.Rows)
{
double a = double.Parse(row[col].ToString()) - min;
double b = max - min;
if (b != 0 )
row[col] = Math.Round(a/b,3);
}
}
return table;
}
}
}