【问题标题】:How to find the most similar array base on value's weight?如何根据值的权重找到最相似的数组?
【发布时间】:2020-03-01 04:34:49
【问题描述】:

我有一个给定用户的对象。

const user = [{ id: '1', city: 'Paris', cuisine: 'italian', allergic: 'no', budget: '50', hasChildren: 'yes' }]

还有一系列带有餐厅的物品。我想给这位用户找一家最符合他要求的餐厅。

const restaurants: [
 {
  cuisine: 'mexican',
  averageCost: '30',
  children: 'no'
 },
 {
  cuisine: 'italian',
  averageCost: '30',
  children: 'yes'
 }
]

所以第二家餐厅更适合用户,所以它应该显示为第一家。每个键都应该有一定的权重,因为例如美食比成本更重要,所以它应该有更大的分数。我怎样才能实现它?在 JavaScript 中是否有任何算法可以做到这一点?

【问题讨论】:

  • 您是否只是在寻找最适合的成本/美食?如果是这样,您必须对菜肴进行加权,以便基本上可以将其归一化为成本差异,然后您可以按匹配的整体适用性进行排序(在这种情况下,适用性将在成本和菜肴偏好方面最接近)
  • 请展示您的尝试并解释您遇到的问题。这是一个简单的排序算法,但您未能提供排序键:您的评估函数。我们不能为你决定。因素的权重和相互作用仍在您的脑海中,或者等待您从复杂的事物中研究您想要的效果。在您定义之前,我们甚至无法将您置于正确的范式中:约束优化、直线评估或某种深度学习模型。

标签: javascript arrays algorithm sorting filtering


【解决方案1】:

这是您可以做的事情的快速快照。

const user = [{ id: '1', city: 'Paris', cuisine: 'italian', allergic: 'no', budget: '50', hasChildren: 'yes' }];
const restaurants = [
    {
     cuisine: 'mexican',
     averageCost: '30',
     children: 'no'
    },
    {
     cuisine: 'italian',
     averageCost: '30',
     children: 'yes'
    }
]

const orderedRestaurants = restaurants.map(r => {
    return {
        ...r,
        score: 0 +
        (r.cuisine === user[0].cuisine ? 4 : 0) +
        (r.averageCost <= user[0].budget ? 8 : 0) +
        (r.children === 'no' && user[0].hasChildren === 'yes' ? 0 : 3)
    }
}).sort((a,b) => b.score - a.score);

console.log(orderedRestaurants);

因此我们需要根据特定标准为每家餐厅分配一个分数。我已经在函数中分配了权重,您可以将其移到外部对象中进行动态评分。

在这个例子中,如果菜品符合用户的兴趣,我们给他们 4 分,如果平均成本低于或等于预算,我们给他们 8 分,如果用户有孩子和餐厅,我们给他们 3 分允许儿童。您可以随意编辑评分系统,但希望这可以帮助您入门。

【讨论】:

    【解决方案2】:

    我在下面给出了一个简单的实现。这个想法是为您的餐厅创建一个scoring function,它依赖于user data 和restaurant data.,然后使用该功能作为餐厅的评分对您的餐厅进行排序。得分下降以获得最“最近”的餐厅。

    您可以根据需要为分数分配权重。

    // Given
    const restaurants = [
     {
      cuisine: 'mexican',
      averageCost: '30',
      children: 'no'
     },
     {
      cuisine: 'italian',
      averageCost: '30',
      children: 'yes'
     }
    ];
    
    const user = [{ id: '1', city: 'Paris', cuisine: 'italian', allergic: 'no', budget: '50', hasChildren: 'yes' }];
    
    
    const calculateScore = (user, restaurant) => {
        // you can fine tune the values for the weights yourself
    
        // asume that children score gives 1
        const childrenScore = (user.hasChildren === restaurant.children) ? 1 : 0;
    
        // asume that cuisine score gives 1
        const cuisineScore = (user.cuisine === restaurant.cuisine) ? 1 : 0;
    
        // asume that cost score gives the absolute difference
        const costScore = Math.abs(parseInt(restaurant.averageCost) - parseInt(user.budget));
    
        return childrenScore + cuisineScore + costScore;
    }
    
    const sortedRestaurants = restaurants.sort((rA, rB) => {
        const rAScore = calculateScore(user[0], rA);
        const rBScore = calculateScore(user[0], rB);
        return rBScore - rAScore; // sort DESC by score
    });
    
    console.log(sortedRestaurants)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-26
      • 2012-08-12
      • 2018-07-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多