Help to solve an error

An error occurred while loading the model: node array from the pickle has an incompatible dtype:

  • expected: {‘names’: [‘left_child’, ‘right_child’, ‘feature’, ‘threshold’, ‘impurity’, ‘n_node_samples’, ‘weighted_n_node_samples’, ‘missing_go_to_left’], ‘formats’: [‘<i8’, ‘<i8’, ‘<i8’, ‘<f8’, ‘<f8’, ‘<i8’, ‘<f8’, ‘u1’], ‘offsets’: [0, 8, 16, 24, 32, 40, 48, 56], ‘itemsize’: 64}
  • got : [(‘left_child’, ‘<i8’), (‘right_child’, ‘<i8’), (‘feature’, ‘<i8’), (‘threshold’, ‘<f8’), (‘impurity’, ‘<f8’), (‘n_node_samples’, ‘<i8’), (‘weighted_n_node_samples’, ‘<f8’)]

this is the error ; how to solve this

Hello

Maybe, you may need to manually convert the loaded model into the expected format. You can iterate over the loaded data and construct a new data structure that matches the expected format.

Can you show an example to do this

(I’ve also changed scikit-learn version as 1.2.2 , but the error still exists)

Here’s an example of how to save and load a scikit-learn model using joblib, which is a commonly used library for serialization:

import numpy as np
import joblib
import pandas as pd
import streamlit as st

# Sample data
data = {
    'left_child': [0, 0, 2, -1, -1, 5, -1, -1, 8, -1, -1],
    'right_child': [1, 4, 3, -1, -1, 6, 7, -1, 9, 10, -1],
    'feature': [2, 3, 2, -2, -2, 3, -2, -2, 3, -2, -2],
    'threshold': [0.800000011920929, 1.75, 4.949999809265137, -2.0, -2.0, 1.6500000953674316, -2.0, -2.0, 1.5499999523162842, -2.0, -2.0],
    'impurity': [0.6666666666666666, 0.6666666666666666, 0.5, -2.0, -2.0, 0.1680384087791495, -2.0, -2.0, 0.4444444444444444, -2.0, -2.0],
    'n_node_samples': [3, 2, 1, -2, -2, 4, -2, -2, 3, -2, -2],
    'weighted_n_node_samples': [3.0, 2.0, 1.0, -2.0, -2.0, 4.0, -2.0, -2.0, 3.0, -2.0, -2.0],
    'missing_go_to_left': [1, 1, 1, 0, 0, 1, 0, 0, 1, 0, 0]
}

# Define the expected data structure
expected_dtype = np.dtype({
    'names': ['left_child', 'right_child', 'feature', 'threshold', 'impurity', 'n_node_samples', 'weighted_n_node_samples', 'missing_go_to_left'],
    'formats': ['<i8', '<i8', '<i8', '<f8', '<f8', '<i8', '<f8', 'u1'],
    'offsets': [0, 8, 16, 24, 32, 40, 48, 56],
    'itemsize': 64
})

# Load the data
loaded_data = np.array(list(zip(*[data[key] for key in expected_dtype.names])), dtype=expected_dtype)

# Save the loaded data
joblib.dump(loaded_data, 'loaded_data.pkl')

# Load the data
reloaded_data = joblib.load('loaded_data.pkl')

# Convert the numpy array to a pandas DataFrame for easier visualization
df = pd.DataFrame(loaded_data)

# Display the DataFrame in Streamlit
st.write(df)

As you given an example to solve the problem but.its not working on my project. Can you help me to do this

check if you have installed the libraries

import streamlit as st
import numpy as np
from PIL import Image
import pickle

Attempt to load the random forest classifier model

rfc = None
try:
with open(‘rfc.pkl’, ‘rb’) as file:
rfc = pickle.load(file)
except FileNotFoundError:
st.error(“The file ‘rfc.pkl’ was not found.”)
except Exception as e:
st.error(f"An error occurred while loading the model: {e}")

Creating the web app

st.title(‘Forest Cover Type Prediction’)

try:
image = Image.open(‘img.jpeg’)
st.image(image, caption=‘myimage’, use_column_width=True)
except FileNotFoundError:
st.error(“Error: The image file ‘img.jpeg’ was not found.”)

user_input = st.text_input(‘Input Features’)

if user_input and rfc:
try:
user_input = user_input.split(‘,’)
features = np.array([user_input], dtype=np.float64)
output = rfc.predict(features).reshape(-1) # Remove unnecessary reshape

    # Create the cover type dictionary with corrected image file names
    cover_type_dict = {
        1: {"name": "Spruce/Fir", "image": "Spruce_Fir.jpg"},
        2: {"name": "Lodgepole Pine", "image": "Lodgepole_Pine.jpg"},
        3: {"name": "Ponderosa Pine", "image": "Ponderosa.jpg"},
        4: {"name": "Cottonwood/Willow", "image": "Cottonwood_Willow.jpg"},
        5: {"name": "Aspen", "image": "Aspen.jpg"},
        6: {"name": "Douglas-fir", "image": "Douglas-fir.jpg"},
        7: {"name": "Krummholz", "image": "Krummholz.jpg"}
    }

    predicted_cover_type = output[0]  # No need to convert to int
    cover_type_info = cover_type_dict.get(predicted_cover_type)

    if cover_type_info:
        col1, col2 = st.columns([2, 3])

        with col1:
            st.write("Predicted Cover Type:")
            st.markdown(f"<h1 style='font-size: 40px; font-weight: bold;'>{cover_type_info['name']}</h1>", unsafe_allow_html=True)

        with col2:
            cover_type_image = Image.open(cover_type_info["image"])
            st.image(cover_type_image, caption=cover_type_info['name'], use_column_width=True)
except Exception as e:
    st.error(f"An error occurred during prediction: {e}")

elif rfc is None:
st.write(“Model not loaded. Unable to make predictions.”)

This is my program code