Python/Python-Lotto

Lotto 자동생성기 - Model 예측하기

오마로 2024. 3. 17. 07:54
반응형

L

Lotto 기존 당첨번호를 불러와서 model 학습하기

1.Lotto 기존 당첨번호 파일 읽어오기
https://gomsfactory.tistory.com/2884

 

Lotto 자동생성기 - 기존 당첨번호 파일 읽어오기 #2

Lotto 기존 당첨번호를 저장한 파일을 python에서 읽어오기 1.Lotto 기존 당첨번호 파일 읽어오기 1)import library # Import necessary libraries import numpy as np 2)load_data() 호출 구분자 ","로 데이타를 가지고 옵니

gomsfactory.tistory.com


2.모델 호출 및 생성하기
https://gomsfactory.tistory.com/2885

 

Lotto 자동생성기 - Model생성하기

Lotto 기존 당첨번호를 불러와서 model 생성하기 1.Lotto 기존 당첨번호 파일 읽어오기 https://gomsfactory.tistory.com/2884 Lotto 자동생성기 - 기존 당첨번호 파일 읽어오기 #2 Lotto 기존 당첨번호를 저장한 파

gomsfactory.tistory.com


3.모델 학습하기
https://gomsfactory.tistory.com/2886

 

Lotto 자동생성기 - Model 학습하기

Lotto 기존 당첨번호를 불러와서 model 학습하기 1.Lotto 기존 당첨번호 파일 읽어오기 https://gomsfactory.tistory.com/2884 Lotto 자동생성기 - 기존 당첨번호 파일 읽어오기 #2 Lotto 기존 당첨번호를 저장한 파

gomsfactory.tistory.com


4.예측하기 및 로또번호 10개 가져오기
 1)호출하기

# Main function to run everything  
def main():

    # Load and preprocess data
    train_data, val_data, max_value = load_data()
   
    # Get number of features from training data
    num_features = train_data.shape[1]                          #6    로또 형태는 6개까지 선택
   
    #train_data[888][6] > 888행, 6열
    print("train_data 갯수 : ", train_data.shape[0])            #888    1110회 중 888회까지 훈련셋
    print("train_data num_features : ", train_data.shape[1])    #6    로또 형태는 6개까지 선택
    #val_data[222][6] > 222, 6열
    print("val_data 갯수 : ", val_data.shape[0])                #222    1110회 중 888회 이후는 검증셋
    print("val_data num_features : ", val_data.shape[1])        #6    로또 형태는 6개까지 선택
   
    print("max_value : ", max_value)             #로또 최대값은 45
    print("num_features : ", num_features)       #로또 형태는 6개까지 선택
   
    # Create and compile model
    # num_features = 6, max_value = 45
    model = create_model(num_features, max_value)
   
    # Train model
    train_model(model, train_data, val_data)
   
    # Save the model
    # model.save("lotto_model.keras")
   
    # Predict numbers using trained model
    predicted_numbers = predict_numbers(model, val_data, num_features)
   
    # Print predicted numbers
    print_predicted_numbers(predicted_numbers)

# Run main function if this script is run directly (not imported as a module)
if __name__ == "__main__":
    main()

2)호출하기 학습된 데이타를 기반으로 예측 로또 번호 가져오기

# Function to predict numbers using the trained model
def predict_numbers(model, val_data, num_features):
    # Predict on the validation data using the model
    # 검증데이타를 기반으로 예측 데이타 가지고 옵니다.
    predictions = model.predict(val_data)
    # Get the indices of the top 'num_features' predictions for each sample in validation data
    # 배열에서 각 샘플에 대한 예측된 번호의 확률을 기준으로 내림차순으로 정렬합니다.
    # [:, -num_features:]을 사용하여 각 샘플에서 가장 높은 확률을 가진 상위 num_features개의 번호의 인덱스를 선택합니다
    indices = np.argsort(predictions, axis=1)[:, -num_features:]
    # Get the predicted numbers using these indices from validation data
    # 위에서 선택한 인덱스를 사용하여 원본 검증 데이터에서 해당 인덱스에 해당하는 번호를 선택합니다.
    # 이를 통해 각 샘플에 대해 가장 확률이 높은 상위 num_features개의 번호를 예측된 번호로 선택합니다.
    predicted_numbers = np.take_along_axis(val_data, indices, axis=1)
    # 각 배열에서 오름차순으로 정렬합니다.
    predicted_numbers.sort()
    return predicted_numbers

3)로또 번호 10개 출력하기

# Function to print the predicted numbers
def print_predicted_numbers(predicted_numbers):
    # Print a separator line and "Predicted Numbers:"
    print("=========================")
    print("Predicted Numbers:")
    # Print only the first row of predicted numbers. 총 열개
    i = 0
    for _ in range(10):
        print(', '.join(map(str, predicted_numbers[i])))
        i = i + 1
   
    print("=========================")

============================
Predicted Numbers:
3, 13, 29, 38, 39, 42
1, 4, 14, 18, 29, 37
9, 13, 28, 31, 39, 41
4, 9, 17, 18, 26, 42
1, 15, 17, 23, 25, 41
19, 32, 37, 40, 41, 43
16, 26, 31, 38, 39, 41
5, 12, 25, 26, 38, 45
6, 7, 12, 22, 26, 36
18, 21, 28, 35, 37, 42
============================

1111회 로또 예측번호 입니다. 
결과는 당첨이 되지 않았지만, 표본이 좀 더 많이 쌓인다면 좀 더 나아지지 않을까 기대해봅니다. 

감사합니다.

반응형