การทำนายข้อมูลด้วย MLPRegressor (Test & Evaluation)
..
👉..
import pandas as pd
import numpy as np
import joblib
from sklearn.metrics import mean_absolute_percentage_error
# 1. Load the saved model and scaler
model = joblib.load(r"C:\Users\user\Desktop\mlp_model.pkl")
print("Model loaded successfully.")
scaler = joblib.load("scaler.pkl")
print("Scaler loaded successfully.")
# 2. Load the CSV file containing the test data
data = pd.read_csv(r"C:\Users\user\Desktop\car_price.csv")
# Example: Suppose the CSV contains features 'Age', 'KM', 'Weight', 'CC', 'Doors'
feature_columns = ['Age', 'KM', 'Weight', 'CC', 'Doors']
target_column = 'Price'
X = data[feature_columns].values
y_true = data[target_column].values
# 3. Preprocess the input features using the same scaler used during training
X_scaled# = scaler.transform(X)
# 4. Use the loaded model to make predictions on the new data
y_pred = model.predict(X_scaled)
# 5. Calculate the Mean Absolute Percentage Error (MAPE)
mape_percentage = mean_absolute_percentage_error(y_true, y_pred) * 100
result = 100 - mape_percentage
print(f"MAPE: {mape_percentage:.2f}%")
print(f"Result (100 - MAPE): {result:.2f}")
เมื่อรันโปรแกรมแล้วผลลัพท์ที่ได้
การทดสอบทำนายข้อมูลด้วย MLPRegressor จากการป้อนข้อมูลแบบ 1D Array
👉..
#import joblib
import numpy as np
# 1. Load the saved model
model = joblib.load(r"C:\Users\user\Desktop\mlp_model.pkl")
print("Model loaded successfully.")
# 2. (Optional) Load the scaler if you used one during training
scaler = joblib.load("scaler.pkl")
print("Scaler loaded successfully.")
# 3. Prepare your new input data as a numpy array
new_data = np.array([[2, 15000, 1200, 1500, 4]])
# 4. Apply the same preprocessing to the new data (if required)
new_data_scaled = scaler.transform(new_data)
# 5. Use the loaded model to make a prediction
prediction = model.predict(new_data_scaled)
print("Prediction:", prediction)
เมื่อรันโปรแกรมแล้วผลลัพท์ที่ได้