2011-10-06 70 views
2

我想將只包含整數的SQL表格(比如說包含用戶ID的一列和包含用戶年齡和ii。n行的一列)轉換爲F#矩陣(相同的尺寸)。 我設法用下面的F#代碼來實現,但我相信這不是最有效的方式。將一個MySQL表格轉換爲F#矩陣

事實上,我發現定義F#矩陣的唯一方法是使用MySQL創建2個帶有單個值(分別爲行數和列數)的表格,並將這些值轉換爲F#。

是否有可能通過F#代碼將「mySQL」表導入F#矩陣,該代碼「識別」矩陣的維度。基本上我想要一個函數,它將表地址作爲參數並返回一個矩陣。

這裏是我的代碼:

#r "FSharp.PowerPack.dll" 
#r "Microsoft.Office.Interop.Excel" 
open System 
open System.Data 
open System.Data.SqlClient 
open Microsoft.Office.Interop 
open Microsoft.FSharp.Math 
open System.Collections.Generic 

//Need of three types : User, number of rows and number of columns 
type user = { 
    ID : int; 
    Age : int;} 


type nbrRows = {NbreL : int ;} 

type nbrCol = {NbreC : int ;} 

// I. Import the SQL data into F# 
// I.1. Import the number of rows of the table into F# 

    let NbrRows = seq { 

    use cnn = new SqlConnection(@"myconnection; database=MyDataBase; integrated security=true") 
    use cmd1 = new SqlCommand("Select * from theTablesWhichContainsTheNumberOfRows", cnn) 


    cnn.Open() 
    use reader = cmd1.ExecuteReader() 
    while reader.Read() do 
    yield { 
     NbreL = unbox(reader.["Expr1"]) 
    } 
} 

let NbrRowsList = NbrRows |> Seq.toList // convert the sequence into a List 


// I.2. Same code to import the number of columns of the table 

let NbrCol = seq { 

    use cnn = new SqlConnection(@"MyConnection; database=myDatabase; integrated security=true") 
    use cmd1 = new SqlCommand("Select * from theTablesWhichContainsTheNumberOfColumns", cnn) 


    cnn.Open() 
    use reader = cmd1.ExecuteReader() 
    while reader.Read() do 
    yield { 
     NbreC = unbox(reader.["Expr1"]) 
    } 
} 

let NbrColsList = NbrCol |> Seq.toList 

// Initialisation of the Matrix 

let matrixF = Matrix.create NbrRowsList.[0].NbreL NbrColsList.[0].NbreC 0. 

//Transfer of the mySQL User table into F# through a sequence as previously 

let GetUsers = seq { 

    use cnn = new SqlConnection(@"myConnection, database=myDatabase; integrated security=true") 
    use cmd = new SqlCommand("Select * from tableUser ORDER BY ID", cnn) 


    cnn.Open() 
    use reader = cmd.ExecuteReader() 
    while reader.Read() do 
    yield { 
     ID = unbox(reader.["ID"]) 
     Age = unbox(reader.["Age"]) 
    } 
} 

// Sequence to list 

let UserDatabaseList = GetUsers |> Seq.toList 

// Fill of the user matrix 

for i in 0 .. (NbrRowList.[0].NbreL - 1) do  
    matrixF.[0,i] <- UserDatabaseList.[i].ID |> float 
    matrixF.[1,i] <- UserDatabaseList.[i].Age|> float 
matrixUsers 

回答

5

有不同的方法來初始化矩陣,如果你不知道它的提前大小。例如,Matrix.ofList會列出一個列表,並自動計算大小。

如果剛UserDatabaseList(你可以創建withtout知道行數和列數),那麼你應該能夠編寫:

Matrix.ofList 
    [ // Create list containing rows from the database 
    for row in UserDatabaseList do 
     // For each row, return list of columns (float values) 
     yield [ float row.ID; float row.Age ] ] 

除了 - F#矩陣是真正有用的主要是,如果你」將要做一些矩陣操作(即使這樣,它也不是最有效的選擇)。如果你正在做一些數據處理,那麼將數據保存在一個普通的列表中可能會更容易。如果你正在做一些嚴肅的數學,那麼你可能想要檢查如何使用Math.NET library from F#,它具有更高效的矩陣類型。

+0

謝謝托馬斯,它非常清晰,看起來不錯。 – fabco63