3 people like it.

# Course 2: Freebase, queries, linear regression

F# introduction course - Getting data about cyclones from Freebase and plotting the dependency of damages in USD on the wind speed (with linear regression). To be used in Try F#.

 ``` 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: ``` ``````// Load Freebase data connection and charting library #r "Samples.DataStore.Freebase.dll" open Samples.Charting.DojoChart open Samples.DataStore.Freebase let freebase = FreebaseData.GetDataContext() // ------------------------------------------------------------------ // DEMO: Querying cyclone data using F# LINQ // ------------------------------------------------------------------ let cyclones = query { for x in freebase.Commons.Meteorology.``Tropical Cyclones`` do where x.``Highest winds``.HasValue where (x.Damages.Currency.Name = "United States dollar") select (x.``Highest winds``.Value, x.Damages.Amount.Value / 1e9) } |> Seq.toList // Plot wind speed and damage in USD as point chart let cyclonChart = Chart.Point(cyclones) .WithYAxis(Title="Damage (US\$)") .WithXAxis("Wind Speed") // ------------------------------------------------------------------ // DEMO: Adding linear regression using Math.NET // ------------------------------------------------------------------ // For more information see // http://christoph.ruegg.name/blog/linear-regression-mathnet-numerics.html #r "MathNet.Numerics.dll" open MathNet.Numerics.LinearAlgebra open MathNet.Numerics.LinearAlgebra.Double // Build matrix and vector representing the equation system let cols : Generic.Vector[] = [| DenseVector(cyclones.Length, 1.0) DenseVector([| for x, y in cyclones -> float x |]) |] let x = DenseMatrix.CreateFromColumns(cols) let y = DenseVector([| for x, y in cyclones -> y |]) // QR decomposition gives us attributes of y=a+x*b line let [| a; b |] = x.QR().Solve(y) |> Seq.toArray // Draw chart with linear regression Chart.Combine [ cyclonChart Chart.Line([20.0, a+b*20.0; 100.0, a+b*100.0 ]) ] ``````
val freebase : obj

Full name: Script.freebase
val cyclones : (obj * float) list

Full name: Script.cyclones
val query : Linq.QueryBuilder

Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.query
val x : obj
custom operation: where (bool)

Calls Linq.QueryBuilder.Where
custom operation: select ('Result)

Calls Linq.QueryBuilder.Select
module Seq

from Microsoft.FSharp.Collections
val toList : source:seq<'T> -> 'T list

Full name: Microsoft.FSharp.Collections.Seq.toList
val cyclonChart : obj

Full name: Script.cyclonChart
val cols : obj []

Full name: Script.cols
Multiple items
val float : value:'T -> float (requires member op_Explicit)

Full name: Microsoft.FSharp.Core.Operators.float

--------------------
type float = System.Double

Full name: Microsoft.FSharp.Core.float

--------------------
type float<'Measure> = float

Full name: Microsoft.FSharp.Core.float<_>
property List.Length: int
val x : obj

Full name: Script.x
val y : obj

Full name: Script.y
val a : obj

Full name: Script.a
val b : obj

Full name: Script.b
val toArray : source:seq<'T> -> 'T []

Full name: Microsoft.FSharp.Collections.Seq.toArray

### More information

 Link: http://fssnip.net/ja Posted: 8 years ago Author: Tomas Petricek Tags: freebase , type provider , try f#