Кодек 'charmap' не может кодировать символы в позиции 18-37: символ отображается в ошибку <undefined>
Я пытаюсь подключить ml модель с помощью django. Здесь я загрузил модель и необходимые кодировщики.
import joblib
import os
#from keras.model import load_model
from keras.src.saving.saving_api import load_model
from django.conf import settings
import numpy as np
def load_keras_model():
# Define the path to the model file
model_path = os.path.join(settings.BASE_DIR, 'Ml_Models', 'football_prediction_model.h5')
print("Keras model path:", model_path)
try:
# Load the model
model1 = load_model(model_path)
# Verify model loading by printing its summary
print("Model successfully loaded.")
print("Model Summary:")
model1.summary()
return model1
except Exception as e:
# Handle exceptions and print error messages
print(f"Error loading model: {str(e)}")
return None
def load_encoder(filename):
encoder_path = os.path.join(settings.BASE_DIR, 'Ml_Models', filename)
print(f"{filename} path:", encoder_path) # Debug path
return joblib.load(encoder_path)
# Load all necessary models and encoders
model = load_keras_model()
team_label_encoder = load_encoder('team_label_encoder.pkl')
outcome_label_encoder = load_encoder('outcome_label_encoder.pkl')
scaler = load_encoder('scaler.pkl')
def predict_outcome(home_team, away_team, year, month, day, temperature):
try:
print(f"Home Team: {home_team}")
print(f"Away Team: {away_team}")
print(f"Year: {year}, Month: {month}, Day: {day}, Temperature: {temperature}")
# Encode and scale the input data
home_team_encoded = team_label_encoder.transform([home_team])[0]
away_team_encoded = team_label_encoder.transform([away_team])[0]
temperature_scaled = scaler.transform([[temperature]])[0][0]
print(f"Encoded Home Team: {home_team_encoded}")
print(f"Encoded Away Team: {away_team_encoded}")
print(f"Scaled Temperature: {temperature_scaled}")
# Prepare the input for the model
input_data = np.array([[home_team_encoded, away_team_encoded, year, month, day, temperature_scaled]])
print(f"input date: {input_data}")
input_data = input_data.reshape((1, 1, 6))
print(f"input date updated: {input_data}")
# Make the prediction
prediction = model.predict(input_data)
print(f"prediction: {prediction}")
outcome_index = np.argmax(prediction)
print(f"outcome index: {outcome_index}")
# Map the prediction back to the original outcome labels
outcome_label = outcome_label_encoder.inverse_transform([outcome_index])
print(f"output label: {outcome_label}")
return outcome_label[0]
except ValueError as e:
return f"Error: {str(e)}"
home_team = 'Scotland'
away_team = 'England'
year = 2024
month = 8
day = 20
temperature = 25
predicted_outcome = predict_outcome(home_team, away_team, year, month, day, temperature)
print(f"Predicted Outcome: {predicted_outcome}")
Для приведенного выше кода ниже показан результат. Обратите внимание, что я включил часть вывода в консоль.
Home Team: Scotland
Away Team: England
Year: 2024, Month: 8, Day: 20, Temperature: 25
D:\My Projects\FootBall-Match-Win-Prediction\BackEnd\venv\Lib\site-packages\sklearn\base.py:465: UserWarning: X does not have valid feature names, but MinMaxScaler was fitted with feature names
warnings.warn(
Encoded Home Team: 3
Encoded Away Team: 1
Scaled Temperature: 0.75
input date: [[3.000e+00 1.000e+00 2.024e+03 8.000e+00 2.000e+01 7.500e-01]]
input date updated: [[[3.000e+00 1.000e+00 2.024e+03 8.000e+00 2.000e+01 7.500e-01]]]
Predicted Outcome: Error: 'charmap' codec can't encode characters in position 18-37:
character maps to <undefined>
System check identified no issues (0 silenced).
August 21, 2024 - 00:30:52
Django version 5.1, using settings 'BackEnd.settings'
Starting development server at http://localhost:8000/
Quit the server with CTRL-BREAK.
Для переменной predicted_outcome
выводится
Error: 'charmap' codec can't encode characters in position 18-37:
character maps to <undefined>.
Как решить эту проблему?