@@ -19,26 +19,26 @@ let alpha = 0.0001
19
19
20
20
for n in 1 ... numberOfIterations {
21
21
for i in 0 ..< numberOfCarAdvertsWeSaw {
22
- let difference = carPrice [ i] - predictedCarPrice( carAge [ i] )
22
+ let difference = carPrice [ i] - predictedCarPrice( carAge: carAge [ i] )
23
23
intercept += alpha * difference
24
24
slope += alpha * difference * carAge[ i]
25
25
}
26
26
}
27
27
28
- print ( " A car age of 4 years is predicted to be worth £ \( Int ( predictedCarPrice ( 4 ) ) ) " )
28
+ print ( " A car age of 4 years is predicted to be worth £ \( Int ( predictedCarPrice ( carAge : 4 ) ) ) " )
29
29
30
30
// A closed form solution
31
31
32
- func average( input: [ Double ] ) -> Double {
33
- return input. reduce ( 0 , combine : + ) / Double( input. count)
32
+ func average( _ input: [ Double ] ) -> Double {
33
+ return input. reduce ( 0 , + ) / Double( input. count)
34
34
}
35
35
36
- func multiply( input1: [ Double ] , _ input2: [ Double ] ) -> [ Double ] {
37
- return input1. enumerate ( ) . map ( { ( index, element) in return element*input2 [ index] } )
36
+ func multiply( _ input1: [ Double ] , _ input2: [ Double ] ) -> [ Double ] {
37
+ return input1. enumerated ( ) . map ( { ( index, element) in return element*input2 [ index] } )
38
38
}
39
39
40
- func linearRegression( xVariable: [ Double ] , _ yVariable: [ Double ] ) -> ( Double -> Double ) {
41
- let sum1 = average ( multiply ( xVariable , yVariable ) ) - average( xVariable) * average( yVariable)
40
+ func linearRegression( _ xVariable: [ Double ] , _ yVariable: [ Double ] ) -> ( ( Double ) -> Double ) {
41
+ let sum1 = average ( multiply ( yVariable , xVariable ) ) - average( xVariable) * average( yVariable)
42
42
let sum2 = average ( multiply ( xVariable, xVariable) ) - pow( average ( xVariable) , 2 )
43
43
let slope = sum1 / sum2
44
44
let intercept = average ( yVariable) - slope * average( xVariable)
0 commit comments