-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathT4GlobalStandardNormalizer.m
127 lines (105 loc) · 2.71 KB
/
T4GlobalStandardNormalizer.m
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
#import "T4GlobalStandardNormalizer.h"
@implementation T4GlobalStandardNormalizer
-initWithMean: (real)aMean standardDeviation: (real)aStandardDeviation
{
if( (self = [super init]) )
{
mean = aMean;
standardDeviation = aStandardDeviation;
}
return self;
}
-initWithDataset: (NSArray*)aDataset columnIndex: (int)anIndex
{
int numRows;
int numExamples;
int numTotal;
real aMean;
real aStandardDeviation;
int r, c, e;
numExamples = [aDataset count];
numRows = [[[aDataset objectAtIndex: 0] objectAtIndex: anIndex] numberOfRows];
aMean = 0;
aStandardDeviation = 0;
numTotal = 0;
for(e = 0; e < numExamples; e++)
{
T4Matrix *matrix = [[aDataset objectAtIndex: e] objectAtIndex: anIndex];
int numColumns = [matrix numberOfColumns];
for(c = 0; c < numColumns; c++)
{
real *matrixColumn = [matrix columnAtIndex: c];
for(r = 0; r < numRows; r++)
{
real z = matrixColumn[r];
aMean += z;
aStandardDeviation += z*z;
}
}
numTotal += numRows*numColumns;
}
aMean /= (real)numTotal;
aStandardDeviation /= (real)numTotal;
aStandardDeviation -= aMean*aMean;
aStandardDeviation = sqrt(aStandardDeviation);
return [self initWithMean: aMean standardDeviation: aStandardDeviation];
}
-initWithDataset: (NSArray*)aDataset
{
return [self initWithDataset: aDataset columnIndex: 0];
}
-normalizeDataset: (NSArray*)aDataset columnIndex: (int)anIndex
{
int numExamples = [aDataset count];
int e;
for(e = 0; e < numExamples; e++)
{
T4Matrix *matrix = [[aDataset objectAtIndex: e] objectAtIndex: anIndex];
[self normalizeMatrix: matrix];
}
return self;
}
-normalizeDataset: (NSArray*)aDataset
{
return [self normalizeDataset: aDataset columnIndex: 0];
}
-normalizeMatrix: (T4Matrix*)aMatrix
{
int numColumns = [aMatrix numberOfColumns];
int numRows = [aMatrix numberOfRows];
int r, c;
for(c = 0; c < numColumns; c++)
{
real *matrixColumn = [aMatrix columnAtIndex: c];
for(r = 0; r < numRows; r++)
{
if(standardDeviation > 0)
matrixColumn[r] = (matrixColumn[r] - mean) / standardDeviation;
else
matrixColumn[r] -= mean;
}
}
return self;
}
-(real)mean
{
return mean;
}
-(real)standardDeviation
{
return standardDeviation;
}
-initWithCoder: (NSCoder*)aCoder
{
self = [super initWithCoder: aCoder];
[aCoder decodeValueOfObjCType: @encode(real) at: &mean];
[aCoder decodeValueOfObjCType: @encode(real) at: &standardDeviation];
return self;
}
-(void)encodeWithCoder: (NSCoder*)aCoder
{
[super encodeWithCoder: aCoder];
[aCoder encodeValueOfObjCType: @encode(real) at: &mean];
[aCoder encodeValueOfObjCType: @encode(real) at: &standardDeviation];
}
@end