/
DataManager.swift
122 lines (76 loc) 路 2.5 KB
/
DataManager.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
//
// DataManager.swift
// MLKit
//
// Created by Guled on 6/30/16.
// Copyright 漏 2016 Somnibyte. All rights reserved.
//
import Foundation
import Upsurge
/**
The Numeric Type Protocol allows for generic functions
to take in integers or doubles or floats.
*/
public protocol NumericType {
func +(lhs: Self, rhs: Self) -> Self
func -(lhs: Self, rhs: Self) -> Self
func *(lhs: Self, rhs: Self) -> Self
func /(lhs: Self, rhs: Self) -> Self
func %(lhs: Self, rhs: Self) -> Self
init(_ v: Int)
}
extension Double : NumericType { }
extension Float : NumericType { }
extension Int : NumericType { }
extension Int8 : NumericType { }
extension Int16 : NumericType { }
extension Int32 : NumericType { }
extension Int64 : NumericType { }
extension UInt : NumericType { }
extension UInt8 : NumericType { }
extension UInt16 : NumericType { }
extension UInt32 : NumericType { }
extension UInt64 : NumericType { }
public class MLDataManager{
enum MLDataHandelingError: ErrorType {
}
/**
Description Goes Here
@param
@return
*/
public static func sumUpData<T: NumericType> (data: Array<T>) -> T {
var sum = T(0)
for val in data{
sum = sum + val
}
return sum
}
/**
Description Goes Here
@param
@return
*/
public static func mean<T: NumericType> (data: Array<T>) -> T {
let totalSum = sumUpData(data)
let totalAmountOfData = T(data.count)
return totalSum/totalAmountOfData
}
public static func dataToMatrix (features:[Array<Float>], output: Array<Float>) -> (Matrix<Float>,ValueArray<Float>) {
// Create Output Matrix
let output_matrix = Matrix<Float>(rows:output.count, columns: 1, elements:output)
// Create "contant/intercept" list
let contant_array = [Float](count: features[0].count, repeatedValue: 1.0)
var matrix_as_array:[[Float]] = []
for (i, _) in contant_array.enumerate() {
var new_row:[Float] = []
new_row.append(contant_array[i])
for feature_array in features {
new_row.append(feature_array[i])
}
matrix_as_array.append(new_row)
}
let feature_matrix = Matrix<Float>(matrix_as_array)
return (feature_matrix, output_matrix.elements)
}
}