Machine Learning For Hackers: Chapter 1, Part 2
June 11, 2013 Leave a comment
I then wanted to show the data graphically. I Added a ASP.NET Web Form project to my solution and added a Chart to the page. That Chart points to an objectDataSource that consumes the UFO Library method:
- <asp:Chart ID="Chart1" runat="server" DataSourceID="ObjectDataSource1">
- <series>
- <asp:Series Name="Series1" XValueMember="Item4" YValueMembers="Item1">
- </asp:Series>
- </series>
- <chartareas>
- <asp:ChartArea Name="ChartArea1">
- </asp:ChartArea>
- </chartareas>
- </asp:Chart>
- <asp:ObjectDataSource ID="ObjectDataSource1" runat="server"
- SelectMethod="GetDetailData"
- TypeName="Tff.MachineLearningWithFSharp.Chapter01.UFOLibrary">
- </asp:ObjectDataSource>
When I ran it, I got this:
So that is cool that the Chart control can access the data and show ‘something’. I then read this tutorial about how to show every state on the X Axis
Unfortunately, I was diving down too deep into the weeds of charting controls, which is really not where I want to be. I then decided to build a function that aggregates the data
- member this.GetSummaryData() =
- let subset =
- this.GetDetailData()
- |> Seq.map(fun (a,b,c,d,e,f,g) -> a,d)
- |> Seq.map(fun (a,b) ->
- a.Year,
- b)
- let summary =
- subset
- |> Seq.groupBy fst
- |> Seq.map (fun (a, b) -> (a, b
- |> Seq.countBy snd))
Sure enough, when I look on my console app
I then decided to switch it so that the state would come up first and each year of UFO sightings would be shown (basically switching the Seq.Map)
- |> Seq.map(fun (a,b) ->
- b,
- a.Year)
And now:
So then I added another method that only returns a state’s aggregate data:
- member this.GetSummaryData(stateCode: string) =
- let stateOnly =
- this.GetSummaryData()
- |> Seq.filter(fun (a,_) -> a = stateCode)
- stateOnly
And I changed the ASP.NET UI to show that state:
- <div class="content-wrapper">
- <asp:DropDownList ID="StatesDropList" runat="server" Width="169px" Height="55px">
- <asp:ListItem Value="AZ"></asp:ListItem>
- <asp:ListItem>MD</asp:ListItem>
- <asp:ListItem>CA</asp:ListItem>
- <asp:ListItem>NC</asp:ListItem>
- </asp:DropDownList>
- <br />
- <br />
- <asp:Chart ID="Chart1" runat="server" DataSourceID="ObjectDataSource1" Width="586px">
- <series>
- <asp:Series Name="Series1" XValueMember="item1" YValueMembers="item2">
- </asp:Series>
- </series>
- <chartareas>
- <asp:ChartArea Name="ChartArea1">
- <AxisX IsLabelAutoFit="False">
- <LabelStyle Interval="Auto" IsStaggered="True" />
- </AxisX>
- </asp:ChartArea>
- </chartareas>
- </asp:Chart>
- <br />
- <asp:ObjectDataSource ID="ObjectDataSource1" runat="server"
- SelectMethod="GetSummaryData"
- TypeName="Tff.MachineLearningWithFSharp.Chapter01.UFOLibrary">
- <SelectParameters>
- <asp:ControlParameter ControlID="StatesDropList" DefaultValue="NC" Name="stateCode" PropertyName="SelectedValue" Type="String" />
- </SelectParameters>
- </asp:ObjectDataSource>
- </div>
The problem is this:
I need to flatten the subTuple so that only native types are sent to the ODS.