gpt4 book ai didi

python - 我无法将 ML 模型加载到尝试使用 joblib 和 pickle 的 flask 中

转载 作者:太空宇宙 更新时间:2023-11-03 20:14:47 31 4
gpt4 key购买 nike

我在将 ML 模型(RandomForest ML 模型)加载到 Flask 时遇到问题。我尝试过 joblib 和 pickle 两个库都会生成相同的错误

model = pickle.load(f)
File "sklearn\tree\_tree.pyx", line 606, in sklearn.tree._tree.Tree.__cinit__
ValueError: Buffer dtype mismatch, expected 'SIZE_t' but got 'long long'

这是我的 flask 应用程序

# all necessary libraries are imported 

# Use pickle to load in the pre-trained model.
with open(f'model/model_2.pkl', 'rb') as f:
model = pickle.load(f)

# model = load('model/model.joblib')

app = flask.Flask(__name__, template_folder='templates')

我的 Jubyter 笔记本:

from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import train_test_split
import numpy as nm
import pandas as pd
from sklearn.metrics import classification_report
from sklearn.metrics import confusion_matrix

df = pd.read_csv('phishing _urls_v2.csv')
columns = ['No_of_dots','Length_of_host','contains (exe)','redirect (//)',]
labels = df['malicious'].values
features_2 = df[list(columns)].values
X_train_2, X_test_2, y_train_2, y_test_2 = train_test_split(features_2, labels, test_size=0.30)
model = RandomForestClassifier(n_estimators=52)
model.fit(X_train_2,y_train_2)
model_predict_2 =model.predict(X_test_2)
accuracy = confusion_matrix(y_test_2, model_predict_2)
print (accuracy)

from joblib import dump, load
dump(model, 'model.joblib')

import pickle
pickle.dump(model, open('model_2.pkl','wb'))

我在 Jubyter Notebook 中加载模型并且工作得很好

model = pickle.load(open('model_2.pkl','rb'))
print(model.predict([[1,10,0,0]]))
[0]

我无法弄清楚这个问题。

最佳答案

感谢 Mayosten 的提示。问题是 Jupyter Notebook 中使用的 python 版本是 32 位,而 Pycharm 中使用的 python 版本是 64 位。

验证使用此命令

import ctypes
print (ctypes.sizeof(ctypes.c_voidp))

# 8 for 64bit and 4 for 32bit

关于python - 我无法将 ML 模型加载到尝试使用 joblib 和 pickle 的 flask 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58523857/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com