<<上篇 | 首页 | 下篇>>

使用Mahout为布尔型数据生成推荐内容

Generating Recommendations with mahout for Boolean data sets (data sets with no preference value)

关于在Spark MLlib中基于布尔型数据集推荐可参考:Spark MLlib中的协同过滤

通过指定alpha:是一个针对于隐性反馈 ALS 版本的参数,这个参数决定了偏好行为强度的基准。

val alpha = 0.01

val model = ALS.trainImplicit(ratings, rank, numIterations, 0.01, alpha)

 

参考:http://spark.apache.org/docs/latest/mllib-collaborative-filtering.html

 

Boolean data Sets
                Input data set that doesn’t have a preference value, ie input data set would be of the format UserId1,ItemId1
UserId2,ItemId2
Here it’d based on some data where an user either likes an item or he doesn’t, there is no preference value associated with this.
 
                When we use Boolean data sets we need to appropriately choose the Similarity algorithms and Recommenders
 
Similarity Algorithms
                For Boolean data sets we can either go in for Tanimoto Coefficient Similarity or Log Likelihood Similarity
 
Recommender
                We need to use Generic Boolean Pref User Based Recommender or Generic Boolean Pref Item Based Recommender
 
                Sample codes for generating User based and Item Based recommendations are given below
 
Used Based Recommender for Boolean Data Sets
 
import java.io.File;
import java.io.IOException;
import java.util.List;
 
import org.apache.mahout.cf.taste.common.TasteException;
import org.apache.mahout.cf.taste.impl.model.file.FileDataModel;
import org.apache.mahout.cf.taste.impl.neighborhood.ThresholdUserNeighborhood;
import org.apache.mahout.cf.taste.impl.recommender.GenericBooleanPrefUserBasedRecommender;
import org.apache.mahout.cf.taste.impl.similarity.TanimotoCoefficientSimilarity;
import org.apache.mahout.cf.taste.neighborhood.UserNeighborhood;
import org.apache.mahout.cf.taste.recommender.RecommendedItem;
import org.apache.mahout.cf.taste.recommender.Recommender;
import org.apache.mahout.cf.taste.similarity.UserSimilarity;
 
public class UserRecommender {
     
      public static void main(String args[])
      {
            // specifying the user id to which the recommendations have to be generated for
            int userId=510;
           
            //specifying the number of recommendations to be generated
            int noOfRecommendations=5;
           
            //specifying theNeighborhood size
            double thresholdValue=0.7;
           
            try
            {
                  // Data model created to accept the input file
                  FileDataModel dataModel = new FileDataModel(newFile("D://input.txt"));
                 
                  /*TanimotoCoefficientSimilarity is intended for "binary" data sets
                  where a user either expresses a generic "yes" preference for an item or has no preference.*/
                  UserSimilarity userSimilarity = new TanimotoCoefficientSimilarity(dataModel);
                 
                  /*ThresholdUserNeighborhood is preferred in situations where we go in for a
                   similarity measure between neighbors and not any number*/
                  UserNeighborhood neighborhood =new ThresholdUserNeighborhood(thresholdValue, userSimilarity, dataModel);
                 
                  /*GenericBooleanPrefUserBasedRecommender is appropriate for use when no notion
                  of preference value exists in the data. */
                  Recommender recommender =new GenericBooleanPrefUserBasedRecommender(dataModel, neighborhood, userSimilarity);
                 
                  //calling the recommend method to generate recommendations
                  List<RecommendedItem> recommendations =recommender.recommend(userId, noOfRecommendations);
           
                  //
                  for (RecommendedItem recommendedItem : recommendations)
                        System.out.println(recommendedItem.getItemID());
            }
            catch (IOException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
            } catch (TasteException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
            }
           
                 
      }
 
}
 
Item Based Recommender for Boolean Data Sets
 
import java.io.File;
import java.io.IOException;
import java.util.List;
 
import org.apache.mahout.cf.taste.common.TasteException;
import org.apache.mahout.cf.taste.impl.model.file.FileDataModel;
import org.apache.mahout.cf.taste.impl.recommender.GenericItemBasedRecommender;
import org.apache.mahout.cf.taste.impl.similarity.LogLikelihoodSimilarity;
import org.apache.mahout.cf.taste.recommender.ItemBasedRecommender;
import org.apache.mahout.cf.taste.recommender.RecommendedItem;
import org.apache.mahout.cf.taste.similarity.ItemSimilarity;
 
public class ItemRecommender {
     
      public static void main(String args[])
      {
            // specifying the user id to which the recommendations have to be generated for
            int userId=510;
           
            //specifying the number of recommendations to be generated
            int noOfRecommendations=5;
           
            try
            {
                  // Data model created to accept the input file
                  FileDataModel dataModel = new FileDataModel(newFile("D://input.txt"));
                 
                  /*Specifies the Similarity algorithm*/
                  ItemSimilarity itemSimilarity = new LogLikelihoodSimilarity(dataModel);
                 
                  /*Initalizing the recommender */
                  ItemBasedRecommender recommender =new GenericItemBasedRecommender(dataModel, itemSimilarity);
                 
                  //calling the recommend method to generate recommendations
                  List<RecommendedItem> recommendations =recommender.recommend(userId, noOfRecommendations);
           
                  //
                  for (RecommendedItem recommendedItem : recommendations)
                        System.out.println(recommendedItem.getItemID());
            }
            catch (IOException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
            } catch (TasteException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
            }
           
      }
}

 

Craig Andrews » Best way to use HttpClient in Android

Google不鼓励使用HttpClient,更喜欢使用HttpURLConnection和HttpsURLConnection。

Many Android applications access the Internet resources over HTTP (and my projects are no exception). There are 2 common ways to do that: use Apache HttpClient 4.x (which is included in Android) or use HttpURLConnection (from Java). Google stated in a September 29, 2011 blog post that they prefer you use HttpURLConnection, but many apps and a large number of Java libraries already use HttpClient and won’t be changing soon (if ever). So HttpClient is here to stay.

With that in mind, the performance and footprint of HttpClient can vary widely based on how its set up. Here are my recommendations:

  • Always use one HttpClient instance for your entire application. HttpClient is not free to instantiate – each additional instance takes time to create and uses more memory. However, more importantly, using one instance allows HttpClient to pool and reuse connections along with other optimizations that can make big differences in how your application performs.
  • Use a thread safe connection manager. If you’re using one global HttpClient, it will be accessed by multiple threads concurrently – so if you don’t use a thread safe connection manager, Bad Things will happen.
  • Use Android’s android.net.SSLCertificateSocketFactory and android.net.SSLSessionCacheif they’re available. Using these instead of the base HttpClient SSLSocketFactorywill reduce round trips when connecting to the same https site multiple times, making your application feel faster.
  • Set the user agent to something useful. That way, the server’s logs will be far more useful, which may save you (or someone else) a lot of time later if (when) a problem occurs.

With all that said, here’s how I get my global HttpClient instance. This code should work on all Android versions (it should even work all the way back to 1.0 – if anyone cares). I use Google Guice‘s Provider interface and injection to get the application, but you can easily adopt this to a form that doesn’t use Guice.

阅读全文……

标签 : ,

[HttpClient]HTTPClient PostMethod 中文乱码问题解决方案(2种) - 追竹 - 博客频道 - CSDN.NET

一、在调用PostMethod方法时设置字符编码:

 

  1. PostMethod postMethod = new PostMethod(  
  2.         "http://127.0.0.1:8080/HttpClientServer/login.do");  
  3. postMethod.getParams(). setParameter( HttpMethodParams.HTTP_CONTENT_CHARSET,"utf-8");  

 

 

 

二、重载PostMethod的getRequestCharSet()方法, 返回我们需要的编码(字符集)名称, 就可以解决 UTF-8 或者其它非默认编码提交 POST 请求时的乱码问题了.

 

 

  1. //Inner class for UTF-8 support     
  2.    public static class UTF8PostMethod extends PostMethod{     
  3.        public UTF8PostMethod(String url){     
  4.            super(url);     
  5.        }     
  6.        @Override    
  7.        public String getRequestCharSet() {     
  8.            //return super.getRequestCharSet();     
  9.            return "utf-8";     
  10.        }     
  11.    }       

 

 

  1. PostMethod postMethod = new UTF8PostMethod(  
  2.                 "http://127.0.0.1:8080/HttpClientServer/login.do");  

 

 

三、最后服务器端要配合客户端,设置下编码字符集:

 

  1. public void doPost(HttpServletRequest request, HttpServletResponse response)  
  2.         throws ServletException, IOException {  
  3.     //解决中文乱码问题,此步不可少  
  4.     request.setCharacterEncoding("UTF-8");  
  5.     response.setContentType("text/html");  
  6.     response.setCharacterEncoding("UTF-8");  
  7.     PrintWriter out = response.getWriter();  
  8.     String username = (String) request.getParameter("username");  
  9.     String password = (String) request.getParameter("password");  
  10.     System.out.println("******************** doPost被执行了 ********************");  
  11.     System.out.println("您的请求参数为:/tusername:" + username + "/tpassword:"  
  12.             + password);  
  13.     out.print("您的请求参数为:/tusername:" + username + "/tpassword:" + password);  
  14.     out.flush();  
  15.     out.close();  
  16. }  

 

 

阅读全文……