Skip to main content

Weights-Calc

License: MIT 一个简单易用的Python加权计算库,提供多种加权算法

安装

pip install weights

功能

  • 加权计算: 基础的加权平均计算
  • 加权随机: 根据权重进行随机选择
  • 时间衰减加权: 基于时间的指数衰减加权

使用方法

导入

方法1

import importlib
Weights_Calc = importlib.import_module('Weights-Calc')

方法2

Weights_Calc = __import__('Weights-Calc')

加权计算

values = [10, 20, 30]
weights = [0.2, 0.3, 0.5]
result = Weights_Calc.weighted_calculate(values, weights)
print(result)  # 输出: 23.0

加权随机

items = ['A', 'B', 'C']
weights = [1, 2, 3]
selected = Weights_Calc.weighted_random(items, weights)
print(selected)  # 输出: ['B'] (概率更高)

时间衰减加权

values = [100, 200, 300]
dates = [1, 5, 10]  # 时间点
half_life = 7  # 半衰期
result = Weights_Calc.time_decay_weighted(values, dates, half_life)
print(result)

API参考

weighted_calculate(values, weights)

计算加权结果。

参数:

  • values: 数值列表
  • weights: 权重列表

返回: 加权计算结果

weighted_random(items, weights)

根据权重随机选择项目。

参数:

  • items: 待选择的项目列表
  • weights: 对应的权重列表

返回: 随机选择的项目列表

time_decay_weighted(values, dates, half_life_days=7)

基于时间衰减的加权计算。

参数:

  • values: 数值列表
  • dates: 时间点列表
  • half_life_days: 半衰期天数(默认7天)

返回: 时间衰减加权结果

Release files for Weights-Calc 1.0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for Weights-Calc 1.0.1
File Size Uploaded
weights_calc-1.0.1.tar.gz 2.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for Weights-Calc 1.0.1
File Interpreter ABI Platform
weights_calc-1.0.1-py3-none-any.whl Python 3 none any Details

Total release size: 6.0 kB

Release files / weights_calc-1.0.1.tar.gz

Download URL weights_calc-1.0.1.tar.gz
Size 2.8 kB
Tags Source
SHA-256 checksum
How to use checksums
5099ef18b737645f89624b0e18d4c69d34da8b19a364dbeb49a661f105eb99dc
BLAKE2b-256 checksum
How to use checksums
4ca532458f189b380b5d144a6d12b29458390776a5823cf2dc9a8fa1ceb1f358
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/2.1.3 CPython/3.12.9 Windows/11

Release files / weights_calc-1.0.1-py3-none-any.whl

Download URL weights_calc-1.0.1-py3-none-any.whl
Size 3.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
4532431bac6a074a753f1e7fe1d98666f637ba6ec2a4920c98bb9f245927f060
BLAKE2b-256 checksum
How to use checksums
4826de99c9fc74a5ccb7a9ab79c48912cf48372732654ea9025e22ea55ab4831
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/2.1.3 CPython/3.12.9 Windows/11

Release history Release notifications | RSS feed

This release

1.0.1 This release

2 release files

1.0.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page