DataFrame's Column doesn't support array of dictionary

I'm following Apple WWDC video (https://developer.apple.com/videos/play/wwdc2021/10037/) about how to create a recommendation model. But I'm getting this error when I run the project on that like of code from their tutorial.

"Column keywords has element of unsupported type Dictionary<String, Double>."

Here is the block of code took from the transcript of WWDC video that cause me issue:

func featuresFromMealAndKeywords(meal: String, keywords: [String]) -> [String: Double] {

    // Capture interactions between content (the dish keywords) and context (meal) by
    // adding a copy of each keyword modified to include the meal.
    let featureNames = keywords + keywords.map { meal + ":" + $0 }
    
    // For each keyword, create an entry in a dictionary of features with a value of 1.0.
    return featureNames.reduce(into: [:]) { features, name in
        features[name] = 1.0
    }
}


var trainingKeywords: [[String: Double]] = []
var trainingTargets: [Double] = []

for item in userPurchasedItems {
    // Add in the positive example.
    trainingKeywords.append(
       featuresFromMealAndKeywords(meal: item.meal, keywords: item.keywords))
    trainingTargets.append(1.0)
            
    // Add in the negative example.
    let negativeKeywords = allKeywords.subtracting(item.keywords)
    trainingKeywords.append(
       featuresFromMealAndKeywords(meal: item.meal, keywords: Array(negativeKeywords)))
    trainingTargets.append(-1.0)
}

// Create the training data.
var trainingData = DataFrame()
trainingData.append(column: Column(name: "keywords" contents: trainingKeywords))
trainingData.append(column: Column(name: "target", contents: trainingTargets))

// Create the model.
let model = try MLLinearRegressor(trainingData: trainingData, targetColumn: "target")

Did DataFrame implementation changed since then and doesn't support Dictionary anymore? I'm at lost right now on how to reproduce their example.

Post not yet marked as solved Up vote post of Jaythaking Down vote post of Jaythaking
523 views

Replies

Try JSON encoding a data model and pass it into DataFrame.

  • Doesn't work, now I'm getting : 'CreateML Generic Error: Expected integer values for feature 'keywords', but got UInt8'.

Add a Comment

I've finally initialized the DataFrame like this

var trainingData: DataFrame = ["keywords" : data.trainingKeywords, "target": data.trainingTargets]

but now i'm getting this error when creating my data model like this:

let model = try MLLinearRegressor(trainingData: trainingData, targetColumn: "target")

CreateML Type Error: Column keywords has element of unsupported type Any.

Any idea from there on?

Thanks for reporting. As a workaround in the mean time, change the type of trainingKeywords to [[String: Any?]].

I'm also having the same problem (Xcode 15.3). I've tried every which was of casting the DataFrame to use [[String: Any?]] but I haven't had any luck. @Jaythaking did you ever get this to work?