Skip to content

Chương 2_Bài tập số 4_Thuật toán xử lý dữ liệu nội suy

PhD Le Toan Thang edited this page Apr 1, 2024 · 1 revision

Dữ liệu Nội suy và Thuật toán:

Câu đố đưa ra 9 ô vuông, mỗi ô tương ứng với một chữ số. Với 8 chữ số cho trước gồm: 8, 2, 60, 6, 4, 20, 7, 3. Nhiệm vụ của bạn hãy tìm và điền con số thích hợp vào ô trống trong hình vuông có dấu hỏi chấm sao cho đúng quy tắc.

Câu đố khó này khiến bạn phải đau đầu và suy nghĩ không ngừng về lời giải. Bạn hãy suy nghĩ thật kỹ lưỡng để tìm ra chữ số còn thiếu và giải thích quy luật của chúng.

image

Điền số thích hợp vào ô trống trong hình vuông trên sao cho đúng với quy tắc của chúng.

Cách 1. Có một số quy tắc có thể áp dụng, nhưng hãy thử một quy tắc có thể: Ta thấy rằng chữ số thứ hai (2) là bình phương của chữ số đầu tiên (8). Chữ số thứ tư (6) bằng tích của chữ số thứ hai (2) và chữ số thứ ba (3). Chữ số thứ sáu (20) bằng tích của chữ số thứ tư (6) và chữ số thứ năm (4). Chữ số thứ tám (3) bằng tích của chữ số thứ sáu (20) và chữ số thứ bảy (7).

Dựa vào quy luật trên, ta có thể suy ra rằng chữ số tiếp theo sẽ là tích của chữ số thứ tám (3) và chữ số thứ chín (tạm thời chưa biết). Vậy con số còn thiếu có thể là: 3 x ? = 9. Do đó, con số còn thiếu là 3.

Vậy lời giải cuối cùng là: 8, 2, 60, 6, 4, 20, 7, 3, 9.

Cách 2: 8×8-2×2=60 6×6-4×4=20 7×7-3×3=40

Cách 3: Dựa vào quy tắc của các chữ số đã cho, ta có thể giải câu đố như sau:

Hàng 1: 8 | 2 | 60

Hàng 2: 6 | 4 | 20

Hàng 3: 7 | 3 | ?

Quy tắc: Số trong ô cột giữa bằng tổng hai số ở hai ô cột bên cạnh. Áp dụng quy tắc này vào hàng 3, ta có: 7 + 3 = 10

Bài tập LotteryAi: là trí tuệ nhân tạo dự đoán xổ số sử dụng công nghệ học máy để dự đoán số trúng thưởng của Lý thuyết trò chơi

Cấu trúc và nội dung dữ liệu file LotteryAi.csv:

2;4;19;32;35;39;49
9;13;28;33;50;53;47
6;9;26;27;34;47;41
6;7;16;21;34;50;31
1;12;23;43;48;52;30
13;15;21;26;34;35;45
11;14;15;24;34;53;18
9;31;39;41;47;48;3

Nội dung code hàm python xây dựng LotteryAI:

!pip install numpy tensorflow keras art text2art

# Import necessary libraries
import numpy as np
import tensorflow as tf
from tensorflow import keras
from keras import layers
from art import text2art

# Function to print the introduction of the program
def print_intro():
    # Generate ASCII art with the text "LotteryAi"
    ascii_art = text2art("C:\Python311\workspaces\LotteryAi.csv")
    # Print the introduction and ASCII art
    print("============================================================")
    print("Source code Python AI: Le Toan Thang")
    print("BTC: from poe.com>PythonChatAI")
    print("ETH & BNB: Github.com/PhDLeToanThang/")
    print("============================================================")
    print(ascii_art)
    print("Lottery prediction artificial intelligence")

# Function to load data from a file and preprocess it
def load_data():
    # Load data from file, ignoring white spaces and accepting unlimited length numbers
    data = np.genfromtxt('C:\Python311\workspaces\LotteryAi.csv', delimiter=';', dtype=int)
    # Replace all -1 values with 0
    data[data == -1] = 0
    # Split data into training and validation sets
    train_data = data[:int(0.8*len(data))]
    val_data = data[int(0.8*len(data)):]
    # Get the maximum value in the data
    max_value = np.max(data)
    return train_data, val_data, max_value

# Function to create the model
def create_model(num_features, max_value):
    # Create a sequential model
    model = keras.Sequential()
    # Add an Embedding layer, LSTM layer, and Dense layer to the model
    model.add(layers.Embedding(input_dim=max_value+1, output_dim=64))
    model.add(layers.LSTM(256))
    model.add(layers.Dense(num_features, activation='softmax'))
    # Compile the model with categorical crossentropy loss, adam optimizer, and accuracy metric
    model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
    return model

# Function to train the model
def train_model(model, train_data, val_data):
    # Fit the model on the training data and validate on the validation data for 100 epochs
    history = model.fit(train_data, train_data, validation_data=(val_data, val_data), epochs=100)

# 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
    indices = np.argsort(predictions, axis=1)[:, -num_features:]
    # Get the predicted numbers using these indices from validation data
    predicted_numbers = np.take_along_axis(val_data, indices, axis=1)
    return predicted_numbers

# 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
   print(', '.join(map(str, predicted_numbers[0])))

# Main function to run everything   
def main():
   # Print introduction of program 
   print_intro()
   
   # 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]
   
   # Create and compile model 
   model = create_model(num_features, max_value)
   
   # Train model 
   train_model(model, train_data, val_data)
   
   # 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()

image

  • Business Analyst (BA) là một nghề tồn tại từ lâu trên toàn cầu, nhưng ở Việt Nam thì vẫn còn khá mới (xuất hiện khoảng hơn 27 năm).
  • Tôi thực sự đánh giá cao nghề này và trong quá trình làm việc, tôi đã gặp phải nhiều thách thức, từ đó tôi đã hiểu rõ những điểm mạnh và điểm yếu của bản thân. Tôi nhận thấy rằng trong lĩnh vực này có nhiều vấn đề phức tạp và khó khăn, nhưng khi chúng được giải quyết, thì cảm giác thật tuyệt vời.
  • BA xuất hiện với mục tiêu giải quyết vấn đề. Đó có thể là biến một điều không tốt thành điều tốt hơn, hoặc cải thiện những điều đã tốt để trở nên tốt hơn. Việc mang lại ý nghĩa cho người khác thực sự là một điều mà tôi khó lòng tranh cãi hay bỏ qua.
Clone this wiki locally