If you have a shopping website, how do you recommend products to your customers? This function is available on many e-commerce websites. You can easily build similar functions through the data mining feature of SQL Server Analysis Services.
It is divided into three parts to demonstrate how to implement this function.
1. Build a Mining Model
2. Compile service interfaces for the Mining Model
3. Develop simple front-end applications
This article describes how to build a simple front-end application for product recommendation based on the mining model and service layer.
Because we use the web service method to encapsulate the query of the Mining Model, theoretically this service can be accessed by many client applications, such as ASP. net, winform, and WPF. Here we select Silverlight 5.
Silverlight 5 is similar to flash and is an RIA solution on the Microsoft platform. On this platform, we can use XAML to develop brilliant applications.
Although its glory has been quickly covered by HTML5, and Microsoft has stopped its subsequent version updates and made full efforts to invest in HTML5, it is still early to develop HTML5, silverlight is still attractive and advantageous, as well as powerful development tool support.
Of course, if your project is ASP. NET or another type of project, you can also refer to this article for operations. In fact, this article is of little significance to an experienced front-end developer, because it mainly describes how to call a web service. But as a series, considering that each person's focus is different, in order to make an overall introduction, we will also detail each step of the operation here.
Open the project created in the previous article, right-click the solution name at the top of the project, and choose add-> new project from the menu.
Select Silverlight application and click OK.
Specify the Silverlight version. Click OK by default.
Then we can see that a new Silverlight project is added to the solution.
Next, add a service reference to reference the service created in the previous article.
Right-click the Silverlight project and choose add service reference.
Because the project where the service is located and the Silverlight project are under the same solution, click discover to automatically find the service address created in the previous article.
Click OK. A service reference is added in Solution Explorer.
As described in the previous article, the system automatically generates a proxy class locally based on the content defined in the service SOAP protocol, in this way, the Silverlight end can call the content in the Web service just like calling a local method.
Then, put a few spaces in Silverlight ., First, place a ListBox control to allow users to select the products they have purchased. In an e-commerce website, this is equivalent to a shopping cart, which records the items selected by the user.
Place a button on the right of the ListBox and click this button to trigger the prediction query. The background code calls the Web service to access the Mining Model for prediction and returns the recommendation data.
Finally, place a DataGrid Control below to receive the recommendation result set returned by the web service.
The following front-end XAML code is for your reference. All the controls dragged on the front interface have the corresponding XAML code:
<Usercontrol
Xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation"
Xmlns: x = "http://schemas.microsoft.com/winfx/2006/xaml"
Xmlns: D = "http://schemas.microsoft.com/expression/blend/2008"
Xmlns: MC = "http://schemas.openxmlformats.org/markup-compatibility/2006"
Xmlns: SDK = "http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk" X: class = "silverlightapplication1.mainpage"
MC: ignorable = "D"
D: designheight = "300" D: designwidth = "400">
<Grid X: Name = "layoutroot" background = "white">
<ListBox X: Name = "lbproduct" horizontalalignment = "Left" Height = "159" margin =, 165 "verticalalignment =" TOP "width =" "selectionmode =" multiple "/>
<SDK: DataGrid X: Name = "dgresult" horizontalalignment = "Left" Height = "100" margin = "10,190, 247" verticalalignment = "TOP" width = ""/>
<Button X: Name = "btnstart" content = "" horizontalalignment = "Left" Height = "32" margin = "197,137, 106 "verticalalignment =" TOP "width =" "/>
</GRID>
</Usercontrol>
Then, enter the code mode on the interface.
It is mainly to add the namespace referenced by the service, and then declare in the class and instantiate the soapclient class required to access the service.
Then, add the following code to the class in code mode:
Public mainpage ()
{
Initializecomponent ();
List <string> prodsource = new list <string> ();
Prodsource. Add ("Touring tire ");
Prodsource. Add ("Touring Tire Tube ");
Prodsource. Add ("water bottle ");
Prodsource. Add ("Bike Wash ");
Prodsource. Add ("classic vest ");
Prodsource. Add ("processing ing cap ");
Prodsource. Add ("HL road tire ");
Prodsource. Add ("hydration pack ");
Prodsource. Add ("ll road tire ");
Lbproduct. itemssource = prodsource;
Btnstart. Click + = btnstart_click;
SC. getmodelresultcompleted + = SC _getmodelresultcompleted;
}
Void SC _getmodelresultcompleted (Object sender, getmodelresultcompletedeventargs E)
{
Dgresult. itemssource = E. result;
}
Void btnstart_click (Object sender, routedeventargs E)
{
Arrayofstring modellist = new arrayofstring ();
Foreach (Object item in lbproduct. selecteditems)
{
Modellist. Add (item. tostring ());
}
SC. getmodelresultasync (modellist );
}
In the constructor, I manually added several optional items for convenience. In the actual project, the product list should be added from the database. Here, we will only make a simple demonstration. If you are interested, you can try to read data from the product table of adventure works.
Because Silverlight calls services asynchronously, you must first specify the method to which the service is redirected after the service is called.
Then, in the Click Event of the button, organize the parameters of the item list to be passed and call the service by calling the getmodelresultasync method.
Note that the parameter received in the previous service definition is list, which is automatically converted to arrarofstring on the client side.
After adding the above code, you can test our prediction query through the interface in the previous section.
We can see that different products are recommended to us by selecting different products based on the mining model.
So far, this simple front-end application has been developed. This application is very simple, but you can refer to this method to add it to your function system.
With regard to the function of making product recommendations through association rules, this series of essays finally come to an end. I have to say that each of these three articles is very rough and complex, but I hope these simple models can inspire everyone and add similar functions to their websites or systems.
With this prediction function, I feel that the processing methods of many platforms are too simple. For example, the recommended products only use products under a category, so we will occasionally recommend other products under the same category to you. In fact, this does not achieve good recommendation results. By mining the patterns found in the model, the recommended products are based on historical data, which is relatively more accurate. Of course, the premise is that you already have enough historical order data. The so-called clever man is hard to find, and the quality of the data is high enough, and your business is clear enough. These are the key to the success or failure of a business intelligence project.
I hope these three articles will help you.