繁体   English   中英

Asp.net Azure机器学习

[英]Asp.net Azure machine learning

我目前正在做一个预测项目,想知道是否有人知道如何将机器学习示例代码与asp.net连接起来? 我对使用ASP.NET的C#感到很陌生,这是示例代码:

// This code requires the Nuget package Microsoft.AspNet.WebApi.Client to be installed.
// Instructions for doing this in Visual Studio:
// Tools -> Nuget Package Manager -> Package Manager Console
// Install-Package Microsoft.AspNet.WebApi.Client

using System;
using System.Collections.Generic;
using System.IO;
using System.Net.Http;
using System.Net.Http.Formatting;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;

namespace CallRequestResponseService
{

    public class StringTable
    {
        public string[] ColumnNames { get; set; }
        public string[,] Values { get; set; }
    }

    class Program
    {
        static void Main(string[] args)
        {
            InvokeRequestResponseService().Wait();
        }

        static async Task InvokeRequestResponseService()
        {
            using (var client = new HttpClient())
            {
                var scoreRequest = new
                {

                    Inputs = new Dictionary<string, StringTable> () { 
                        { 
                            "input1", 
                            new StringTable() 
                            {
                                ColumnNames = new string[] { "Number1", "Number2", "Number3", "Number4", "Number5", "Number6" },
                                Values = new string[,] {  { "0", "0", "0", "0", "0", "0" },  { "0", "0", "0", "0", "0", "0" },  }
                            }
                        },
                    },
                    GlobalParameters = new Dictionary<string, string>() {
}
                };
                const string apiKey = "abc123"; // Replace this with the API key for the web service
                client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue( "Bearer", apiKey);

                client.BaseAddress = new Uri("https://ussouthcentral.services.azureml.net/workspaces/08ebbc1613d5478285ec11b4516223d4/services/b1fcf5664b1b4b188b1dda6de194c91e/execute?api-version=2.0&details=true");

                // WARNING: The 'await' statement below can result in a deadlock if you are calling this code from the UI thread of an ASP.Net application.
                // One way to address this would be to call ConfigureAwait(false) so that the execution does not attempt to resume on the original context.
                // For instance, replace code such as:
                //      result = await DoSomeTask()
                // with the following:
                //      result = await DoSomeTask().ConfigureAwait(false)


                HttpResponseMessage response = await client.PostAsJsonAsync("", scoreRequest);

                if (response.IsSuccessStatusCode)
                {
                    string result = await response.Content.ReadAsStringAsync();
                    Console.WriteLine("Result: {0}", result);
                }
                else
                {
                    Console.WriteLine(string.Format("The request failed with status code: {0}", response.StatusCode));

                    // Print the headers - they include the requert ID and the timestamp, which are useful for debugging the failure
                    Console.WriteLine(response.Headers.ToString());

                    string responseContent = await response.Content.ReadAsStringAsync();
                    Console.WriteLine(responseContent);
                }
            }
        }
    }
}

我之前已经创建了有关如何进行简单预测的教程(有关如何根据性别和年龄来预测收入的超级简单示例)。

它使用挪威语,但是了解我在做什么很简单,因此请遵循以下步骤: https : //channel9.msdn.com/Series/MSDEVNO/Lr-Azure-Machine-Learning-p-1-2- 3

数据集在描述中(因此您可以复制它)。

我还创建了一个Github存储库,您可以克隆(或下载zip): https : //github.com/readyforchaos/howmuchmonneh

您可能要下载“ HowMuchMoney解决方案 ”,然后看看我如何根据一个年龄和性别输入向API发送请求(发布),然后从中读取(解析)JSON响应(GET)。它返回的API(响应)。

[HttpPost]位于“ Controllers / HomeController.cs”中

为了使内容更整洁,我将API密钥放入了“ HowMuchMonneh / WebService / IncomeWebService.cs”文件中。

请按照上述说明进行操作-

更换

HttpResponseMessage response = client.PostAsJsonAsync("",scoreRequest).Result;

HttpResponseMessage response = await client.PostAsJsonAsync("", scoreRequest);

并将其复制到匹配行

string result = await response.Content.ReadAsStringAsync().ConfigureAwait(false);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM