Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
6 years ago
Solved

Azure Machine Learning - Python Query Script with Power BI dataset

Hi.

Currently I've created a machine learning experiment and deployed, and want to use dataset in Power BI as input, and consume the data from Azure Machine Learning. Currently using the new Azure Machine Learning Preview. I have created a real.time Endpoint, got the URL and keys, also Python script. I can test and get data from Azure ML.

Now i want to use data in a dataset as input, and get a new column with the result from the ML.
In Power BI query, i select Pythn Script, and paste the script from Azure Machine Learning. It seems to connect, but guess i need an output section in the script. I'm not an experienced python programmer, but have programmed different solutions.

 

I hope someone could give me the push to consume the Azure ML service.

  • Anonymous's avatar
    Anonymous
    6 years ago

    This post started with how to code Python to consume Azure ML predictive model. I ended up with scripting in "R", as i found good examples.
    1: Have a Azure ML predictive model
    2: Want to use Power BI to consume and get the prediction from azure ML (web service)

    3: Add the prediction to the original dataset in Power BI.

     

    1. Added datasource in Power BI.

    2. Edit Queries

    3. Transform, and choose "Run R- script"

    Added this code (dataset is Power BI data, you get it directly):

     

    # 'dataset' holds the input data for this script
    library("RCurl")
    library("rjson")
    library("bitops")
    # Accept SSL certificates issued by public Certificate Authorities
    options(RCurlOptions = list(cainfo = system.file("CurlSSL", "cacert.pem", package = "RCurl")))
    dataset <- dataset[complete.cases(dataset), ] ##remove NaN
    h = basicTextGatherer()
    hdr = basicHeaderGatherer()
    req =  list(
      Inputs = list(
        input1 = setNames(lapply(split(dataset, seq(nrow(dataset))), FUN = as.list), NULL)
      ),
      GlobalParameters = setNames(fromJSON('{}'), character(0))
    )
    body = enc2utf8(toJSON(req))
    api_key = "<replace key>" # Replace this with the API key for the web service
    authz_hdr = paste('Bearer', api_key, sep=' ')
    h$reset()
    curlPerform(url = "<replace url>", # Replace this with the URL for the web service
                httpheader=c('Content-Type' = "application/json", 'Authorization' = authz_hdr),
                postfields=body,
                writefunction = h$update,
                headerfunction = hdr$update,
                verbose = TRUE
    )
    headers = hdr$value()
    httpStatus = headers["status"]
    if (httpStatus >= 400){
      result.error <- data.frame("Error")
    }else{
      result.json = h$value()
      result.list = fromJSON(result.json)$Results$output1
      result.list.final <- lapply(result.list, FUN = function(x) lapply(x, FUN = function(y) if( is.null(y) ){y <- NA}else{y <- y}))
      result.df <- data.frame(do.call(rbind.data.frame, result.list.final))
      result.df = merge(dataset,result.df,by.x="row_id",by.y="row_id") #to combine original dataset with prediction
    }

     

     
    Found most of this code in this website:
     
    In this way, it is possible to consume Azure ML without Premium license.

10 Replies