Skip to main content

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

Project description

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天)

返回: 时间衰减加权结果

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

weights_calc-1.0.1.tar.gz (2.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

weights_calc-1.0.1-py3-none-any.whl (3.3 kB view details)

Uploaded Python 3

File details

Details for the file weights_calc-1.0.1.tar.gz.

File metadata

  • Download URL: weights_calc-1.0.1.tar.gz
  • Upload date:
  • Size: 2.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.3 CPython/3.12.9 Windows/11

File hashes

Hashes for weights_calc-1.0.1.tar.gz
Algorithm Hash digest
SHA256 5099ef18b737645f89624b0e18d4c69d34da8b19a364dbeb49a661f105eb99dc
MD5 e10201c22516b42974262377f69ec3a4
BLAKE2b-256 4ca532458f189b380b5d144a6d12b29458390776a5823cf2dc9a8fa1ceb1f358

See more details on using hashes here.

File details

Details for the file weights_calc-1.0.1-py3-none-any.whl.

File metadata

  • Download URL: weights_calc-1.0.1-py3-none-any.whl
  • Upload date:
  • Size: 3.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.3 CPython/3.12.9 Windows/11

File hashes

Hashes for weights_calc-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 4532431bac6a074a753f1e7fe1d98666f637ba6ec2a4920c98bb9f245927f060
MD5 35e9868d2d6b91660aa77cb2fd2e5d1c
BLAKE2b-256 4826de99c9fc74a5ccb7a9ab79c48912cf48372732654ea9025e22ea55ab4831

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page