首页 > 解决方案 > 尝试在训练模型上使用 predict() 函数时出现异常

问题描述

在用 pickle 保存我的模型之前,模型运行良好,而且我得到了0.80精确度。现在,当我尝试使用该函数predict()获取图像的预测类时,出现以下异常:

ValueError: shapes (12,1) and (12,) not aligned: 1 (dim 1) != 12 (dim 0)

请注意,我使用了对训练集进行的所有预处理,并且f_scaled每行的大小相同,X_train其中包含 12 个特征

features = feature_extraction(image_path)
scaler = MinMaxScaler()
f_scaled = scaler.fit_transform(features.reshape(-1, 1))
psvm_model = pickle.load(open('C:/Users/DELL/Desktop/SkinCancerDataset/psvmModel.sav', 'rb'))
print("prediction:", psvm_model.predict(np.squeeze(f_scaled).reshape(-1, 1)))

当我删除reshape(-1, 1)内部预测函数时,我得到以下结果:

[1 1 1 1 1 1 1 1 1 1 1 1] 

我猜:这意味着f_scalar数组中的每个标量都被视为一个条目,这解释了prediction (1, 12)

它应该只给出一个标签范围[0,2]

这就是数据 ( f_scaled) 在 pyCharm 中的显示方式:

f_scaled

标签: pythonexceptionsvmpicklepredict

解决方案


推荐阅读