我如何修复类型错误“只有大小为 1 的数组可以转换为 Python 标量

问题描述

我正在尝试从时尚 mnist 数据集创建朴素贝叶斯分类器,但我无法在我的代码中修复这种类型的错误问题。我已经阅读了一些关于此的主题,但我无法修复它,请我需要帮助。 我已经创建了 i 类,我在其中构建了一些函数(拟合、预测等),但在 def guassian 中,问题在于我何时尝试计算 den 变量。正如您所看到的,我正在下载 mnist 数据并将数组 Xtrain 和 ytrain 传递给类中的 fit 函数。我使用了 .astype(int) 但没有用。如果您想查看完整代码,我可以编辑问题以便发布。

    def gaussian(self,class_index,X):
        mean = self.mean[class_index]
        var = self.var[class_index]
        num = np.exp(-(X-mean)**2/(2*var))  
        den = np.math.sqrt(2*np.pi*var) #Here is the problem 
        result = num/dev
        
        return result
(Xtrain,ytrain),(Xtest,ytest) = fashion_mnist.load_data()

Xtrain = Xtrain.astype('float32')
Xtest = Xtest.astype('float')
Xtrain=Xtrain/255.0
Xtest=Xtest/255.0


    #Reshaping data 
Xtest=Xtest.reshape(Xtest.shape[0],Xtest.shape[1] * Xtest.shape[2])
Xtrain =Xtrain.reshape(Xtrain.shape[0],Xtrain.shape[1] * Xtrain.shape[2])

naive_bayes = Nbayes()
naive_bayes.fit(Xtrain,ytrain)
pred = naive_bayes.prediction(Xtest)
acc = (ytest,pred)

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)