predict_with_history.py
import csv
import datetime
from collections import Counter
def predict_next_angles(sequence_str, recent_n=50, weight_long=0.4, weight_recent=0.6):
numbers = list(map(int, sequence_str.strip().split('-')))
total_len = len(numbers)
overall_counts = Counter(numbers)
overall_probs = {k: v / total_len for k, v in overall_counts.items()}
recent_numbers = numbers[-recent_n:]
recent_counts = Counter(recent_numbers)
recent_probs = {k: v / recent_n for k, v in recent_counts.items()}
scores = {}
for num in [1, 2, 3, 4]:
op = overall_probs.get(num, 0)
rp = recent_probs.get(num, 0)
scores[num] = weight_long * op + weight_recent * rp
sorted_nums = sorted(scores, key=lambda x: scores[x], reverse=True)
predicted = sorted_nums[:3]
# 补齐为 dict 方便存储
overall_prob_dict = {str(k): round(overall_probs.get(k, 0)*100, 2) for k in [1,2,3,4]}
recent_prob_dict = {str(k): round(recent_probs.get(k, 0)*100, 2) for k in [1,2,3,4]}
score_dict = {str(k): round(scores.get(k, 0)*100, 2) for k in [1,2,3,4]}
return predicted, overall_prob_dict, recent_prob_dict, score_dict, total_len
def append_history(record, filename='prediction_history.csv'):
file_exists = False
try:
with open(filename, 'r', encoding='utf-8') as f:
file_exists = True
except FileNotFoundError:
pass
with open(filename, 'a', newline='', encoding='utf-8') as f:
writer = csv.writer(f)
if not file_exists:
writer.writerow([
'日期时间', '数据长度',
'整体概率_1', '整体概率_2', '整体概率_3', '整体概率_4',
'近期概率_1', '近期概率_2', '近期概率_3', '近期概率_4',
'综合评分_1', '综合评分_2', '综合评分_3', '综合评分_4',
'预测角1', '预测角2', '预测角3'
])
writer.writerow(record)
if name == "main":
print("=" * 60)
print(" 三期角预测工具(带历史记录)")
print("=" * 60)
print("请粘贴完整的数字序列(形如 2-2-1-4-3-4-...):")
data_input = input().strip()
if not data_input:
print("未检测到输入,程序退出。")
exit()
try:
predicted, overall_probs, recent_probs, scores, length = predict_next_angles(data_input, recent_n=50)
now_str = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
record = [
now_str,
length,
overall_probs['1'], overall_probs['2'], overall_probs['3'], overall_probs['4'],
recent_probs['1'], recent_probs['2'], recent_probs['3'], recent_probs['4'],
scores['1'], scores['2'], scores['3'], scores['4'],
predicted[0], predicted[1], predicted[2]
]
append_history(record)
print("\n【预测结果】")
print(f"数据长度: {length}")
print(f"整体概率: {overall_probs}")
print(f"近期概率: {recent_probs}")
print(f"综合评分: {scores}")
print(f"预测下一期三个角(不重复): {predicted}")
print(f"\n本次预测已追加到 prediction_history.csv,可打开查看历史记录。")
except Exception as e:
print("发生错误:", e)
input("\n按回车键退出...")
回答
对不起,我已经学习的知识中不包含问题相关内容,暂时无法提供答案。如果你有Hamibot产品的其他相关问题,我会帮助你解答。
ℹ️ 本回答由 AI 生成,内容仅供参考