本文整理汇总了C#中DataModel.GetNumUsers方法的典型用法代码示例。如果您正苦于以下问题:C# DataModel.GetNumUsers方法的具体用法?C# DataModel.GetNumUsers怎么用?C# DataModel.GetNumUsers使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类DataModel
的用法示例。
在下文中一共展示了DataModel.GetNumUsers方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Evaluate
/**
* {@inheritDoc}
*/
public double Evaluate(RecommenderBuilder recommenderBuilder,
DataModel dataModel,
double trainingPercentage,
double evaluationPercentage)
{
if (recommenderBuilder == null)
{
throw new ArgumentNullException("recommenderBuilder is null");
}
if (dataModel == null)
{
throw new ArgumentNullException("dataModel is null");
}
if (double.IsNaN(trainingPercentage) || trainingPercentage <= 0.0 || trainingPercentage >= 1.0)
{
throw new ArgumentException("Invalid trainingPercentage: " + trainingPercentage);
}
if (double.IsNaN(evaluationPercentage) || evaluationPercentage <= 0.0 || evaluationPercentage > 1.0)
{
throw new ArgumentException("Invalid evaluationPercentage: " + evaluationPercentage);
}
log.Info("Beginning evaluation using " + trainingPercentage + " of " + dataModel);
int numUsers = dataModel.GetNumUsers();
ICollection<User> trainingUsers = new List<User>(1 + (int) (trainingPercentage * (double) numUsers));
IDictionary<User, ICollection<Preference>> testUserPrefs =
new Dictionary<User, ICollection<Preference>>(1 + (int) ((1.0 - trainingPercentage) * (double) numUsers));
foreach (User user in dataModel.GetUsers())
{
if (random.NextDouble() < evaluationPercentage)
{
ICollection<Preference> trainingPrefs = new List<Preference>();
ICollection<Preference> testPrefs = new List<Preference>();
Preference[] prefs = user.GetPreferencesAsArray();
foreach (Preference pref in prefs)
{
Item itemCopy = new GenericItem<String>(pref.Item.ID.ToString());
Preference newPref = new GenericPreference(null, itemCopy, pref.Value);
if (random.NextDouble() < trainingPercentage)
{
trainingPrefs.Add(newPref);
} else {
testPrefs.Add(newPref);
}
}
if (log.IsDebugEnabled) {
log.Debug("Training against " + trainingPrefs.Count + " preferences");
log.Debug("Evaluating accuracy of " + testPrefs.Count + " preferences");
}
if (trainingPrefs.Count > 0)
{
User trainingUser = new GenericUser<String>(user.ID.ToString(), trainingPrefs);
trainingUsers.Add(trainingUser);
if (testPrefs.Count > 0)
{
testUserPrefs.Add(trainingUser, testPrefs);
}
}
}
}
DataModel trainingModel = new GenericDataModel(trainingUsers);
Recommender recommender = recommenderBuilder.BuildRecommender(trainingModel);
double result = GetEvaluation(testUserPrefs, recommender);
log.Info("Evaluation result: " + result);
return result;
}
示例2: Evaluate
public IRStatistics Evaluate(RecommenderBuilder recommenderBuilder,
DataModel dataModel,
int at,
double relevanceThreshold,
double evaluationPercentage)
{
if (recommenderBuilder == null) {
throw new ArgumentNullException("recommenderBuilder is null");
}
if (dataModel == null) {
throw new ArgumentNullException("dataModel is null");
}
if (at < 1) {
throw new ArgumentException("at must be at least 1");
}
if (double.IsNaN(evaluationPercentage) || evaluationPercentage <= 0.0 || evaluationPercentage > 1.0)
{
throw new ArgumentException("Invalid evaluationPercentage: " + evaluationPercentage);
}
if (double.IsNaN(relevanceThreshold)) {
throw new ArgumentException("Invalid relevanceThreshold: " + evaluationPercentage);
}
RunningAverage precision = new FullRunningAverage();
RunningAverage recall = new FullRunningAverage();
foreach (User user in dataModel.GetUsers())
{
Object id = user.ID;
if (random.NextDouble() < evaluationPercentage)
{
ICollection<Item> relevantItems = new HashedSet<Item>(/* at */);
Preference[] prefs = user.GetPreferencesAsArray();
foreach (Preference pref in prefs)
{
if (pref.Value >= relevanceThreshold)
{
relevantItems.Add(pref.Item);
}
}
int numRelevantItems = relevantItems.Count;
if (numRelevantItems > 0)
{
ICollection<User> trainingUsers = new List<User>(dataModel.GetNumUsers());
foreach (User user2 in dataModel.GetUsers())
{
if (id.Equals(user2.ID))
{
ICollection<Preference> trainingPrefs = new List<Preference>();
prefs = user2.GetPreferencesAsArray();
foreach (Preference pref in prefs)
{
if (!relevantItems.Contains(pref.Item))
{
trainingPrefs.Add(pref);
}
}
if (trainingPrefs.Count > 0)
{
User trainingUser = new GenericUser<String>(id.ToString(), trainingPrefs);
trainingUsers.Add(trainingUser);
}
}
else
{
trainingUsers.Add(user2);
}
}
DataModel trainingModel = new GenericDataModel(trainingUsers);
Recommender recommender = recommenderBuilder.BuildRecommender(trainingModel);
try
{
trainingModel.GetUser(id);
}
catch (NoSuchElementException)
{
continue; // Oops we excluded all prefs for the user -- just move on
}
int intersectionSize = 0;
foreach (RecommendedItem recommendedItem in recommender.Recommend(id, at))
{
if (relevantItems.Contains(recommendedItem.Item))
{
intersectionSize++;
}
}
precision.AddDatum((double) intersectionSize / (double) at);
recall.AddDatum((double) intersectionSize / (double) numRelevantItems);
}
}
}
return new IRStatisticsImpl(precision.Average, recall.Average);
}