-
Notifications
You must be signed in to change notification settings - Fork 1
Chương 2_Trang 9_Tối ưu hóa hàm python với Data input và Tensorflow models Mega 6 Ball 45
PhD Le Toan Thang edited this page Mar 31, 2024
·
1 revision
Mega 6 Ball 45 - Lottery Numbers được load từ CSV:
Nội dung file mega6b45.csv:
2;8;14;19;24;42
4;17;19;27;28;36
5;7;10;12;15;26
1;10;21;25;32;39
20;22;24;26;28;37
3;10;17;20;22;27
11;15;34;39;41;43
2;26;28;40;41;45
4;12;19;23;36;41
1;13;25;30;34;40
9;11;16;29;31;33
1;3;5;26;30;42
8;14;26;28;43;45
5;6;11;20;24;45
1;14;22;28;32;42
6;29;31;35;42;44
4;7;25;34;35;38
Chúng ta thay biến mảng các giá trị Lottery trước bằng file csv: Tiếp theo thay đoạn code Python Tensorflow và load file csv:
import numpy as np
import pandas as pd
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Input
# Đọc dữ liệu từ tệp CSV
lottery_data = pd.read_csv(r"C:\Python311\workspaces\mega6b45.csv", sep=';').values
# Chuẩn hóa dữ liệu về khoảng [0, 1]
normalized_data = lottery_data / 45.0
# Tạo dữ liệu huấn luyện và mục tiêu huấn luyện
previous_draws = normalized_data[:-1]
next_draws = normalized_data[1:]
# Xây dựng mô hình mạng neural
model = Sequential()
model.add(Dense(16, activation='relu', input_shape=(6,)))
model.add(Dense(16, activation='relu'))
model.add(Dense(6, activation='sigmoid'))
# Biên dịch mô hình
model.compile(optimizer='adam', loss='mse')
# Huấn luyện mô hình
model.fit(previous_draws, next_draws, epochs=100)
# Dự đoán kỳ quay thưởng tiếp theo
last_draw = normalized_data[-1:]
predicted_next_draw = model.predict(last_draw)
# Chuyển đổi dự đoán về dạng ban đầu
predicted_next_draw = predicted_next_draw * 45.0
rounded_predictions = np.round(predicted_next_draw)
print("Dự đoán kỳ quay thưởng tiếp theo (làm tròn):")
print(rounded_predictions)
Chú ý: Khi chạy đoạn code trên, chúng ta gặp cảnh báo:
C:\Python311\Lib\site-packages\keras\src\layers\core\dense.py:88: UserWarning: Do not pass an `input_shape`/`input_dim` argument to a layer. When using Sequential models, prefer using an `Input(shape)` object as the first layer in the model instead.
super().__init__(activity_regularizer=activity_regularizer, **kwargs)
Đó chỉ là một cảnh báo nhắc nhở từ thư viện Keras. Để khắc phục cảnh báo này, chúng ta có thể sử dụng lớp Input từ tensorflow.keras.layers như là lớp đầu tiên trong mô hình thay vì sử dụng Dense với đối số input_shape. Chúng ta thay thế bằng đoạn mã sau:
import numpy as np
import pandas as pd
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Input
# Đọc dữ liệu từ tệp CSV
lottery_data = pd.read_csv(r"C:\Python311\workspaces\mega6b45.csv", sep=';').values
# Chuẩn hóa dữ liệu về khoảng [0, 1]
normalized_data = lottery_data / 45.0
# Tạo dữ liệu huấn luyện và mục tiêu huấn luyện
previous_draws = normalized_data[:-1]
next_draws = normalized_data[1:]
# Xây dựng mô hình mạng neural
model = Sequential()
model.add(Input(shape=(6,))) # Sử dụng lớp Input thay vì Dense với input_shape
model.add(Dense(16, activation='relu'))
model.add(Dense(16, activation='relu'))
model.add(Dense(6, activation='sigmoid'))
# Biên dịch mô hình
model.compile(optimizer='adam', loss='mse')
# Huấn luyện mô hình
model.fit(previous_draws, next_draws, epochs=100)
# Dự đoán kỳ quay thưởng tiếp theo
last_draw = normalized_data[-1:]
predicted_next_draw = model.predict(last_draw)
# Chuyển đổi dự đoán về dạng ban đầu
predicted_next_draw = predicted_next_draw * 45.0
rounded_predictions = np.round(predicted_next_draw)
print("Dự đoán kỳ quay thưởng tiếp theo (làm tròn):")
print(rounded_predictions)
- 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.