Skip to content

Instantly share code, notes, and snippets.

@allisonmorgan
Last active September 8, 2015 20:20
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save allisonmorgan/d288c5a0a8209bc53300 to your computer and use it in GitHub Desktop.
Save allisonmorgan/d288c5a0a8209bc53300 to your computer and use it in GitHub Desktop.
Anomalyzer Example from CSV
package main
import (
"encoding/csv"
"log"
"os"
"strconv"
"github.com/lytics/anomalyzer"
)
func ReadCSV(filepath string) ([][]string, error) {
csvfile, err := os.Open(filepath)
if err != nil {
return nil, err
}
defer csvfile.Close()
reader := csv.NewReader(csvfile)
fields, err := reader.ReadAll()
return fields, nil
}
func main() {
// determine what parameters you'd like to use here
conf := &anomalyzer.AnomalyzerConf{
Sensitivity: 0.1,
ActiveSize: 1,
NSeasons: 4,
Methods: []string{"diff", "highrank", "lowrank", "magnitude"},
}
// load your csv
records, err := ReadCSV("./bitcoin_simplified.csv")
if err != nil {
log.Fatal(err)
}
// write anomaly detection results to a new csv
outfile, err := os.Create("./bitcoin_anomalyzer.csv")
if err != nil {
log.Fatal("Unable to open output")
}
defer outfile.Close()
writer := csv.NewWriter(outfile)
anom, _ := anomalyzer.NewAnomalyzer(conf, []float64{})
for i, record := range records {
// assuming your csv looks like (time, value)
time := record[0]
value := record[1]
// if your csv has a header (like mine), you will want to skip that row
if i == 0 {
writer.Write([]string{time, value, "anomaly"})
continue
}
price, err := strconv.ParseFloat(value, 64)
if err != nil {
log.Fatal("Record: %v, Error: %v", value, err)
}
prob := anom.Push(price)
priceString := strconv.FormatFloat(price, 'f', 8, 64)
probString := strconv.FormatFloat(prob, 'f', 8, 64)
//fmt.Printf("Result: %v\n", []string{time, priceString, probString})
writer.Write([]string{time, priceString, probString})
}
writer.Flush()
}
package main
import (
"encoding/csv"
"fmt"
"log"
"os"
"strconv"
"time"
"github.com/lytics/anomalyzer"
)
func ReadCSV(filepath string) ([][]string, error) {
csvfile, err := os.Open(filepath)
if err != nil {
return nil, err
}
defer csvfile.Close()
reader := csv.NewReader(csvfile)
fields, err := reader.ReadAll()
return fields, nil
}
func main() {
// determine what parameters you'd like to use here
conf := &anomalyzer.AnomalyzerConf{
Sensitivity: 0.1,
ActiveSize: 1,
NSeasons: 4,
Methods: []string{"diff", "highrank", "lowrank", "magnitude"},
}
// load your csv
records, err := ReadCSV("./bitcoin_simplified.csv")
if err != nil {
log.Fatal(err)
}
// write anomaly detection results to a new csv
outfile, err := os.Create("./bitcoin_anomalyzer.csv")
if err != nil {
log.Fatal("Unable to open output")
}
defer outfile.Close()
writer := csv.NewWriter(outfile)
j := 1
var data []float64
start := time.Now()
for i, record := range records {
// assuming your csv looks like (time, value)
time := record[0]
value := record[1]
// if your csv has a header (like mine), you will want to skip that row
if i == 0 {
writer.Write([]string{time, value, "anomaly"})
continue
}
price, err := strconv.ParseFloat(value, 64)
if err != nil {
log.Fatal("Record: %v, Error: %v", value, err)
}
// if you've got a lot of data, you might start to remove old data
// to increase speed
var prob float64
data = append(data, price)
if i < 100 {
anom, _ := anomalyzer.NewAnomalyzer(conf, data)
prob = anom.Eval()
} else {
anom, _ := anomalyzer.NewAnomalyzer(conf, data[j:])
prob = anom.Eval()
j++
}
priceString := strconv.FormatFloat(price, 'f', 8, 64)
probString := strconv.FormatFloat(prob, 'f', 8, 64)
//fmt.Printf("Result #%v: %v\n", i, []string{time, priceString, probString})
writer.Write([]string{time, priceString, probString})
}
writer.Flush()
fmt.Printf("Time elapsed: %v\n", time.Now().Sub(start))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment