690. Employee Importance 员工重要性

本文介绍了一个算法问题,即如何计算一名员工及其所有下属的总体重要性。通过构建员工信息的数据结构,并利用字典来快速查找员工及下属的信息,实现递归求和。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

You are given a data structure of employee information, which includes the employee's unique id, his importance value and his direct subordinates' id.

For example, employee 1 is the leader of employee 2, and employee 2 is the leader of employee 3. They have importance value 15, 10 and 5, respectively. Then employee 1 has a data structure like [1, 15, [2]], and employee 2 has [2, 10, [3]], and employee 3 has [3, 5, []]. Note that although employee 3 is also a subordinate of employee 1, the relationship is not direct.

Now given the employee information of a company, and an employee id, you need to return the total importance value of this employee and all his subordinates.

Example 1:

Input: [[1, 5, [2, 3]], [2, 3, []], [3, 3, []]], 1
Output: 11
Explanation:
Employee 1 has importance value 5, and he has two direct subordinates: employee 2 and employee 3. They both have importance value 3. So the total importance value of employee 1 is 5 + 3 + 3 = 11.

Note:

  1. One employee has at most one direct leader and may have several subordinates.
  2. The maximum number of employees won't exceed 2000.

计算该员工及其所有下属的总体重要性。

   
  1. # Employee info
  2. class Employee:
  3. def __init__(self, id, importance, subordinates):
  4. # It's the unique id of each node.
  5. # unique id of this employee
  6. self.id = id
  7. # the importance value of this employee
  8. self.importance = importance
  9. # the id of direct subordinates
  10. self.subordinates = subordinates
  11. class Solution:
  12. def getImportance(self, employees, id):
  13. """
  14. :type employees: Employee
  15. :type id: int
  16. :rtype: int
  17. """
  18. val = 0
  19. employDict = dict()
  20. for i in employees:
  21. employDict.update({i.id:i})
  22. emp = employDict[id]
  23. if emp :
  24. val += emp.importance
  25. sub = emp.subordinates
  26. while sub:
  27. item = employDict[sub.pop(0)]
  28. if item:
  29. val += item.importance
  30. if item.subordinates:
  31. sub += item.subordinates
  32. return val







转载于:https://www.cnblogs.com/xiejunzhao/p/7613047.html

### SIMCA 模型中的变量重要性图 (Variable Importance) #### 背景介绍 SIMCA(Soft Independent Modeling of Class Analogy)是一种基于主成分分析(PCA)的分类方法,广泛应用于化学计量学领域。它通过对每类数据单独建立 PCA 模型来实现分类[^3]。在 SIMCA 中,变量的重要性可以通过多种方式评估,其中一种常用的方法是计算 **VIP 值**(Variable Importance in Projection),该指标能够反映每个变量对模型贡献的程度。 #### VIP 的定义与计算 VIP 是偏最小二乘法回归(PLS Regression)中常用的变量重要性度量之一,尽管其概念可以扩展到其他类型的建模技术。VIP 表示某个变量在整个 PLS 或类似模型中的全局重要性。具体而言: \[ \text{VIP}_j = \sqrt{\frac{t\sum_{a=1}^{A}\left(w_a^T R_y w_a\right)\left(v_jw_{aj}/s_a\right)}{\sum_{k=1}^{K}s_k}} \] 其中: - \( j \) 表示第 \( j \) 个变量; - \( A \) 表示潜变量的数量; - \( K \) 表示类别数量; - \( s_a \) 和 \( v_j \) 分别表示权重向量和得分矩阵的相关参数; 此公式用于衡量单个变量对整体模型性能的影响程度[^4]。 #### 变量重要性图的生成过程 为了生成 SIMCA 模型中的变量重要性图,以下是具体的流程描述: 1. 对于每一类数据分别构建独立的 PCA 模型。 2. 计算各变量在这些模型中的载荷值以及对应的方差解释率。 3. 利用上述 VIP 公式或其他相似算法综合评价各个变量的作用大小。 4. 将所得结果绘制成柱状图或者热力图形式展示出来以便直观理解哪些特征最为关键。 #### 解析注意事项 当观察到某些特定变量具有较高的 VIP 数值时,这表明它们可能是在区分不同组别间起到决定作用的关键因素。但是需要注意的是,仅仅依靠数值高低判断还不够全面,还需要结合专业知识背景进一步验证实际意义所在。 ```python import numpy as np from sklearn.cross_decomposition import PLSRegression def calculate_vip(model): t = model.x_scores_ w = model.x_weights_ q = model.y_loadings_ m, p = X.shape a = model.n_components vip = np.zeros((p,)) for i in range(a): weight = np.abs(q[i,:] * t[:,i].T @ t[:,i]) / np.linalg.norm(t[:,i])**2 vip += weight * sum(np.diag(model.y_loadings_.T @ model.coef_)) vip /= np.sqrt(m) return vip ``` 以上代码片段展示了如何利用 Python 实现基本的 VIP 计算逻辑。 #### 结论 综上所述,通过合理运用 VIP 方法可以帮助研究人员更好地理解和优化他们的 SIMCA 模型,并从中提取有价值的信息指导后续实验设计或决策制定工作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值