From 8a1eba3aad3c8f97665b9c2c26746561eb25f1ac Mon Sep 17 00:00:00 2001 From: britikos Date: Fri, 19 Jan 2024 05:41:13 +0000 Subject: [PATCH] =?UTF-8?q?=D0=97=D0=B0=D0=B3=D1=80=D1=83=D0=B7=D0=B8?= =?UTF-8?q?=D1=82=D1=8C=20=D1=84=D0=B0=D0=B9=D0=BB=D1=8B=20=D0=B2=20=C2=AB?= =?UTF-8?q?/=C2=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- data.json | 24 ++++++ go.mod | 10 +++ go.sum | 8 ++ store.go | 249 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ store.yml | 5 ++ 5 files changed, 296 insertions(+) create mode 100644 data.json create mode 100644 go.mod create mode 100644 go.sum create mode 100644 store.go create mode 100644 store.yml diff --git a/data.json b/data.json new file mode 100644 index 0000000..9975388 --- /dev/null +++ b/data.json @@ -0,0 +1,24 @@ +{ + "store": { + "item": [ + { + "id": 0, + "category": "ML_model", + "title": "Titanic", + "price": 400 + }, + { + "id": 1, + "category": "ML_model", + "title": "Diabet", + "price": 400 + }, + { + "id": 2, + "category": "Not_ML_Model", + "title": "Punk_Rock", + "price": 700 + } + ] + } +} \ No newline at end of file diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..bf042e6 --- /dev/null +++ b/go.mod @@ -0,0 +1,10 @@ +module store + +go 1.21.6 + +require ( + github.com/nspcc-dev/neo-go/pkg/interop v0.0.0-20240112103746-ed7362810021 + github.com/sajari/regression v1.0.1 +) + +require gonum.org/v1/gonum v0.14.0 // indirect diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..480d134 --- /dev/null +++ b/go.sum @@ -0,0 +1,8 @@ +github.com/nspcc-dev/neo-go/pkg/interop v0.0.0-20240112103746-ed7362810021 h1:ojv5usqbh/Dxe67osTxjpA5mn51TxzyTOn7fbzBHdx0= +github.com/nspcc-dev/neo-go/pkg/interop v0.0.0-20240112103746-ed7362810021/go.mod h1:J/Mk6+nKeKSW4wygkZQFLQ6SkLOSGX5Ga0RuuuktEag= +github.com/sajari/regression v1.0.1 h1:iTVc6ZACGCkoXC+8NdqH5tIreslDTT/bXxT6OmHR5PE= +github.com/sajari/regression v1.0.1/go.mod h1:NeG/XTW1lYfGY7YV/Z0nYDV/RGh3wxwd1yW46835flM= +golang.org/x/exp v0.0.0-20230321023759-10a507213a29 h1:ooxPy7fPvB4kwsA2h+iBNHkAbp/4JxTSwCmvdjEYmug= +golang.org/x/exp v0.0.0-20230321023759-10a507213a29/go.mod h1:CxIveKay+FTh1D0yPZemJVgC/95VzuuOLq5Qi4xnoYc= +gonum.org/v1/gonum v0.14.0 h1:2NiG67LD1tEH0D7kM+ps2V+fXmsAnpUeec7n8tcr4S0= +gonum.org/v1/gonum v0.14.0/go.mod h1:AoWeoz0becf9QMWtE8iWXNXc27fK4fNeHNf/oMejGfU= diff --git a/store.go b/store.go new file mode 100644 index 0000000..649e500 --- /dev/null +++ b/store.go @@ -0,0 +1,249 @@ +package store + +import ( + "github.com/nspcc-dev/neo-go/pkg/interop/native/std" + "github.com/nspcc-dev/neo-go/pkg/interop/storage" +) + +type User struct { + balance int + titanic int + diabetes int + punk_rock []string +} + +const storeURLKey = "storeURL" + +func _deploy(data interface{}, isUpdate bool) { + if isUpdate { + return + } + + ctx := storage.GetContext() + storeURL := "" + storage.Put(ctx, storeURLKey, storeURL) +} + +func NewUser(name string) { + ctx := storage.GetContext() + + existingUser := storage.Get(ctx, name) + if existingUser != nil { + panic("user already exists") + } + + punk_rock := make([]string, 0) + + user := User{ + balance: 1000, + titanic: -1, + diabetes: -1, + punk_rock: punk_rock, + } + storage.Put(ctx, name, std.Serialize(user)) +} + +func getUser(name string) User { + ctx := storage.GetReadOnlyContext() + data := storage.Get(ctx, name) + if data == nil { + panic("user not found") + } + + return std.Deserialize(data.([]byte)).(User) +} + +func Balance(name string) int { + u := getUser(name) + return u.balance +} + +func Titanic(name string) int { + u := getUser(name) + return u.titanic +} + +func Diabetes(name string) int { + u := getUser(name) + return u.diabetes +} + +func Punk_rock(name string) []string { + u := getUser(name) + return u.punk_rock +} + +func DiabetesModel(name string, age int, sex int, w int, h int) { + + h = h / 100 + var bmi int = w / (h * h) + user := getUser(name) + + //Predicted = 152.1335 + age*138.9039 + sex*-36.1353 + bmi*926.9120 + var predicted = (1521335 + age*1389039 + sex*-361353 + bmi*9269120) / 100 + + user.diabetes = predicted + user.balance -= 400 + + ctx := storage.GetContext() + storage.Put(ctx, name, std.Serialize(user)) + + /* + f, err := os.Open("diabetes.csv") + if err != nil { + log.Fatal(err) + } + defer f.Close() + + reader := csv.NewReader(f) + + reader.FieldsPerRecord = -1 + trainingData, err := reader.ReadAll() + if err != nil { + log.Fatal(err) + } + + var r regression.Regression + r.SetObserved("y") + r.SetVar(0, "age") + r.SetVar(1, "sex") + r.SetVar(2, "bmi") + + for i, record := range trainingData { + + if i == 0 { + continue + } + + yVal, err := strconv.ParseFloat(record[10], 64) + if err != nil { + log.Fatal(err) + } + + ageVal, err := strconv.ParseFloat(record[0], 64) + if err != nil { + log.Fatal(err) + } + + sexVal, err := strconv.ParseFloat(record[1], 64) + if err != nil { + log.Fatal(err) + } + + bmiVal, err := strconv.ParseFloat(record[2], 64) + if err != nil { + log.Fatal(err) + } + + r.Train(regression.DataPoint(yVal, []float64{ageVal, sexVal, bmiVal})) + } + r.Run() + Predicted, err := r.Predict([]float64{age, sex, bmi}) + + if err != nil { + log.Fatal(err) + }*/ + +} + +func TitanicModel(name string, age int, sibSp int, parch int, embarked int, sex int, pclass int) { + user := getUser(name) + + //Predicted = 0.9438 + Age*-0.0065 + SibSp*-0.0507 + Parch*-0.0081 + Embarked*-0.0320 + Sex*0.4848 + Pclass*-0.1973 + var predicted = (9438 + age*-65 + sibSp*-507 + parch*-81 + embarked*-320 + sex*4848 + pclass*-1973) / 100 + + user.titanic = predicted + user.balance -= 400 + ctx := storage.GetContext() + storage.Put(ctx, name, std.Serialize(user)) + /* + f, err := os.Open("titanic_numeric.csv") + if err != nil { + log.Fatal(err) + } + defer f.Close() + + reader := csv.NewReader(f) + + reader.FieldsPerRecord = 7 + trainingData, err := reader.ReadAll() + if err != nil { + log.Fatal(err) + } + var r regression.Regression + r.SetObserved("Survived") + r.SetVar(0, "Age") + r.SetVar(1, "SibSp") + r.SetVar(2, "Parch") + r.SetVar(3, "Embarked") + r.SetVar(4, "Sex") + r.SetVar(5, "Pclass") + + for i, record := range trainingData { + + if i == 0 { + continue + } + survivedVal, err := strconv.ParseFloat(record[6], 64) + if err != nil { + log.Fatal(err) + } + + ageVal, err := strconv.ParseFloat(record[0], 64) + if err != nil { + log.Fatal(err) + } + + sibSpVal, err := strconv.ParseFloat(record[1], 64) + if err != nil { + log.Fatal(err) + } + + parchVal, err := strconv.ParseFloat(record[2], 64) + if err != nil { + log.Fatal(err) + } + + embarkedVal, err := strconv.ParseFloat(record[3], 64) + if err != nil { + log.Fatal(err) + } + + sexVal, err := strconv.ParseFloat(record[4], 64) + if err != nil { + log.Fatal(err) + } + + pclassVal, err := strconv.ParseFloat(record[5], 64) + if err != nil { + log.Fatal(err) + } + + r.Train(regression.DataPoint(survivedVal, []float64{ageVal, sibSpVal, parchVal, embarkedVal, sexVal, pclassVal})) + } + + r.Run() + predicted, err := r.Predict([]float64{age, sibSp, parch, embarked, sex, pclass}) + + if err != nil { + log.Fatal(err) + } + */ +} + +func Punk_Rock(name string, group string) { + user := getUser(name) + + punkRockGroups := []string{"Йорш", "Элизиум", "Бригадный подряд", "Порнофильмы"} + + for _, g := range punkRockGroups { + if group == g { + user.punk_rock = append(user.punk_rock, "good") + } else { + user.punk_rock = append(user.punk_rock, "hmm") + } + } + user.balance -= 700 + ctx := storage.GetContext() + storage.Put(ctx, name, std.Serialize(user)) +} diff --git a/store.yml b/store.yml new file mode 100644 index 0000000..9ab6ab3 --- /dev/null +++ b/store.yml @@ -0,0 +1,5 @@ +name: MLStore +supportedstandards: [] +events: +permissions: + - methods: '*' \ No newline at end of file