---
# System prepended metadata

title: 2020-04-07 | IA | DatasetManager
tags: [swift, ia, gobelins]

---

# 2020-04-07 | IA | DatasetManager

###### tags: `swift` `ia` `gobelins`

## Import
```swift
import Foundation
```

## Class
```swift
class DatasetManager {
    static let instance = DatasetManager()
    var dataset:[ ([Double],Double) ] = []

    init() {
    }

    func appendData(data:[Double],expectedResponse:Double)  {
        dataset.append((data,expectedResponse))
    }
    
    // NEW
    func appendData(data:[CGPoint],expectedResponse:Double)  {
        var finaldata = DataPreparation(array:data)
        dataset.append((finaldata.formatArray(n:data.count*2),expectedResponse))
    }

    func getData() -> [ ([Double],Double) ] {
        return dataset
    }
    
    // NEW
    func rawDataset(nbValues:Int) -> [ ([Double],Double) ] {
        for i in 0..<dataset.count {
            var data = DataPreparation(array:dataset[i].0)
            dataset[i].0 = data.formatArray(n:nbValues)
        }
        return dataset
    }


    // CGPoint -> [double]
    func toRawDataArray(coord:[CGPoint], nbValues:Int) -> [Double] {
        var data = DataPreparation(array:coord)
        var finalData = DataPreparation(array:data.formatArray(n:nbValues))

        return finalData.formatArray(n:nbValues)
    }

    // CGPoint -> [double] -> normaliser le tableau
    func toNormalizedArray(coord:[CGPoint], nbValues:Int) -> [Double] {
        var data = DataPreparation(array:coord)
        var finalData = DataPreparation(array:data.formatArray(n:nbValues))

        return finalData.normalize()
    }

    // CGPoint -> [double] -> standardiser le tableau
    func toStandardizedArray(coord:[CGPoint], nbValues:Int) -> [Double] {
        var data = DataPreparation(array:coord)
        var finalData = DataPreparation(array:data.formatArray(n:nbValues))

        return finalData.standardization()
    }

    func normalizedDataset(nbValues:Int) -> [ ([Double],Double) ] {
        // Normaliser le dataset
        for i in 0..<dataset.count {
            var data = DataPreparation(array:dataset[i].0)
            var finalData = DataPreparation(array:data.formatArray(n:nbValues))
            dataset[i].0 = finalData.normalize()
        }
        
        return dataset
    }

    func standardizeDataset(nbValues:Int) -> [ ([Double],Double) ] {
        // Standardiser le dataset
        for i in 0..<dataset.count {
            var data = DataPreparation(array:dataset[i].0)
            var finalData = DataPreparation(array:data.formatArray(n:nbValues))
            dataset[i].0 = finalData.standardization()
        }
        
        return dataset
    }

    func clearDataset() {
        dataset = []
    }
}
```

## How to use
```swift
var pos = [CGPoint(x: 0, y: 1),CGPoint(x: 2, y: 5),CGPoint(x: 4, y: 1),CGPoint(x: 1, y: 0),CGPoint(x: 4, y: 9),CGPoint(x: 0, y: 1),CGPoint(x: 2, y: 5),CGPoint(x: 4, y: 1),CGPoint(x: 1, y: 0),CGPoint(x: 4, y: 9),CGPoint(x: 0, y: 1),CGPoint(x: 2, y: 5),CGPoint(x: 4, y: 1),CGPoint(x: 1, y: 0),CGPoint(x: 4, y: 9),CGPoint(x: 0, y: 1),CGPoint(x: 2, y: 5),CGPoint(x: 4, y: 1),CGPoint(x: 1, y: 0),CGPoint(x: 4, y: 9)]

// Transformer des tableaux de CGPoint en tableaux de Double
var array1 = DatasetManager.instance.toRawDataArray(coord:pos, nbValues:5)

// Transformer des tableaux de CGPoint en tableaux de Double ET le normaliser
var array2 = DatasetManager.instance.toNormalizedArray(coord:pos, nbValues:10)

// Transformer des tableaux de CGPoint en tableaux de Double ET le standardiser
var array3 = DatasetManager.instance.toStandardizedArray(coord:pos, nbValues:10)

// ajouter des données à partir de tableaux de Double
DatasetManager.instance.appendData(data:array1,expectedResponse:1)
DatasetManager.instance.appendData(data:array2,expectedResponse:0)
DatasetManager.instance.appendData(data:array3,expectedResponse:1)

// ajouter des données à partir de tableaux de CGPoint
DatasetManager.instance.appendData(data:pos,expectedResponse:1)

// Récupérer les tableaux
print(DatasetManager.instance.getData())

// Récupérer les tableaux en leur donnant la même taille
print(DatasetManager.instance.rawDataset(nbValues:4))

// normaliser le dataset
print(DatasetManager.instance.normalizedDataset(nbValues:3))

// standardiser le dataset
print(DatasetManager.instance.standardizeDataset(nbValues:3))

// vider le dataset
DatasetManager.instance.clearDataset()
```

## Result
```console
[([0.0, 1.0, 2.0, 5.0, 4.0], 1.0), ([0.0, 0.1111111111111111, 0.2222222222222222, 0.5555555555555556, 0.4444444444444444, 0.1111111111111111, 0.1111111111111111, 0.0, 0.4444444444444444, 1.0], 0.0), ([-1.375, -0.6527299120066193, -0.1250000000000001, 0.5340517461872339, 1.1249999999999998, -0.6527299120066193, -0.7500000000000001, -0.9494253265550827, 1.1249999999999998, 1.7208334043810871], 1.0), ([0.0, 1.0, 2.0, 5.0, 4.0, 1.0, 1.0, 0.0, 4.0, 9.0, 0.0, 1.0, 2.0, 5.0, 4.0, 1.0, 1.0, 0.0, 4.0, 9.0, 0.0, 1.0, 2.0, 5.0, 4.0, 1.0, 1.0, 0.0, 4.0, 9.0, 0.0, 1.0, 2.0, 5.0, 4.0, 1.0, 1.0, 0.0, 4.0, 9.0], 1.0)]
[([0.0, 1.0, 2.0, 5.0], 1.0), ([0.0, 0.1111111111111111, 0.2222222222222222, 0.5555555555555556], 0.0), ([-1.375, -0.6527299120066193, -0.1250000000000001, 0.5340517461872339], 1.0), ([0.0, 1.0, 2.0, 5.0], 1.0)]
[([0.0, 0.5, 1.0], 1.0), ([0.0, 0.5, 1.0], 0.0), ([0.0, 0.5778160703947045, 1.0], 1.0), ([0.0, 0.5, 1.0], 1.0)]
[([-1.0954451150103321, 1.2247448713915892, 0.5477225575051662], 1.0), ([-1.0954451150103321, 1.2247448713915892, 0.5477225575051662], 0.0), ([-1.0954451150103321, 1.224744871391589, 0.5477225575051662], 1.0), ([-1.0954451150103321, 1.2247448713915892, 0.5477225575051662], 1.0)]
```