본문 바로가기

Python

로또 번호 추출 파이썬 프로그래밍

# main.py

 

import pandas as pd
from collections import Counter
import random
import os
from sklearn.linear_model import LinearRegression
import numpy as np

# 데이터 파일 경로
file_path = 'C:/Wokrspace/PycharmProjects/pythonProject/sample/lotto.txt'

# 파일 경로 확인
if not os.path.exists(file_path):
    print(f"파일을 찾을 수 없습니다: {file_path}")
    exit(1)

# 데이터 파일을 공백으로 구분하여 읽어오기
with open(file_path, 'r') as file:
    lines = file.readlines()

# 각 라인의 공백 제거 및 숫자 리스트로 변환
data = [list(map(int, line.split())) for line in lines if line.strip()]

# 7자리 숫자에서 마지막 숫자(보너스 번호)를 제외하고 6자리로 변환
cleaned_data = [numbers[:6] if len(numbers) == 7 else numbers for numbers in data]

# 데이터가 여러 행에 걸쳐 있는 경우, 데이터 프레임을 일차원 시리즈로 변환
numbers = pd.Series([num for sublist in cleaned_data for num in sublist])

# 분석을 위한 함수들
def analyze_consecutive_numbers(numbers):
    consecutive_counts = []
    for subset in cleaned_data:
        subset.sort()
        count = 0
        for i in range(1, len(subset)):
            if subset[i] == subset[i - 1] + 1:
                count += 1
            else:
                if count > 0:
                    consecutive_counts.append(count + 1)
                count = 0
        if count > 0:
            consecutive_counts.append(count + 1)
    return Counter(consecutive_counts)

def analyze_number_ranges(numbers, range_size=10):
    range_counts = Counter((num // range_size) * range_size for num in numbers)
    return range_counts

def analyze_intervals(numbers):
    intervals = []
    for subset in cleaned_data:
        subset.sort()
        for i in range(1, len(subset)):
            intervals.append(subset[i] - subset[i - 1])
    return Counter(intervals)

def analyze_specific_numbers(numbers):
    specific_counts = Counter(numbers)
    return specific_counts

def trend_analysis():
    # 예: 과거 데이터로부터 추세 분석을 위한 간단한 예제
    X = np.arange(len(cleaned_data)).reshape(-1, 1)  # 간단한 인덱스 배열
    y = [sum(subset) for subset in cleaned_data]  # 각 로또 번호 집합의 합

    model = LinearRegression()
    model.fit(X, y)

    # 예측: 다음 회차의 번호 집합의 합 예측
    next_index = np.array([[len(cleaned_data) + 1]])
    predicted_sum = model.predict(next_index)[0]

    return predicted_sum

# 패턴 분석 결과
consecutive_numbers = analyze_consecutive_numbers(numbers)
sorted_consecutive_numbers = sorted(consecutive_numbers.items(), key=lambda x: x[1], reverse=True)

number_ranges = analyze_number_ranges(numbers, range_size=10)
sorted_number_ranges = sorted(number_ranges.items(), key=lambda x: x[1], reverse=True)

specific_number_counts = analyze_specific_numbers(numbers)
sorted_specific_number_counts = sorted(specific_number_counts.items(), key=lambda x: x[1], reverse=True)

predicted_sum_trend = trend_analysis()

# 패턴 기반 예측: 분석 결과를 반영하여 6자리 번호 생성
def generate_numbers_based_on_patterns(num_predictions=10):
    all_numbers = list(range(1, 46))  # 일반적인 로또 번호 범위 1-45
    predictions = []

    def create_prediction():
        prediction = set()
        while len(prediction) < 6:
            num = random.choice(all_numbers)
            prediction.add(num)
        return sorted(prediction)

    def is_valid_prediction(prediction):
        valid = True
        if sorted_consecutive_numbers:
            most_common_length = sorted_consecutive_numbers[0][0]
            if not any(prediction[i] == prediction[i - 1] + 1 for i in range(1, len(prediction))):
                valid = False
        if sorted_number_ranges:
            most_common_range = sorted_number_ranges[0][0]
            if not any(num in range(most_common_range, most_common_range + 10) for num in prediction):
                valid = False
        return valid

    while len(predictions) < num_predictions:
        prediction = create_prediction()
        if is_valid_prediction(prediction):
            if prediction not in predictions:
                predictions.append(prediction)

    return predictions

def calculate_probability(prediction):
    # 기본 확률을 1로 시작
    probability = 1.0

    # 연속된 숫자 분석 결과의 가중치 반영 (2개 연속된 숫자에 가중치 추가)
    if sorted_consecutive_numbers:
        if 2 in dict(sorted_consecutive_numbers) and dict(sorted_consecutive_numbers)[2] >= 622:
            if any(prediction[i] == prediction[i - 1] + 1 for i in range(1, len(prediction))):
                probability *= 2.0  # 2개 연속된 숫자에 대한 가중치 증가

    # 번호 구간별 빈도 분석의 가중치 반영
    if sorted_number_ranges:
        for range_start, count in sorted_number_ranges:
            if range_start in [10, 20, 30, 0]:
                if any(num in range(range_start, range_start + 10) for num in prediction):
                    # 각 구간에 대한 가중치 증가
                    if range_start == 10:
                        probability *= 1.5  # 10-19 구간에 가중치 증가
                    elif range_start == 20:
                        probability *= 1.4  # 20-29 구간에 가중치 증가
                    elif range_start == 30:
                        probability *= 1.3  # 30-39 구간에 가중치 증가
                    elif range_start == 0:
                        probability *= 1.2  # 0-9 구간에 가중치 증가

    # 특정 숫자 빈도 분석의 가중치 반영
    if sorted_specific_number_counts:
        for num in prediction:
            if num in dict(sorted_specific_number_counts):
                probability *= (1 + dict(sorted_specific_number_counts)[num] / 500)  # 빈도에 따른 가중치 증가

    # 트렌드 분석 결과 반영
    if predicted_sum_trend:
        if sum(prediction) in range(int(predicted_sum_trend - 5), int(predicted_sum_trend + 5)):
            probability *= 1.3  # 예측된 합과 비슷한 경우 가중치 증가

    return probability

predicted_numbers = generate_numbers_based_on_patterns(num_predictions=10)
print("\n예측된 로또 번호 조합과 각 조합의 예상 확률:")
for prediction in predicted_numbers:
    probability = calculate_probability(prediction)
    if probability >= 1:
        print(f"{prediction} - 예상 확률: {probability:.4f}")

print("\n연속된 숫자 분석 결과 (많은 순서):")
for length, count in sorted_consecutive_numbers:
    print(f"{length}개 연속된 숫자: {count}회")

print("\n번호 구간별 빈도 분석 결과 (많은 순서):")
for range_start, count in sorted_number_ranges:
    print(f"{range_start}-{range_start + 9}: {count}회")

print("\n특정 숫자 빈도 분석 결과 (많은 순서):")
for num, count in sorted_specific_number_counts:
    print(f"{num}: {count}회")

 

#main.py END

-----------------------------------------------------------------------------------------------------------------------------------

Github URL

https://github.com/hyoha/Python_Lotto

 

GitHub - hyoha/Python_Lotto

Contribute to hyoha/Python_Lotto development by creating an account on GitHub.

github.com

 

아래 파이썬 로또 분석 프로그램 실행 화면 

 

 

소스의 기준데이터인

lotto.txt 파일은

실제 지금까지 당첨된 실제 로또 번호를

메모장에 전부 넣어서 사용하였습니다.

 

파싱처리한 실제 로또 번호를 분석후 추출하며

 pandas numpy 를 사용하여

학습시 가중치에 변화를 줄수 있는 소스코드입니다.

 

실제 상기 프로그램으로 번호 추출해서

 

동네 편의점에서

로또 번호 3장 구입하여

15(+3)개의 테스트를 해보았으나
실제 맞는 숫자는 없었습니다.


테스트로만 참고하시기 바랍니다.