-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharray.go
116 lines (105 loc) · 2.19 KB
/
array.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
package hyperopt
/*
Mostly, this file is copied from the github.com/c-bata/goptuna project
*/
import "sort"
func ones1d(size int) []float64 {
ones := make([]float64, size)
for i := 0; i < size; i++ {
ones[i] = 1
}
return ones
}
func linspace(start, stop float64, num int, endPoint bool) []float64 {
step := 0.
if endPoint {
if num == 1 {
return []float64{start}
}
step = (stop - start) / float64(num-1)
} else {
if num == 0 {
return []float64{}
}
step = (stop - start) / float64(num)
}
r := make([]float64, num, num)
for i := 0; i < num; i++ {
r[i] = start + float64(i)*step
}
return r
}
func choice(array []float64, idxs []int) []float64 {
results := make([]float64, len(idxs))
for i, idx := range idxs {
results[i] = array[idx]
}
return results
}
func bincount(x []int, weights []float64, minlength int) []float64 {
// Count the number of occurrences of each value in array of non-negative ints.
// https://docs.scipy.org/doc/numpy/reference/generated/numpy.bincount.html
counts := make([]float64, minlength)
for i := range x {
if x[i] > len(counts)-1 {
for j := len(counts) - 1; j < x[i]; j++ {
counts = append(counts, 0)
}
}
if x[i] > len(weights)-1 {
counts[x[i]]++
} else {
counts[x[i]] += weights[x[i]]
}
}
return counts
}
func clip(array []float64, min, max float64) {
for i := range array {
if array[i] < min {
array[i] = min
} else if array[i] > max {
array[i] = max
}
}
}
func location(array []float64, key float64) int {
i := 0
size := len(array)
for {
mid := (i + size) / 2
if i == size {
break
}
if array[mid] < key {
i = mid + 1
} else {
size = mid
}
}
return i
}
func argSort2d(lossVals [][2]float64) []int {
type sortable struct {
index int
lossVal [2]float64
}
x := make([]sortable, len(lossVals))
for i := 0; i < len(lossVals); i++ {
x[i] = sortable{
index: i,
lossVal: lossVals[i],
}
}
sort.SliceStable(x, func(i, j int) bool {
if x[i].lossVal[0] == x[j].lossVal[0] {
return x[i].lossVal[1] < x[j].lossVal[1]
}
return x[i].lossVal[0] < x[j].lossVal[0]
})
results := make([]int, len(x))
for i := 0; i < len(x); i++ {
results[i] = x[i].index
}
return results
}