TabularData

RSS for tag

Import, organize, and prepare a table of data to train a machine learning model.

Posts under TabularData tag

32 Posts

Post

Replies

Boosts

Views

Activity

TabularData Resources
TabularData framework lets you import, organize, and export a table of data. It’s great when you’re training a machine learning model but it’s a handy tool in many other scenarios as well. General: DevForums tag: TabularData TabularData framework documentation Explore and manipulate data in Swift with TabularData tech talk For a ‘hello world’ style example, see this DevForums post Share and Enjoy — Quinn “The Eskimo!” @ Developer Technical Support @ Apple let myEmail = "eskimo" + "1" + "@" + "apple.com"
0
0
1.8k
Mar ’23
TabularData doesn't respect Double type when values match Int
Hello, I'm trying to figure out why an Int is being inferred over my explicit Double I'm parsing a CSV that contains 2 tables. I don't own the data so I'm not able to change it. The first row contains one cell that's used as a title for the document The second row is empty The third row contains one cell that's used as the header for the first table There is a header row for the table There's a dynamic number of rows for this table The an empty spacer row There is a row that's used as a title for the second table There is a header row for the table There's a dynamic number of rows for this table Im able to separate and create two DataFrame's from the data without issue. And this is the initializer I'm using. DataFrame( csvData: csvData, rows: rows, types: types, options: options ) Column names and their CSV types looks like this var types: [String: CSVType] { [ // ... "Column 38": .double, // ... ] } The data in the CSV is 0 nil nil nil 2 And this is what the one of the columns in question looks like when printed ▿ 38 : ┏━━━━━━━━━━━┓ ┃ Column 38 ┃ ┃ <Int> ┃ ┡━━━━━━━━━━━┩ │ 0 │ │ nil │ │ nil │ │ nil │ │ 2 │ └───────────┘ - name : "Column 38" - count : 5 ▿ contents : PackedOptionalsArray<Int> ▿ storage : <PackedOptionalsStorage<Int>: 0x600000206360> The docs state /// - types: A dictionary of column names and their CSV types. /// The data frame infers the types for column names that aren't in the dictionary. Since types contains the column name and it's still being inferred, my assumption is that the issue involves the renaming of the header row when it has empty cells occurs after the types are checked. Edit: After setting hasHeaderRow: false from true and adjusting my row offset, the types are now being assigned correctly. I'd recommend opening a feedback enhancement where renaming columns occurs before type assignment.
1
0
83
Feb ’26
Filtered dataFrame displays original dataFrame values
I'm importing a csv file of 299 rows and creating a subset by filtering on one column value (24 rows). I want to Chart just the filtered values. However, when I print one column I get values from the original dataFrame. Any suggestions? Thanks, David The code: import SwiftUI import Charts import TabularData struct DataPoint: Identifiable { var id = UUID() // This makes it conform to Identifiable var date: Date var value: Double } struct ContentView: View { @State private var dataPoints: [DataPoint] = [] var body: some View { Text("Hello") Chart { ForEach(dataPoints) { dataPoint in PointMark( x: .value("Date", dataPoint.date), y: .value("Value", dataPoint.value) ) } } .frame(height: 300) .padding() .onAppear(perform: loadData) } func loadData() { print("In Loading Data") // Load the CSV file if let url = Bundle.main.url(forResource: "observations", withExtension: "csv") { do { let options = CSVReadingOptions(hasHeaderRow: true, delimiter: ",") var data0 = try DataFrame(contentsOfCSVFile: url, options: options) let formattingOptions = FormattingOptions( maximumLineWidth: 200, maximumCellWidth: 15, maximumRowCount: 30 ) // print(data0.description(options: formattingOptions)) print("Number of Columns: \(data0.columns.count)") let columnsSet = ["plant_id", "date", "plot", "plantNumber", "plantCount"] data0 = try DataFrame(contentsOfCSVFile:url, columns: columnsSet, options: options) print("Number of Columns (after columnsSet): \(data0.columns.count)") print("Printing data0") print(data0.description(options: formattingOptions)) let data = data0.filter { $0["plant_id"] as? Int == 15 } print("Printing data") print(data.description(options: formattingOptions)) print(" Number of Rows \(data.rows.count)") for i in 0 ... data.rows.count { // print("\(i): \(data["plantCount"][i]!)") if let plantCount = data["plantCount"][i] as? Int { print("\(i): \(plantCount)") } else { print("\(i): Value not found or invalid type") } } // // var newDataPoints: [DataPoint] = [] // Here I plan to add the filtered data to DataPoint // DispatchQueue.main.async { dataPoints = newDataPoints } } catch { print("Error reading CSV file: \(error)") } } else { print("Didn't load csv file") } } } struct ContentView_Previews: PreviewProvider { static var previews: some View { ContentView() } } Here is the new dataFrame and print output Printing data ┏━━━━━┳━━━━━━━━━━┳━━━━━━━━━━━━┳━━━━━━━┳━━━━━━━━━━━━━┳━━━━━━━━━━━━┓ ┃ ┃ plant_id ┃ date ┃ plot ┃ plantNumber ┃ plantCount ┃ ┃ ┃ <Int> ┃ <String> ┃ <Int> ┃ <Int> ┃ <Int> ┃ ┡━━━━━╇━━━━━━━━━━╇━━━━━━━━━━━━╇━━━━━━━╇━━━━━━━━━━━━━╇━━━━━━━━━━━━┩ │ 0 │ 15 │ 2023-09-07 │ 1 │ 5 │ 5 │ │ 32 │ 15 │ 2023-09-07 │ 2 │ 10 │ 10 │ │ 38 │ 15 │ 2023-09-07 │ 2 │ 20 │ 20 │ │ 66 │ 15 │ 2023-09-07 │ 4 │ 25 │ 25 │ │ 77 │ 15 │ 2023-09-07 │ 5 │ 5 │ 5 │ │ 99 │ 15 │ 2023-09-14 │ 7 │ 45 │ 45 │ │ 142 │ 15 │ 2024-05-30 │ 1 │ 20 │ 20 │ │ 162 │ 15 │ 2024-05-30 │ 4 │ 5 │ 5 │ │ 169 │ 15 │ 2024-05-30 │ 5 │ 10 │ 10 │ │ 175 │ 15 │ 2024-05-30 │ 7 │ 10 │ 10 │ │ 188 │ 15 │ 2024-07-11 │ 1 │ 20 │ 40 │ │ 199 │ 15 │ 2024-07-11 │ 2 │ 5 │ 5 │ │ 215 │ 15 │ 2024-07-11 │ 5 │ 20 │ 30 │ │ 220 │ 15 │ 2024-07-11 │ 7 │ 30 │ 40 │ │ 236 │ 15 │ 2024-09-06 │ 1 │ 20 │ 60 │ │ 238 │ 15 │ 2024-09-06 │ 2 │ 30 │ 35 │ │ 248 │ 15 │ 2024-09-06 │ 5 │ 5 │ 35 │ │ 254 │ 15 │ 2024-09-06 │ 7 │ 50 │ 90 │ │ 267 │ 15 │ 2025-05-04 │ 1 │ 10 │ 70 │ │ 273 │ 15 │ 2025-05-04 │ 2 │ 10 │ 45 │ │ 282 │ 15 │ 2025-05-04 │ 5 │ 10 │ 45 │ │ 287 │ 15 │ 2025-05-04 │ 7 │ 30 │ 120 │ │ 292 │ 15 │ 2025-05-04 │ 8 │ 10 │ 0 │ │ 297 │ 15 │ 2925-05-04 │ 3 │ 10 │ 0 │ └─────┴──────────┴────────────┴───────┴─────────────┴────────────┘ 24 rows, 5 columns Number of Rows 24 0: 5 1: 80 2: 1 3: 1 4: 1 5: 3 6: 3 7: 1 8: 6 9: 1 10: 1 11: 1 12: 1 13: 10 14: 50 15: 1 16: 2 17: 1 18: 3 19: 8 20: 5 21: 3 22: 7 23: 2 24: 1
2
0
165
May ’25
How to Visualize Data in a DataFrame Using Charts
I want to visualize the data stored in a DataFrame using various charts (barmark, sectormark, linemark, etc.). My questions are as follows: Can a DataFrame be used directly within a chart? If so, could you provide a simple example? If it cannot be used directly, what is the correct way to use it? Could you provide an example? Thank you for your help. Best regards.
0
1
420
Jan ’25
CreatML stop training
It appears that there is a size limit when training the Tabular Classification model in CreatML. When the training data is small, the training process completes smoothly after a specified period. However, as the data volume increases, the following issues occur: initially, the training process indicates that it is in progress, but after approximately 24 hours, it is automatically terminated after an hour. I am certain that this is not a manual termination by myself or others, but rather an automatic termination by the machine. This issue persists despite numerous attempts, and the only message displayed is “Training Canceled.” I would appreciate it if someone could explain the reason behind this behavior and provide a solution. Thank you for your assistance.
1
0
662
Jan ’25
Create ML Trouble Loading CSV to Train Word Tagger With Commas in Training Data
I'm using Numbers to build a spreadsheet that I'm exporting as a CSV. I then import this file into Create ML to train a word tagger model. Everything has been working fine for all the models I've trained so far, but now I'm coming across a use case that has been breaking the import process: commas within the training data. This is a case that none of Apple's examples show. My project takes Navajo text that has been tokenized by syllables and labels the parts-of-speech. Case that works... Raw text: Naaltsoos yídéeshtah. Tokens column: Naal,tsoos, ,yí,déesh,tah,. Labels column: NObj,NObj,Space,Verb,Verb,VStem,Punct Case that breaks... Raw text: óola, béésh łigaii, tłʼoh naadą́ą́ʼ, wáin, akʼah, dóó á,shįįh Tokens column with tokenized text (commas quoted): óo,la,",", ,béésh, ,łi,gaii,",", ,tłʼoh, ,naa,dą́ą́ʼ,",", ,wáin,",", ,a,kʼah,",", ,dóó, ,á,shįįh (Create ML reports mismatched columns) Tokens column with tokenized text (commas escaped): óo,la,\,, ,béésh, ,łi,gaii,\,, ,tłʼoh, ,naa,dą́ą́ʼ,\,, ,wáin,\,, ,a,kʼah,\,, ,dóó, ,á,shįįh (Create ML reports mismatched columns) Tokens column with tokenized text (commas escape-quoted): óo,la,\",\", ,béésh, ,łi,gaii,\",\", ,tłʼoh, ,naa,dą́ą́ʼ,\",\", ,wáin,\",\", ,a,kʼah,\",\", ,dóó, ,á,shįįh (record not detected by Create ML) Tokens column with tokenized text (commas escape-quoted): óo,la,"","", ,béésh, ,łi,gaii,"","", ,tłʼoh, ,naa,dą́ą́ʼ,"","", ,wáin,"","", ,a,kʼah,"","", ,dóó, ,á,shįįh (Create ML reports mismatched columns) Labels column: NSub,NSub,Punct,Space,NSub,Space,NSub,NSub,Punct,Space,NSub,Space,NSub,NSub,Punct,Space,NSub,Punct,Space,NSub,NSub,Punct,Space,Conj,Space,NSub,NSub Sample From Spreadsheet Solution Needed It's simple enough to escape commas within CSV files, but the format needed by Create ML essentially combines entire CSV records into single columns, so I'm ending up needing a CSV record that contains a mixture of commas to use for parsing and ones to use as character literals. That's where this gets complicated. For this particular use case (which seems like it would frequently arise when training a word tagger model), how should I properly escape a comma literal?
6
0
864
Jan ’25
How to confirm whether CreatML is training
I am currently training a Tabular Classification model in CreatML. The dataset comprises 30 features, including 1,000,000 training data points and 1,000,000 verification data points. Could you please estimate the approximate training time for an M4Max MacBook Pro? During the training process, CreatML has been displaying the “Processing” status, but there is no progress bar. I would like to ascertain whether the training is still ongoing, as I have often suspected that it has ceased.
1
0
694
Jan ’25
How to update data in a DataFrame
My project loads a CSV into a DataFrame and displays it in a Table (a MacOS app). So far so good ... but when trying to update a value in a column, I dont see anyway to update this value. The table gets the value for the column like this: func getColumnValue(row :DataFrame.Rows.Element, columnName :String) -&gt; String { if row.base.containsColumn(columnName) { var value = "" if row[columnName] != nil { value = "\(row[columnName]!)" } return value } ... But the documentation and googles dont show any way to update the same column. Any help is appreciated with cookies. Attempt to update: func setColumnValue(row :DataFrame.Rows.Element, columnName :String, value :String) { var column: [String?] = data[columnName] column[row.id] = value ... }
6
0
738
Dec ’24
Help Loading an External CSV File on iOS in Swift
Hi everyone! I’m fairly new to Swift and currently working on a small iOS app in SwiftUI. The app is able to load a CSV file embedded in the Xcode project (using Bundle.main.path(forResource:)), and everything works well with that. Now, I want to take it a step further by allowing the app to load an external CSV file located in the iPhone’s directories (like “Documents” or “Downloads”). However, I’m struggling to make it work. I tried using a DocumentPicker to select the CSV file, and I believe I’m passing the file URL correctly, but the app keeps reading only the embedded file instead of the one selected by the user. Could anyone offer guidance on how to properly set up loading an external CSV file? I’m still learning, so any suggestions or examples would be really appreciated! Thanks a lot in advance for the help! Here’s the code that isn’t working as expected: import Foundation struct Product: Identifiable { let id = UUID() var codice: String var descrizione: String var prezzo: Double var installazione: Double var trasporto: Double } class ProductViewModel: ObservableObject { @Published var products: [Product] = [] @Published var filteredProducts: [Product] = [] func loadCSV(from url: URL) { products = [] do { let data = try String(contentsOf: url) let lines = data.components(separatedBy: "\n") // Legge e processa ogni riga del CSV (saltando la prima riga se è l'intestazione) for line in lines.dropFirst() { let values = line.components(separatedBy: ";") // Assicurati che ci siano abbastanza colonne e gestisci i valori mancanti if values.count &gt;= 5 { let codice = values[0].trimmingCharacters(in: .whitespaces) let descrizione = values[1].trimmingCharacters(in: .whitespaces) let prezzo = parseEuropeanDouble(values[2]) ?? 0.0 let installazione = parseEuropeanDouble(values[3].isEmpty ? "0,00" : values[3]) ?? 0.0 let trasporto = parseEuropeanDouble(values[4].isEmpty ? "0,00" : values[4]) ?? 0.0 let product = Product( codice: codice, descrizione: descrizione, prezzo: prezzo, installazione: installazione, trasporto: trasporto ) products.append(product) } } filteredProducts = products } catch { print("Errore nel caricamento del CSV: \(error)") } } private func parseEuropeanDouble(_ value: String) -&gt; Double? { let formatter = NumberFormatter() formatter.locale = Locale(identifier: "it_IT") formatter.numberStyle = .decimal return formatter.number(from: value)?.doubleValue } } struct ContentView: View { @StateObject var viewModel = ProductViewModel() @State private var showFilePicker = false var body: some View { VStack { Button("Carica file CSV") { showFilePicker = true } .fileImporter(isPresented: $showFilePicker, allowedContentTypes: [.commaSeparatedText]) { result in switch result { case .success(let url): viewModel.loadCSV(from: url) case .failure(let error): print("Errore nel caricamento del file: \(error.localizedDescription)") } } List(viewModel.filteredProducts) { product in VStack(alignment: .leading) { Text("Codice: \(product.codice)") Text("Descrizione: \(product.descrizione)") Text("Prezzo Lordo: €\(String(format: "%.2f", product.prezzo))") Text("Installazione: €\(String(format: "%.2f", product.installazione))") Text("Trasporto: €\(String(format: "%.2f", product.trasporto))") } } } .padding() } }
2
0
944
Nov ’24
CSV File Load Into Swift
Hi, I am fairly new Xcode/Swift and am trying to load a CSV File for use in my development. I have placed the CSV file in my Assets folder but when I try to create my Data Model and load the CSV file. I run into the error: No exact matches in call to initializer. Below is the code. I have attached CSV File. Any help fixing this error would be greatly appreciated. Thanks in advance for your help. Brian Hospital_Demographic_Data_Sample.csv import Foundation import CSV struct HospitalData: Codable { let providerNumber: String let hospital: String let address: String let city: String let state: String let zip: String let wageIndex: Double let caseMix: Double let averageCharge: Double let discharges: Int let totalCharges: Double let adjTotalCharges: Double // Add other fields as needed based on the columns in your CSV file } func loadHospitalData() -> [HospitalData]? { guard let filePath = Bundle.main.path(forResource: "Hospital_Demographic_Data", ofType: "csv") else { print("File not found") return nil } do { let csv = try CSV(url: URL(fileURLWithPath: filePath)) var hospitalDataList = [HospitalData]() // Initialize as an empty array for row in csv.namedRows { if let providerNumber = String(row["Provider CCN"] ?? ""), // Replace "Provider CCN" with actual column name let hospital = String(row["Hospital Name"] ?? ""), // Replace with actual column name let address = String(row["Street Address"] ?? ""), let city = String(row["City"] ?? ""), // Replace with actual column name let state = String(row["State Code"] ?? ""), // Replace with actual column name let zip = String(row["Zip Code"] ?? ""), // Replace with actual column name let wageIndex = Double(row["Wage Index"] ?? ""), let caseMix = Double(row["Case Max"] ?? ""), let averageCharge = Double(row["Base Charge"] ?? ""), // Replace with actual column name let discharges = Int(row["Medicare Discharges"] ?? ""), let totalCharges = Double(row["Total Charges"] ?? ""), let adjTotalCharges = Double(row["Total Wage Normalized Charges"] ?? "") { // Replace with actual column name let hospitalData = HospitalData( providerNumber: providerNumber, hospital: hospital, address: address, city: city, state: state, zip: zip, wageIndex: wageIndex, caseMix: caseMix, averageCharge: averageCharge, discharges: discharges, totalCharges: totalCharges, adjTotalCharges: adjTotalCharges ) hospitalDataList.append(hospitalData) } } return hospitalDataList } catch { print("Failed to load CSV file: \(error)") return nil } } // Usage Example func main() { if let hospitalData = loadHospitalData() { for data in hospitalData { print("Hospital: (data.hospital), City: (data.city), Average Charge: (data.averageCharge)") } } }
3
0
1.3k
Aug ’24
FB13516799: Training Tabular Regression ML Models on large datasets in Xcode 15 continuously "Processing"
Hi, In Xcode 14 I was able to train linear regression models with Create ML using large CSV files (I tested on about 30000 items and 5 features): However, in Xcode 15 (I tested on 15.0.1 and 15.1), the training continuously stays in the "Processing" state: When using a dataset with 900 items, everything works fine. I filed a feedback for this issue: FB13516799. Does anybody else have this issue / can reproduce it?
3
1
1.5k
Jan ’24
Is there a way to apply for formatting option to a Dataframe column outside of the explicit description(options:) method?
I'm building up a data frame for the sole purpose of using that lovely textual grid output. I'm getting output without any issue, but I'm trying to sort out how I might apply a formatter to a specific column so that print(dataframeInstance) "just works" nicely. In my use case, I'm running a function, collecting its output - appending that into a frame, and then using TabularData to get a nice output in a unit test, so I can see the patterns within the output. I found https://developer.apple.com/documentation/tabulardata/column/description(options:), but wasn't able to find any way to "pre-bind" that to a dataframe Column when I was creating it. (I have some double values that get a bit "excessive" in length due to the joys of floating point rounding) Is there a way of setting a formatter on a column at creation time, or after (using a property) that could basically use the same pattern as that description method above?
1
0
1.1k
Nov ’23
TabluarData DataFrame removing row results in EXC_BAD_ACCESS
I am working with data in Swift using the TabularData framework. I load data from a CSV file into a DataFrame, then copy the data into a second DataFrame, and finally remove a row from the second DataFrame. The problem arises when I try to remove a row from the second DataFrame, at which point I receive an EXC_BAD_ACCESS error. However, if I modify the "timings" column (the final column) before removing the row (even to an identical value), the code runs without errors. Interestingly, this issue only occurs when a row in the column of the CSV file contains more than 15 characters. This is the code I'm using: func loadCSV() { let documentsDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first! let url = documentsDirectory.appendingPathComponent("example.csv") var dataframe: DataFrame do { dataframe = try .init( contentsOfCSVFile: url, columns: ["user", "filename", "syllable count", "timings"], types: ["user": .string, "filename": .string, "syllable count": .integer, "timings": .string] ) } catch { fatalError("Failed to load csv data") } print("First data frame",dataframe, separator: "\n") /// This works var secondFrame = DataFrame() secondFrame.append(column: Column<String>(name: "user", capacity: 1000)) secondFrame.append(column: Column<String>(name: "filename", capacity: 1000)) secondFrame.append(column: Column<Int>(name: "syllable count", capacity: 1000)) secondFrame.append(column: Column<String>(name: "timings", capacity: 1000)) for row in 0..<dataframe.rows.count { secondFrame.appendEmptyRow() for col in 0..<4 { secondFrame.rows[row][col] = dataframe.rows[row][col] } } // secondFrame.rows[row][3, String.self] = String("0123456789ABCDEF") /* If we include this line, it will not crash, even though the content is the same */ print("Second data frame before removing row",dataframe, separator: "\n") // Before removal secondFrame.removeRow(at: 0) print("Second data frame after removing row",dataframe, separator: "\n") // After removal—we will get Thread 1: EXC_BAD_ACCESS here. The line will still print, however } and the csv (minimal example): user,filename,syllable count,timings john,john-001,12,0123456789ABCDEF jane,jane-001,10,0123456789ABCDE I've been able to replicate this bug on macOS and iOS using minimal projects. I'm unsure why this error is occurring and why modifying the "timings" column prevents it. It should be noted that this same error occurs with a single data frame loaded from a CSV file, which means that I basically cannot load from CSV if I want to modify the DataFrame afterwards.
2
1
1.1k
Aug ’23
error: cannot find 'MLDataTable' in scope
I have tried multiple playgrounds and consistently get the same error in any playground I create. There is a tabular data playground that does work but I see nothing I am not doing. Here is the code that fails with Error: cannot find 'MLDataTable' in scope /* code start */ import CoreML import Foundation import TabularData let jsonFile = Bundle.main.url(forResource: "sentiment_analysis", withExtension: "json")! let tempTable = try DataTable let dataTable = try MLDataTable(contentsOf: jsonFile) print(dataTable) /* code end */
3
0
1.2k
Jul ’23
Issue inserting row in TabularData's DataFrame
I'm fairly new to Swift programming so I might be overlooking something, but I'm puzzled why the following code doesn't properly insert a row in a DataFrame. The goal is to move a row at a given index to a new index. I would normally: Copy the row that I want to move Remove the row from the original dataset Insert the copy to the new position The CSV I'm using is from Wikipedia: Year,Make,Model,Description,Price 1997,Ford,E350,"ac, abs, moon",3000.00 1999,Chevy,"Venture ""Extended Edition""","",4900.00 1999,Chevy,"Venture ""Extended Edition, Very Large""","",5000.00 1996,Jeep,Grand Cherokee,"MUST SELL! air, moon roof, loaded",4799.00 My code (Swift playground): import Foundation import TabularData let fileUrl = Bundle.main.url(forResource: "data", withExtension: "csv") let options = CSVReadingOptions(hasHeaderRow: true, delimiter: ",") var dataFrame = try! DataFrame(contentsOfCSVFile: fileUrl!, options: options) print("Original data") print(dataFrame) let rowToMove: Int = 2 let row = dataFrame.rows[rowToMove] print("Row to move") print(row) dataFrame.removeRow(at: rowToMove) print("After removing") print(dataFrame) dataFrame.insert(row: row, at: 0) print("After inserting") print(dataFrame) This results in the following: Original data ┏━━━┳━━━━━━━┳━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━┓ ┃ ┃ Year ┃ Make ┃ Model ┃ Description ┃ Price ┃ ┃ ┃ <Int> ┃ <String> ┃ <String> ┃ <String> ┃ <Double> ┃ ┡━━━╇━━━━━━━╇━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━┩ │ 0 │ 1,997 │ Ford │ E350 │ ac, abs, moon │ 3,000.0 │ │ 1 │ 1,999 │ Chevy │ Venture "Extended Edition" │ │ 4,900.0 │ │ 2 │ 1,999 │ Chevy │ Venture "Extended Edition, Very Large" │ │ 5,000.0 │ │ 3 │ 1,996 │ Jeep │ Grand Cherokee │ MUST SELL! air, moon roof, loaded │ 4,799.0 │ └───┴───────┴──────────┴────────────────────────────────────────┴───────────────────────────────────┴──────────┘ 4 rows, 5 columns Row to move ┏━━━┳━━━━━━━┳━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━┳━━━━━━━━━━┓ ┃ ┃ Year ┃ Make ┃ Model ┃ Description ┃ Price ┃ ┃ ┃ <Int> ┃ <String> ┃ <String> ┃ <String> ┃ <Double> ┃ ┡━━━╇━━━━━━━╇━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━╇━━━━━━━━━━┩ │ 2 │ 1,999 │ Chevy │ Venture "Extended Edition, Very Large" │ │ 5,000.0 │ └───┴───────┴──────────┴────────────────────────────────────────┴─────────────┴──────────┘ 1 row, 5 columns After removing ┏━━━┳━━━━━━━┳━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━┓ ┃ ┃ Year ┃ Make ┃ Model ┃ Description ┃ Price ┃ ┃ ┃ <Int> ┃ <String> ┃ <String> ┃ <String> ┃ <Double> ┃ ┡━━━╇━━━━━━━╇━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━┩ │ 0 │ 1,997 │ Ford │ E350 │ ac, abs, moon │ 3,000.0 │ │ 1 │ 1,999 │ Chevy │ Venture "Extended Edition" │ │ 4,900.0 │ │ 2 │ 1,996 │ Jeep │ Grand Cherokee │ MUST SELL! air, moon roof, loaded │ 4,799.0 │ └───┴───────┴──────────┴────────────────────────────┴───────────────────────────────────┴──────────┘ 3 rows, 5 columns After inserting ┏━━━┳━━━━━━━┳━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━┓ ┃ ┃ Year ┃ Make ┃ Model ┃ Description ┃ Price ┃ ┃ ┃ <Int> ┃ <String> ┃ <String> ┃ <String> ┃ <Double> ┃ ┡━━━╇━━━━━━━╇━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━┩ │ 0 │ 1,999 │ Chevy │ │ │ 5,000.0 │ │ 1 │ 1,997 │ Ford │ E350 │ ac, abs, moon │ 3,000.0 │ │ 2 │ 1,996 │ Jeep │ Grand Cherokee │ MUST SELL! air, moon roof, loaded │ 4,799.0 │ │ 3 │ nil │ nil │ nil │ nil │ nil │ └───┴───────┴──────────┴────────────────────────────────────────┴───────────────────────────────────┴──────────┘ 4 rows, 5 columns Everything is fine up until inserting. I spot a few issues: A row gets deleted (original data row 1) A row filled with nil's is added (at index 3) the row I want to insert isn't properly inserted (notice how the 'model' text has gone). I assume I'm missing something - does it have to do with the row copy keeping its index (2)? How can I fix this?
4
0
1.1k
Jul ’23
CSV files and their usefulness in Data Storage - SwiftUI and Xcode 16 - An important Question?
Although possibly frowned upon, collections of data are commonly input into spreadsheets and this remains, and probably alway will, the main means of data collection and storage rather than databases for scientists everywhere. It is necessary, therefore, to ascertain ways in which data acquired and stored through these means can be successfully extracted and put in a format which can be displayed using modern means of technology. Mobile phones and Tablets are commonly and increasingly being used to access and display data. However, the means by which these data are transferred are through Databases which may not be appealing to some who use spreadsheets to acquire and store their data. The important question is: Is there some straightforward means by which spreadsheet data (perhaps comma separated value (csv) files) can be read by the latest SwiftUI and Xcode 16 formats? This would be an enormous advantage to those who update files of data regularly. If this is so, then I will gladly spend the time to learn the nomenclature and structural language necessary to implement a simple app to display some rather complex data. I have scanned the forum for satisfactory suggestions to this problem but it seems that the software has moved on but the problem remains. Kind Regards,
1
0
1.4k
Jun ’23
Save DataFrames as CSVs
Hi,  I am relatively new to Swift.  How do you save DataFrames as CSVs (using the TabularData Framework)?  For example, having already ‘joined’ two DataFrames on an Xcode Playground, I would like to save that ‘new’ DataFrame as a CSV - locally on my MacBook. Any example code would be much appreciated. Thanks
10
0
2.9k
Jan ’23
Help wanted with how to train a model
Hey there, I am new to developing in the Apple ecosystem, as well as a novice in the field of ML. I have dabbled a little bit in ML with face recognition in Python, but that's about it. The thing I've found is that there is quite a lot of training algorithms and such for computer vision related things, but for my use case I don't really know what I am looking for, from a ML perspective. Thing is, I have real time telemetry data where I have to determine the CURRENT (or near-current) state based on historic data (from NOW and BACKWARDS for 0 to something like 20 seconds, or more). The initial state is pretty easy to determine based on a few values, basically a UInt16 is set to 65535. Most of the possible states can be determined over time from the telemetry, but there are certain cases that can vary quite a lot. These cases are very likely possible to pick up as a human examining the logs, but it's something that seems hard or impossible to create a programmatic logic around. The sample rate in real time is pretty steady at 60 Hz. Basically, I'm curious about what kind of ML model would be suitable to train around this kind of data, and how? Generally, it shouldn't be a huge problem creating the training data. Even if it takes a while to manually mark up the various state changes, it is far from infeasible. Much of the data will differ wildly from dataset to dataset, while some will be very similar from one dataset to another. So, basically I would need a model that can take several datasets of telemetry data (including when the state changes), run it through the ML to train a model that can, using real time data for the last 0-10 seconds or so, determine what state we're in at the moment. In most cases it can even have a "delay" of a second or so. The state change itself is not time critical as such, but it should be able to determine state with a very high confidence as possible within the space of at most 3 seconds. As I understand it the Create ML app can be used for training a model, for use with the app in question. But as mentioned, I have no real idea what is most suitable for training a model with this kind of data that is supposed to analyze data over time. Tabular Regression, Recommendation? Something else entirely? I'm guessing that real time data in production code would be supplied as a dataset with the last 20-30 seconds of data as input to the model, rather than just the last packet of telemetry data. But this is just my assumption. I am attaching a text file with comma separated values from sample telemetry, somewhat truncated for brevity. There are a variety of fields, including coordinates in XYZ space, which have some relation to the state, but can vary wildly from one dataset to another (depending on location). But I assume that the training would automatically give those fields less weight? If anyone can point me in the right direction, I would be really grateful. The finished model will eventually be used in an iOS app that will display the real time telemetry data. Thanks in advance, /Peter sample-telemetry.txt
2
0
1.8k
Dec ’22
Import or use csv data.
Fist of all, I am new in developing. I want to use data from CSV files to process and use for calculation. I tried to use TabularData, but I don't know how and where to write to import or use the csv data. My file name is fnudos.csv. import Foundation import TabularData Data = "fnudos.csv" let nudos = try DataFrame(contentsOfCSVFile: policiesURL, rows: 0..&lt;100) print(nudos) Something like that? But I don't have results. Thanks in advance for your answers.
1
0
1.4k
Nov ’22
TabularData Resources
TabularData framework lets you import, organize, and export a table of data. It’s great when you’re training a machine learning model but it’s a handy tool in many other scenarios as well. General: DevForums tag: TabularData TabularData framework documentation Explore and manipulate data in Swift with TabularData tech talk For a ‘hello world’ style example, see this DevForums post Share and Enjoy — Quinn “The Eskimo!” @ Developer Technical Support @ Apple let myEmail = "eskimo" + "1" + "@" + "apple.com"
Replies
0
Boosts
0
Views
1.8k
Activity
Mar ’23
TabularData doesn't respect Double type when values match Int
Hello, I'm trying to figure out why an Int is being inferred over my explicit Double I'm parsing a CSV that contains 2 tables. I don't own the data so I'm not able to change it. The first row contains one cell that's used as a title for the document The second row is empty The third row contains one cell that's used as the header for the first table There is a header row for the table There's a dynamic number of rows for this table The an empty spacer row There is a row that's used as a title for the second table There is a header row for the table There's a dynamic number of rows for this table Im able to separate and create two DataFrame's from the data without issue. And this is the initializer I'm using. DataFrame( csvData: csvData, rows: rows, types: types, options: options ) Column names and their CSV types looks like this var types: [String: CSVType] { [ // ... "Column 38": .double, // ... ] } The data in the CSV is 0 nil nil nil 2 And this is what the one of the columns in question looks like when printed ▿ 38 : ┏━━━━━━━━━━━┓ ┃ Column 38 ┃ ┃ <Int> ┃ ┡━━━━━━━━━━━┩ │ 0 │ │ nil │ │ nil │ │ nil │ │ 2 │ └───────────┘ - name : "Column 38" - count : 5 ▿ contents : PackedOptionalsArray<Int> ▿ storage : <PackedOptionalsStorage<Int>: 0x600000206360> The docs state /// - types: A dictionary of column names and their CSV types. /// The data frame infers the types for column names that aren't in the dictionary. Since types contains the column name and it's still being inferred, my assumption is that the issue involves the renaming of the header row when it has empty cells occurs after the types are checked. Edit: After setting hasHeaderRow: false from true and adjusting my row offset, the types are now being assigned correctly. I'd recommend opening a feedback enhancement where renaming columns occurs before type assignment.
Replies
1
Boosts
0
Views
83
Activity
Feb ’26
Filtered dataFrame displays original dataFrame values
I'm importing a csv file of 299 rows and creating a subset by filtering on one column value (24 rows). I want to Chart just the filtered values. However, when I print one column I get values from the original dataFrame. Any suggestions? Thanks, David The code: import SwiftUI import Charts import TabularData struct DataPoint: Identifiable { var id = UUID() // This makes it conform to Identifiable var date: Date var value: Double } struct ContentView: View { @State private var dataPoints: [DataPoint] = [] var body: some View { Text("Hello") Chart { ForEach(dataPoints) { dataPoint in PointMark( x: .value("Date", dataPoint.date), y: .value("Value", dataPoint.value) ) } } .frame(height: 300) .padding() .onAppear(perform: loadData) } func loadData() { print("In Loading Data") // Load the CSV file if let url = Bundle.main.url(forResource: "observations", withExtension: "csv") { do { let options = CSVReadingOptions(hasHeaderRow: true, delimiter: ",") var data0 = try DataFrame(contentsOfCSVFile: url, options: options) let formattingOptions = FormattingOptions( maximumLineWidth: 200, maximumCellWidth: 15, maximumRowCount: 30 ) // print(data0.description(options: formattingOptions)) print("Number of Columns: \(data0.columns.count)") let columnsSet = ["plant_id", "date", "plot", "plantNumber", "plantCount"] data0 = try DataFrame(contentsOfCSVFile:url, columns: columnsSet, options: options) print("Number of Columns (after columnsSet): \(data0.columns.count)") print("Printing data0") print(data0.description(options: formattingOptions)) let data = data0.filter { $0["plant_id"] as? Int == 15 } print("Printing data") print(data.description(options: formattingOptions)) print(" Number of Rows \(data.rows.count)") for i in 0 ... data.rows.count { // print("\(i): \(data["plantCount"][i]!)") if let plantCount = data["plantCount"][i] as? Int { print("\(i): \(plantCount)") } else { print("\(i): Value not found or invalid type") } } // // var newDataPoints: [DataPoint] = [] // Here I plan to add the filtered data to DataPoint // DispatchQueue.main.async { dataPoints = newDataPoints } } catch { print("Error reading CSV file: \(error)") } } else { print("Didn't load csv file") } } } struct ContentView_Previews: PreviewProvider { static var previews: some View { ContentView() } } Here is the new dataFrame and print output Printing data ┏━━━━━┳━━━━━━━━━━┳━━━━━━━━━━━━┳━━━━━━━┳━━━━━━━━━━━━━┳━━━━━━━━━━━━┓ ┃ ┃ plant_id ┃ date ┃ plot ┃ plantNumber ┃ plantCount ┃ ┃ ┃ <Int> ┃ <String> ┃ <Int> ┃ <Int> ┃ <Int> ┃ ┡━━━━━╇━━━━━━━━━━╇━━━━━━━━━━━━╇━━━━━━━╇━━━━━━━━━━━━━╇━━━━━━━━━━━━┩ │ 0 │ 15 │ 2023-09-07 │ 1 │ 5 │ 5 │ │ 32 │ 15 │ 2023-09-07 │ 2 │ 10 │ 10 │ │ 38 │ 15 │ 2023-09-07 │ 2 │ 20 │ 20 │ │ 66 │ 15 │ 2023-09-07 │ 4 │ 25 │ 25 │ │ 77 │ 15 │ 2023-09-07 │ 5 │ 5 │ 5 │ │ 99 │ 15 │ 2023-09-14 │ 7 │ 45 │ 45 │ │ 142 │ 15 │ 2024-05-30 │ 1 │ 20 │ 20 │ │ 162 │ 15 │ 2024-05-30 │ 4 │ 5 │ 5 │ │ 169 │ 15 │ 2024-05-30 │ 5 │ 10 │ 10 │ │ 175 │ 15 │ 2024-05-30 │ 7 │ 10 │ 10 │ │ 188 │ 15 │ 2024-07-11 │ 1 │ 20 │ 40 │ │ 199 │ 15 │ 2024-07-11 │ 2 │ 5 │ 5 │ │ 215 │ 15 │ 2024-07-11 │ 5 │ 20 │ 30 │ │ 220 │ 15 │ 2024-07-11 │ 7 │ 30 │ 40 │ │ 236 │ 15 │ 2024-09-06 │ 1 │ 20 │ 60 │ │ 238 │ 15 │ 2024-09-06 │ 2 │ 30 │ 35 │ │ 248 │ 15 │ 2024-09-06 │ 5 │ 5 │ 35 │ │ 254 │ 15 │ 2024-09-06 │ 7 │ 50 │ 90 │ │ 267 │ 15 │ 2025-05-04 │ 1 │ 10 │ 70 │ │ 273 │ 15 │ 2025-05-04 │ 2 │ 10 │ 45 │ │ 282 │ 15 │ 2025-05-04 │ 5 │ 10 │ 45 │ │ 287 │ 15 │ 2025-05-04 │ 7 │ 30 │ 120 │ │ 292 │ 15 │ 2025-05-04 │ 8 │ 10 │ 0 │ │ 297 │ 15 │ 2925-05-04 │ 3 │ 10 │ 0 │ └─────┴──────────┴────────────┴───────┴─────────────┴────────────┘ 24 rows, 5 columns Number of Rows 24 0: 5 1: 80 2: 1 3: 1 4: 1 5: 3 6: 3 7: 1 8: 6 9: 1 10: 1 11: 1 12: 1 13: 10 14: 50 15: 1 16: 2 17: 1 18: 3 19: 8 20: 5 21: 3 22: 7 23: 2 24: 1
Replies
2
Boosts
0
Views
165
Activity
May ’25
How to Visualize Data in a DataFrame Using Charts
I want to visualize the data stored in a DataFrame using various charts (barmark, sectormark, linemark, etc.). My questions are as follows: Can a DataFrame be used directly within a chart? If so, could you provide a simple example? If it cannot be used directly, what is the correct way to use it? Could you provide an example? Thank you for your help. Best regards.
Replies
0
Boosts
1
Views
420
Activity
Jan ’25
CreatML stop training
It appears that there is a size limit when training the Tabular Classification model in CreatML. When the training data is small, the training process completes smoothly after a specified period. However, as the data volume increases, the following issues occur: initially, the training process indicates that it is in progress, but after approximately 24 hours, it is automatically terminated after an hour. I am certain that this is not a manual termination by myself or others, but rather an automatic termination by the machine. This issue persists despite numerous attempts, and the only message displayed is “Training Canceled.” I would appreciate it if someone could explain the reason behind this behavior and provide a solution. Thank you for your assistance.
Replies
1
Boosts
0
Views
662
Activity
Jan ’25
Create ML Trouble Loading CSV to Train Word Tagger With Commas in Training Data
I'm using Numbers to build a spreadsheet that I'm exporting as a CSV. I then import this file into Create ML to train a word tagger model. Everything has been working fine for all the models I've trained so far, but now I'm coming across a use case that has been breaking the import process: commas within the training data. This is a case that none of Apple's examples show. My project takes Navajo text that has been tokenized by syllables and labels the parts-of-speech. Case that works... Raw text: Naaltsoos yídéeshtah. Tokens column: Naal,tsoos, ,yí,déesh,tah,. Labels column: NObj,NObj,Space,Verb,Verb,VStem,Punct Case that breaks... Raw text: óola, béésh łigaii, tłʼoh naadą́ą́ʼ, wáin, akʼah, dóó á,shįįh Tokens column with tokenized text (commas quoted): óo,la,",", ,béésh, ,łi,gaii,",", ,tłʼoh, ,naa,dą́ą́ʼ,",", ,wáin,",", ,a,kʼah,",", ,dóó, ,á,shįįh (Create ML reports mismatched columns) Tokens column with tokenized text (commas escaped): óo,la,\,, ,béésh, ,łi,gaii,\,, ,tłʼoh, ,naa,dą́ą́ʼ,\,, ,wáin,\,, ,a,kʼah,\,, ,dóó, ,á,shįįh (Create ML reports mismatched columns) Tokens column with tokenized text (commas escape-quoted): óo,la,\",\", ,béésh, ,łi,gaii,\",\", ,tłʼoh, ,naa,dą́ą́ʼ,\",\", ,wáin,\",\", ,a,kʼah,\",\", ,dóó, ,á,shįįh (record not detected by Create ML) Tokens column with tokenized text (commas escape-quoted): óo,la,"","", ,béésh, ,łi,gaii,"","", ,tłʼoh, ,naa,dą́ą́ʼ,"","", ,wáin,"","", ,a,kʼah,"","", ,dóó, ,á,shįįh (Create ML reports mismatched columns) Labels column: NSub,NSub,Punct,Space,NSub,Space,NSub,NSub,Punct,Space,NSub,Space,NSub,NSub,Punct,Space,NSub,Punct,Space,NSub,NSub,Punct,Space,Conj,Space,NSub,NSub Sample From Spreadsheet Solution Needed It's simple enough to escape commas within CSV files, but the format needed by Create ML essentially combines entire CSV records into single columns, so I'm ending up needing a CSV record that contains a mixture of commas to use for parsing and ones to use as character literals. That's where this gets complicated. For this particular use case (which seems like it would frequently arise when training a word tagger model), how should I properly escape a comma literal?
Replies
6
Boosts
0
Views
864
Activity
Jan ’25
How to confirm whether CreatML is training
I am currently training a Tabular Classification model in CreatML. The dataset comprises 30 features, including 1,000,000 training data points and 1,000,000 verification data points. Could you please estimate the approximate training time for an M4Max MacBook Pro? During the training process, CreatML has been displaying the “Processing” status, but there is no progress bar. I would like to ascertain whether the training is still ongoing, as I have often suspected that it has ceased.
Replies
1
Boosts
0
Views
694
Activity
Jan ’25
How to update data in a DataFrame
My project loads a CSV into a DataFrame and displays it in a Table (a MacOS app). So far so good ... but when trying to update a value in a column, I dont see anyway to update this value. The table gets the value for the column like this: func getColumnValue(row :DataFrame.Rows.Element, columnName :String) -&gt; String { if row.base.containsColumn(columnName) { var value = "" if row[columnName] != nil { value = "\(row[columnName]!)" } return value } ... But the documentation and googles dont show any way to update the same column. Any help is appreciated with cookies. Attempt to update: func setColumnValue(row :DataFrame.Rows.Element, columnName :String, value :String) { var column: [String?] = data[columnName] column[row.id] = value ... }
Replies
6
Boosts
0
Views
738
Activity
Dec ’24
Help Loading an External CSV File on iOS in Swift
Hi everyone! I’m fairly new to Swift and currently working on a small iOS app in SwiftUI. The app is able to load a CSV file embedded in the Xcode project (using Bundle.main.path(forResource:)), and everything works well with that. Now, I want to take it a step further by allowing the app to load an external CSV file located in the iPhone’s directories (like “Documents” or “Downloads”). However, I’m struggling to make it work. I tried using a DocumentPicker to select the CSV file, and I believe I’m passing the file URL correctly, but the app keeps reading only the embedded file instead of the one selected by the user. Could anyone offer guidance on how to properly set up loading an external CSV file? I’m still learning, so any suggestions or examples would be really appreciated! Thanks a lot in advance for the help! Here’s the code that isn’t working as expected: import Foundation struct Product: Identifiable { let id = UUID() var codice: String var descrizione: String var prezzo: Double var installazione: Double var trasporto: Double } class ProductViewModel: ObservableObject { @Published var products: [Product] = [] @Published var filteredProducts: [Product] = [] func loadCSV(from url: URL) { products = [] do { let data = try String(contentsOf: url) let lines = data.components(separatedBy: "\n") // Legge e processa ogni riga del CSV (saltando la prima riga se è l'intestazione) for line in lines.dropFirst() { let values = line.components(separatedBy: ";") // Assicurati che ci siano abbastanza colonne e gestisci i valori mancanti if values.count &gt;= 5 { let codice = values[0].trimmingCharacters(in: .whitespaces) let descrizione = values[1].trimmingCharacters(in: .whitespaces) let prezzo = parseEuropeanDouble(values[2]) ?? 0.0 let installazione = parseEuropeanDouble(values[3].isEmpty ? "0,00" : values[3]) ?? 0.0 let trasporto = parseEuropeanDouble(values[4].isEmpty ? "0,00" : values[4]) ?? 0.0 let product = Product( codice: codice, descrizione: descrizione, prezzo: prezzo, installazione: installazione, trasporto: trasporto ) products.append(product) } } filteredProducts = products } catch { print("Errore nel caricamento del CSV: \(error)") } } private func parseEuropeanDouble(_ value: String) -&gt; Double? { let formatter = NumberFormatter() formatter.locale = Locale(identifier: "it_IT") formatter.numberStyle = .decimal return formatter.number(from: value)?.doubleValue } } struct ContentView: View { @StateObject var viewModel = ProductViewModel() @State private var showFilePicker = false var body: some View { VStack { Button("Carica file CSV") { showFilePicker = true } .fileImporter(isPresented: $showFilePicker, allowedContentTypes: [.commaSeparatedText]) { result in switch result { case .success(let url): viewModel.loadCSV(from: url) case .failure(let error): print("Errore nel caricamento del file: \(error.localizedDescription)") } } List(viewModel.filteredProducts) { product in VStack(alignment: .leading) { Text("Codice: \(product.codice)") Text("Descrizione: \(product.descrizione)") Text("Prezzo Lordo: €\(String(format: "%.2f", product.prezzo))") Text("Installazione: €\(String(format: "%.2f", product.installazione))") Text("Trasporto: €\(String(format: "%.2f", product.trasporto))") } } } .padding() } }
Replies
2
Boosts
0
Views
944
Activity
Nov ’24
CSV File Load Into Swift
Hi, I am fairly new Xcode/Swift and am trying to load a CSV File for use in my development. I have placed the CSV file in my Assets folder but when I try to create my Data Model and load the CSV file. I run into the error: No exact matches in call to initializer. Below is the code. I have attached CSV File. Any help fixing this error would be greatly appreciated. Thanks in advance for your help. Brian Hospital_Demographic_Data_Sample.csv import Foundation import CSV struct HospitalData: Codable { let providerNumber: String let hospital: String let address: String let city: String let state: String let zip: String let wageIndex: Double let caseMix: Double let averageCharge: Double let discharges: Int let totalCharges: Double let adjTotalCharges: Double // Add other fields as needed based on the columns in your CSV file } func loadHospitalData() -> [HospitalData]? { guard let filePath = Bundle.main.path(forResource: "Hospital_Demographic_Data", ofType: "csv") else { print("File not found") return nil } do { let csv = try CSV(url: URL(fileURLWithPath: filePath)) var hospitalDataList = [HospitalData]() // Initialize as an empty array for row in csv.namedRows { if let providerNumber = String(row["Provider CCN"] ?? ""), // Replace "Provider CCN" with actual column name let hospital = String(row["Hospital Name"] ?? ""), // Replace with actual column name let address = String(row["Street Address"] ?? ""), let city = String(row["City"] ?? ""), // Replace with actual column name let state = String(row["State Code"] ?? ""), // Replace with actual column name let zip = String(row["Zip Code"] ?? ""), // Replace with actual column name let wageIndex = Double(row["Wage Index"] ?? ""), let caseMix = Double(row["Case Max"] ?? ""), let averageCharge = Double(row["Base Charge"] ?? ""), // Replace with actual column name let discharges = Int(row["Medicare Discharges"] ?? ""), let totalCharges = Double(row["Total Charges"] ?? ""), let adjTotalCharges = Double(row["Total Wage Normalized Charges"] ?? "") { // Replace with actual column name let hospitalData = HospitalData( providerNumber: providerNumber, hospital: hospital, address: address, city: city, state: state, zip: zip, wageIndex: wageIndex, caseMix: caseMix, averageCharge: averageCharge, discharges: discharges, totalCharges: totalCharges, adjTotalCharges: adjTotalCharges ) hospitalDataList.append(hospitalData) } } return hospitalDataList } catch { print("Failed to load CSV file: \(error)") return nil } } // Usage Example func main() { if let hospitalData = loadHospitalData() { for data in hospitalData { print("Hospital: (data.hospital), City: (data.city), Average Charge: (data.averageCharge)") } } }
Replies
3
Boosts
0
Views
1.3k
Activity
Aug ’24
FB13516799: Training Tabular Regression ML Models on large datasets in Xcode 15 continuously "Processing"
Hi, In Xcode 14 I was able to train linear regression models with Create ML using large CSV files (I tested on about 30000 items and 5 features): However, in Xcode 15 (I tested on 15.0.1 and 15.1), the training continuously stays in the "Processing" state: When using a dataset with 900 items, everything works fine. I filed a feedback for this issue: FB13516799. Does anybody else have this issue / can reproduce it?
Replies
3
Boosts
1
Views
1.5k
Activity
Jan ’24
Is there a way to apply for formatting option to a Dataframe column outside of the explicit description(options:) method?
I'm building up a data frame for the sole purpose of using that lovely textual grid output. I'm getting output without any issue, but I'm trying to sort out how I might apply a formatter to a specific column so that print(dataframeInstance) "just works" nicely. In my use case, I'm running a function, collecting its output - appending that into a frame, and then using TabularData to get a nice output in a unit test, so I can see the patterns within the output. I found https://developer.apple.com/documentation/tabulardata/column/description(options:), but wasn't able to find any way to "pre-bind" that to a dataframe Column when I was creating it. (I have some double values that get a bit "excessive" in length due to the joys of floating point rounding) Is there a way of setting a formatter on a column at creation time, or after (using a property) that could basically use the same pattern as that description method above?
Replies
1
Boosts
0
Views
1.1k
Activity
Nov ’23
Sample code for tabulardata functions
Can anyone show me some sample codes of the following function? func aggregated<Element, Result>( on columnNames: [String], naming: (String) -> String, transform: (DiscontiguousColumnSlice) throws -> Result? ) rethrows -> DataFrame
Replies
1
Boosts
0
Views
719
Activity
Nov ’23
TabluarData DataFrame removing row results in EXC_BAD_ACCESS
I am working with data in Swift using the TabularData framework. I load data from a CSV file into a DataFrame, then copy the data into a second DataFrame, and finally remove a row from the second DataFrame. The problem arises when I try to remove a row from the second DataFrame, at which point I receive an EXC_BAD_ACCESS error. However, if I modify the "timings" column (the final column) before removing the row (even to an identical value), the code runs without errors. Interestingly, this issue only occurs when a row in the column of the CSV file contains more than 15 characters. This is the code I'm using: func loadCSV() { let documentsDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first! let url = documentsDirectory.appendingPathComponent("example.csv") var dataframe: DataFrame do { dataframe = try .init( contentsOfCSVFile: url, columns: ["user", "filename", "syllable count", "timings"], types: ["user": .string, "filename": .string, "syllable count": .integer, "timings": .string] ) } catch { fatalError("Failed to load csv data") } print("First data frame",dataframe, separator: "\n") /// This works var secondFrame = DataFrame() secondFrame.append(column: Column<String>(name: "user", capacity: 1000)) secondFrame.append(column: Column<String>(name: "filename", capacity: 1000)) secondFrame.append(column: Column<Int>(name: "syllable count", capacity: 1000)) secondFrame.append(column: Column<String>(name: "timings", capacity: 1000)) for row in 0..<dataframe.rows.count { secondFrame.appendEmptyRow() for col in 0..<4 { secondFrame.rows[row][col] = dataframe.rows[row][col] } } // secondFrame.rows[row][3, String.self] = String("0123456789ABCDEF") /* If we include this line, it will not crash, even though the content is the same */ print("Second data frame before removing row",dataframe, separator: "\n") // Before removal secondFrame.removeRow(at: 0) print("Second data frame after removing row",dataframe, separator: "\n") // After removal—we will get Thread 1: EXC_BAD_ACCESS here. The line will still print, however } and the csv (minimal example): user,filename,syllable count,timings john,john-001,12,0123456789ABCDEF jane,jane-001,10,0123456789ABCDE I've been able to replicate this bug on macOS and iOS using minimal projects. I'm unsure why this error is occurring and why modifying the "timings" column prevents it. It should be noted that this same error occurs with a single data frame loaded from a CSV file, which means that I basically cannot load from CSV if I want to modify the DataFrame afterwards.
Replies
2
Boosts
1
Views
1.1k
Activity
Aug ’23
error: cannot find 'MLDataTable' in scope
I have tried multiple playgrounds and consistently get the same error in any playground I create. There is a tabular data playground that does work but I see nothing I am not doing. Here is the code that fails with Error: cannot find 'MLDataTable' in scope /* code start */ import CoreML import Foundation import TabularData let jsonFile = Bundle.main.url(forResource: "sentiment_analysis", withExtension: "json")! let tempTable = try DataTable let dataTable = try MLDataTable(contentsOf: jsonFile) print(dataTable) /* code end */
Replies
3
Boosts
0
Views
1.2k
Activity
Jul ’23
Issue inserting row in TabularData's DataFrame
I'm fairly new to Swift programming so I might be overlooking something, but I'm puzzled why the following code doesn't properly insert a row in a DataFrame. The goal is to move a row at a given index to a new index. I would normally: Copy the row that I want to move Remove the row from the original dataset Insert the copy to the new position The CSV I'm using is from Wikipedia: Year,Make,Model,Description,Price 1997,Ford,E350,"ac, abs, moon",3000.00 1999,Chevy,"Venture ""Extended Edition""","",4900.00 1999,Chevy,"Venture ""Extended Edition, Very Large""","",5000.00 1996,Jeep,Grand Cherokee,"MUST SELL! air, moon roof, loaded",4799.00 My code (Swift playground): import Foundation import TabularData let fileUrl = Bundle.main.url(forResource: "data", withExtension: "csv") let options = CSVReadingOptions(hasHeaderRow: true, delimiter: ",") var dataFrame = try! DataFrame(contentsOfCSVFile: fileUrl!, options: options) print("Original data") print(dataFrame) let rowToMove: Int = 2 let row = dataFrame.rows[rowToMove] print("Row to move") print(row) dataFrame.removeRow(at: rowToMove) print("After removing") print(dataFrame) dataFrame.insert(row: row, at: 0) print("After inserting") print(dataFrame) This results in the following: Original data ┏━━━┳━━━━━━━┳━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━┓ ┃ ┃ Year ┃ Make ┃ Model ┃ Description ┃ Price ┃ ┃ ┃ <Int> ┃ <String> ┃ <String> ┃ <String> ┃ <Double> ┃ ┡━━━╇━━━━━━━╇━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━┩ │ 0 │ 1,997 │ Ford │ E350 │ ac, abs, moon │ 3,000.0 │ │ 1 │ 1,999 │ Chevy │ Venture "Extended Edition" │ │ 4,900.0 │ │ 2 │ 1,999 │ Chevy │ Venture "Extended Edition, Very Large" │ │ 5,000.0 │ │ 3 │ 1,996 │ Jeep │ Grand Cherokee │ MUST SELL! air, moon roof, loaded │ 4,799.0 │ └───┴───────┴──────────┴────────────────────────────────────────┴───────────────────────────────────┴──────────┘ 4 rows, 5 columns Row to move ┏━━━┳━━━━━━━┳━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━┳━━━━━━━━━━┓ ┃ ┃ Year ┃ Make ┃ Model ┃ Description ┃ Price ┃ ┃ ┃ <Int> ┃ <String> ┃ <String> ┃ <String> ┃ <Double> ┃ ┡━━━╇━━━━━━━╇━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━╇━━━━━━━━━━┩ │ 2 │ 1,999 │ Chevy │ Venture "Extended Edition, Very Large" │ │ 5,000.0 │ └───┴───────┴──────────┴────────────────────────────────────────┴─────────────┴──────────┘ 1 row, 5 columns After removing ┏━━━┳━━━━━━━┳━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━┓ ┃ ┃ Year ┃ Make ┃ Model ┃ Description ┃ Price ┃ ┃ ┃ <Int> ┃ <String> ┃ <String> ┃ <String> ┃ <Double> ┃ ┡━━━╇━━━━━━━╇━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━┩ │ 0 │ 1,997 │ Ford │ E350 │ ac, abs, moon │ 3,000.0 │ │ 1 │ 1,999 │ Chevy │ Venture "Extended Edition" │ │ 4,900.0 │ │ 2 │ 1,996 │ Jeep │ Grand Cherokee │ MUST SELL! air, moon roof, loaded │ 4,799.0 │ └───┴───────┴──────────┴────────────────────────────┴───────────────────────────────────┴──────────┘ 3 rows, 5 columns After inserting ┏━━━┳━━━━━━━┳━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━┓ ┃ ┃ Year ┃ Make ┃ Model ┃ Description ┃ Price ┃ ┃ ┃ <Int> ┃ <String> ┃ <String> ┃ <String> ┃ <Double> ┃ ┡━━━╇━━━━━━━╇━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━┩ │ 0 │ 1,999 │ Chevy │ │ │ 5,000.0 │ │ 1 │ 1,997 │ Ford │ E350 │ ac, abs, moon │ 3,000.0 │ │ 2 │ 1,996 │ Jeep │ Grand Cherokee │ MUST SELL! air, moon roof, loaded │ 4,799.0 │ │ 3 │ nil │ nil │ nil │ nil │ nil │ └───┴───────┴──────────┴────────────────────────────────────────┴───────────────────────────────────┴──────────┘ 4 rows, 5 columns Everything is fine up until inserting. I spot a few issues: A row gets deleted (original data row 1) A row filled with nil's is added (at index 3) the row I want to insert isn't properly inserted (notice how the 'model' text has gone). I assume I'm missing something - does it have to do with the row copy keeping its index (2)? How can I fix this?
Replies
4
Boosts
0
Views
1.1k
Activity
Jul ’23
CSV files and their usefulness in Data Storage - SwiftUI and Xcode 16 - An important Question?
Although possibly frowned upon, collections of data are commonly input into spreadsheets and this remains, and probably alway will, the main means of data collection and storage rather than databases for scientists everywhere. It is necessary, therefore, to ascertain ways in which data acquired and stored through these means can be successfully extracted and put in a format which can be displayed using modern means of technology. Mobile phones and Tablets are commonly and increasingly being used to access and display data. However, the means by which these data are transferred are through Databases which may not be appealing to some who use spreadsheets to acquire and store their data. The important question is: Is there some straightforward means by which spreadsheet data (perhaps comma separated value (csv) files) can be read by the latest SwiftUI and Xcode 16 formats? This would be an enormous advantage to those who update files of data regularly. If this is so, then I will gladly spend the time to learn the nomenclature and structural language necessary to implement a simple app to display some rather complex data. I have scanned the forum for satisfactory suggestions to this problem but it seems that the software has moved on but the problem remains. Kind Regards,
Replies
1
Boosts
0
Views
1.4k
Activity
Jun ’23
Save DataFrames as CSVs
Hi,  I am relatively new to Swift.  How do you save DataFrames as CSVs (using the TabularData Framework)?  For example, having already ‘joined’ two DataFrames on an Xcode Playground, I would like to save that ‘new’ DataFrame as a CSV - locally on my MacBook. Any example code would be much appreciated. Thanks
Replies
10
Boosts
0
Views
2.9k
Activity
Jan ’23
Help wanted with how to train a model
Hey there, I am new to developing in the Apple ecosystem, as well as a novice in the field of ML. I have dabbled a little bit in ML with face recognition in Python, but that's about it. The thing I've found is that there is quite a lot of training algorithms and such for computer vision related things, but for my use case I don't really know what I am looking for, from a ML perspective. Thing is, I have real time telemetry data where I have to determine the CURRENT (or near-current) state based on historic data (from NOW and BACKWARDS for 0 to something like 20 seconds, or more). The initial state is pretty easy to determine based on a few values, basically a UInt16 is set to 65535. Most of the possible states can be determined over time from the telemetry, but there are certain cases that can vary quite a lot. These cases are very likely possible to pick up as a human examining the logs, but it's something that seems hard or impossible to create a programmatic logic around. The sample rate in real time is pretty steady at 60 Hz. Basically, I'm curious about what kind of ML model would be suitable to train around this kind of data, and how? Generally, it shouldn't be a huge problem creating the training data. Even if it takes a while to manually mark up the various state changes, it is far from infeasible. Much of the data will differ wildly from dataset to dataset, while some will be very similar from one dataset to another. So, basically I would need a model that can take several datasets of telemetry data (including when the state changes), run it through the ML to train a model that can, using real time data for the last 0-10 seconds or so, determine what state we're in at the moment. In most cases it can even have a "delay" of a second or so. The state change itself is not time critical as such, but it should be able to determine state with a very high confidence as possible within the space of at most 3 seconds. As I understand it the Create ML app can be used for training a model, for use with the app in question. But as mentioned, I have no real idea what is most suitable for training a model with this kind of data that is supposed to analyze data over time. Tabular Regression, Recommendation? Something else entirely? I'm guessing that real time data in production code would be supplied as a dataset with the last 20-30 seconds of data as input to the model, rather than just the last packet of telemetry data. But this is just my assumption. I am attaching a text file with comma separated values from sample telemetry, somewhat truncated for brevity. There are a variety of fields, including coordinates in XYZ space, which have some relation to the state, but can vary wildly from one dataset to another (depending on location). But I assume that the training would automatically give those fields less weight? If anyone can point me in the right direction, I would be really grateful. The finished model will eventually be used in an iOS app that will display the real time telemetry data. Thanks in advance, /Peter sample-telemetry.txt
Replies
2
Boosts
0
Views
1.8k
Activity
Dec ’22
Import or use csv data.
Fist of all, I am new in developing. I want to use data from CSV files to process and use for calculation. I tried to use TabularData, but I don't know how and where to write to import or use the csv data. My file name is fnudos.csv. import Foundation import TabularData Data = "fnudos.csv" let nudos = try DataFrame(contentsOfCSVFile: policiesURL, rows: 0..&lt;100) print(nudos) Something like that? But I don't have results. Thanks in advance for your answers.
Replies
1
Boosts
0
Views
1.4k
Activity
Nov ’22