import pandas as pd
import math

# 读取Excel文件
df = pd.read_excel('商品会员数据.xlsx')

# 提取所需列的数据
com_lat = df['商品GPS纬度']
com_lon = df['商品GPS经度']
mem_lat = df['会员GPS纬度']
mem_lon = df['会员GPS经度']
price = df['任务标价']

# 计算距离和引力子
def calculate_gravitational_force(r, k, n):
    y = []
    for i in range(len(com_lat)):
        distance = math.sqrt((com_lat[i] - mem_lat[i])**2 + (com_lon[i] - mem_lon[i])**2)
        force = k * price[i] / (distance**n)
        y.append(force)
    return y

# 遍历不同的常量值,找出最小引力子常量
minimum_y = float('inf')
best_k = 0
best_n = 0

for k in range(1, 10):
    for n in range(1, 5):
        y = calculate_gravitational_force(r, k, n)
        total_y = sum(y)
        if total_y < minimum_y:
            minimum_y = total_y
            best_k = k
            best_n = n

# 输出带常量的公式
print(f'y = {best_k}*q1*q2/r^{{best_n}}')
基于引力模型的商品推荐算法Python实现

原文地址: https://www.cveoy.top/t/topic/fz8o 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录