Skip to content

Instantly share code, notes, and snippets.

@denisvmedia
Last active May 19, 2023 11:54
Show Gist options
  • Save denisvmedia/e8979d5c1f7efd7422be54f83814cbbd to your computer and use it in GitHub Desktop.
Save denisvmedia/e8979d5c1f7efd7422be54f83814cbbd to your computer and use it in GitHub Desktop.
Predicting the Missing Humidity Values

Predicting the Missing Humidity Values

part1 part2 part3 part4

package main
import (
"fmt"
"time"
"github.com/sjwhitworth/golearn/base"
"github.com/sjwhitworth/golearn/linear_models"
)
func mustParseDT(dtStr string) int64 {
layout := "2006-01-02 15:04"
str := dtStr
t, err := time.Parse(layout, str)
if err != nil {
panic(err)
}
return t.Unix()
}
func abs(n int64) int64 {
y := n >> 63
return (n ^ y) - y
}
func checkerr(err error) {
if err != nil {
panic(err)
}
}
func predictMissingHumidity(startDate string, endDate string, knownTimestamps []string, humidity []float64, timestamps []string) []float64 {
lm := linear_models.NewLinearRegression()
grid := base.NewDenseInstances()
attrs := []base.Attribute{
base.NewFloatAttribute("Temperature"),
base.NewFloatAttribute("Timestamp"),
}
specs := make([]base.AttributeSpec, len(attrs))
// Allocate the Instances to return
for i, a := range attrs {
spec := grid.AddAttribute(a)
specs[i] = spec
}
checkerr(grid.Extend(len(knownTimestamps)))
for i, item := range humidity {
grid.Set(specs[0], i, base.PackFloatToBytes(item))
}
for i, item := range knownTimestamps {
ts := abs(time.Unix(0, 0).Unix() - mustParseDT(item))
grid.Set(specs[1], i, base.PackFloatToBytes(float64(ts)))
}
checkerr(grid.AddClassAttribute(attrs[0]))
checkerr(lm.Fit(grid))
gridZ := base.NewStructuralCopy(grid)
attrsZ := gridZ.AllAttributes()
specsZ := base.ResolveAttributes(gridZ, attrsZ)
checkerr(gridZ.Extend(len(timestamps)))
for i, item := range timestamps {
ts := abs(time.Unix(0, 0).Unix() - mustParseDT(item))
//gridZ.Set(specsZ[0], i, base.PackFloatToBytes(0.0))
gridZ.Set(specsZ[1], i, base.PackFloatToBytes(float64(ts)))
}
predicted, err := lm.Predict(gridZ)
checkerr(err)
result := make([]float64, len(timestamps))
for i := range result {
result[i] = base.UnpackBytesToFloat(predicted.Get(specsZ[0], i))
}
return result
}
func main() {
fmt.Println(predictMissingHumidity("2013-01-01", "2013-01-01", []string{
"2013-01-01 07:00",
"2013-01-01 08:00",
"2013-01-01 09:00",
"2013-01-01 10:00",
"2013-01-01 11:00",
"2013-01-01 12:00",
}, []float64{
10.0,
11.1,
13.2,
14.8,
15.6,
16.7,
}, []string{
"2013-01-01 13:00",
"2013-01-01 14:00",
}))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment