rocketlaunchr / dataframe-go

DataFrames for Go: For statistics, machine-learning, and data manipulation/exploration
Other
1.19k stars 95 forks source link

stat init #4

Closed vlamai closed 6 years ago

vlamai commented 6 years ago

another try

rocketlaunchr-cto commented 6 years ago

Any issues with this one?

vlamai commented 6 years ago

Please stop write messages like in chat or create chat in discord or gitter.

This function is just for test, how it will work. Isn't work

package main

import (
    "fmt"
    "github.com/kzoper/dataframe-go"
    "github.com/kzoper/dataframe-go/stat"
)

func main() {
    s2 := dataframe.NewSeriesFloat64("sales", nil, 50.3, -23.4, 56.2, nil, nil, 84.2, 72, 89)
    stat.StFloat64{SeriesFloat64: s2}.Abs()
    fmt.Println(s2)
}

screenshot from 2018-11-03 15-47-26

rocketlaunchr-cto commented 6 years ago

Github automatically makes it like chat format. I just wrote "single comments" in the "Files Changed" tab. They end up in the "conversations" section as "chat like" comments.

rocketlaunchr-cto commented 6 years ago

The main issue is in your stat package, you are referencing "github.com/rocketlaunchr/dataframe-go", but in your main file, you are referencing "github.com/kzoper/dataframe-go" so they are different types.

I also see what you are trying to do. In that case remove the pointers and pointer references:

type StFloat64 struct {
    dataframe.SeriesFloat64
}

type SeriesInt64 struct {
    dataframe.SeriesInt64
}

func (s StFloat64) Abs() {
    for key, value := range s.Values {
        if value == nil {
            continue
        }
        val := math.Abs(*value)
        s.Values[key] = &val
    }
}

func (s SeriesInt64) Abs() {
    for key, value := range s.Values {
        val := *value
        if val < 0 {
            val = val * -1
        }
        s.Values[key] = &val
    }
}

In your main.go:

    s2 := dataframe.NewSeriesFloat64("sales", nil, 50.3, -23.4, 56.2, nil, nil, 84.2, 72, 89)
    stat.StFloat64{*s2}.Abs()
    fmt.Println(s2)
vlamai commented 6 years ago

No. Change reference and :

package main

import (
    "fmt"
    "github.com/kzoper/dataframe-go"
    "github.com/kzoper/dataframe-go/stat"
)

func main() {
    //s1 := dataframe.NewSeriesInt64("day", nil, 1, 2, 3, 4, 5, 6, 7, 8)
    s2 := dataframe.NewSeriesFloat64("sales", nil, 50.3, -23.4, 56.2, nil, nil, 84.2, 72, 89)
    //df := dataframe.NewDataFrame(s1, s2)
    stat.StFloat64{SeriesFloat64: s2}.Abs() // error

    fmt.Println(s2)
}

./main.go:13:35: cannot call pointer method on stat.StFloat64 literal ./main.go:13:35: cannot take the address of stat.StFloat64 literal

rocketlaunchr-cto commented 6 years ago

See my prior comment:

1) In stat package - remove pointers and pointer receivers in the your functions (sorry for misleading you. I now have a better understanding of what you are trying to do) 2) stat.StFloat64{*s2}.Abs() // dereference the pointer

vlamai commented 6 years ago
package stat

import (
    "github.com/kzoper/dataframe-go"
    "math"
)

type Stat interface {
    dataframe.Series
    Abs()
}

type StFloat64 struct {
    dataframe.SeriesFloat64
}

type SeriesInt64 struct {
    dataframe.SeriesInt64
}

func (s *StFloat64) Abs() {
    for key, value := range s.Values {
        val := math.Abs(*value)
        s.Values[key] = &val
    }
}

func (s *SeriesInt64) Abs() {
    for key, value := range s.Values {
        val := *value
        if val < 0 {
            val = val * -1
        }
        s.Values[key] = &val
    }
}
// it remove pointer here isn't work too
func (s StFloat64) Abs() {
    for key, value := range s.Values {
        val := math.Abs(*value)
        s.Values[key] = &val
    }
}

change. Still isn't working

rocketlaunchr-cto commented 6 years ago

screen shot 2018-11-03 at 10 20 32 pm screen shot 2018-11-03 at 10 21 33 pm

I just cloned your package and made the above changes and it works.

vlamai commented 6 years ago
package stat

import (
    "github.com/kzoper/dataframe-go"
    "math"
)

type Stat interface {
    dataframe.Series
    Abs()
}

type StFloat64 struct {
    dataframe.SeriesFloat64
}

type SeriesInt64 struct {
    dataframe.SeriesInt64
}

func (s StFloat64) Abs() {
    for key, value := range s.Values {
        val := math.Abs(*value) // ERROR HERE
        s.Values[key] = &val
    }
}

func (s *SeriesInt64) Abs() {
    for key, value := range s.Values {
        val := *value
        if val < 0 {
            val = val * -1
        }
        s.Values[key] = &val
    }
}
package main

import (
    "fmt"
    "github.com/kzoper/dataframe-go"
    "github.com/kzoper/dataframe-go/stat"
)

func main() {
    s2 := dataframe.NewSeriesFloat64("sales", nil, 50.3, -23.4, 56.2, nil, nil, 84.2, 72, 89)
    stat.StFloat64{*s2}.Abs()

    fmt.Println(s2)
}
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0x4ac3d4]

goroutine 1 [running]:
github.com/kzoper/dataframe-go/stat.StFloat64.Abs(0x4e9c18, 0x0, 0x0, 0x0, 0x4e263a, 0x5, 0xc000024300, 0x8, 0x8)
    /home/kzoper/go/src/github.com/kzoper/dataframe-go/stat/stat.go:23 +0x74
main.main()
    /home/kzoper/go/src/github.com/kzoper/dataFramTest/main.go:11 +0x1e7

Process finished with exit code 2
rocketlaunchr-cto commented 6 years ago

You have to do a nil check because some of your values are nil. See the image I attached. 50.3, -23.4, 56.2, nil, nil, 84.2, 72, 89

The 2 nils are causing the issue.

vlamai commented 6 years ago

Now it works

rocketlaunchr-cto commented 6 years ago

Why did you close the PR?

vlamai commented 6 years ago
  1. Because Abs is only one function that change values. Other function return series of bools or one value.
  2. Using stat.StFloat64{*s2} inconveniently. We have to find another way. screenshot from 2018-11-04 04-28-49