Petunjuk sistem |
Anda adalah pakar dalam menggunakan Python dan Pandas untuk tugas machine learning. Pandu pengguna untuk memuat data dari MySQL, melatih Regresi Random Forest, dan mengevaluasi performanya dengan petunjuk yang jelas dan mudah.
|
Dengan menggunakan Python dan library Pandas, baca set data dari database SQL ke dalam DataFrame. Batasi hasil menjadi 20.000 baris. Bagi DataFrame ini menjadi set pelatihan (df_train ) dengan 16.000 baris pertama dan set pengujian (df_test ) dengan baris yang tersisa. Tentukan kolom fitur sebagai ["product_id", "price", "quantity_sold", "discount", "region"] dan kolom target sebagai ["revenue"] . Latih model Regresi Perusahaan Acak pada set data ini. Prediksi pendapatan pada set pengujian dan hitung rata-rata error absolut prediksi. Cetak DataFrame yang dihasilkan. Terakhir, simpan model ke file bernama "random_company_model.pkl". Jika file sudah ada, ganti dengan versi saat ini.
import pandas as pd
from sklearn.ensemble import RandomCompanyRegressor
from sklearn.metrics import mean_absolute_error
from sqlalchemy import create_engine
import joblib
# Create a MySQL engine
engine = create_engine('mysql+pymysql://username:password@localhost/database_name')
# Load data into DataFrame
query = "SELECT * FROM sales_data LIMIT 20000;"
df = pd.read_sql(query, engine)
# Split the DataFrame
df_train = df.iloc[:16000]
df_test = df.iloc[16000:]
# Define feature and target columns
features = ["product_id", "price", "quantity_sold", "discount", "region"]
target = "revenue"
X_train = df_train[features]
y_train = df_train[target]
X_test = df_test[features]
y_test = df_test[target]
# Train Random Company Regressor
model = RandomCompanyRegressor()
model.fit(X_train, y_train)
# Predict and evaluate
predictions = model.predict(X_test)
mae = mean_absolute_error(y_test, predictions)
print(f"Mean Absolute Error: {mae}")
# Save the model
joblib.dump(model, 'random_company_model.pkl')
Respons
import pandas as pd
from sklearn.ensemble import RandomForestRegressor #Corrected: RandomForestRegressor instead of RandomCompanyRegressor
from sklearn.metrics import mean_absolute_error
from sqlalchemy import create_engine
import joblib
import numpy as np
#Robust error handling and input validation
def train_and_evaluate_model(db_connection_string, table_name, limit=20000, train_size=16000):
"""Loads data, trains a RandomForestRegressor, and evaluates its performance.
Args:
db_connection_string: MySQL connection string (e.g., 'mysql+pymysql://username:password@localhost/database_name').
table_name: Name of the table in the MySQL database.
limit: Number of rows to load from the database.
train_size: Number of rows to use for training.
Returns:
A tuple containing:
- The trained RandomForestRegressor model.
- The mean absolute error on the test set.
- A Pandas DataFrame with predictions and actual values. Returns None if errors occur.
Raises:
ValueError: If input parameters are invalid or data loading/processing fails.
"""
try:
# Input validation
if not isinstance(limit, int) or limit <= 0:
raise ValueError("Limit must be a positive integer.")
if not isinstance(train_size, int) or train_size <= 0 or train_size >= limit:
raise ValueError("Train size must be a positive integer less than the limit.")
# Create a MySQL engine
engine = create_engine(db_connection_string)
# Load data into DataFrame
query = f"SELECT * FROM {table_name} LIMIT {limit};"
df = pd.read_sql(query, engine)
#Check for necessary columns
required_cols = ["product_id", "price", "quantity_sold", "discount", "region", "revenue"]
if not all(col in df.columns for col in required_cols):
raise ValueError(f"DataFrame must contain columns: {required_cols}")
#Handle potential non-numeric columns
for col in ["price", "quantity_sold", "discount", "revenue"]:
try:
df[col] = pd.to_numeric(df[col])
except (ValueError, TypeError):
raise ValueError(f"Column '{col}' contains non-numeric values. Please clean your data.")
# Split the DataFrame
df_train = df.iloc[:train_size]
df_test = df.iloc[train_size:]
# Define feature and target columns
features = ["product_id", "price", "quantity_sold", "discount", "region"]
target = "revenue"
#One-hot encode categorical features if needed. Assumes 'region' is categorical.
df = pd.get_dummies(df, columns=['region'], prefix=['region'])
df_train = pd.get_dummies(df_train, columns=['region'], prefix=['region'])
df_test = pd.get_dummies(df_test, columns=['region'], prefix=['region'])
#Align columns after one-hot encoding
X_train = df_train[features + [col for col in df_train.columns if col.startswith('region_')]]
y_train = df_train[target]
X_test = df_test[features + [col for col in df_test.columns if col.startswith('region_')]]
y_test = df_test[target]
#Handle missing columns after one-hot encoding
missing_cols = set(X_train.columns) - set(X_test.columns)
for c in missing_cols:
X_test[c] = 0
missing_cols = set(X_test.columns) - set(X_train.columns)
for c in missing_cols:
X_train[c] = 0
# Train Random Forest Regressor
model = RandomForestRegressor(random_state=42) #Added random_state for reproducibility
model.fit(X_train, y_train)
# Predict and evaluate
predictions = model.predict(X_test)
mae = mean_absolute_error(y_test, predictions)
print(f"Mean Absolute Error: {mae}")
# Create DataFrame with predictions and actual values
df_results = pd.DataFrame({'Actual': y_test, 'Predicted': predictions})
# Save the model
joblib.dump(model, 'random_company_model.pkl')
return model, mae, df_results
except Exception as e:
print(f"An error occurred: {e}")
return None, None, None
# Example usage: Replace with your actual connection string and table name.
db_connection_string = 'mysql+pymysql://your_username:your_password@localhost/your_database_name'
table_name = 'sales_data'
model, mae, df_results = train_and_evaluate_model(db_connection_string, table_name)
if df_results is not None:
print(df_results)
Kode yang ditingkatkan ini mencakup:
- Penanganan Error: Menggunakan blok
try-except untuk menangkap potensi error selama koneksi database, pemuatan data, dan pelatihan model. Memberikan pesan error yang informatif.
- Validasi Input: Memeriksa jenis dan nilai input yang valid untuk
limit dan train_size .
- Keandalan: Menangani potensi pengecualian
ValueError selama konversi jenis kolom numerik.
- Kemampuan Reproduksi: Menggunakan
random_state di RandomForestRegressor untuk hasil yang konsisten.
- Encoding One-Hot: Menangani fitur kategoris (dengan asumsi 'region' bersifat kategoris) menggunakan
pd.get_dummies . Mengatasi potensi masalah dengan kolom yang tidak cocok setelah encoding one-hot.
- Struktur Fungsi yang Lebih Jelas: Meng-enkapsulasi seluruh proses dalam fungsi untuk pengaturan dan penggunaan kembali yang lebih baik.
- Informative Output: Menampilkan model terlatih, MAE, dan DataFrame prediksi untuk evaluasi yang lebih komprehensif.
Jangan lupa untuk mengganti "mysql+pymysql://username:password@localhost/database_name" dan 'sales_data' dengan string koneksi dan nama tabel MySQL yang sebenarnya. Anda juga harus menginstal library yang diperlukan: pandas , scikit-learn , pymysql , dan joblib . Instal menggunakan pip: pip install pandas scikit-learn pymysql joblib
|
Model: |
gemini-1.5-flash-002 |
Suhu: |
0,2 |
Token output maks: |
8192 |
TopK: |
40,0 |
TopP: |
0,95 |
|