-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnorm.go
320 lines (276 loc) · 10.1 KB
/
norm.go
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
package norm
import (
"encoding/csv"
"fmt"
"io"
"math/big"
"os"
"reflect"
"vscode/gostart/gen"
)
func DeNormalizeValue() {
realNum, _ := new(big.Int).SetString("67551153748445296705730", 10)
minValue, _ := new(big.Int).SetString("56706253667142884486418", 10)
maxValue, _ := new(big.Int).SetString("75513164846306307011680", 10)
minValueBF := new(big.Float).SetInt(minValue)
maxValueBF := new(big.Float).SetInt(maxValue)
estimatedValue, _ := new(big.Float).SetString("0.5775671903588346")
diff := new(big.Float)
muldiff := new(big.Float)
result := new(big.Float)
diff.Sub(maxValueBF, minValueBF)
muldiff.Mul(estimatedValue, diff)
result.Add(muldiff, minValueBF)
resultNum := new(big.Int)
result.Int(resultNum)
fmt.Printf("resultNum : %30v\n", resultNum)
fmt.Printf("realNum - resultNum: %30v\n", realNum.Sub(realNum, resultNum))
}
func NormalizeRecords(rawRecordsFileName string, bitSize uint, recordsFileName string) {
rawRecordsFile, osOpenErr := os.Open(rawRecordsFileName)
if osOpenErr != nil {
panic(fmt.Sprintf("Failed to open csv file, %v\n", osOpenErr))
}
// defer file close so the file is always closed
defer rawRecordsFile.Close()
csvReader := csv.NewReader(rawRecordsFile)
headers, err := csvReader.Read()
if err != nil {
panic(fmt.Sprintf("Failed to read %v first record, err %v", rawRecordsFileName, err))
}
if !reflect.DeepEqual(gen.RawRecordFieldNames(), headers) {
panic(fmt.Sprintf("Failed to read %v headers, expected %v headers, got headers %v", rawRecordsFileName, gen.RawRecordFieldNames(), headers))
}
// initialize min/max
numberNMin := new(big.Int)
numberNMax := new(big.Int)
smallerPrimeMin := new(big.Int)
smallerPrimeMax := new(big.Int)
biggerPrimeMin := new(big.Int)
biggerPrimeMax := new(big.Int)
primesSumMin := new(big.Int)
primesSumMax := new(big.Int)
numberNMax.SetInt64(0)
smallerPrimeMax.SetInt64(0)
biggerPrimeMax.SetInt64(0)
primesSumMax.SetInt64(0)
// calculate max possible value according to bitSize
bitSizeAsBigInt := new(big.Int)
bitSizeAsBigInt.SetInt64(int64(bitSize))
numberNMin.SetInt64(2)
numberNMin.Exp(numberNMin, bitSizeAsBigInt, nil)
smallerPrimeMin.Set(numberNMin)
biggerPrimeMin.Set(numberNMin)
primesSumMin.Set(numberNMin)
// read first record since for "increment" is executed after
record, err := csvReader.Read()
for ; record != nil && err == nil; record, err = csvReader.Read() {
readRawRecord := convertToRawRecord(record)
if numberNMin.Cmp(readRawRecord.NumberN) > 0 {
numberNMin.Set(readRawRecord.NumberN)
}
if smallerPrimeMin.Cmp(readRawRecord.SmallerPrime) > 0 {
smallerPrimeMin.Set(readRawRecord.SmallerPrime)
}
if biggerPrimeMin.Cmp(readRawRecord.BiggerPrime) > 0 {
biggerPrimeMin.Set(readRawRecord.BiggerPrime)
}
if primesSumMin.Cmp(readRawRecord.PrimesSum) > 0 {
primesSumMin.Set(readRawRecord.PrimesSum)
}
if numberNMax.Cmp(readRawRecord.NumberN) < 0 {
numberNMax.Set(readRawRecord.NumberN)
}
if smallerPrimeMax.Cmp(readRawRecord.SmallerPrime) < 0 {
smallerPrimeMax.Set(readRawRecord.SmallerPrime)
}
if biggerPrimeMax.Cmp(readRawRecord.BiggerPrime) < 0 {
biggerPrimeMax.Set(readRawRecord.BiggerPrime)
}
if primesSumMax.Cmp(readRawRecord.PrimesSum) < 0 {
primesSumMax.Set(readRawRecord.PrimesSum)
}
}
if err != nil && err != io.EOF {
panic(fmt.Sprintf("Failed to read %v a record, err %v", rawRecordsFileName, err))
}
// reset file handler to begin of the file
rawRecordsFile.Seek(0, 0)
headers, err = csvReader.Read()
if err != nil {
panic(fmt.Sprintf("Failed to re-read %v first record, err %v", rawRecordsFileName, err))
}
// create summary CSV file
{
// create records file
recordsFile, osCreateErr := os.Create(recordsFileName + "_summar.csv")
if osCreateErr != nil {
panic(fmt.Sprintf("Failed to create csv file, %v\n", osCreateErr))
}
// defer file close so the file is always closed
defer recordsFile.Close()
// write header
csvWriter := csv.NewWriter(recordsFile)
csvWriteErr := csvWriter.Write([]string{
"numberNMin",
"numberNMax",
"smallerPrimeMin",
"smallerPrimeMax",
"biggerPrimeMin",
"biggerPrimeMax",
"primesSumMin",
"primesSumMax"})
if csvWriteErr != nil {
panic(fmt.Sprintf("Failed to write header to csv file, %v\n", csvWriteErr))
}
csvWriteErr = csvWriter.Write([]string{
fmt.Sprintf("%v", numberNMin),
fmt.Sprintf("%v", numberNMax),
fmt.Sprintf("%v", smallerPrimeMin),
fmt.Sprintf("%v", smallerPrimeMax),
fmt.Sprintf("%v", biggerPrimeMin),
fmt.Sprintf("%v", biggerPrimeMax),
fmt.Sprintf("%v", primesSumMin),
fmt.Sprintf("%v", primesSumMax),
},
)
if csvWriteErr != nil {
panic(fmt.Sprintf("Failed to write header to csv file, %v\n", csvWriteErr))
}
// defer flush because write does not immediatly write records
// and the defer close will not flush before closing the file
defer csvWriter.Flush()
}
// convert max/mins to big float to avoid multiple conversions
numberNMinAsBigFloat := new(big.Float).SetInt(numberNMin)
numberNMaxAsBigFloat := new(big.Float).SetInt(numberNMax)
smallerPrimeMinAsBigFloat := new(big.Float).SetInt(smallerPrimeMin)
smallerPrimeMaxAsBigFloat := new(big.Float).SetInt(smallerPrimeMax)
biggerPrimeMinAsBigFloat := new(big.Float).SetInt(biggerPrimeMin)
biggerPrimeMaxAsBigFloat := new(big.Float).SetInt(biggerPrimeMax)
primesSumMinAsBigFloat := new(big.Float).SetInt(primesSumMin)
primesSumMaxAsBigFloat := new(big.Float).SetInt(primesSumMax)
// Shaddy normalization, using the same scale for both smaller and bigger prime
biggerPrimeMinAsBigFloat.Set(smallerPrimeMinAsBigFloat)
smallerPrimeMaxAsBigFloat.Set(biggerPrimeMaxAsBigFloat)
// create records file
recordsFile, osCreateErr := os.Create(recordsFileName)
if osCreateErr != nil {
panic(fmt.Sprintf("Failed to create csv file, %v\n", osCreateErr))
}
// defer file close so the file is always closed
defer recordsFile.Close()
// write header
csvWriter := csv.NewWriter(recordsFile)
csvWriteErr := csvWriter.Write(RecordFieldNames())
if csvWriteErr != nil {
panic(fmt.Sprintf("Failed to write header to csv file, %v\n", csvWriteErr))
}
// defer flush because write does not immediatly write records
// and the defer close will not flush before closing the file
defer csvWriter.Flush()
// read first raw record since for "increment" is executed after
record, err = csvReader.Read()
for ; record != nil && err == nil; record, err = csvReader.Read() {
readRawRecord := convertToRawRecord(record)
normalizedRecord := normalizeRecord(
readRawRecord,
numberNMinAsBigFloat,
numberNMaxAsBigFloat,
smallerPrimeMinAsBigFloat,
smallerPrimeMaxAsBigFloat,
biggerPrimeMinAsBigFloat,
biggerPrimeMaxAsBigFloat,
primesSumMinAsBigFloat,
primesSumMaxAsBigFloat)
csvWriteErr := csvWriter.Write(normalizedRecord.AsStringSlice())
if csvWriteErr != nil {
panic(fmt.Sprintf("Failed to write csv file, %v\n", csvWriteErr))
}
}
}
type Record struct {
NumberN float64
SmallerPrime float64
BiggerPrime float64
PrimesSum float64
}
func RecordFieldNames() []string {
return []string{"NumberN", "SmallerPrime", "BiggerPrime", "PrimesSum"}
}
func (source Record) AsStringSlice() []string {
return []string{fmt.Sprintf("%v", source.NumberN), fmt.Sprintf("%v", source.SmallerPrime), fmt.Sprintf("%v", source.BiggerPrime), fmt.Sprintf("%v", source.PrimesSum)}
}
// normalize record using min-max strategy
// reference https://docs.microsoft.com/en-us/azure/machine-learning/studio-module-reference/normalize-data#-how-to-configure-normalize-data
func normalizeRecord(readRawRecord gen.RawRecord,
numberNMin *big.Float,
numberNMax *big.Float,
smallerPrimeMin *big.Float,
smallerPrimeMax *big.Float,
biggerPrimeMin *big.Float,
biggerPrimeMax *big.Float,
primesSumMin *big.Float,
primesSumMax *big.Float) Record {
var normalizedRecord Record
dividend := new(big.Float)
divisor := new(big.Float)
quotient := new(big.Float)
// normalize NumberN
dividend.SetInt(readRawRecord.NumberN)
dividend.Sub(dividend, numberNMin)
divisor.Sub(numberNMax, numberNMin)
quotient.Quo(dividend, divisor)
normalizedRecord.NumberN, _ = quotient.Float64()
// normalize SmallerPrime
dividend.SetInt(readRawRecord.SmallerPrime)
dividend.Sub(dividend, smallerPrimeMin)
divisor.Sub(smallerPrimeMax, smallerPrimeMin)
quotient.Quo(dividend, divisor)
normalizedRecord.SmallerPrime, _ = quotient.Float64()
// normalize BiggerPrime
dividend.SetInt(readRawRecord.BiggerPrime)
dividend.Sub(dividend, biggerPrimeMin)
divisor.Sub(biggerPrimeMax, biggerPrimeMin)
quotient.Quo(dividend, divisor)
normalizedRecord.BiggerPrime, _ = quotient.Float64()
// normalize PrimesSum
dividend.SetInt(readRawRecord.PrimesSum)
dividend.Sub(dividend, primesSumMin)
divisor.Sub(primesSumMax, primesSumMin)
quotient.Quo(dividend, divisor)
normalizedRecord.PrimesSum, _ = quotient.Float64()
return normalizedRecord
}
func convertToRawRecord(record []string) gen.RawRecord {
rawRecordNumberOfColumns := len(gen.RawRecordFieldNames())
var parsedRawRecord gen.RawRecord
if len(record) != rawRecordNumberOfColumns {
panic(fmt.Sprintf("Failed to parse a raw record, unexpected number of columns, expected %v columns, got this %v", rawRecordNumberOfColumns, record))
}
numberN := new(big.Int)
smallerPrime := new(big.Int)
biggerPrime := new(big.Int)
primesSum := new(big.Int)
_, numberWasSet := numberN.SetString(record[0], 10)
if !numberWasSet {
panic(fmt.Sprintf("Failed to parse a raw record number, read value as string %v", record[0]))
}
_, numberWasSet = smallerPrime.SetString(record[1], 10)
if !numberWasSet {
panic(fmt.Sprintf("Failed to parse a raw record number, read value as string %v", record[1]))
}
_, numberWasSet = biggerPrime.SetString(record[2], 10)
if !numberWasSet {
panic(fmt.Sprintf("Failed to parse a raw record number, read value as string %v", record[2]))
}
_, numberWasSet = primesSum.SetString(record[3], 10)
if !numberWasSet {
panic(fmt.Sprintf("Failed to parse a raw record number, read value as string %v", record[3]))
}
parsedRawRecord.NumberN = numberN
parsedRawRecord.SmallerPrime = smallerPrime
parsedRawRecord.BiggerPrime = biggerPrime
parsedRawRecord.PrimesSum = primesSum
return parsedRawRecord
}