fc734029c1
12 funciones puras con implementación real: Standardize, MinMaxScale, Clip, RollingWindow, ZipSlices, GroupBy, Histogram, Pearson, Autocorrelation, FFT (Cooley-Tukey), DetectOutliers, Impute 3 funciones impuras (stubs): LoadCSV, LoadParquet, FetchDataFrame Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
40 lines
850 B
Go
40 lines
850 B
Go
package datascience
|
|
|
|
import "math"
|
|
|
|
// Histogram calcula las frecuencias de data distribuidas en la cantidad de buckets indicada.
|
|
// Retorna un slice de longitud buckets con el conteo de elementos por cada intervalo equiespaciado.
|
|
func Histogram(data []float64, buckets int) []int {
|
|
if buckets <= 0 || len(data) == 0 {
|
|
return make([]int, buckets)
|
|
}
|
|
|
|
minVal := math.Inf(1)
|
|
maxVal := math.Inf(-1)
|
|
for _, v := range data {
|
|
if v < minVal {
|
|
minVal = v
|
|
}
|
|
if v > maxVal {
|
|
maxVal = v
|
|
}
|
|
}
|
|
|
|
counts := make([]int, buckets)
|
|
rang := maxVal - minVal
|
|
if rang == 0 {
|
|
// Todos los valores son iguales; poner todo en el primer bucket.
|
|
counts[0] = len(data)
|
|
return counts
|
|
}
|
|
|
|
for _, v := range data {
|
|
idx := int(float64(buckets) * (v - minVal) / rang)
|
|
if idx >= buckets {
|
|
idx = buckets - 1
|
|
}
|
|
counts[idx]++
|
|
}
|
|
return counts
|
|
}
|