rocketlaunchr / dataframe-go

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

Dev kzo #3

Closed vlamai closed 6 years ago

vlamai commented 6 years ago

test pull requests. Add Abs function to float and int series

rocketlaunchr-cto commented 6 years ago

Based on your PR, I can see that your intention is to transform all the values in the series to the Absolute value.

The way to do it is actually like this:


s1 := dataframe.NewSeriesInt64("day", nil, 1, 2, 3, 4, 5, 6, 7, 8)

s1.Lock()
n := s1.NRows(Options{DontLock:true})
for row := 0; row < n; row++ {
    val := s1.Value(row, Options{DontLock:true})
    if val == nil { continue }
    s1.Update(row, abs(val), Options{DontLock:true})
}
s1.Unlock()

You can make this into a function of your own.

My opinion is that having an Abs() function for a series doesn't make sense to many types of data types such as strings and dates and perhaps may more. It really only makes sense for numbers.

Do you have any good reasons for including it into the API? Maybe you have some good reasons.

vlamai commented 6 years ago
  1. Pandas series have this function.
  2. Any function can be done by yourself.
  3. It is simplest function that i can find. To test PR and find out what you think about API.

How do you think need try to add all this functions ? pandas Or you have some priorities to add functions ?

rocketlaunchr-cto commented 6 years ago

I can see from the pandas API: http://pandas.pydata.org/pandas-docs/stable/api.html#computations-descriptive-stats

I think since these are only applicable to SeriesInt64 and SeriesFloat64.

This will allow the main package to contain only the bare essentials.

What do you think of this idea?

rocketlaunchr-cto commented 6 years ago

I also want to add to the main package: Filter, Map and Reduce and use https://github.com/robpike/filter for inspiration.

This is created by the co-creator of Go and can work with any type (including time etc)

rocketlaunchr-cto commented 6 years ago

https://godoc.org/gonum.org/v1/gonum/stat can be leveraged for statistics.

vlamai commented 6 years ago

I'm obviously doing something wrong. create new package stat |-stat |--stat.go

package stat

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

type Stat interface {
    dataframe.Series
    Abs()
}

type SeriesFloat64 struct {
    dataframe.SeriesFloat64
}

type SeriesInt64 struct {
    dataframe.SeriesInt64
}

func (s *SeriesFloat64) 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
    }
}

but in stat_test.go i can't call function

package stat

import (
    "github.com/rocketlaunchr/dataframe-go"
    "testing"
)

func TestSeriesFloat64_Abs(t *testing.T) {
    s1 := dataframe.NewSeriesFloat64("num",nil,-1.0,1.0)
    s1.Abs() // error
}

can you create a sample ?

rocketlaunchr-cto commented 6 years ago

You are very close.

The error is because s1 doesn't have the Abs method. s1 is from the dataframe package.

It'll be something like this:


s1 := SeriesFloat64{ dataframe.NewSeriesFloat64("num",nil,-1.0,1.0) }
s1.Abs()

Also remember to check for nil values in your Abs( ) function.

You will also need to lock the series before you manipulate any of the contained values. (since your intention is to do an InPlace manipulation).

rocketlaunchr-cto commented 6 years ago

Something like this:

func (s *SeriesFloat64) Abs() {
    s.Lock()
    defer s.Unlock()

    for key := range s.Values {
        val := s.Values[key]
        if val != nil && *val < 0 {
            s.Values[key] = &[]float64{-1*val}[0]
        }
    }
}
vlamai commented 6 years ago
  1. In test it works, in code no.
  2. I think this method of using the function is inconvenient screenshot from 2018-11-03 14-22-19

If it's hard make an example of a function. My knowledge is apparently not enough

rocketlaunchr-cto commented 6 years ago

What are you trying to achieve?

rocketlaunchr-cto commented 6 years ago

Also embed the pointer:

type SeriesFloat64 struct {
    *dataframe.SeriesFloat64
}

type SeriesInt64 struct {
    *dataframe.SeriesInt64
}