Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
simplyYan authored Jun 11, 2024
1 parent 044f2c3 commit 71ec849
Show file tree
Hide file tree
Showing 42 changed files with 7,512 additions and 0 deletions.
123 changes: 123 additions & 0 deletions GalaktaGlareDATA/GalaktaGlareDATA.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
package GalaktaGlareDATA

import (
"math"
"sort"
)

func Mean(numbers []float64) float64 {
total := 0.0
for _, num := range numbers {
total += num
}
return total / float64(len(numbers))
}

func Median(numbers []float64) float64 {
sort.Float64s(numbers)
mid := len(numbers) / 2
if len(numbers)%2 == 0 {
return (numbers[mid-1] + numbers[mid]) / 2
}
return numbers[mid]
}

func StandardDeviation(numbers []float64) float64 {
mean := Mean(numbers)
variance := 0.0
for _, num := range numbers {
variance += math.Pow(num-mean, 2)
}
variance /= float64(len(numbers))
return math.Sqrt(variance)
}

func Max(numbers []float64) float64 {
max := numbers[0]
for _, num := range numbers {
if num > max {
max = num
}
}
return max
}

func Min(numbers []float64) float64 {
min := numbers[0]
for _, num := range numbers {
if num < min {
min = num
}
}
return min
}

func Mode(numbers []float64) []float64 {
frequency := make(map[float64]int)
maxFrequency := 0
for _, num := range numbers {
frequency[num]++
if frequency[num] > maxFrequency {
maxFrequency = frequency[num]
}
}
var modes []float64
for num, freq := range frequency {
if freq == maxFrequency {
modes = append(modes, num)
}
}
return modes
}

func Quartiles(numbers []float64) (float64, float64, float64) {
sort.Float64s(numbers)
n := len(numbers)
q1 := Median(numbers[:n/2])
q2 := Median(numbers)
q3 := Median(numbers[(n + 1) / 2:])
return q1, q2, q3
}

func Percentile(numbers []float64, p float64) float64 {
sort.Float64s(numbers)
n := float64(len(numbers))
rank := p / 100 * (n - 1)
lower := math.Floor(rank)
upper := math.Ceil(rank)
if lower == upper {
return numbers[int(rank)]
}
return numbers[int(lower)] + (rank - lower) * (numbers[int(upper)] - numbers[int(lower)])
}

func Correlation(x, y []float64) float64 {
if len(x) != len(y) {
panic("Tamanho das listas de números não corresponde")
}
n := len(x)
sumX, sumY, sumXY, sumXSquare, sumYSquare := 0.0, 0.0, 0.0, 0.0, 0.0
for i := 0; i < n; i++ {
sumX += x[i]
sumY += y[i]
sumXY += x[i] * y[i]
sumXSquare += math.Pow(x[i], 2)
sumYSquare += math.Pow(y[i], 2)
}
numerator := float64(n)*sumXY - sumX*sumY
denominator := math.Sqrt((float64(n)*sumXSquare - math.Pow(sumX, 2)) * (float64(n)*sumYSquare - math.Pow(sumY, 2)))
return numerator / denominator
}

func DetectAnomalies(numbers []float64, threshold float64) []float64 {
anomalies := make([]float64, 0)
mean := Mean(numbers)
stdDev := StandardDeviation(numbers)
for _, num := range numbers {
zScore := (num - mean) / stdDev
if math.Abs(zScore) > threshold {
anomalies = append(anomalies, num)
}
}
return anomalies
}
3 changes: 3 additions & 0 deletions GalaktaGlareDATA/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/simplyYan/GalaktaGlare/GalaktaGlareDATA

go 1.22.4
161 changes: 161 additions & 0 deletions GalaktaGlareDT/GalaktaGlareDT.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
package GalaktaGlareDT

import (
"errors"
"math"
)

type Node struct {
IsLeaf bool
Prediction interface{}
SplitFeature int
SplitValue interface{}
Left *Node
Right *Node
}

type DecisionTree struct {
Root *Node
}

func NewDecisionTree() *DecisionTree {
return &DecisionTree{}
}

func (dt *DecisionTree) Fit(data [][]interface{}, labels []interface{}, features []int, maxDepth int) error {
if len(data) == 0 || len(data) != len(labels) {
return errors.New("dados ou rótulos inválidos")
}
dt.Root = dt.buildTree(data, labels, features, maxDepth, 1)
return nil
}

func (dt *DecisionTree) buildTree(data [][]interface{}, labels []interface{}, features []int, maxDepth, currentDepth int) *Node {
if len(data) == 0 || currentDepth >= maxDepth {
return &Node{IsLeaf: true, Prediction: majorityVote(labels)}
}

bestFeature, bestValue := chooseBestFeatureToSplit(data, labels, features)
leftData, leftLabels, rightData, rightLabels := splitData(data, labels, bestFeature, bestValue)

leftChild := dt.buildTree(leftData, leftLabels, features, maxDepth, currentDepth+1)
rightChild := dt.buildTree(rightData, rightLabels, features, maxDepth, currentDepth+1)

return &Node{
SplitFeature: bestFeature,
SplitValue: bestValue,
Left: leftChild,
Right: rightChild,
}
}

func (dt *DecisionTree) Predict(input []interface{}) (interface{}, error) {
currentNode := dt.Root
for !currentNode.IsLeaf {
value := input[currentNode.SplitFeature]
if compareValues(value, currentNode.SplitValue) {
currentNode = currentNode.Left
} else {
currentNode = currentNode.Right
}
}
return currentNode.Prediction, nil
}

func allSame(items []interface{}) bool {
first := items[0]
for _, item := range items[1:] {
if item != first {
return false
}
}
return true
}

func majorityVote(items []interface{}) interface{} {
counts := make(map[interface{}]int)
for _, item := range items {
counts[item]++
}
var majority interface{}
maxCount := 0
for key, count := range counts {
if count > maxCount {
majority = key
maxCount = count
}
}
return majority
}

func chooseBestFeatureToSplit(data [][]interface{}, labels []interface{}, features []int) (int, interface{}) {
bestFeature := -1
var bestValue interface{}
bestImpurity := math.Inf(1)

for _, featureIndex := range features {
for _, row := range data {
value := row[featureIndex]
_, leftLabels, _, rightLabels := splitData(data, labels, featureIndex, value)
impurity := calculateImpurity(leftLabels, rightLabels)

if impurity < bestImpurity {
bestImpurity = impurity
bestFeature = featureIndex
bestValue = value
}
}
}

return bestFeature, bestValue
}

func calculateImpurity(labels ...[]interface{}) float64 {
totalSamples := 0
labelCounts := make(map[interface{}]int)

for _, labelSet := range labels {
for _, label := range labelSet {
totalSamples++
labelCounts[label]++
}
}

entropy := 0.0
for _, count := range labelCounts {
probability := float64(count) / float64(totalSamples)
entropy -= probability * math.Log2(probability)
}

return entropy
}

func splitData(data [][]interface{}, labels []interface{}, featureIndex int, value interface{}) ([][]interface{}, []interface{}, [][]interface{}, []interface{}) {
leftData, rightData := [][]interface{}{}, [][]interface{}{}
leftLabels, rightLabels := []interface{}{}, []interface{}{}
for i, row := range data {
if compareValues(row[featureIndex], value) {
leftData = append(leftData, row)
leftLabels = append(leftLabels, labels[i])
} else {
rightData = append(rightData, row)
rightLabels = append(rightLabels, labels[i])
}
}
return leftData, leftLabels, rightData, rightLabels
}

func compareValues(a, b interface{}) bool {
switch a.(type) {
case int:
return a.(int) < b.(int)
case float64:
return a.(float64) < b.(float64)
case string:
return a.(string) < b.(string)
case bool:
return !a.(bool) && b.(bool)
default:
return false
}
}
3 changes: 3 additions & 0 deletions GalaktaGlareDT/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/simplyYan/GalaktaGlare/GalaktaGlareDT

go 1.19
21 changes: 21 additions & 0 deletions GalaktaGlareDT/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
github.com/corona10/goimagehash v1.1.0 h1:teNMX/1e+Wn/AYSbLHX8mj+mF9r60R1kBeqE9MkoYwI=
github.com/corona10/goimagehash v1.1.0/go.mod h1:VkvE0mLn84L4aF8vCb6mafVajEb6QYMHl2ZJLn0mOGI=
github.com/hajimehoshi/go-mp3 v0.3.3 h1:cWnfRdpye2m9ElSoVqneYRcpt/l3ijttgjMeQh+r+FE=
github.com/hajimehoshi/go-mp3 v0.3.3/go.mod h1:qMJj/CSDxx6CGHiZeCgbiq2DSUkbK0UbtXShQcnfyMM=
github.com/hajimehoshi/oto v0.6.1/go.mod h1:0QXGEkbuJRohbJaxr7ZQSxnju7hEhseiPx2hrh6raOI=
github.com/hajimehoshi/oto/v2 v2.2.0 h1:qhTriSacJ/2pdONRa90hjTvpEZH7xIP4W3itwYyE1Uk=
github.com/hajimehoshi/oto/v2 v2.2.0/go.mod h1:seWLbgHH7AyUMYKfKYT9pg7PhUu9/SisyJvNTT+ASQo=
github.com/hegedustibor/htgo-tts v0.0.0-20230402053941-cd8d1a158135 h1:HWBBhNF2kQVwlPCmcJL3fQxkuzP2LOTiLOib5sx6swI=
github.com/hegedustibor/htgo-tts v0.0.0-20230402053941-cd8d1a158135/go.mod h1:VBNcur+xWvaQIWCaLH8w7j68zPeqQwVfjREn2S7kYbY=
github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646 h1:zYyBkD/k9seD2A7fsi6Oo2LfFZAehjjQMERAvZLEDnQ=
github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646/go.mod h1:jpp1/29i3P1S/RLdc7JQKbRpFeM1dOBd8T9ki5s+AY8=
github.com/pelletier/go-toml v1.9.5 h1:4yBQzkHv+7BHq2PQUZF3Mx0IYxG7LsP222s7Agd3ve8=
github.com/pelletier/go-toml v1.9.5/go.mod h1:u1nR/EPcESfeI/szUZKdtJ0xRNbUoANCkoOuaOx1Y+c=
golang.org/x/exp v0.0.0-20190306152737-a1d7652674e8/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
golang.org/x/image v0.0.0-20190227222117-0694c2d4d067/go.mod h1:kZ7UVZpmo3dzQBMxlp+ypCbDeSB+sBbTgSJuh5dn5js=
golang.org/x/mobile v0.0.0-20190415191353-3e0bab5405d6/go.mod h1:E/iHnbuqvinMTCcRqshq8CkpyQDoeVncDDYHnLhea+o=
golang.org/x/sys v0.0.0-20190312061237-fead79001313/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20190429190828-d89cdac9e872/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20220712014510-0a85c31ab51e h1:NHvCuwuS43lGnYhten69ZWqi2QOj/CiDNcKbVqwVoew=
golang.org/x/sys v0.0.0-20220712014510-0a85c31ab51e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
72 changes: 72 additions & 0 deletions GalaktaGlareIMG/GalaktaGlareIMG.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package galaktaglareimg

import (
"errors"
"image"
"image/color"
"math"

"github.com/disintegration/imaging"
)

func LoadImage(path string) (image.Image, error) {
img, err := imaging.Open(path)
if err != nil {
return nil, err
}
return img, nil
}

func SaveImage(img image.Image, path string) error {
return imaging.Save(img, path)
}

func ResizeImage(img image.Image, width, height int) image.Image {
return imaging.Resize(img, width, height, imaging.Lanczos)
}

func ConvertToGrayscale(img image.Image) image.Image {
return imaging.Grayscale(img)
}

func CompareImages(img1, img2 image.Image) (float64, error) {
bounds1 := img1.Bounds()
bounds2 := img2.Bounds()

if bounds1 != bounds2 {
return 0, errors.New("images must have the same dimensions")
}

var sum float64
var count int

for y := bounds1.Min.Y; y < bounds1.Max.Y; y++ {
for x := bounds1.Min.X; x < bounds1.Max.X; x++ {
r1, g1, b1, _ := img1.At(x, y).RGBA()
r2, g2, b2, _ := img2.At(x, y).RGBA()
dr := float64(r1) - float64(r2)
dg := float64(g1) - float64(g2)
db := float64(b1) - float64(b2)
sum += dr*dr + dg*dg + db*db
count++
}
}

mse := sum / float64(count)
return 10 * math.Log10(65535*65535/mse), nil
}

func ImageToGrayscaleArray(img image.Image) [][]uint8 {
bounds := img.Bounds()
width, height := bounds.Max.X, bounds.Max.Y
grayscaleArray := make([][]uint8, height)
for y := 0; y < height; y++ {
grayscaleArray[y] = make([]uint8, width)
for x := 0; x < width; x++ {
r, g, b, _ := img.At(x, y).RGBA()
gray := color.GrayModel.Convert(color.RGBA{uint8(r >> 8), uint8(g >> 8), uint8(b >> 8), 255})
grayscaleArray[y][x] = gray.(color.Gray).Y
}
}
return grayscaleArray
}
Loading

0 comments on commit 71ec849

Please sign in to comment.