8000 Update README.md · codee/swift-algorithm-club@f2564ce · GitHub
[go: up one dir, main page]

Skip to content
8000

Commit f2564ce

Browse files
committed
Update README.md
1 parent 40d0748 commit f2564ce

File tree

1 file changed

+3
-3
lines changed

1 file changed

+3
-3
lines changed

Linear Regression/README.markdown

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -123,12 +123,12 @@ We are using the ```map``` function to multiply each element.
123123
Finally, the function which fits the line to the data:
124124

125125
```swift
126-
func linearRegression(_ xs: [Double], _ ys: [Double]) -> (Double -> Double) {
127-
let sum1 = average(multiply(xs, ys)) - average(xs) * average(ys)
126+
func linearRegression(_ xs: [Double], _ ys: [Double]) -> ((Double) -> Double) {
127+
let sum1 = average(multiply(ys, xs)) - average(xs) * average(ys)
128128
let sum2 = average(multiply(xs, xs)) - pow(average(xs), 2)
129129
let slope = sum1 / sum2
130130
let intercept = average(ys) - slope * average(xs)
131-
return { intercept + slope * $0 }
131+
return { x in intercept + slope * x }
132132
}
133133
```
134134
This function takes as arguments two arrays of Doubles, and returns a function which is the line of best fit. The formulas to calculate the slope and the intercept can be derived from our definition of the function J. Let's see how the output from this line fits our data:

0 commit comments

Comments
 (0)
0