Azure ML Web服务请求无法在C#中工作

时间:2016-02-24 11:28:13

标签: c# asp.net azure azure-machine-learning-studio cortana-intelligence

我创建了一个Azure ML Web服务,根据请求输出JSON响应,示例请求的结构如下:

{
  "Inputs": {
    "input1": {
      "ColumnNames": [
        "gender",
        "age",
        "income"
      ],
      "Values": [
        [
          "value",
          "0",
          "0"
        ],
        [
          "value",
          "0",
          "0"
        ]
      ]
    }
  },
  "GlobalParameters": {}
}

输入参数应该是这样的:

性别字符串
年龄数字
收入数字

My Post方法如下所示:

    [HttpPost]
        public ActionResult GetPredictionFromWebService()
        {
            var gender = Request.Form["gender"];
            var age = Request.Form["age"];


            if (!string.IsNullOrEmpty(gender) && !string.IsNullOrEmpty(age))
            {
                var resultResponse = _incomeWebService.InvokeRequestResponseService<ResultOutcome>(gender, age).Result;


                if (resultResponse != null)
                {
                    var result = resultResponse.Results.Output1.Value.Values;
                    PersonResult = new Person
                    {
                        Gender = result[0, 0],
                        Age = Int32.Parse(result[0, 1]),
                        Income = Int32.Parse(result[0, 2])
                    };
                }
            }




            return RedirectToAction("index");
        }

但无论出于何种原因; Azure ML Webservice似乎没有响应我的请求。 有谁知道原因可能是什么?我没有看到任何错误或任何错误,只是空洞的回应。

1 个答案:

答案 0 :(得分:2)

您的问题的答案是,在Azure ML的输入参数中写入的“数字”数据类型实际上是浮点数,而不是您的收入度量的整数。因此,当尝试从Azure ML请求响应时,您没有为其提供正确格式所需的“足够”信息,以使其正确响应,从而导致它没有给您任何响应。

我相信您的模型会根据您的输入参数看起来与此类似:

public class Person
    {
        public string Gender { get; set; }
        public int Age { get; set; }
        public int Income { get; set; }


        public override string ToString()
        {
            return Gender + "," + Age + "," + Income;
        }
    }

您必须将您的Income数据类型更改为float,如下所示:

public class Person
{
    public string Gender { get; set; }
    public int Age { get; set; }
    public float Income { get; set; }

    public override string ToString()
    {
        return Gender + "," + Age + "," + Income;
    }
}

然后你的post-method看起来像这样:

    [HttpPost]
    public ActionResult GetPredictionFromWebService()
    {
        var gender = Request.Form["gender"];
        var age = Request.Form["age"];

        if (!string.IsNullOrEmpty(gender) && !string.IsNullOrEmpty(age))
        {
            var resultResponse = _incomeWebService.InvokeRequestResponseService<ResultOutcome>(gender, age).Result;

                if (resultResponse != null)
                {
                    var result = resultResponse.Results.Output1.Value.Values;
                    PersonResult = new Person
                    {
                        Gender = result[0, 0],
                        Age = Int32.Parse(result[0, 1]),
                        Income = float.Parse(result[0, 3], CultureInfo.InvariantCulture.NumberFormat)
                };
            }
        }

        ViewBag.myData = PersonResult.Income.ToString();
        return View("Index");
    }

这里的关键是:

Income = float.Parse(result[0, 3], CultureInfo.InvariantCulture.NumberFormat)

而不是你的遗产

Income = Int32.Parse(result[0, 2])