import json import numpy as np # 已同向的非负指标,各列和为正。第一列恒定,差异全部在第二列。 x = np.array([[2., 0.], [2., 1.], [2., 3.]]) shares = x / x.sum(axis=0) terms = np.zeros_like(shares) positive = shares > 0 terms[positive] = shares[positive] * np.log(shares[positive]) entropy = -terms.sum(axis=0) / np.log(len(x)) # 用 1-熵 表示指标的差异度。清掉浮点舍入导致的近零差异。 diversity = np.maximum(1 - entropy, 0) weights = diversity / diversity.sum() assert np.allclose(weights, [0, 1]) result = {"entropy": entropy.tolist(), "weights": weights.tolist()} print("LAB_RESULT " + json.dumps(result, ensure_ascii=False))